[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Sitemaps: WP_Sitemaps_Stylesheet class
   4   *
   5   * This class provides the XSL stylesheets to style all sitemaps.
   6   *
   7   * @package WordPress
   8   * @subpackage Sitemaps
   9   * @since 5.5.0
  10   */
  11  
  12  /**
  13   * Stylesheet provider class.
  14   *
  15   * @since 5.5.0
  16   */
  17  #[AllowDynamicProperties]
  18  class WP_Sitemaps_Stylesheet {
  19      /**
  20       * Renders the XSL stylesheet depending on whether it's the sitemap index or not.
  21       *
  22       * @since 5.5.0
  23       *
  24       * @param string $type Stylesheet type. Either 'sitemap' or 'index'.
  25       * @return never
  26       */
  27  	public function render_stylesheet( $type ) {
  28          header( 'Content-Type: application/xml; charset=UTF-8' );
  29  
  30          if ( 'sitemap' === $type ) {
  31              // All content is escaped below.
  32              echo $this->get_sitemap_stylesheet();
  33          }
  34  
  35          if ( 'index' === $type ) {
  36              // All content is escaped below.
  37              echo $this->get_sitemap_index_stylesheet();
  38          }
  39  
  40          exit;
  41      }
  42  
  43      /**
  44       * Returns the escaped XSL for all sitemaps, except index.
  45       *
  46       * @since 5.5.0
  47       */
  48  	public function get_sitemap_stylesheet() {
  49          $css         = $this->get_stylesheet_css();
  50          $title       = esc_xml( __( 'XML Sitemap' ) );
  51          $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
  52          $learn_more  = sprintf(
  53              '<a href="%s">%s</a>',
  54              esc_url( __( 'https://www.sitemaps.org/' ) ),
  55              esc_xml( __( 'Learn more about XML sitemaps.' ) )
  56          );
  57  
  58          $text = sprintf(
  59              /* translators: %s: Number of URLs. */
  60              esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
  61              '<xsl:value-of select="count( sitemap:urlset/sitemap:url )" />'
  62          );
  63  
  64          $lang       = get_language_attributes( 'html' );
  65          $url        = esc_xml( __( 'URL' ) );
  66          $lastmod    = esc_xml( __( 'Last Modified' ) );
  67          $changefreq = esc_xml( __( 'Change Frequency' ) );
  68          $priority   = esc_xml( __( 'Priority' ) );
  69  
  70          $xsl_content = <<<XSL
  71  <?xml version="1.0" encoding="UTF-8"?>
  72  <xsl:stylesheet
  73          version="1.0"
  74          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  75          xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
  76          exclude-result-prefixes="sitemap"
  77          >
  78  
  79      <xsl:output method="html" encoding="UTF-8" indent="yes" />
  80  
  81      <!--
  82        Set variables for whether lastmod, changefreq or priority occur for any url in the sitemap.
  83        We do this up front because it can be expensive in a large sitemap.
  84        -->
  85      <xsl:variable name="has-lastmod"    select="count( /sitemap:urlset/sitemap:url/sitemap:lastmod )"    />
  86      <xsl:variable name="has-changefreq" select="count( /sitemap:urlset/sitemap:url/sitemap:changefreq )" />
  87      <xsl:variable name="has-priority"   select="count( /sitemap:urlset/sitemap:url/sitemap:priority )"   />
  88  
  89      <xsl:template match="/">
  90          <html {$lang}>
  91              <head>
  92                  <title>{$title}</title>
  93                  <style>
  94                      {$css}
  95                  </style>
  96              </head>
  97              <body>
  98                  <div id="sitemap">
  99                      <div id="sitemap__header">
 100                          <h1>{$title}</h1>
 101                          <p>{$description}</p>
 102                          <p>{$learn_more}</p>
 103                      </div>
 104                      <div id="sitemap__content">
 105                          <p class="text">{$text}</p>
 106                          <table id="sitemap__table">
 107                              <thead>
 108                                  <tr>
 109                                      <th class="loc">{$url}</th>
 110                                      <xsl:if test="\$has-lastmod">
 111                                          <th class="lastmod">{$lastmod}</th>
 112                                      </xsl:if>
 113                                      <xsl:if test="\$has-changefreq">
 114                                          <th class="changefreq">{$changefreq}</th>
 115                                      </xsl:if>
 116                                      <xsl:if test="\$has-priority">
 117                                          <th class="priority">{$priority}</th>
 118                                      </xsl:if>
 119                                  </tr>
 120                              </thead>
 121                              <tbody>
 122                                  <xsl:for-each select="sitemap:urlset/sitemap:url">
 123                                      <tr>
 124                                          <td class="loc"><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc" /></a></td>
 125                                          <xsl:if test="\$has-lastmod">
 126                                              <td class="lastmod"><xsl:value-of select="sitemap:lastmod" /></td>
 127                                          </xsl:if>
 128                                          <xsl:if test="\$has-changefreq">
 129                                              <td class="changefreq"><xsl:value-of select="sitemap:changefreq" /></td>
 130                                          </xsl:if>
 131                                          <xsl:if test="\$has-priority">
 132                                              <td class="priority"><xsl:value-of select="sitemap:priority" /></td>
 133                                          </xsl:if>
 134                                      </tr>
 135                                  </xsl:for-each>
 136                              </tbody>
 137                          </table>
 138                      </div>
 139                  </div>
 140              </body>
 141          </html>
 142      </xsl:template>
 143  </xsl:stylesheet>
 144  
 145  XSL;
 146  
 147          /**
 148           * Filters the content of the sitemap stylesheet.
 149           *
 150           * @since 5.5.0
 151           *
 152           * @param string $xsl_content Full content for the XML stylesheet.
 153           */
 154          return apply_filters( 'wp_sitemaps_stylesheet_content', $xsl_content );
 155      }
 156  
 157      /**
 158       * Returns the escaped XSL for the index sitemaps.
 159       *
 160       * @since 5.5.0
 161       */
 162  	public function get_sitemap_index_stylesheet() {
 163          $css         = $this->get_stylesheet_css();
 164          $title       = esc_xml( __( 'XML Sitemap' ) );
 165          $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
 166          $learn_more  = sprintf(
 167              '<a href="%s">%s</a>',
 168              esc_url( __( 'https://www.sitemaps.org/' ) ),
 169              esc_xml( __( 'Learn more about XML sitemaps.' ) )
 170          );
 171  
 172          $text = sprintf(
 173              /* translators: %s: Number of URLs. */
 174              esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
 175              '<xsl:value-of select="count( sitemap:sitemapindex/sitemap:sitemap )" />'
 176          );
 177  
 178          $lang    = get_language_attributes( 'html' );
 179          $url     = esc_xml( __( 'URL' ) );
 180          $lastmod = esc_xml( __( 'Last Modified' ) );
 181  
 182          $xsl_content = <<<XSL
 183  <?xml version="1.0" encoding="UTF-8"?>
 184  <xsl:stylesheet
 185          version="1.0"
 186          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 187          xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
 188          exclude-result-prefixes="sitemap"
 189          >
 190  
 191      <xsl:output method="html" encoding="UTF-8" indent="yes" />
 192  
 193      <!--
 194        Set variables for whether lastmod occurs for any sitemap in the index.
 195        We do this up front because it can be expensive in a large sitemap.
 196        -->
 197      <xsl:variable name="has-lastmod" select="count( /sitemap:sitemapindex/sitemap:sitemap/sitemap:lastmod )" />
 198  
 199      <xsl:template match="/">
 200          <html {$lang}>
 201              <head>
 202                  <title>{$title}</title>
 203                  <style>
 204                      {$css}
 205                  </style>
 206              </head>
 207              <body>
 208                  <div id="sitemap">
 209                      <div id="sitemap__header">
 210                          <h1>{$title}</h1>
 211                          <p>{$description}</p>
 212                          <p>{$learn_more}</p>
 213                      </div>
 214                      <div id="sitemap__content">
 215                          <p class="text">{$text}</p>
 216                          <table id="sitemap__table">
 217                              <thead>
 218                                  <tr>
 219                                      <th class="loc">{$url}</th>
 220                                      <xsl:if test="\$has-lastmod">
 221                                          <th class="lastmod">{$lastmod}</th>
 222                                      </xsl:if>
 223                                  </tr>
 224                              </thead>
 225                              <tbody>
 226                                  <xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
 227                                      <tr>
 228                                          <td class="loc"><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc" /></a></td>
 229                                          <xsl:if test="\$has-lastmod">
 230                                              <td class="lastmod"><xsl:value-of select="sitemap:lastmod" /></td>
 231                                          </xsl:if>
 232                                      </tr>
 233                                  </xsl:for-each>
 234                              </tbody>
 235                          </table>
 236                      </div>
 237                  </div>
 238              </body>
 239          </html>
 240      </xsl:template>
 241  </xsl:stylesheet>
 242  
 243  XSL;
 244  
 245          /**
 246           * Filters the content of the sitemap index stylesheet.
 247           *
 248           * @since 5.5.0
 249           *
 250           * @param string $xsl_content Full content for the XML stylesheet.
 251           */
 252          return apply_filters( 'wp_sitemaps_stylesheet_index_content', $xsl_content );
 253      }
 254  
 255      /**
 256       * Gets the CSS to be included in sitemap XSL stylesheets.
 257       *
 258       * @since 5.5.0
 259       *
 260       * @return string The CSS.
 261       */
 262  	public function get_stylesheet_css() {
 263          $text_align = is_rtl() ? 'right' : 'left';
 264  
 265          $css = <<<EOF
 266  
 267                      body {
 268                          font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
 269                          color: #444;
 270                      }
 271  
 272                      #sitemap {
 273                          max-width: 980px;
 274                          margin: 0 auto;
 275                      }
 276  
 277                      #sitemap__table {
 278                          width: 100%;
 279                          border: solid 1px #ccc;
 280                          border-collapse: collapse;
 281                      }
 282  
 283                       #sitemap__table tr td.loc {
 284                          /*
 285                           * URLs should always be LTR.
 286                           * See https://core.trac.wordpress.org/ticket/16834
 287                           * and https://core.trac.wordpress.org/ticket/49949
 288                           */
 289                          direction: ltr;
 290                      }
 291  
 292                      #sitemap__table tr th {
 293                          text-align: {$text_align};
 294                      }
 295  
 296                      #sitemap__table tr td,
 297                      #sitemap__table tr th {
 298                          padding: 10px;
 299                      }
 300  
 301                      #sitemap__table tr:nth-child(odd) td {
 302                          background-color: #eee;
 303                      }
 304  
 305                      a:hover {
 306                          text-decoration: none;
 307                      }
 308  
 309  EOF;
 310  
 311          /**
 312           * Filters the CSS only for the sitemap stylesheet.
 313           *
 314           * @since 5.5.0
 315           *
 316           * @param string $css CSS to be applied to default XSL file.
 317           */
 318          return apply_filters( 'wp_sitemaps_stylesheet_css', $css );
 319      }
 320  }


Generated : Sat Sep 26 08:20:30 2026 Cross-referenced by PHPXref