[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/sitemaps/ -> class-wp-sitemaps-renderer.php (source)

   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           * Following the deprecation of XSLT by browser manufacturers the
  62           * outcome of this filter is ignored.
  63           *
  64           * @since 5.5.0
  65           * @deprecated 7.2.0 Stylesheets are no longer supported.
  66           *
  67           * @param string $sitemap_url Full URL for the sitemaps XSL file.
  68           */
  69          apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' );
  70  
  71          return '';
  72      }
  73  
  74      /**
  75       * Gets the URL for the sitemap index stylesheet.
  76       *
  77       * @since 5.5.0
  78       * @deprecated 7.2.0 Stylesheets are no longer supported.
  79       *
  80       * @return string Empty string.
  81       */
  82  	public function get_sitemap_index_stylesheet_url() {
  83          _deprecated_function( __METHOD__, '7.2.0' );
  84  
  85          /**
  86           * Filters the URL for the sitemap index stylesheet.
  87           *
  88           * Following the deprecation of XSLT by browser manufacturers the
  89           * outcome of this filter is ignored.
  90           *
  91           * @since 5.5.0
  92           * @deprecated 7.2.0 Stylesheets are no longer supported.
  93           *
  94           * @param string $sitemap_url Full URL for the sitemaps index XSL file.
  95           */
  96          apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' );
  97  
  98          return '';
  99      }
 100  
 101      /**
 102       * Renders a sitemap index.
 103       *
 104       * @since 5.5.0
 105       *
 106       * @param array $sitemaps Array of sitemap URLs.
 107       */
 108  	public function render_index( $sitemaps ) {
 109          if ( ! headers_sent() ) {
 110              header( 'Content-Type: application/xml; charset=UTF-8' );
 111          }
 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          if ( ! headers_sent() ) {
 177              header( 'Content-Type: application/xml; charset=UTF-8' );
 178          }
 179  
 180          $this->check_for_simple_xml_availability();
 181  
 182          $sitemap_xml = $this->get_sitemap_xml( $url_list );
 183  
 184          if ( ! empty( $sitemap_xml ) ) {
 185              // All output is escaped within get_sitemap_xml().
 186              echo $sitemap_xml;
 187          }
 188      }
 189  
 190      /**
 191       * Gets XML for a sitemap.
 192       *
 193       * @since 5.5.0
 194       *
 195       * @param array $url_list Array of URLs for a sitemap.
 196       * @return string|false A well-formed XML string for a sitemap index. False on error.
 197       */
 198  	public function get_sitemap_xml( $url_list ) {
 199          $this->apply_deprecated_stylesheet_filters( 'sitemap' );
 200  
 201          $urlset = new SimpleXMLElement(
 202              sprintf(
 203                  '%1$s%2$s',
 204                  '<?xml version="1.0" encoding="UTF-8" ?>',
 205                  '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'
 206              )
 207          );
 208  
 209          foreach ( $url_list as $url_item ) {
 210              $url = $urlset->addChild( 'url' );
 211  
 212              // Add each element as a child node to the <url> entry.
 213              foreach ( $url_item as $name => $value ) {
 214                  if ( 'loc' === $name ) {
 215                      $url->addChild( $name, esc_url( $value ) );
 216                  } elseif ( in_array( $name, array( 'lastmod', 'changefreq', 'priority' ), true ) ) {
 217                      $url->addChild( $name, esc_xml( $value ) );
 218                  } else {
 219                      _doing_it_wrong(
 220                          __METHOD__,
 221                          sprintf(
 222                              /* translators: %s: List of element names. */
 223                              __( 'Fields other than %s are not currently supported for sitemaps.' ),
 224                              implode( ',', array( 'loc', 'lastmod', 'changefreq', 'priority' ) )
 225                          ),
 226                          '5.5.0'
 227                      );
 228                  }
 229              }
 230          }
 231  
 232          return $urlset->asXML();
 233      }
 234  
 235      /**
 236       * Applies the removed stylesheet filters so that their callbacks trigger deprecation notices.
 237       *
 238       * The stylesheet filters no longer have any effect. Applying them where a sitemap is
 239       * generated ensures a callback still added to one is reported rather than silently ignored.
 240       * No notice is triggered for a filter that has no callbacks.
 241       *
 242       * @since 7.2.0
 243       *
 244       * @param 'sitemap'|'index' $type Sitemap type.
 245       */
 246  	private function apply_deprecated_stylesheet_filters( string $type ): void {
 247          if ( 'index' === $type ) {
 248              /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */
 249              apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' );
 250  
 251              /**
 252               * Filters the content of the sitemap index stylesheet.
 253               *
 254               * Following the deprecation of XSLT by browser manufacturers the
 255               * outcome of this filter is ignored.
 256               *
 257               * @since 5.5.0
 258               * @deprecated 7.2.0 Stylesheets are no longer supported.
 259               *
 260               * @param string $xsl_content Full content for the XML stylesheet.
 261               */
 262              apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_content', array( '' ), '7.2.0' );
 263          } else {
 264              /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */
 265              apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' );
 266  
 267              /**
 268               * Filters the content of the sitemap stylesheet.
 269               *
 270               * Following the deprecation of XSLT by browser manufacturers the
 271               * outcome of this filter is ignored.
 272               *
 273               * @since 5.5.0
 274               * @deprecated 7.2.0 Stylesheets are no longer supported.
 275               *
 276               * @param string $xsl_content Full content for the XML stylesheet.
 277               */
 278              apply_filters_deprecated( 'wp_sitemaps_stylesheet_content', array( '' ), '7.2.0' );
 279          }
 280  
 281          /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php */
 282          apply_filters_deprecated( 'wp_sitemaps_stylesheet_css', array( '' ), '7.2.0' );
 283      }
 284  
 285      /**
 286       * Checks for the availability of the SimpleXML extension and errors if missing.
 287       *
 288       * @since 5.5.0
 289       */
 290  	private function check_for_simple_xml_availability() {
 291          if ( ! class_exists( 'SimpleXMLElement' ) ) {
 292              add_filter(
 293                  'wp_die_handler',
 294                  static function () {
 295                      return '_xml_wp_die_handler';
 296                  }
 297              );
 298  
 299              wp_die(
 300                  sprintf(
 301                      /* translators: %s: SimpleXML */
 302                      esc_xml( __( 'Could not generate XML sitemap due to missing %s extension' ) ),
 303                      'SimpleXML'
 304                  ),
 305                  esc_xml( __( 'WordPress &rsaquo; Error' ) ),
 306                  array(
 307                      'response' => 501, // "Not implemented".
 308                  )
 309              );
 310          }
 311      }
 312  }


Generated : Thu Nov 12 08:20:02 2026 Cross-referenced by PHPXref