<?php

class WP_Sitemaps_Stylesheet
{
    public function render_stylesheet($type)
    {
        header('Content-type: application/xml; charset=UTF-8');
        if ('sitemap' === $type) {
            echo $this->get_sitemap_stylesheet();
        }
        if ('index' === $type) {
            echo $this->get_sitemap_index_stylesheet();
        }
        exit;
    }
    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(esc_xml(__('Number of URLs in this XML Sitemap: %s.')), '<xsl:value-of select="count( sitemap:urlset/sitemap:url )" />');
        $lang = get_language_attributes('html');
        $url = esc_xml(__('URL'));
        $lastmod = esc_xml(__('Last Modified'));
        $changefreq = esc_xml(__('Change Frequency'));
        $priority = esc_xml(__('Priority'));
        $xsl_content = <<<XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
\t\tversion="1.0"
\t\txmlns:xsl="http://www.w3.org/1999/XSL/Transform"
\t\txmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
\t\texclude-result-prefixes="sitemap"
\t\t>

\t<xsl:output method="html" encoding="UTF-8" indent="yes"/>

\t<!--
\t  Set variables for whether lastmod, changefreq or priority occur for any url in the sitemap.
\t  We do this up front because it can be expensive in a large sitemap.
\t  -->
\t<xsl:variable name="has-lastmod"    select="count( /sitemap:urlset/sitemap:url/sitemap:lastmod )"    />
\t<xsl:variable name="has-changefreq" select="count( /sitemap:urlset/sitemap:url/sitemap:changefreq )" />
\t<xsl:variable name="has-priority"   select="count( /sitemap:urlset/sitemap:url/sitemap:priority )"   />

\t<xsl:template match="/">
\t\t<html {$lang}>
\t\t\t<head>
\t\t\t\t<title>{$title}</title>
\t\t\t\t<style>
\t\t\t\t\t{$css}
\t\t\t\t</style>
\t\t\t</head>
\t\t\t<body>
\t\t\t\t<div id="sitemap">
\t\t\t\t\t<div id="sitemap__header">
\t\t\t\t\t\t<h1>{$title}</h1>
\t\t\t\t\t\t<p>{$description}</p>
\t\t\t\t\t\t<p>{$learn_more}</p>
\t\t\t\t\t</div>
\t\t\t\t\t<div id="sitemap__content">
\t\t\t\t\t\t<p class="text">{$text}</p>
\t\t\t\t\t\t<table id="sitemap__table">
\t\t\t\t\t\t\t<thead>
\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t<th class="loc">{$url}</th>
\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-lastmod">
\t\t\t\t\t\t\t\t\t\t<th class="lastmod">{$lastmod}</th>
\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-changefreq">
\t\t\t\t\t\t\t\t\t\t<th class="changefreq">{$changefreq}</th>
\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-priority">
\t\t\t\t\t\t\t\t\t\t<th class="priority">{$priority}</th>
\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t</tr>
\t\t\t\t\t\t\t</thead>
\t\t\t\t\t\t\t<tbody>
\t\t\t\t\t\t\t\t<xsl:for-each select="sitemap:urlset/sitemap:url">
\t\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t\t<td class="loc"><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc" /></a></td>
\t\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-lastmod">
\t\t\t\t\t\t\t\t\t\t\t<td class="lastmod"><xsl:value-of select="sitemap:lastmod" /></td>
\t\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-changefreq">
\t\t\t\t\t\t\t\t\t\t\t<td class="changefreq"><xsl:value-of select="sitemap:changefreq" /></td>
\t\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-priority">
\t\t\t\t\t\t\t\t\t\t\t<td class="priority"><xsl:value-of select="sitemap:priority" /></td>
\t\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t</tr>
\t\t\t\t\t\t\t\t</xsl:for-each>
\t\t\t\t\t\t\t</tbody>
\t\t\t\t\t\t</table>
\t\t\t\t\t</div>
\t\t\t\t</div>
\t\t\t</body>
\t\t</html>
\t</xsl:template>
</xsl:stylesheet>

XSL;
        return apply_filters('wp_sitemaps_stylesheet_content', $xsl_content);
    }
    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(esc_xml(__('Number of URLs in this XML Sitemap: %s.')), '<xsl:value-of select="count( sitemap:sitemapindex/sitemap:sitemap )" />');
        $lang = get_language_attributes('html');
        $url = esc_xml(__('URL'));
        $lastmod = esc_xml(__('Last Modified'));
        $xsl_content = <<<XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
