<?php
/**
 * Frontend InVision Player Controller for WordPress
 */

if (!defined('ABSPATH')) exit;

class Proto_Preview_Player {

    public function init() {
        add_action('init', [$this, 'add_rewrite_rules']);
        add_filter('query_vars', [$this, 'add_query_vars']);
        add_action('template_redirect', [$this, 'template_redirect_player']);
    }

    public function add_rewrite_rules() {
        add_rewrite_rule('^proto-preview/?$', 'index.php?proto_preview=1', 'top');
    }

    public function add_query_vars($vars) {
        $vars[] = 'proto_preview';
        $vars[] = 'project_id';
        $vars[] = 'token';
        return $vars;
    }

    public function template_redirect_player() {
        $is_preview = get_query_var('proto_preview') || isset($_GET['proto_preview']);
        
        // Also check if viewing single proto_project
        if (is_singular('proto_project') || $is_preview) {
            // Prevent all search engines from indexing or crawling prototype projects
            header('X-Robots-Tag: noindex, nofollow, noarchive, nosnippet', true);
            require_once PROTO_PREVIEW_PLUGIN_DIR . 'templates/player.php';
            exit;
        }
    }
}
