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