<?php
namespace ShopyPress\Hooks;
if ( ! defined( 'ABSPATH' ) ) exit;

use ShopyPress\Models\ShopyPressProduct;
use ShopyPress\Views\Frontend\ProductSingle;

class FilterPropertySingle implements IHook {

    static function register() {
        // Hook into template loading for fallback
        add_filter('template_include', [self::class, 'overrideTemplate'], 11);

        // Hook into the_content for block themes
        add_filter('the_content', [self::class, 'injectPropertyContent'], 10, 1);
    }

    /**
     * Override template for classic themes or fallback
     */
    static function overrideTemplate($template) {
        if (is_singular(ShopyPressProduct::PRODUCT_SLUG) && !wp_is_block_theme()) {
            return plugin_dir_path(__FILE__) . '../Views/Frontend/Templates/ShopyPressSingle.php';
        }
        return $template;
    }

    /**
     * Inject custom property content into the post content
     */
    static function injectPropertyContent($content) {
        if (is_singular(ShopyPressProduct::PRODUCT_SLUG) && wp_is_block_theme()) {
            $content = ProductSingle::getContent();
        }
        return $content;
    }
}