<?php /** * Sitemaps: WP_Sitemaps_Stylesheet class * * This class provides the XSL stylesheets to style all sitemaps. * * @package WordPress * @subpackage Sitemaps * @since 5.5.0 */
/** * Stylesheet provider class. * * @since 5.5.0 */ #[AllowDynamicProperties] class WP_Sitemaps_Stylesheet { /** * Renders the XSL stylesheet depending on whether it's the sitemap index or not. * * @param string $type Stylesheet type. Either 'sitemap' or 'index'. */ public function render_stylesheet( $type ) { header( 'Content-Type: application/xml; charset=UTF-8' );
if ( 'sitemap' === $type ) { // All content is escaped below. echo $this->get_sitemap_stylesheet(); }
if ( 'index' === $type ) { // All content is escaped below. echo $this->get_sitemap_index_stylesheet(); }
exit; }
/** * Returns the escaped XSL for all sitemaps, except index. * * @since 5.5.0 */ public function get_sitemap_stylesheet() { $css = $this->get_stylesheet_css(); $title = esc_xml( __( 'XML Sitemap' ) ); $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) ); $learn_more = sprintf( '<a href="%s">%s</a>', esc_url( __( 'https://www.sitemaps.org/' ) ), esc_xml( __( 'Learn more about XML sitemaps.' ) ) );
$text = sprintf( /* translators: %s: Number of URLs. */ esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ), '<xsl:value-of select="count( sitemap:urlset/sitemap:url )" />' );
<!-- Set variables for whether lastmod, changefreq or priority occur for any url in the sitemap. We do this up front because it can be expensive in a large sitemap. --> <xsl:variable name="has-lastmod" select="count( /sitemap:urlset/sitemap:url/sitemap:lastmod )" /> <xsl:variable name="has-changefreq" select="count( /sitemap:urlset/sitemap:url/sitemap:changefreq )" /> <xsl:variable name="has-priority" select="count( /sitemap:urlset/sitemap:url/sitemap:priority )" />
/** * Filters the content of the sitemap stylesheet. * * @since 5.5.0 * * @param string $xsl_content Full content for the XML stylesheet. */ return apply_filters( 'wp_sitemaps_stylesheet_content', $xsl_content ); }
/** * Returns the escaped XSL for the index sitemaps. * * @since 5.5.0 */ public function get_sitemap_index_stylesheet() { $css = $this->get_stylesheet_css(); $title = esc_xml( __( 'XML Sitemap' ) ); $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) ); $learn_more = sprintf( '<a href="%s">%s</a>', esc_url( __( 'https://www.sitemaps.org/' ) ), esc_xml( __( 'Learn more about XML sitemaps.' ) ) );
$text = sprintf( /* translators: %s: Number of URLs. */ esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ), '<xsl:value-of select="count( sitemap:sitemapindex/sitemap:sitemap )" />' );
<!-- Set variables for whether lastmod occurs for any sitemap in the index. We do this up front because it can be expensive in a large sitemap. --> <xsl:variable name="has-lastmod" select="count( /sitemap:sitemapindex/sitemap:sitemap/sitemap:lastmod )" />
/** * Filters the content of the sitemap index stylesheet. * * @since 5.5.0 * * @param string $xsl_content Full content for the XML stylesheet. */ return apply_filters( 'wp_sitemaps_stylesheet_index_content', $xsl_content ); }
/** * Gets the CSS to be included in sitemap XSL stylesheets. * * @since 5.5.0 * * @return string The CSS. */ public function get_stylesheet_css() { $text_align = is_rtl() ? 'right' : 'left';
/** * Filters the CSS only for the sitemap stylesheet. * * @since 5.5.0 * * @param string $css CSS to be applied to default XSL file. */ return apply_filters( 'wp_sitemaps_stylesheet_css', $css ); } }