author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
16 | 1 |
<?php |
2 |
/** |
|
3 |
* Sitemaps: WP_Sitemaps_Provider class |
|
4 |
* |
|
5 |
* This class is a base class for other sitemap providers to extend and contains shared functionality. |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Sitemaps |
|
9 |
* @since 5.5.0 |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* Class WP_Sitemaps_Provider. |
|
14 |
* |
|
15 |
* @since 5.5.0 |
|
16 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
#[AllowDynamicProperties] |
16 | 18 |
abstract class WP_Sitemaps_Provider { |
19 |
/** |
|
20 |
* Provider name. |
|
21 |
* |
|
22 |
* This will also be used as the public-facing name in URLs. |
|
23 |
* |
|
24 |
* @since 5.5.0 |
|
25 |
* |
|
26 |
* @var string |
|
27 |
*/ |
|
28 |
protected $name = ''; |
|
29 |
||
30 |
/** |
|
31 |
* Object type name (e.g. 'post', 'term', 'user'). |
|
32 |
* |
|
33 |
* @since 5.5.0 |
|
34 |
* |
|
35 |
* @var string |
|
36 |
*/ |
|
37 |
protected $object_type = ''; |
|
38 |
||
39 |
/** |
|
40 |
* Gets a URL list for a sitemap. |
|
41 |
* |
|
42 |
* @since 5.5.0 |
|
43 |
* |
|
44 |
* @param int $page_num Page of results. |
|
45 |
* @param string $object_subtype Optional. Object subtype name. Default empty. |
|
19 | 46 |
* @return array[] Array of URL information for a sitemap. |
16 | 47 |
*/ |
48 |
abstract public function get_url_list( $page_num, $object_subtype = '' ); |
|
49 |
||
50 |
/** |
|
51 |
* Gets the max number of pages available for the object type. |
|
52 |
* |
|
53 |
* @since 5.5.0 |
|
54 |
* |
|
55 |
* @param string $object_subtype Optional. Object subtype. Default empty. |
|
56 |
* @return int Total number of pages. |
|
57 |
*/ |
|
58 |
abstract public function get_max_num_pages( $object_subtype = '' ); |
|
59 |
||
60 |
/** |
|
61 |
* Gets data about each sitemap type. |
|
62 |
* |
|
63 |
* @since 5.5.0 |
|
64 |
* |
|
65 |
* @return array[] Array of sitemap types including object subtype name and number of pages. |
|
66 |
*/ |
|
67 |
public function get_sitemap_type_data() { |
|
68 |
$sitemap_data = array(); |
|
69 |
||
70 |
$object_subtypes = $this->get_object_subtypes(); |
|
71 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
* If there are no object subtypes, include a single sitemap for the |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
* entire object type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
*/ |
16 | 76 |
if ( empty( $object_subtypes ) ) { |
77 |
$sitemap_data[] = array( |
|
78 |
'name' => '', |
|
79 |
'pages' => $this->get_max_num_pages(), |
|
80 |
); |
|
81 |
return $sitemap_data; |
|
82 |
} |
|
83 |
||
84 |
// Otherwise, include individual sitemaps for every object subtype. |
|
85 |
foreach ( $object_subtypes as $object_subtype_name => $data ) { |
|
86 |
$object_subtype_name = (string) $object_subtype_name; |
|
87 |
||
88 |
$sitemap_data[] = array( |
|
89 |
'name' => $object_subtype_name, |
|
90 |
'pages' => $this->get_max_num_pages( $object_subtype_name ), |
|
91 |
); |
|
92 |
} |
|
93 |
||
94 |
return $sitemap_data; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Lists sitemap pages exposed by this provider. |
|
99 |
* |
|
100 |
* The returned data is used to populate the sitemap entries of the index. |
|
101 |
* |
|
102 |
* @since 5.5.0 |
|
103 |
* |
|
104 |
* @return array[] Array of sitemap entries. |
|
105 |
*/ |
|
106 |
public function get_sitemap_entries() { |
|
107 |
$sitemaps = array(); |
|
108 |
||
109 |
$sitemap_types = $this->get_sitemap_type_data(); |
|
110 |
||
111 |
foreach ( $sitemap_types as $type ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
for ( $page = 1; $page <= $type['pages']; $page++ ) { |
16 | 113 |
$sitemap_entry = array( |
114 |
'loc' => $this->get_sitemap_url( $type['name'], $page ), |
|
115 |
); |
|
116 |
||
117 |
/** |
|
118 |
* Filters the sitemap entry for the sitemap index. |
|
119 |
* |
|
120 |
* @since 5.5.0 |
|
121 |
* |
|
122 |
* @param array $sitemap_entry Sitemap entry for the post. |
|
123 |
* @param string $object_type Object empty name. |
|
124 |
* @param string $object_subtype Object subtype name. |
|
125 |
* Empty string if the object type does not support subtypes. |
|
126 |
* @param int $page Page number of results. |
|
127 |
*/ |
|
128 |
$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page ); |
|
129 |
||
130 |
$sitemaps[] = $sitemap_entry; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
return $sitemaps; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Gets the URL of a sitemap entry. |
|
139 |
* |
|
140 |
* @since 5.5.0 |
|
141 |
* |
|
142 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
|
143 |
* |
|
144 |
* @param string $name The name of the sitemap. |
|
145 |
* @param int $page The page of the sitemap. |
|
146 |
* @return string The composed URL for a sitemap entry. |
|
147 |
*/ |
|
148 |
public function get_sitemap_url( $name, $page ) { |
|
149 |
global $wp_rewrite; |
|
150 |
||
151 |
// Accounts for cases where name is not included, ex: sitemaps-users-1.xml. |
|
152 |
$params = array_filter( |
|
153 |
array( |
|
154 |
'sitemap' => $this->name, |
|
155 |
'sitemap-subtype' => $name, |
|
156 |
'paged' => $page, |
|
157 |
) |
|
158 |
); |
|
159 |
||
160 |
$basename = sprintf( |
|
161 |
'/wp-sitemap-%1$s.xml', |
|
162 |
implode( '-', $params ) |
|
163 |
); |
|
164 |
||
165 |
if ( ! $wp_rewrite->using_permalinks() ) { |
|
19 | 166 |
$basename = '/?' . http_build_query( $params, '', '&' ); |
16 | 167 |
} |
168 |
||
169 |
return home_url( $basename ); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Returns the list of supported object subtypes exposed by the provider. |
|
174 |
* |
|
175 |
* @since 5.5.0 |
|
176 |
* |
|
177 |
* @return array List of object subtypes objects keyed by their name. |
|
178 |
*/ |
|
179 |
public function get_object_subtypes() { |
|
180 |
return array(); |
|
181 |
} |
|
182 |
} |