| 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 |
/** |
| 29 |
* Only for Development Mode. |
| 30 |
*/ |
| 31 |
if ( defined( 'TEMPLATELY_DEV_VIEWS' ) && TEMPLATELY_DEV_VIEWS ) { |
| 32 |
add_action( 'templately_builder_header_before', [ $this, 'header_helper' ], 0 ); |
| 33 |
add_action( 'templately_builder_footer_before', [ $this, 'footer_helper' ], 0 ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function header_helper() { |
| 38 |
echo '<small>Header</small>'; |
| 39 |
} |
| 40 |
|
| 41 |
public function footer_helper() { |
| 42 |
echo '<small>Footer</small>'; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_header() { |
| 46 |
if(!self::is_header_footer('header')){ |
| 47 |
return; |
| 48 |
} |
| 49 |
$this->views->get_header(); |
| 50 |
|
| 51 |
$templates = []; |
| 52 |
$templates[] = 'header.php'; |
| 53 |
|
| 54 |
remove_all_actions( 'wp_head' ); |
| 55 |
|
| 56 |
ob_start(); |
| 57 |
locate_template( $templates, true ); |
| 58 |
ob_get_clean(); |
| 59 |
} |
| 60 |
|
| 61 |
public function get_footer( $name ) { |
| 62 |
if(!self::is_header_footer('footer')){ |
| 63 |
return; |
| 64 |
} |
| 65 |
$this->views->get_footer(); |
| 66 |
|
| 67 |
$templates = []; |
| 68 |
$name = (string) $name; |
| 69 |
if ( '' !== $name ) { |
| 70 |
$templates[] = "footer-{$name}.php"; |
| 71 |
} |
| 72 |
|
| 73 |
$templates[] = 'footer.php'; |
| 74 |
|
| 75 |
// remove_all_actions( 'wp_footer' ); |
| 76 |
|
| 77 |
ob_start(); |
| 78 |
locate_template( $templates, true ); |
| 79 |
ob_get_clean(); |
| 80 |
} |
| 81 |
|
| 82 |
public static function is_header_footer($type = null){ |
| 83 |
if(class_exists( 'Elementor\Plugin' )){ |
| 84 |
$pid = get_the_ID(); |
| 85 |
$post_type = get_post_type($pid); |
| 86 |
$document = Plugin::$instance->documents->get( $pid ); |
| 87 |
|
| 88 |
if( |
| 89 |
$post_type === 'elementor_library' && |
| 90 |
( |
| 91 |
$document->get_type() === 'header' || |
| 92 |
$document->get_type() === 'footer') |
| 93 |
){ |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$header = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'header' ); |
| 99 |
$footer = templately()->theme_builder::$conditions_manager->get_templates_by_location( 'footer' ); |
| 100 |
|
| 101 |
if($type === 'header'){ |
| 102 |
return $header; |
| 103 |
} |
| 104 |
else if($type === 'footer'){ |
| 105 |
return $footer; |
| 106 |
} |
| 107 |
|
| 108 |
if(!empty($header) || !empty($footer)){ |
| 109 |
return true; |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
public function wrapper_attributes( $attributes, $document ) { |
| 115 |
$post_type = get_post_type($document->get_main_id()); |
| 116 |
|
| 117 |
if( $post_type === 'templately_library' ){ |
| 118 |
$template = templately()->theme_builder->get_template( $document->get_main_id() ); |
| 119 |
if($template){ |
| 120 |
$attributes['data-elementor-type'] = 'templately-' . $template->get_type(); |
| 121 |
$attributes['data-elementor-id'] = $template->get_main_id(); |
| 122 |
$attributes['data-elementor-post-type'] = 'templately_library'; |
| 123 |
$attributes['data-elementor-title'] = $template->get_title(); |
| 124 |
$attributes['class'] .= ' ' . implode( ' ', get_post_class() ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $attributes; |
| 129 |
} |
| 130 |
|
| 131 |
public function set_global_product(){ |
| 132 |
global $product; |
| 133 |
|
| 134 |
if ( class_exists('\WC_Product') && ! $product instanceof \WC_Product ) { |
| 135 |
$product_id = get_the_ID(); |
| 136 |
if ( $product_id ) { |
| 137 |
wc_setup_product_data( $product_id ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} |