\t\tversion="1.0"
\t\txmlns:xsl="http://www.w3.org/1999/XSL/Transform"
\t\txmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
\t\texclude-result-prefixes="sitemap"
\t\t>

\t<xsl:output method="html" encoding="UTF-8" indent="yes" />

\t<!--
\t  Set variables for whether lastmod occurs for any sitemap in the index.
\t  We do this up front because it can be expensive in a large sitemap.
\t  -->
\t<xsl:variable name="has-lastmod" select="count( /sitemap:sitemapindex/sitemap:sitemap/sitemap:lastmod )" />

\t<xsl:template match="/">
\t\t<html {$lang}>
\t\t\t<head>
\t\t\t\t<title>{$title}</title>
\t\t\t\t<style>
\t\t\t\t\t{$css}
\t\t\t\t</style>
\t\t\t</head>
\t\t\t<body>
\t\t\t\t<div id="sitemap">
\t\t\t\t\t<div id="sitemap__header">
\t\t\t\t\t\t<h1>{$title}</h1>
\t\t\t\t\t\t<p>{$description}</p>
\t\t\t\t\t\t<p>{$learn_more}</p>
\t\t\t\t\t</div>
\t\t\t\t\t<div id="sitemap__content">
\t\t\t\t\t\t<p class="text">{$text}</p>
\t\t\t\t\t\t<table id="sitemap__table">
\t\t\t\t\t\t\t<thead>
\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t<th class="loc">{$url}</th>
\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-lastmod">
\t\t\t\t\t\t\t\t\t\t<th class="lastmod">{$lastmod}</th>
\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t</tr>
\t\t\t\t\t\t\t</thead>
\t\t\t\t\t\t\t<tbody>
\t\t\t\t\t\t\t\t<xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
\t\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t\t<td class="loc"><a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc" /></a></td>
\t\t\t\t\t\t\t\t\t\t<xsl:if test="\$has-lastmod">
\t\t\t\t\t\t\t\t\t\t\t<td class="lastmod"><xsl:value-of select="sitemap:lastmod" /></td>
\t\t\t\t\t\t\t\t\t\t</xsl:if>
\t\t\t\t\t\t\t\t\t</tr>
\t\t\t\t\t\t\t\t</xsl:for-each>
\t\t\t\t\t\t\t</tbody>
\t\t\t\t\t\t</table>
\t\t\t\t\t</div>
\t\t\t\t</div>
\t\t\t</body>
\t\t</html>
\t</xsl:template>
</xsl:stylesheet>

XSL;
        return apply_filters('wp_sitemaps_stylesheet_index_content', $xsl_content);
    }
    public function get_stylesheet_css()
    {
        $text_align = is_rtl() ? 'right' : 'left';
        $css = <<<EOF

\t\t\t\t\tbody {
\t\t\t\t\t\tfont-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
\t\t\t\t\t\tcolor: #444;
\t\t\t\t\t}

\t\t\t\t\t#sitemap {
\t\t\t\t\t\tmax-width: 980px;
\t\t\t\t\t\tmargin: 0 auto;
\t\t\t\t\t}

\t\t\t\t\t#sitemap__table {
\t\t\t\t\t\twidth: 100%;
\t\t\t\t\t\tborder: solid 1px #ccc;
\t\t\t\t\t\tborder-collapse: collapse;
\t\t\t\t\t}

\t\t\t \t\t#sitemap__table tr td.loc {
\t\t\t\t\t\t/*
\t\t\t\t\t\t * URLs should always be LTR.
\t\t\t\t\t\t * See https://core.trac.wordpress.org/ticket/16834
\t\t\t\t\t\t * and https://core.trac.wordpress.org/ticket/49949
\t\t\t\t\t\t */
\t\t\t\t\t\tdirection: ltr;
\t\t\t\t\t}

\t\t\t\t\t#sitemap__table tr th {
\t\t\t\t\t\ttext-align: {$text_align};
\t\t\t\t\t}

\t\t\t\t\t#sitemap__table tr td,
\t\t\t\t\t#sitemap__table tr th {
\t\t\t\t\t\tpadding: 10px;
\t\t\t\t\t}

\t\t\t\t\t#sitemap__table tr:nth-child(odd) td {
\t\t\t\t\t\tbackground-color: #eee;
\t\t\t\t\t}

\t\t\t\t\ta:hover {
\t\t\t\t\t\ttext-decoration: none;
\t\t\t\t\t}

EOF;
        return apply_filters('wp_sitemaps_stylesheet_css', $css);
    }
}