<?php
if ( ! defined( 'ABSPATH' ) ) exit;
final class Sifency_Portfolio_Single_Render {

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

        add_filter( 'template_include', [ $this, 'override_portfolio_single_template' ], 99999 );
    }
    public function override_portfolio_single_template( $template ) {
        if ( ! is_singular( 'portfolio' ) ) {
            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( 'portfolio' );
        if ( empty( $sifency_addons_template_id ) ) {
            return $template;
        }
        set_query_var( 'sifency_portfolio_single_template_id', $sifency_addons_template_id );
        $canvas_path = defined( 'SIFENCY_TB_PATH' )
            ? trailingslashit( SIFENCY_TB_PATH ) . 'post-type/portfolio/templates/sifency-portfolio-single-canvas.php'
            : plugin_dir_path( __FILE__ ) . 'templates/sifency-portfolio-single-canvas.php';

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

