| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Templately\Builder\Managers\LocationManager; |
| 7 |
use Templately\Builder\Managers\ThemeCompatibility; |
| 8 |
use Templately\Builder\Types\BaseTemplate; |
| 9 |
use Templately\Utils\Views; |
| 10 |
use ElementorPro\Modules\ThemeBuilder\Module; |
| 11 |
|
| 12 |
class TemplateLoader { |
| 13 |
/** |
| 14 |
* @var Views |
| 15 |
*/ |
| 16 |
private $views; |
| 17 |
|
| 18 |
public function __construct( $builder, $views ) { |
| 19 |
$this->views = $views; |
| 20 |
|
| 21 |
// new ThemeCompatibility(); |
| 22 |
|
| 23 |
add_action( 'get_header', [ $this, 'get_header' ], 0 ); |
| 24 |
add_action( 'get_footer', [ $this, 'get_footer' ], 0 ); |
| 25 |
add_action( 'elementor/document/wrapper_attributes', [ $this, 'wrapper_attributes' ], 10, 2 ); |
| 26 |
add_action( 'template_redirect', array( $this, 'set_global_product' ) ); |
| 27 |
|
| 28 |
add_action( 'templately_builder_header_after', [ self::class, 'print_style_tags' ], 0 ); |
| 29 |
add_action( 'templately_builder_footer_before', [ self::class, 'print_style_tags' ], 0 ); |
| 30 |
/** |
| 31 |
* Only for Development Mode. |
| 32 |
*/ |
| 33 |
if ( defined( 'TEMPLATELY_DEV_VIEWS' ) && TEMPLATELY_DEV_VIEWS ) { |
| 34 |
add_action( 'templately_builder_header_before', [ $this, 'header_helper' ], 0 ); |
| 35 |
add_action( 'templately_builder_footer_before', [ $this, 'footer_helper' ], 0 ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function header_helper() { |
| 40 |
echo '<small>Header</small>'; |
| 41 |
} |
| 42 |
|
| 43 |
public function footer_helper() { |
| 44 |
echo '<small>Footer</small>'; |
| 45 |
} |
| 46 |
|
| 47 |
public function get_header() { |
| 48 |
if(!self::is_header_footer('header')){ |
| 49 |
return; |
| 50 |
} |
| 51 |
$this->views->get_header(); |
| 52 |
|
| 53 |
$templates = []; |
| 54 |
$templates[] = 'header.php'; |
| 55 |
|
| 56 |
remove_all_actions( 'wp_head' ); |
| 57 |
|
| 58 |
ob_start(); |
| 59 |
locate_template( $templates, true ); |
| 60 |
ob_get_clean(); |
| 61 |
} |
| 62 |
|
| 63 |
public function get_footer( $name ) { |
| 64 |
if(!self::is_header_footer('footer')){ |
| 65 |
return; |
| 66 |
} |
| 67 |
$this->views->get_footer(); |
| 68 |
|
| 69 |
$templates = []; |
| 70 |
$name = (string) $name; |
| 71 |
if ( '' !== $name ) { |
| 72 |
$templates[] = "footer-{$name}.php"; |
| 73 |
} |
| 74 |
|
| 75 |
$templates[] = 'footer.php'; |
| 76 |
|
| 77 |
// remove_all_actions( 'wp_footer' ); |
| 78 |
|
| 79 |
ob_start(); |
| 80 |
locate_template( $templates, true ); |
| 81 |
ob_get_clean(); |
| 82 |
} |
| 83 |
|
| 84 |
public static function is_header_footer($type = null){ |
| 85 |
if(class_exists( 'Elementor\Plugin' )){ |
| 86 |
$pid = get_the_ID(); |
| 87 |
$post_type = get_post_type($pid); |
| 88 |
$document = Plugin::$instance->documents->get( $pid ); |
| 89 |
|
| 90 |
if( |
| 91 |
$post_type === 'elementor_library' && |
| 92 |
( |
| 93 |
$document->get_type() === 'header' || |
| 94 |
$document->get_type() === 'footer') |
| 95 |
){ |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$header = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'header' ); |
| 101 |
$footer = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'footer' ); |
| 102 |
|
| 103 |
if($type === 'header'){ |
| 104 |
return $header; |
| 105 |
} |
| 106 |
else if($type === 'footer'){ |
| 107 |
return $footer; |
| 108 |
} |
| 109 |
|
| 110 |
if(!empty($header) || !empty($footer)){ |
| 111 |
return true; |
| 112 |
} |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
public function wrapper_attributes( $attributes, $document ) { |
| 117 |
$post_type = get_post_type($document->get_main_id()); |
| 118 |
|
| 119 |
if( $post_type === 'templately_library' ){ |
| 120 |
$template = templately()->theme_builder->get_template( $document->get_main_id() ); |
| 121 |
if($template){ |
| 122 |
$attributes['data-elementor-type'] = 'templately-' . $template->get_type(); |
| 123 |
$attributes['data-elementor-id'] = $template->get_main_id(); |
| 124 |
$attributes['data-elementor-post-type'] = 'templately_library'; |
| 125 |
$attributes['data-elementor-title'] = $template->get_title(); |
| 126 |
$attributes['class'] .= ' ' . implode( ' ', get_post_class() ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return $attributes; |
| 131 |
} |
| 132 |
|
| 133 |
public function set_global_product(){ |
| 134 |
global $product; |
| 135 |
|
| 136 |
if ( class_exists('\WC_Product') && ! $product instanceof \WC_Product ) { |
| 137 |
$product_id = get_the_ID(); |
| 138 |
if ( $product_id ) { |
| 139 |
wc_setup_product_data( $product_id ); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Print remaining enqueued styles with error handling. |
| 146 |
*/ |
| 147 |
public static function print_style_tags() { |
| 148 |
try { |
| 149 |
$wp_styles = wp_styles(); |
| 150 |
// handles dependencies |
| 151 |
$wp_styles->do_items(); |
| 152 |
if ( is_object( $wp_styles ) && is_array( $wp_styles->queue ?? null ) ) { |
| 153 |
foreach ( $wp_styles->queue as $style ) { |
| 154 |
if ( is_string( $style ) && ! $wp_styles->query( $style, 'done' ) ) { |
| 155 |
$wp_styles->do_item( $style ); |
| 156 |
$wp_styles->done[] = $style; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} catch ( \Throwable $e ) { |
| 161 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 162 |
error_log( 'Templately: Error printing styles - ' . $e->getMessage() ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
} |