PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / sitemaps / class-sitemaps-renderer.php

class-sitemaps-renderer.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at inc/sitemaps/class-sitemaps-renderer.php

343 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\XML_Sitemaps
6 */
7
8 /**
9 * Renders XML output for sitemaps.
10 */
11 class WPSEO_Sitemaps_Renderer {
12
13 /**
14 * XSL stylesheet for styling a sitemap for web browsers.
15 *
16 * @var string
17 */
18 protected $stylesheet = '';
19
20 /**
21 * Holds the get_bloginfo( 'charset' ) value to reuse for performance.
22 *
23 * @var string
24 */
25 protected $charset = 'UTF-8';
26
27 /**
28 * Holds charset of output, might be converted.
29 *
30 * @var string
31 */
32 protected $output_charset = 'UTF-8';
33
34 /**
35 * If data encoding needs to be converted for output.
36 *
37 * @var bool
38 */
39 protected $needs_conversion = false;
40
41 /**
42 * Set up object properties.
43 */
44 public function __construct() {
45 $stylesheet_url = preg_replace( '/(^http[s]?:)/', '', $this->get_xsl_url() );
46 $this->stylesheet = '<?xml-stylesheet type="text/xsl" href="' . esc_url( $stylesheet_url ) . '"?>';
47 $this->charset = get_bloginfo( 'charset' );
48 $this->output_charset = $this->charset;
49
50 if (
51 $this->charset !== 'UTF-8'
52 && function_exists( 'mb_list_encodings' )
53 && in_array( $this->charset, mb_list_encodings(), true )
54 ) {
55 $this->output_charset = 'UTF-8';
56 }
57
58 $this->needs_conversion = $this->output_charset !== $this->charset;
59 }
60
61 /**
62 * Builds the sitemap index.
63 *
64 * @param array $links Set of sitemaps index links.
65 *
66 * @return string
67 */
68 public function get_index( $links ) {
69
70 $xml = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
71
72 foreach ( $links as $link ) {
73 $xml .= $this->sitemap_index_url( $link );
74 }
75
76 /**
77 * Filter to append sitemaps to the index.
78 *
79 * @param string $index String to append to sitemaps index, defaults to empty.
80 */
81 $xml .= apply_filters( 'wpseo_sitemap_index', '' );
82 $xml .= '</sitemapindex>';
83
84 return $xml;
85 }
86
87 /**
88 * Builds the sitemap.
89 *
90 * @param array $links Set of sitemap links.
91 * @param string $type Sitemap type.
92 * @param int $current_page Current sitemap page number.
93 *
94 * @return string
95 */
96 public function get_sitemap( $links, $type, $current_page ) {
97
98 $urlset = '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" '
99 . 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd '
100 . 'http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" '
101 . 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
102
103 /**
104 * Filters the `urlset` for a sitemap by type.
105 *
106 * @api string $urlset The output for the sitemap's `urlset`.
107 */
108 $xml = apply_filters( "wpseo_sitemap_{$type}_urlset", $urlset );
109
110 foreach ( $links as $url ) {
111 $xml .= $this->sitemap_url( $url );
112 }
113
114 /**
115 * Filter to add extra URLs to the XML sitemap by type.
116 *
117 * Only runs for the first page, not on all.
118 *
119 * @param string $content String content to add, defaults to empty.
120 */
121 if ( $current_page === 1 ) {
122 $xml .= apply_filters( "wpseo_sitemap_{$type}_content", '' );
123 }
124
125 $xml .= '</urlset>';
126
127 return $xml;
128 }
129
130 /**
131 * Produce final XML output with debug information.
132 *
133 * @param string $sitemap Sitemap XML.
134 *
135 * @return string
136 */
137 public function get_output( $sitemap ) {
138
139 $output = '<?xml version="1.0" encoding="' . esc_attr( $this->output_charset ) . '"?>';
140
141 if ( $this->stylesheet ) {
142 /**
143 * Filter the stylesheet URL for the XML sitemap.
144 *
145 * @param string $stylesheet Stylesheet URL.
146 */
147 $output .= apply_filters( 'wpseo_stylesheet_url', $this->stylesheet ) . "\n";
148 }
149
150 $output .= $sitemap;
151 $output .= "\n<!-- XML Sitemap generated by Yoast SEO -->";
152
153 return $output;
154 }
155
156 /**
157 * Get charset for the output.
158 *
159 * @return string
160 */
161 public function get_output_charset() {
162 return $this->output_charset;
163 }
164
165 /**
166 * Set a custom stylesheet for this sitemap. Set to empty to just remove the default stylesheet.
167 *
168 * @param string $stylesheet Full XML-stylesheet declaration.
169 */
170 public function set_stylesheet( $stylesheet ) {
171 $this->stylesheet = $stylesheet;
172 }
173
174 /**
175 * Build the `<sitemap>` tag for a given URL.
176 *
177 * @param array $url Array of parts that make up this entry.
178 *
179 * @return string
180 */
181 protected function sitemap_index_url( $url ) {
182
183 $date = null;
184
185 if ( ! empty( $url['lastmod'] ) ) {
186 $date = YoastSEO()->helpers->date->format( $url['lastmod'] );
187 }
188
189 $url['loc'] = htmlspecialchars( $url['loc'], ENT_COMPAT, $this->output_charset, false );
190
191 $output = "\t<sitemap>\n";
192 $output .= "\t\t<loc>" . $url['loc'] . "</loc>\n";
193 $output .= empty( $date ) ? '' : "\t\t<lastmod>" . htmlspecialchars( $date, ENT_COMPAT, $this->output_charset, false ) . "</lastmod>\n";
194 $output .= "\t</sitemap>\n";
195
196 return $output;
197 }
198
199 /**
200 * Build the `<url>` tag for a given URL.
201 *
202 * Public access for backwards compatibility reasons.
203 *
204 * @param array $url Array of parts that make up this entry.
205 *
206 * @return string
207 */
208 public function sitemap_url( $url ) {
209
210 $date = null;
211
212
213 if ( ! empty( $url['mod'] ) ) {
214 // Create a DateTime object date in the correct timezone.
215 $date = YoastSEO()->helpers->date->format( $url['mod'] );
216 }
217
218 $url['loc'] = htmlspecialchars( $url['loc'], ENT_COMPAT, $this->output_charset, false );
219
220 $output = "\t<url>\n";
221 $output .= "\t\t<loc>" . $this->encode_url_rfc3986( $url['loc'] ) . "</loc>\n";
222 $output .= empty( $date ) ? '' : "\t\t<lastmod>" . htmlspecialchars( $date, ENT_COMPAT, $this->output_charset, false ) . "</lastmod>\n";
223
224 if ( empty( $url['images'] ) ) {
225 $url['images'] = [];
226 }
227
228 foreach ( $url['images'] as $img ) {
229
230 if ( empty( $img['src'] ) ) {
231 continue;
232 }
233
234 $output .= "\t\t<image:image>\n";
235 $output .= "\t\t\t<image:loc>" . esc_html( $this->encode_url_rfc3986( $img['src'] ) ) . "</image:loc>\n";
236
237 if ( ! empty( $img['title'] ) ) {
238
239 $title = $img['title'];
240
241 if ( $this->needs_conversion ) {
242 $title = mb_convert_encoding( $title, $this->output_charset, $this->charset );
243 }
244
245 $title = _wp_specialchars( html_entity_decode( $title, ENT_QUOTES, $this->output_charset ) );
246 $output .= "\t\t\t<image:title><![CDATA[{$title}]]></image:title>\n";
247 }
248
249 if ( ! empty( $img['alt'] ) ) {
250
251 $alt = $img['alt'];
252
253 if ( $this->needs_conversion ) {
254 $alt = mb_convert_encoding( $alt, $this->output_charset, $this->charset );
255 }
256
257 $alt = _wp_specialchars( html_entity_decode( $alt, ENT_QUOTES, $this->output_charset ) );
258 $output .= "\t\t\t<image:caption><![CDATA[{$alt}]]></image:caption>\n";
259 }
260
261 $output .= "\t\t</image:image>\n";
262 }
263 unset( $img, $title, $alt );
264
265 $output .= "\t</url>\n";
266
267 /**
268 * Filters the output for the sitemap URL tag.
269 *
270 * @api string $output The output for the sitemap url tag.
271 *
272 * @param array $url The sitemap URL array on which the output is based.
273 */
274 return apply_filters( 'wpseo_sitemap_url', $output, $url );
275 }
276
277 /**
278 * Apply some best effort conversion to comply with RFC3986.
279 *
280 * @param string $url URL to encode.
281 *
282 * @return string
283 */
284 protected function encode_url_rfc3986( $url ) {
285
286 if ( filter_var( $url, FILTER_VALIDATE_URL ) ) {
287 return $url;
288 }
289
290 $path = wp_parse_url( $url, PHP_URL_PATH );
291
292 if ( ! empty( $path ) && $path !== '/' ) {
293 $encoded_path = explode( '/', $path );
294
295 // First decode the path, to prevent double encoding.
296 $encoded_path = array_map( 'rawurldecode', $encoded_path );
297
298 $encoded_path = array_map( 'rawurlencode', $encoded_path );
299 $encoded_path = implode( '/', $encoded_path );
300
301 $url = str_replace( $path, $encoded_path, $url );
302 }
303
304 $query = wp_parse_url( $url, PHP_URL_QUERY );
305
306 if ( ! empty( $query ) ) {
307
308 parse_str( $query, $parsed_query );
309
310 $parsed_query = http_build_query( $parsed_query, '', '&amp;', PHP_QUERY_RFC3986 );
311
312 $url = str_replace( $query, $parsed_query, $url );
313 }
314
315 return $url;
316 }
317
318 /**
319 * Retrieves the XSL URL that should be used in the current environment
320 *
321 * When home_url and site_url are not the same, the home_url should be used.
322 * This is because the XSL needs to be served from the same domain, protocol and port
323 * as the XML file that is loading it.
324 *
325 * @return string The XSL URL that needs to be used.
326 */
327 protected function get_xsl_url() {
328 if ( home_url() !== site_url() ) {
329 return home_url( 'main-sitemap.xsl' );
330 }
331
332 /*
333 * Fallback to circumvent a cross-domain security problem when the XLS file is
334 * loaded from a different (sub)domain.
335 */
336 if ( strpos( plugins_url(), home_url() ) !== 0 ) {
337 return home_url( 'main-sitemap.xsl' );
338 }
339
340 return plugin_dir_url( WPSEO_FILE ) . 'css/main-sitemap.xsl';
341 }
342 }
343