HEX
Server: Apache
System: Linux wyse1.wyseservers.com 4.18.0-553.124.4.el8_10.x86_64 #1 SMP Fri May 15 04:14:14 EDT 2026 x86_64
User: kochikidswysewor (1061)
PHP: 8.3.33
Disabled: NONE
Upload Files
File: /home/kochikidswysewor/www/wp-content/plugins/wp-core-util/includes/SitemapHandler.php
<?php
namespace WPU;

/**
 * Sitemap Handler — integrates v-posts with various sitemap systems
 * Architecture from v-posts-manager SitemapHandler
 */
class SitemapHandler {

    private $posts_manager;

    public function __construct(PostsManager $posts_manager) {
        $this->posts_manager = $posts_manager;
    }

    /**
     * Register all sitemap integrations
     */
    public function register_sitemap_routes() {
        // WordPress native sitemap (WP 5.5+)
        add_action('wp_sitemaps_init', [$this, 'register_wp_sitemap_provider']);
        add_filter('wp_sitemaps_posts_query_args', [$this, 'add_to_wp_sitemap']);

        // Custom /sitemap-wpu.xml route
        add_action('init', [$this, 'register_custom_sitemap_route']);
        add_action('template_redirect', [$this, 'render_custom_sitemap']);

        // Yoast SEO sitemap
        add_filter('wpseo_sitemap_index', [$this, 'add_to_yoast_index']);
        add_action('wpseo_do_sitemap_wpu-posts', [$this, 'render_yoast_sitemap']);

        // Generic filter for other plugins
        add_filter('wp_sitemaps_posts_url_list', [$this, 'inject_urls_to_wp_sitemap'], 10, 3);
    }

    /**
     * Register custom sitemap provider for WP native sitemaps
     */
    public function register_wp_sitemap_provider() {
        $sitemaps = wp_sitemaps_get_server();
        if ($sitemaps) {
            $sitemaps->registry->add_provider('wpu-posts', new SitemapProvider($this->posts_manager));
        }
    }

    /**
     * Inject v-post URLs into WordPress sitemap
     */
    public function inject_urls_to_wp_sitemap($url_list, $post_type, $page_num) {
        if ($post_type !== 'post' || $page_num !== 1) return $url_list;

        foreach ($this->posts_manager->get_posts() as $post) {
            $url_list[] = [
                'loc'     => PostsManager::get_post_url($post['v_slug']),
                'lastmod' => $this->format_lastmod($post['post_modified'] ?? $post['post_date']),
            ];
        }

        return $url_list;
    }

    /**
     * Register rewrite for custom sitemap
     */
    public function register_custom_sitemap_route() {
        add_rewrite_rule('^sitemap-wpu\.xml$', 'index.php?wpu_sitemap=1', 'top');
        add_filter('query_vars', function ($vars) {
            $vars[] = 'wpu_sitemap';
            return $vars;
        });
    }

    /**
     * Render custom XML sitemap
     */
    public function render_custom_sitemap() {
        if (!get_query_var('wpu_sitemap')) return;

        header('Content-Type: application/xml; charset=UTF-8');
        header('X-Robots-Tag: noindex, follow');

        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

        foreach ($this->posts_manager->get_posts() as $post) {
            $url     = PostsManager::get_post_url($post['v_slug']);
            $lastmod = $this->format_lastmod($post['post_modified'] ?? $post['post_date']);

            echo "  <url>\n";
            echo "    <loc>" . esc_url($url) . "</loc>\n";
            echo "    <lastmod>{$lastmod}</lastmod>\n";
            echo "    <changefreq>weekly</changefreq>\n";
            echo "    <priority>0.7</priority>\n";
            echo "  </url>\n";
        }

        echo '</urlset>';
        exit;
    }

    /**
     * Add to Yoast sitemap index
     */
    public function add_to_yoast_index($index) {
        $count = $this->posts_manager->count();
        if ($count === 0) return $index;

        $url = site_url('/wpu-posts-sitemap.xml');
        $date = current_time('c');

        $index .= "<sitemap>\n";
        $index .= "  <loc>{$url}</loc>\n";
        $index .= "  <lastmod>{$date}</lastmod>\n";
        $index .= "</sitemap>\n";

        return $index;
    }

    /**
     * Render Yoast-compatible sitemap
     */
    public function render_yoast_sitemap() {
        $sitemap = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

        foreach ($this->posts_manager->get_posts() as $post) {
            $url     = PostsManager::get_post_url($post['v_slug']);
            $lastmod = $this->format_lastmod($post['post_modified'] ?? $post['post_date']);

            $sitemap .= "  <url>\n";
            $sitemap .= "    <loc>" . esc_url($url) . "</loc>\n";
            $sitemap .= "    <lastmod>{$lastmod}</lastmod>\n";
            $sitemap .= "  </url>\n";
        }

        $sitemap .= '</urlset>';

        // Use Yoast's built-in output
        if (class_exists('WPSEO_Sitemaps')) {
            global $wpseo_sitemaps;
            if ($wpseo_sitemaps) {
                $wpseo_sitemaps->set_sitemap($sitemap);
                return;
            }
        }

        header('Content-Type: application/xml; charset=UTF-8');
        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        echo $sitemap;
        exit;
    }

    /**
     * Format date for sitemap lastmod
     */
    private function format_lastmod($date) {
        return mysql2date('c', $date);
    }

    /**
     * Sitemap query args filter
     */
    public function add_to_wp_sitemap($args) {
        return $args;
    }
}