PluginProbe
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder / 3.0.2
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder v3.0.2
3.1.4 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 All 93 releases
ultimate-store-kit / includes / builder / builder-integration.php

builder-integration.php in Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder 3.0.2, at includes/builder/builder-integration.php

514 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimateStoreKit\Builder;
4
5 if (! defined('WPINC')) {
6 die;
7 }
8
9 use Elementor\Controls_Manager;
10 use Elementor\Plugin;
11 use UltimateStoreKit\Includes\Builder\Builder_Template_Helper;
12 use UltimateStoreKit\Base\Singleton;
13 use UltimateStoreKit\Includes\Builder\Meta;
14 use UltimateStoreKit\Includes\Controls\SelectInput\Dynamic_Select;
15
16
17 class Builder_Integration {
18
19 use Singleton;
20
21 private $current_template = null;
22 public $current_template_id = null;
23
24 function __construct() {
25 add_filter('template_include', [$this, 'set_builder_template'], 9999);
26 add_action('elementor/editor/init', [$this, 'set_sample_post'], 999);
27
28 add_action('print_default_editor_scripts', array($this, 'my_custom_fonts'));
29 add_filter("elementor/document/urls/wp_preview", [$this, 'change_preview_editor_url'], 999, 2);
30
31 add_action('elementor/documents/register_controls', [$this, 'register_document_controls']);
32
33 // Add demo bypass filter for template preview
34 add_filter('ultimate_store_kit/preview/verified_bypass', function ($verify) {
35 // Check if we're in a demo environment or development site
36 // For demo sites, you might want to check domain names or other indicators
37 $demo_hosts = apply_filters('ultimate_store_kit/demo_hosts', [
38 'storekit.pro',
39 'demo.storekit.pro',
40 'localhost',
41 '127.0.0.1'
42 ]);
43
44 $current_host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field($_SERVER['HTTP_HOST']) : '';
45
46 foreach ($demo_hosts as $host) {
47 if (strpos($current_host, $host) !== false) {
48 return true;
49 }
50 }
51
52 return $verify;
53 });
54
55 // Add filter to enable demo mode for preview URLs
56 add_filter('ultimate_store_kit/preview/use_demo_bypass', function ($use_demo) {
57 // Enable demo bypass in Elementor editor
58 if (isset($_GET['action']) && $_GET['action'] === 'elementor') {
59 return true;
60 }
61 return $use_demo;
62 });
63 }
64
65 public function change_preview_editor_url($url, $document) {
66 $post_id = $document->get_main_id();
67 $template_type = get_post_meta($post_id, Meta::TEMPLATE_TYPE, true);
68
69 if (empty($template_type) || get_post_type($post_id) !== Meta::POST_TYPE) {
70 return $url;
71 }
72
73 $template_data = explode(Builder_Template_Helper::separator(), $template_type);
74 if (count($template_data) < 2) {
75 return $url;
76 }
77
78 $post_type = $template_data[0];
79 $template_slug = $template_data[1];
80
81 // Get template URL based on template type
82 $template_url = '';
83
84 // Check for product category first
85 if ($template_slug === 'product-category') {
86 $product_cats = get_terms([
87 'taxonomy' => 'product_cat',
88 'hide_empty' => true,
89 'number' => 1,
90 ]);
91
92 if (!empty($product_cats) && !is_wp_error($product_cats)) {
93 $template_url = get_term_link($product_cats[0]);
94 }
95 }
96
97 // Check for product tag
98 elseif ($template_slug === 'product-tag') {
99 $product_tags = get_terms([
100 'taxonomy' => 'product_tag',
101 'hide_empty' => true,
102 'number' => 1,
103 ]);
104
105 if (!empty($product_tags) && !is_wp_error($product_tags)) {
106 $template_url = get_term_link($product_tags[0]);
107 }
108 }
109
110 elseif ($template_slug === 'shop' || $template_slug === 'archive') {
111 $template_url = get_permalink(wc_get_page_id('shop'));
112 }
113
114 elseif ($template_slug === 'single' && $post_type === 'product') {
115 $page_settings_manager = \Elementor\Core\Settings\Manager::get_settings_managers('page');
116 $page_settings_model = $page_settings_manager->get_model($post_id);
117 $sample_product = $page_settings_model->get_settings('usk_builder_sample_post_id');
118
119 $template_url = !empty($sample_product) ?
120 get_permalink($sample_product) :
121 $this->get_default_product_url();
122 }
123
124 elseif ($template_slug === 'cart') {
125 $template_url = wc_get_cart_url();
126 }
127
128 elseif ($template_slug === 'checkout') {
129 $template_url = wc_get_checkout_url();
130 }
131
132 elseif ($template_slug === 'myaccount' || strpos($template_slug, 'myaccount-') === 0) {
133 $template_url = get_permalink(wc_get_page_id('myaccount'));
134 }
135
136 elseif ($template_slug === 'order-received') {
137 $orders = wc_get_orders(['limit' => 1]);
138 if (!empty($orders)) {
139 $order = $orders[0];
140 $order_id = $order->get_id();
141 $template_url = add_query_arg('order-received', $order_id, wc_get_checkout_url());
142 }
143 }
144
145 if (empty($template_url)) {
146 return $url;
147 }
148
149 $is_demo = apply_filters('ultimate_store_kit/preview/use_demo_bypass', false);
150 $nonce_value = $is_demo ? 'verified' : wp_create_nonce('template_preview_' . $post_id);
151
152 $param = [
153 'usk_template_id' => $post_id,
154 'preview_nonce' => $nonce_value,
155 'preview' => true
156 ];
157
158 // Add parameters two URL
159 $url = add_query_arg($param, $template_url);
160
161 return $url;
162 }
163
164 public function my_custom_fonts() {
165 if (is_admin() && Plugin::instance()->editor->is_edit_mode()) {
166 if (isset($_REQUEST['usk-template'])) {
167 wp_register_style('usk-template-builder-hide-preview-btn-inline', false); // phpcs:ignore
168 wp_enqueue_style('usk-template-builder-hide-preview-btn-inline');
169 wp_add_inline_style(
170 'usk-template-builder-hide-preview-btn-inline',
171 '#elementor-panel-footer-saver-preview {display:none!important}'
172 );
173 }
174 }
175 }
176 function set_sample_post() {
177 if (Builder_Template_Helper::isTemplateEditMode()) {
178 $object = \UltimateStoreKit\Includes\Builder\Builder_Post_Singleton::instance();
179 $object::set_sample_post();
180 }
181 }
182
183 function register_document_controls($document) {
184 if (
185 ! $document instanceof \Elementor\Core\DocumentTypes\PageBase
186 || ! $document::get_property('has_elements')
187 ) {
188 return;
189 }
190
191 if (Plugin::instance()->preview->is_preview_mode())
192 return;
193
194 if (! Builder_Template_Helper::isTemplateEditMode()) {
195 return;
196 }
197
198 global $post;
199
200 if (! isset($post->ID)) {
201 return;
202 }
203 $meta = get_post_meta($post->ID);
204
205 $templateMeta = optional($meta)[Meta::TEMPLATE_TYPE];
206 if (! isset($templateMeta[0])) {
207 return;
208 }
209 $postMeta = $templateMeta[0];
210 $postMeta = explode('|', $postMeta);
211 $postType = $postMeta[0];
212
213 if ($postMeta[1] != 'single') {
214 return;
215 }
216
217 $document->start_controls_section(
218 'usk_page_setting_preview',
219 [
220 'label' => esc_html__('Builder Settings', 'ultimate-store-kit'),
221 'tab' => Controls_Manager::TAB_SETTINGS,
222 ]
223 );
224
225 $document->add_control(
226 'usk_builder_sample_post_id',
227 [
228 'label' => __('Builder Post', 'ultimate-store-kit'),
229 'type' => Dynamic_Select::TYPE,
230 'multiple' => false,
231 'label_block' => true,
232 'query_args' => [
233 'post_type' => $postType
234 ],
235 ]
236 );
237
238 $document->add_control(
239 'usk_builder_sample_apply_preview',
240 [
241 'type' => Controls_Manager::BUTTON,
242 'label' => esc_html__('Apply & Preview', 'ultimate-store-kit'),
243 'label_block' => true,
244 'show_label' => false,
245 'text' => esc_html__('Apply & Preview', 'ultimate-store-kit'),
246 'separator' => 'none',
247 'event' => 'ultimateStoreKitBuilderSetting:applySinglePagePostOnPreview',
248 ]
249 );
250
251 $document->end_controls_section();
252 }
253
254 /**
255 * Rewrite default template
256 *
257 */
258 function set_builder_template($template) {
259 if (Builder_Template_Helper::isTemplateEditMode()) {
260 return $this->setBackendTemplate($template);
261 } else {
262 return $this->setFrontendTemplate($template);
263 }
264 }
265
266
267 protected function setBackendTemplate($template) {
268 return $template;
269 }
270
271
272 protected function setFrontendTemplate($template) {
273
274 if (get_post_type() == 'product') {
275 global $product;
276 $product = wc_get_product();
277 }
278
279 if (defined('ELEMENTOR_PATH')) {
280 $elementorTem = ELEMENTOR_PATH . "modules/page-templates/templates/";
281 $elementorTem = explode($elementorTem, $template);
282 if (count($elementorTem) == 2) {
283 return $template;
284 }
285 }
286
287
288 if (is_post_type_archive('product') || is_page(wc_get_page_id('shop')) || is_product_taxonomy()) {
289 $template_type = 'shop';
290
291 if (is_tax('product_cat')) {
292 $template_type = 'category';
293 } elseif (is_tax('product_tag')) {
294 $template_type = 'tag';
295 }
296
297 if ($custom_template = $this->get_template_id($template_type)) {
298 $this->current_template_id = $custom_template;
299 return $this->getTemplatePath('woocommerce/archive-product', $template);
300 }
301 }
302
303
304 if (is_cart()) {
305 if ($custom_template = $this->get_template_id('cart', 'product')) {
306 $this->current_template_id = $custom_template;
307 return $this->getTemplatePath('woocommerce/cart', $template);
308 }
309 }
310
311 if (is_order_received_page()) {
312 if ($custom_template = $this->get_template_id('order-received', 'product')) {
313 $this->current_template_id = $custom_template;
314 return $this->getTemplatePath('woocommerce/order-received', $template);
315 }
316 }
317
318 if (is_checkout()) {
319 if ($custom_template = $this->get_template_id('checkout', 'product')) {
320 $this->current_template_id = $custom_template;
321 return $this->getTemplatePath('woocommerce/checkout', $template);
322 }
323 }
324
325 if (is_single() && get_post_type() == 'product') {
326 if ($custom_template = $this->get_template_id('single', 'product')) {
327 $this->current_template_id = $custom_template;
328 return $this->getTemplatePath('woocommerce/single-product', $template);
329 }
330 }
331
332 if (is_account_page()) {
333 global $wp;
334 $query_vars = WC()->query->get_query_vars();
335
336 /**
337 * Add wishlist endpoint
338 */
339 $query_vars['wishlist'] = 'wishlist';
340
341 if ($endpoint = array_intersect_key($wp->query_vars, $query_vars)) {
342 $endpoint = array_key_first($endpoint);
343
344 if ($endpoint && $custom_template = $this->get_template_id($endpoint, 'account')) {
345 $this->current_template_id = $custom_template;
346
347 if ($newTemplate = $this->getTemplatePath("woocommerce/{$endpoint}")) {
348 return $newTemplate;
349 }
350
351 return $this->getTemplatePath("woocommerce/my-account", '');
352 }
353
354 if ($custom_template = $this->get_template_id('myaccount-orders', 'account')) {
355 $this->current_template_id = $custom_template;
356 return $this->getTemplatePath('woocommerce/my-account', $template);
357 }
358 } else {
359 if ($custom_template = $this->get_template_id('myaccount', 'account')) {
360 $this->current_template_id = $custom_template;
361 return $this->getTemplatePath('woocommerce/my-account', $template);
362 }
363 }
364 }
365
366
367 if (is_single()) {
368 if ($custom_template = $this->get_template_id('single', 'post')) {
369 $this->current_template_id = $custom_template;
370 return $this->getTemplatePath('single-post', $template);
371 }
372 }
373
374 if (is_archive()) {
375 if ($custom_template = $this->get_template_id('archive', 'post')) {
376 $this->current_template_id = $custom_template;
377 return $this->getTemplatePath('archive-post', $template);
378 }
379 }
380
381 if (is_home()) {
382 if ($custom_template = $this->get_template_id('home', 'post')) {
383 $this->current_template_id = $custom_template;
384 return $this->getTemplatePath('home', $template);
385 }
386 }
387
388 if ($page_Id = intval(get_option('bdt_usk_compare_products_page_id'))) {
389 if (is_page($page_Id)) {
390 if ($custom_template = $this->get_template_id('compare-products', 'product')) {
391 $this->current_template_id = $custom_template;
392 return $this->getTemplatePath('home', $template);
393 }
394 }
395 }
396
397 return $template;
398 }
399
400
401 public function getThemeTemplatePath($slug) {
402
403 $fullPath = get_template_directory() . "/ultimate-store-kit/$slug";
404 if (file_exists($fullPath)) {
405 return $fullPath;
406 }
407 }
408
409 public function getPluginTemplatePath($slug) {
410
411 $fullPath = BDTUSK_PATH . "includes/builder/templates/$slug";
412 if (file_exists($fullPath)) {
413 return $fullPath;
414 }
415 }
416
417
418 /**
419 * Get Template Path ID
420 *
421 * @param $slug
422 * @param $postType
423 *
424 * @return mixed|void|null
425 */
426 public function get_template_id($slug, $postType = false) {
427 // If we already have a template ID for this request, return it
428 if (null !== $this->current_template_id) {
429 return $this->current_template_id;
430 }
431
432 // Handle template preview from URL parameters
433 if (!empty($_GET['preview']) && !empty($_GET['usk_template_id']) && !empty($_GET['preview_nonce'])) {
434 $usk_template_id = sanitize_text_field(wp_unslash($_GET['usk_template_id']));
435 $nonce = sanitize_text_field(wp_unslash($_GET['preview_nonce']));
436
437 // Special handling for demo bypass
438 $is_demo_bypass = ($nonce === 'verified');
439 $nonce_verified = $is_demo_bypass ?
440 apply_filters('ultimate_store_kit/preview/verified_bypass', false) :
441 wp_verify_nonce($nonce, 'template_preview_' . $usk_template_id);
442
443 if ($nonce_verified) {
444 // For demo bypass mode, we don't need to check template type
445 if ($is_demo_bypass) {
446 $this->current_template_id = (int)$usk_template_id;
447 return $this->current_template_id;
448 }
449
450 // For normal preview, check template type
451 $template_type = get_post_meta($usk_template_id, Meta::TEMPLATE_TYPE, true);
452 if (!empty($template_type)) {
453 $template_data = explode(Builder_Template_Helper::separator(), $template_type);
454 if (count($template_data) >= 2 && $template_data[1] === $slug && ($postType === false || $template_data[0] === $postType)) {
455 $this->current_template_id = (int)$usk_template_id;
456 return $this->current_template_id;
457 }
458 }
459 }
460 }
461
462 // Regular template retrieval
463 $templateId = Builder_Template_Helper::getTemplate($slug, $postType);
464 $this->current_template_id = apply_filters('ultimate-store-kit-builder/custom-shop-template', $templateId);
465
466 return $this->current_template_id;
467 }
468
469
470 /**
471 * Get Template Path
472 *
473 * @param $slug
474 * @param $default
475 *
476 * @return mixed|string|void
477 */
478 protected function getTemplatePath($slug, $default = '') {
479 $phpSlug = "{$slug}.php";
480
481 if ($template = $this->getThemeTemplatePath($phpSlug)) {
482 return $template;
483 }
484
485 if ($template = $this->getPluginTemplatePath($phpSlug)) {
486 return $template;
487 }
488
489 return $default;
490 }
491
492 /**
493 * Get default product URL with proper error checking
494 *
495 * @return string
496 */
497 protected function get_default_product_url() {
498 $products = wc_get_products(['status' => 'publish', 'limit' => 1]);
499
500 if (!empty($products) && isset($products[0]) && is_object($products[0])) {
501 $product = $products[0];
502 if (method_exists($product, 'get_id')) {
503 return get_permalink($product->get_id());
504 }
505 }
506
507 // Fallback to shop page if no products found
508 return get_permalink(wc_get_page_id('shop'));
509 }
510 }
511
512
513 Builder_Integration::instance();
514