archive.php
1 week ago
base-content.php
2 years ago
base.php
1 week ago
cart.php
1 week ago
checkout.php
1 week ago
shop.php
4 years ago
single.php
1 week ago
base.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Page_Templates\Hooks; |
| 4 | |
| 5 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 6 | use ShopEngine\Compatibility\Conflicts\Theme_Hooks; |
| 7 | use ShopEngine\Core\Builders\Templates; |
| 8 | use ShopEngine\Core\Template_Cpt; |
| 9 | use ShopEngine\Utils\Helper; |
| 10 | |
| 11 | defined('ABSPATH') || exit; |
| 12 | |
| 13 | abstract class Base { |
| 14 | protected $template_loaded = false; |
| 15 | protected $config; |
| 16 | protected $template_name = null; |
| 17 | protected $prod_tpl_id; |
| 18 | protected $tpl_loaded = false; |
| 19 | |
| 20 | abstract protected function template_include_pre_condition(): bool; |
| 21 | |
| 22 | /** |
| 23 | * Fires Before the_content() |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function before_hooks() {} |
| 28 | |
| 29 | /** |
| 30 | * Fires After the_content() |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function after_hooks() {} |
| 35 | |
| 36 | abstract public function init(): void; |
| 37 | |
| 38 | public function __construct() { |
| 39 | |
| 40 | if(!$this->template_include_pre_condition()) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $this->prod_tpl_id = $this->get_registered_template_id($this->get_page_type_option_slug()); |
| 45 | |
| 46 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here |
| 47 | if(isset($_GET['elementor-preview'])) { |
| 48 | /** |
| 49 | * Remove checkout template extra markup; |
| 50 | */ |
| 51 | remove_action('woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10); |
| 52 | } |
| 53 | |
| 54 | if(empty($this->prod_tpl_id) || !$this->is_template_active()) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | add_filter('body_class', [$this, 'hook_body_class']); |
| 59 | |
| 60 | if(\ShopEngine\Core\Builders\Action::edit_with($this->prod_tpl_id) == 'elementor' && !Helper::is_elementor_active()) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if(\ShopEngine\Core\Builders\Action::edit_with($this->prod_tpl_id) == 'gutenberg' && !did_action('shopengine-gutenberg-addon/before_loaded')) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | Base_Content::instance()->set_tpl_data($this->prod_tpl_id, $this->get_page_type_option_slug()); |
| 69 | |
| 70 | |
| 71 | $this->config = Templates::get_template_types()[$this->page_type]; |
| 72 | |
| 73 | add_filter('template_include', [$this, 'redirect_page_template'], 999); |
| 74 | |
| 75 | add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts'], 999); |
| 76 | |
| 77 | add_action('shopengine/templates/elementor/content/before', [$this, 'theme_conflicts_in_widget']); |
| 78 | add_action('shopengine/builder/gutenberg/before-content', [$this, 'theme_conflicts_in_widget']); |
| 79 | |
| 80 | $this->init(); |
| 81 | |
| 82 | /** |
| 83 | * This variable is used inside compatibility\conflicts\manifest.php file |
| 84 | */ |
| 85 | global $is_used_shopengine_template; |
| 86 | $is_used_shopengine_template = true; |
| 87 | } |
| 88 | |
| 89 | public function theme_conflicts_in_widget($template_type = null) { |
| 90 | if(!$template_type) $template_type = $this->page_type ; |
| 91 | |
| 92 | if(in_array($template_type, ['shop', 'archive'])) { |
| 93 | |
| 94 | Theme_Hooks::instance()->theme__conflicts__shop_and_archive(); |
| 95 | |
| 96 | } elseif(in_array($template_type, ['single'])) { |
| 97 | |
| 98 | Theme_Hooks::instance()->theme__conflicts__single_page(); |
| 99 | |
| 100 | } elseif(in_array($template_type, ['cart'])) { |
| 101 | |
| 102 | Theme_Hooks::instance()->theme_conflicts_cart_page(); |
| 103 | |
| 104 | } elseif(in_array($template_type, ['my_account'])) { |
| 105 | |
| 106 | Theme_Hooks::instance()->theme_conflicts_my_account_page(); |
| 107 | |
| 108 | } elseif(in_array($template_type, ['empty_cart'])) { |
| 109 | |
| 110 | Theme_Hooks::instance()->theme_conflicts_empty_cart_page(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function enqueue_scripts() { |
| 115 | |
| 116 | if(\ShopEngine\Core\Builders\Action::is_edit_with_gutenberg($this->prod_tpl_id)) { |
| 117 | |
| 118 | add_action('wp_head', function () { |
| 119 | $css = \Wpmet\Gutenova\BlockManager::instance()->render_css($this->prod_tpl_id); |
| 120 | shopengine_content_render($css); |
| 121 | }, 999); |
| 122 | |
| 123 | } else { |
| 124 | |
| 125 | if(Helper::is_elementor_active()) { |
| 126 | $css_file = Post_CSS::create($this->prod_tpl_id); |
| 127 | |
| 128 | if('internal' !== get_option('elementor_css_print_method')) { |
| 129 | $css_file->enqueue(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if( in_array( $this->page_type, ['single', 'archive','shop', 'my_account', 'checkout', 'cart', 'empty_cart', 'account_orders', 'account_orders'] ) ) { |
| 135 | |
| 136 | if ( get_template() == 'masterstudy' ) { |
| 137 | |
| 138 | wp_dequeue_style('stm_theme_styles'); |
| 139 | wp_dequeue_script('masterstudy-bootstrap'); |
| 140 | |
| 141 | //prevent override the checkout payement template from theme |
| 142 | add_filter( 'woocommerce_locate_template', [Theme_Hooks::instance(), 'prevent_theme_override_for_checkout'], 10, 3 ); |
| 143 | |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function redirect_page_template($template) { |
| 149 | |
| 150 | $this->tpl_loaded = true; |
| 151 | |
| 152 | if(\ShopEngine\Core\Builders\Action::is_edit_with_gutenberg($this->prod_tpl_id)) { |
| 153 | |
| 154 | $user_template = get_post_meta( $this->prod_tpl_id, '_wp_page_template', true ); |
| 155 | |
| 156 | $template = $this->get_builder_template_dir() . '/gutenberg-'.$user_template.'.php'; |
| 157 | |
| 158 | if(empty($user_template)) { |
| 159 | |
| 160 | $template = $this->get_builder_template_dir() . '/gutenberg-default.php'; |
| 161 | } |
| 162 | |
| 163 | add_action('shopengine/builder/gutenberg/simple', function () { |
| 164 | |
| 165 | include_once $this->get_template_part(); |
| 166 | }); |
| 167 | |
| 168 | return $template; |
| 169 | } |
| 170 | |
| 171 | if(Helper::is_elementor_active()) { |
| 172 | |
| 173 | //this code is from elementor: /elementor/modules/page-templates/module.php:82 |
| 174 | $elementor_document = \Elementor\Plugin::$instance->documents->get_doc_for_frontend($this->prod_tpl_id); |
| 175 | $elementor_template_module = \Elementor\Plugin::$instance->modules_manager->get_modules('page-templates'); |
| 176 | |
| 177 | if($elementor_document && $elementor_document::get_property('support_wp_page_templates')) { |
| 178 | $page_template = $elementor_document->get_meta('_wp_page_template'); |
| 179 | $page_template = (in_array($page_template, ['elementor_header_footer', 'elementor_canvas']) ? $page_template : 'elementor_header_footer'); |
| 180 | |
| 181 | $template_path = $elementor_template_module->get_template_path($page_template); |
| 182 | |
| 183 | if('elementor_theme' !== $page_template && !$template_path && $elementor_document->is_built_with_elementor()) { |
| 184 | $kit_default_template = \Elementor\Plugin::$instance->kits_manager->get_current_settings('default_page_template'); |
| 185 | $template_path = $elementor_template_module->get_template_path($kit_default_template); |
| 186 | } |
| 187 | |
| 188 | if($template_path) { |
| 189 | $template = $template_path; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $elementor_template_module->set_print_callback(function () { |
| 194 | include_once $this->get_template_part(); |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | return $template; |
| 199 | } |
| 200 | |
| 201 | public function get_registered_template_id($type) { |
| 202 | |
| 203 | return \ShopEngine\Core\Builders\Templates::get_registered_template_id($type); |
| 204 | } |
| 205 | |
| 206 | protected function get_page_type_option_slug(): string { |
| 207 | |
| 208 | return $this->page_type; |
| 209 | } |
| 210 | |
| 211 | public function get_template_part() { |
| 212 | return $this->get_builder_template_dir() . $this->template_part; |
| 213 | } |
| 214 | |
| 215 | public function hook_body_class($classes) { |
| 216 | |
| 217 | $post_type = get_post_type(); |
| 218 | |
| 219 | if($post_type != Template_Cpt::TYPE && !$this->template_include_pre_condition()) { |
| 220 | return $classes; |
| 221 | } |
| 222 | |
| 223 | $classes[] = 'woocommerce'; |
| 224 | $classes[] = $this->get_body_class(); |
| 225 | $classes[] = Templates::BODY_CLASS; |
| 226 | $classes[] = (empty($this->get_page_type_option_slug()) ? '' : 'shopengine-' . $this->get_page_type_option_slug()); |
| 227 | $classes[] = ($this->config['css'] ?? ''); |
| 228 | |
| 229 | return $classes; |
| 230 | } |
| 231 | |
| 232 | protected function get_body_class() { |
| 233 | // child class may include custom classes here. |
| 234 | } |
| 235 | |
| 236 | protected function get_builder_template_dir() { |
| 237 | |
| 238 | return \ShopEngine::core_dir() . 'page-templates/screens/'; |
| 239 | } |
| 240 | |
| 241 | protected function is_template_cpt() { |
| 242 | $post_id = get_the_ID(); |
| 243 | $post_type = get_post_type(); |
| 244 | |
| 245 | return (Template_Cpt::TYPE === $post_type) && ($this->prod_tpl_id == $post_id); |
| 246 | } |
| 247 | |
| 248 | protected function is_template_active() { |
| 249 | |
| 250 | $prod_tpl_post_obj = get_post($this->prod_tpl_id); |
| 251 | |
| 252 | if(empty($prod_tpl_post_obj)) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return $prod_tpl_post_obj->post_status == 'publish'; |
| 257 | } |
| 258 | } |
| 259 |