| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Sitemaps: WP_Sitemaps_Renderer class 4 * 5 * Responsible for rendering Sitemaps data to XML in accordance with sitemap protocol. 6 * 7 * @package WordPress 8 * @subpackage Sitemaps 9 * @since 5.5.0 10 */ 11 12 /** 13 * Class WP_Sitemaps_Renderer 14 * 15 * @since 5.5.0 16 */ 17 #[AllowDynamicProperties] 18 class WP_Sitemaps_Renderer { 19 /** 20 * XSL stylesheet for styling a sitemap for web browsers. 21 * 22 * @since 5.5.0 23 * @deprecated 7.2.0 Stylesheets are no longer supported. 24 * 25 * @var string 26 */ 27 protected $stylesheet = ''; 28 29 /** 30 * XSL stylesheet for styling a sitemap for web browsers. 31 * 32 * @since 5.5.0 33 * @deprecated 7.2.0 Stylesheets are no longer supported. 34 * 35 * @var string 36 */ 37 protected $stylesheet_index = ''; 38 39 /** 40 * WP_Sitemaps_Renderer constructor. 41 * 42 * @since 5.5.0 43 * @since 7.2.0 No longer sets up the stylesheets, which are no longer supported. 44 */ 45 public function __construct() {} 46 47 /** 48 * Gets the URL for the sitemap stylesheet. 49 * 50 * @since 5.5.0 51 * @deprecated 7.2.0 Stylesheets are no longer supported. 52 * 53 * @return string Empty string. 54 */ 55 public function get_sitemap_stylesheet_url() { 56 _deprecated_function( __METHOD__, '7.2.0' ); 57 58 /** 59 * Filters the URL for the sitemap stylesheet. 60 * 61 * If a falsey value is returned, no stylesheet will be used and 62 * Following the deprecation of XSLT by browser manufacturers the 63 * outcome of this filter is ignored. 64 * 65 * @since 5.5.0 66 * @deprecated 7.2.0 Stylesheets are no longer supported. 67 * 68 * @param string $sitemap_url Full URL for the sitemaps XSL file. 69 */ 70 apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' ); 71 72 return ''; 73 } 74 75 /** 76 * Gets the URL for the sitemap index stylesheet. 77 * 78 * @since 5.5.0 79 * @deprecated 7.2.0 Stylesheets are no longer supported. 80 * 81 * @return string Empty string. 82 */ 83 public function get_sitemap_index_stylesheet_url() { 84 _deprecated_function( __METHOD__, '7.2.0' ); 85 86 /** 87 * Filters the URL for the sitemap index stylesheet. 88 * 89 * If a falsey value is returned, no stylesheet will be used and 90 * Following the deprecation of XSLT by browser manufacturers the 91 * outcome of this filter is ignored. 92 * 93 * @since 5.5.0 94 * @deprecated 7.2.0 Stylesheets are no longer supported. 95 * 96 * @param string $sitemap_url Full URL for the sitemaps index XSL file. 97 */ 98 apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' ); 99 100 return ''; 101 } 102 103 /** 104 * Renders a sitemap index. 105 * 106 * @since 5.5.0 107 * 108 * @param array $sitemaps Array of sitemap URLs. 109 */ 110 public function render_index( $sitemaps ) { 111 header( 'Content-Type: application/xml; charset=UTF-8' ); 112 113 $this->check_for_simple_xml_availability(); 114 115 $index_xml = $this->get_sitemap_index_xml( $sitemaps ); 116 117 if ( ! empty( $index_xml ) ) { 118 // All output is escaped within get_sitemap_index_xml(). 119 echo $index_xml; 120 } 121 } 122 123 /** 124 * Gets XML for a sitemap index. 125 * 126 * @since 5.5.0 127 * 128 * @param array $sitemaps Array of sitemap URLs. 129 * @return string|false A well-formed XML string for a sitemap index. False on error. 130 */ 131 public function get_sitemap_index_xml( $sitemaps ) { 132 $this->apply_deprecated_stylesheet_filters( 'index' ); 133 134 $sitemap_index = new SimpleXMLElement( 135 sprintf( 136 '%1$s%2$s', 137 '<?xml version="1.0" encoding="UTF-8" ?>', 138 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />' 139 ) 140 ); 141 142 foreach ( $sitemaps as $entry ) { 143 $sitemap = $sitemap_index->addChild( 'sitemap' ); 144 145 // Add each element as a child node to the <sitemap> entry. 146 foreach ( $entry as $name => $value ) { 147 if ( 'loc' === $name ) { 148 $sitemap->addChild( $name, esc_url( $value ) ); 149 } elseif ( 'lastmod' === $name ) { 150 $sitemap->addChild( $name, esc_xml( $value ) ); 151 } else { 152 _doing_it_wrong( 153 __METHOD__, 154 sprintf( 155 /* translators: %s: List of element names. */ 156 __( 'Fields other than %s are not currently supported for the sitemap index.' ), 157 implode( ',', array( 'loc', 'lastmod' ) ) 158 ), 159 '5.5.0' 160 ); 161 } 162 } 163 } 164 165 return $sitemap_index->asXML(); 166 } 167 168 /** 169 * Renders a sitemap. 170 * 171 * @since 5.5.0 172 * 173 * @param array $url_list Array of URLs for a sitemap. 174 */ 175 public function render_sitemap( $url_list ) { 176 header( 'Content-Type: application/xml; charset=UTF-8' ); 177 178 $this->check_for_simple_xml_availability(); 179 180 $sitemap_xml = $this->get_sitemap_xml( $url_list ); 181 182 if ( ! empty( $sitemap_xml ) ) { 183 // All output is escaped within get_sitemap_xml(). 184 echo $sitemap_xml; 185 } 186 } 187 188 /** 189 * Gets XML for a sitemap. 190 * 191 * @since 5.5.0 192 * 193 * @param array $url_list Array of URLs for a sitemap. 194 * @return string|false A well-formed XML string for a sitemap index. False on error. 195 */ 196 public function get_sitemap_xml( $url_list ) { 197 $this->apply_deprecated_stylesheet_filters( 'sitemap' ); 198 199 $urlset = new SimpleXMLElement( 200 sprintf( 201 '%1$s%2$s', 202 '<?xml version="1.0" encoding="UTF-8" ?>', 203 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />' 204 ) 205 ); 206 207 foreach ( $url_list as $url_item ) { 208 $url = $urlset->addChild( 'url' ); 209 210 // Add each element as a child node to the <url> entry. 211 foreach ( $url_item as $name => $value ) { 212 if ( 'loc' === $name ) { 213 $url->addChild( $name, esc_url( $value ) ); 214 } elseif ( in_array( $name, array( 'lastmod', 'changefreq', 'priority' ), true ) ) { 215 $url->addChild( $name, esc_xml( $value ) ); 216 } else { 217 _doing_it_wrong( 218 __METHOD__, 219 sprintf( 220 /* translators: %s: List of element names. */ 221 __( 'Fields other than %s are not currently supported for sitemaps.' ), 222 implode( ',', array( 'loc', 'lastmod', 'changefreq', 'priority' ) ) 223 ), 224 '5.5.0' 225 ); 226 } 227 } 228 } 229 230 return $urlset->asXML(); 231 } 232 233 /** 234 * Applies the removed stylesheet filters so that their callbacks trigger deprecation notices. 235 * 236 * The stylesheet filters no longer have any effect. Applying them where a sitemap is 237 * generated ensures a callback still added to one is reported rather than silently ignored. 238 * No notice is triggered for a filter that has no callbacks. 239 * 240 * @since 7.2.0 241 * 242 * @param 'sitemap'|'index' $type Sitemap type. 243 */ 244 private function apply_deprecated_stylesheet_filters( string $type ): void { 245 if ( 'index' === $type ) { 246 /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */ 247 apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' ); 248 249 /** 250 * Filters the content of the sitemap index stylesheet. 251 * 252 * Following the deprecation of XSLT by browser manufacturers the 253 * outcome of this filter is ignored. 254 * 255 * @since 5.5.0 256 * @deprecated 7.2.0 Stylesheets are no longer supported. 257 * 258 * @param string $xsl_content Full content for the XML stylesheet. 259 */ 260 apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_content', array( '' ), '7.2.0' ); 261 } else { 262 /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */ 263 apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' ); 264 265 /** 266 * Filters the content of the sitemap stylesheet. 267 * 268 * Following the deprecation of XSLT by browser manufacturers the 269 * outcome of this filter is ignored. 270 * 271 * @since 5.5.0 272 * @deprecated 7.2.0 Stylesheets are no longer supported. 273 * 274 * @param string $xsl_content Full content for the XML stylesheet. 275 */ 276 apply_filters_deprecated( 'wp_sitemaps_stylesheet_content', array( '' ), '7.2.0' ); 277 } 278 279 /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php */ 280 apply_filters_deprecated( 'wp_sitemaps_stylesheet_css', array( '' ), '7.2.0' ); 281 } 282 283 /** 284 * Checks for the availability of the SimpleXML extension and errors if missing. 285 * 286 * @since 5.5.0 287 */ 288 private function check_for_simple_xml_availability() { 289 if ( ! class_exists( 'SimpleXMLElement' ) ) { 290 add_filter( 291 'wp_die_handler', 292 static function () { 293 return '_xml_wp_die_handler'; 294 } 295 ); 296 297 wp_die( 298 sprintf( 299 /* translators: %s: SimpleXML */ 300 esc_xml( __( 'Could not generate XML sitemap due to missing %s extension' ) ), 301 'SimpleXML' 302 ), 303 esc_xml( __( 'WordPress › Error' ) ), 304 array( 305 'response' => 501, // "Not implemented". 306 ) 307 ); 308 } 309 } 310 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Oct 3 08:20:34 2026 | Cross-referenced by PHPXref |