<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Sifency Documentation Single Render
 * Dynamically replaces the default documentation single template with an Elementor one.
 */
if ( Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) {
final class Sifency_Documentation_Single_Render {

    public function __construct() {
        if ( is_admin() ) return;

        add_filter( 'template_include', [ $this, 'override_documentation_single_template' ], 99999 );
    }

    /**
     * Replace the default single template for 'documentation' CPT if a custom Elementor one exists.
     */
    public function override_documentation_single_template( $template ) {
        if ( ! is_singular( 'doc' ) ) {
            return $template;
        }
        remove_filter( 'template_include', [ Sifency_Addons_Theme_Builder_Master::$sifency_addons_frontend_render, 'load_canvas_template' ], 999 );
        $sifency_addons_template_id = Sifency_Frontend_Helper::find_matching_template( 'documentation' );
        if ( empty( $sifency_addons_template_id ) ) {
            return $template;
        }
        set_query_var( 'sifency_documentation_single_template_id', $sifency_addons_template_id );
        $canvas_path = defined( 'SIFENCY_TB_PATH' )
            ? trailingslashit( SIFENCY_TB_PATH ) . 'post-type/doc/templates/sifency-doc-single-canvas.php'
            : plugin_dir_path( __FILE__ ) . 'templates/sifency-doc-single-canvas.php';

        return file_exists( $canvas_path ) ? $canvas_path : $template;
    }
}
new Sifency_Documentation_Single_Render();
}

