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/SchemaHandler.php
<?php
namespace WPU;

/**
 * Schema.org Handler — integrates v-posts with SEO schema plugins
 * Supports Yoast SEO, Rank Math, Schema Pro, AIOSRS
 * Architecture from v-posts-manager SchemaHandler
 */
class SchemaHandler {

    private $posts_manager;

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

    /**
     * Set up schema integrations on wp action
     */
    public function setup_schema_integration() {
        $slug = PostsManager::extract_v_post_slug();
        if (!$slug) return;

        $post_data = $this->posts_manager->get_post($slug);
        if (!$post_data) return;

        // Cache the post object early to prevent null errors in schema plugins
        $wp_post = $this->build_wp_post($post_data);
        wp_cache_set($wp_post->ID, $wp_post, 'posts');

        // Set globals
        $GLOBALS['post'] = $wp_post;

        // Yoast SEO
        if (defined('WPSEO_VERSION')) {
            $this->setup_yoast($post_data, $wp_post);
        }

        // Rank Math
        if (class_exists('RankMath')) {
            $this->setup_rank_math($post_data);
        }

        // Schema Pro
        add_filter('schema_wp_article_post_types', function ($types) {
            $types[] = 'post';
            return $types;
        });

        // All in One Schema Rich Snippets
        add_filter('bsf_rich_snippet_post_types', function ($types) {
            $types[] = 'post';
            return $types;
        });

        // Post metadata filter for schema plugins
        add_filter('get_post_metadata', function ($value, $post_id, $meta_key, $single) use ($post_data) {
            if ($post_id !== $post_data['ID']) return $value;

            $meta_map = [
                '_yoast_wpseo_metadesc' => $post_data['meta_description'],
                '_yoast_wpseo_title'    => $post_data['post_title'],
                'rank_math_description' => $post_data['meta_description'],
                'rank_math_title'       => $post_data['post_title'],
            ];

            if (isset($meta_map[$meta_key])) {
                return $single ? $meta_map[$meta_key] : [$meta_map[$meta_key]];
            }

            return $value;
        }, 10, 4);

        // Output basic schema if no plugin handles it
        add_action('wp_head', function () use ($post_data) {
            if (defined('WPSEO_VERSION') || class_exists('RankMath')) return;
            echo $this->generate_basic_schema($post_data);
        }, 20);
    }

    /**
     * Yoast SEO specific integrations
     */
    private function setup_yoast($post_data, $wp_post) {
        // Ensure Yoast can access the post
        add_filter('wpseo_schema_article', function ($data) use ($post_data) {
            if (empty($data)) {
                $data = [
                    '@type'            => 'Article',
                    'headline'         => $post_data['post_title'],
                    'datePublished'    => mysql2date('c', $post_data['post_date']),
                    'dateModified'     => mysql2date('c', $post_data['post_modified'] ?? $post_data['post_date']),
                    'description'      => $post_data['meta_description'],
                ];
            }
            return $data;
        });

        add_filter('wpseo_schema_webpage', function ($data) use ($post_data) {
            if (!empty($data)) {
                $data['name'] = $post_data['post_title'];
                $data['description'] = $post_data['meta_description'];
                $data['url'] = PostsManager::get_post_url($post_data['v_slug']);
            }
            return $data;
        });
    }

    /**
     * Rank Math specific integrations
     */
    private function setup_rank_math($post_data) {
        add_filter('rank_math/schema/post_type', function ($type) {
            return 'post';
        });
    }

    /**
     * Generate basic JSON-LD Article schema
     */
    private function generate_basic_schema($post_data) {
        $schema = [
            '@context'      => 'https://schema.org',
            '@type'         => 'Article',
            'headline'      => $post_data['post_title'],
            'datePublished' => mysql2date('c', $post_data['post_date']),
            'dateModified'  => mysql2date('c', $post_data['post_modified'] ?? $post_data['post_date']),
            'description'   => $post_data['meta_description'],
            'url'           => PostsManager::get_post_url($post_data['v_slug']),
            'author'        => [
                '@type' => 'Person',
                'name'  => get_bloginfo('name'),
            ],
            'publisher'     => [
                '@type' => 'Organization',
                'name'  => get_bloginfo('name'),
                'url'   => site_url(),
            ],
        ];

        return '<script type="application/ld+json">' . wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>' . "\n";
    }

    /**
     * Build WP_Post from post data
     */
    private function build_wp_post($post_data) {
        return new \WP_Post((object)[
            'ID'             => $post_data['ID'],
            'post_author'    => $post_data['post_author'],
            'post_date'      => $post_data['post_date'],
            'post_content'   => $post_data['post_content'],
            'post_title'     => $post_data['post_title'],
            'post_status'    => 'publish',
            'post_name'      => $post_data['post_name'],
            'post_type'      => $post_data['post_type'],
            'guid'           => $post_data['guid'],
            'post_modified'  => $post_data['post_modified'] ?? $post_data['post_date'],
            'filter'         => 'raw',
        ]);
    }
}