PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.4
ShopBuilder – WooCommerce Builder For Elementor v3.2.4
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Helpers / BuilderFns.php

BuilderFns.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.4, at app/Helpers/BuilderFns.php

532 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * BuilderFns class
4 *
5 * The builder.
6 *
7 * @package RadiusTheme\SB
8 * @since 1.0.0
9 */
10
11 namespace RadiusTheme\SB\Helpers;
12
13 // Do not allow directly accessing this file.
14 use RadiusTheme\SB\Models\TemplateSettings;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit( 'This script cannot be accessed directly.' );
18 }
19
20 /**
21 * BuilderFns class
22 */
23 class BuilderFns {
24 /**
25 * Local Cache.
26 *
27 * @var array
28 */
29 private static $cache = [];
30
31 /**
32 * Template builder post type
33 *
34 * @var string
35 */
36 public static $post_type_tb = 'rtsb_builder';
37 /**
38 * Template builder
39 *
40 * @var string
41 */
42 public static $template_meta = 'rtsb_tb_template';
43 /**
44 * Template builder
45 *
46 * @var string
47 */
48 public static $product_template_meta = 'rtsb_selected_product_template';
49 /**
50 * Template builder
51 *
52 * @var string
53 */
54 public static $view_template_for_products_meta = 'rtsb_template_for_selected_products';
55 /**
56 * Template builder
57 *
58 * @var string
59 */
60 public static $view_template_for_archive_meta = 'rtsb_template_for_selected_category';
61
62 /**
63 * Template builder
64 *
65 * @var string
66 */
67 public static $categories_product_page_template = 'rtsb_categories_product_page_template';
68 /**
69 * Template builder
70 *
71 * @var string
72 */
73 public static $tags_product_page_template = 'rtsb_tags_product_page_template';
74 /**
75 * Get template ID based on page type.
76 *
77 * @param string $type Page type (product, archive, etc.).
78 * @return int Template ID or 0.
79 */
80 public static function builder_page_id_by_type( $type ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity
81 global $post;
82
83 if ( ! array_key_exists( $type, self::builder_page_types() ) ) {
84 return 0;
85 }
86 // Wpml Supported.
87 $options = self::option_name( $type );
88 $post_id = TemplateSettings::instance()->get_option( $options );
89 if ( empty( $post ) ) {
90 return 0;
91 }
92 if ( empty( $post_id ) ) {
93 $options = self::option_name( $type, true );
94 $post_id = TemplateSettings::instance()->get_option( $options );
95 }
96
97 $cache_key = 'rtsb_builder_page_id_by_type_' . $type;
98 if ( isset( self::$cache[ $cache_key ] ) ) {
99 return self::$cache[ $cache_key ];
100 }
101
102 switch ( $type ) {
103 case 'product':
104 if ( 'product' !== $post->post_type ) {
105 break;
106 }
107 $cache_key_product = 'template_for_product_page_' . get_the_ID();
108 $template = get_transient( $cache_key_product );
109 if ( $template && 'publish' === get_post_status( $template ) ) {
110 $post_id = $template;
111 break;
112 }
113 Cache::set_transient_cache_key( $cache_key_product );
114 $builder_id = get_post_meta( get_the_ID(), self::$view_template_for_products_meta, true );
115
116 if ( self::get_specific_product_as_default( $builder_id ) && 'publish' === get_post_status( $builder_id ) ) {
117 $post_id = $builder_id;
118 set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours.
119 break;
120 }
121
122 $terms = get_the_terms( get_the_ID(), 'product_cat' );
123 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
124 foreach ( $terms as $term ) {
125 $template_id = get_term_meta( $term->term_id, self::$categories_product_page_template, true );
126 if ( empty( $template_id ) || 'publish' !== get_post_status( $template_id ) ) {
127 continue;
128 }
129 $set_default_option_name = self::option_name_product_page_specific_cat_set_default( $template_id );
130 if ( TemplateSettings::instance()->get_option( $set_default_option_name ) ) {
131 $post_id = $template_id;
132 set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours.
133 break 2;
134 }
135 }
136 }
137 $tags = get_the_terms( get_the_ID(), 'product_tag' );
138 if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) {
139 foreach ( $tags as $term ) {
140 $template_id = get_term_meta( $term->term_id, self::$tags_product_page_template, true );
141 if ( empty( $template_id ) || 'publish' !== get_post_status( $template_id ) ) {
142 continue;
143 }
144 $set_default_option_name = self::option_name_product_page_specific_tag_set_default( $template_id );
145 if ( TemplateSettings::instance()->get_option( $set_default_option_name ) ) {
146 $post_id = $template_id;
147 set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours.
148 break 2;
149 }
150 }
151 }
152 set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS );
153 break;
154 case 'archive':
155 $queried_object = get_queried_object();
156 if ( ! empty( $queried_object->taxonomy ) && 'product_cat' === $queried_object->taxonomy ) {
157 $cache_key_archive = 'template_for_archive_page_' . $queried_object->term_id;
158 $template = get_transient( $cache_key_archive );
159 if ( $template && 'publish' === get_post_status( $template ) ) {
160 $post_id = $template;
161 break;
162 }
163 Cache::set_transient_cache_key( $cache_key_archive );
164 $builder_id = get_term_meta( $queried_object->term_id, self::$view_template_for_archive_meta, true );
165 if ( self::get_specific_category_as_default( $builder_id ) && 'publish' === get_post_status( $builder_id ) ) {
166 $post_id = $builder_id;
167 set_transient( $cache_key_archive, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours.
168 }
169 }
170 break;
171
172 default:
173 }
174 if ( 'publish' === get_post_status( $post_id ) && self::builder_type( $post_id ) === $type ) {
175 self::$cache[ $cache_key ] = $post_id;
176 return $post_id;
177 }
178
179 return 0;
180 }
181
182 /**
183 * Get current page builder ID via filter.
184 *
185 * @return int Template ID or 0.
186 */
187 public static function builder_page_id_by_page() {
188 $type = apply_filters( 'rtsb/builder/set/current/page/type', '' );
189 if ( ! $type ) {
190 return 0;
191 }
192
193 return self::builder_page_id_by_type( $type );
194 }
195
196 /**
197 * Page builder Page for.
198 *
199 * @return array
200 */
201 public static function builder_page_types() {
202 $page_types = [
203 'shop' => esc_html__( 'Shop', 'shopbuilder' ),
204 'archive' => esc_html__( 'Category Archive', 'shopbuilder' ),
205 'product' => esc_html__( 'Product Page', 'shopbuilder' ),
206 'cart' => esc_html__( 'Cart', 'shopbuilder' ),
207 'checkout' => esc_html__( 'Checkout', 'shopbuilder' ),
208 ];
209
210 return apply_filters( 'rtsb/builder/register/page/types', $page_types );
211 }
212
213 /**
214 * Get option key for template storage.
215 *
216 * @param string $type Template type.
217 * @param bool $default_lang Use default language.
218 * @return string Option key.
219 */
220 public static function option_name( $type, $default_lang = false ) {
221
222 $type = str_replace( [ '-', ' ' ], '_', $type );
223 $suffix = $type;
224
225 if ( $default_lang ) {
226 return self::$template_meta . '_default_' . $suffix;
227 }
228 $current_lang = Fns::wpml_current_language();
229
230 if ( ! empty( $current_lang ) ) {
231 $suffix = $type . '_' . $current_lang;
232 }
233
234 return self::$template_meta . '_default_' . $suffix;
235 }
236
237 /**
238 * Option name to mark product template as default.
239 *
240 * @param int $post_id Template ID.
241 * @return string|null
242 */
243 public static function option_name_for_specific_product_set_default( $post_id ) {
244 if ( ! $post_id ) {
245 return;
246 }
247
248 return 'rtsb_template_specific_product_set_default_' . $post_id;
249 }
250
251 /**
252 * Check if a specific product template is default.
253 *
254 * @param int $post_id Template ID.
255 * @return mixed
256 */
257 public static function get_specific_product_as_default( $post_id ) {
258 if ( ! $post_id ) {
259 return;
260 }
261 $options = self::option_name_for_specific_product_set_default( $post_id );
262
263 return TemplateSettings::instance()->get_option( $options );
264 }
265
266 /**
267 * Option name for default category template.
268 *
269 * @param int $post_id Template ID.
270 * @return string|null
271 */
272 public static function option_name_for_specific_category_set_default( $post_id ) {
273 if ( ! $post_id ) {
274 return;
275 }
276
277 return 'rtsb_template_specific_category_set_default_' . $post_id;
278 }
279
280 /**
281 * Check if a specific category template is default.
282 *
283 * @param int $post_id Template ID.
284 * @return mixed
285 */
286 public static function get_specific_category_as_default( $post_id ) {
287 if ( ! $post_id ) {
288 return;
289 }
290 $options = self::option_name_for_specific_category_set_default( $post_id );
291
292 return TemplateSettings::instance()->get_option( $options );
293 }
294
295 /**
296 * Get meta key by template ID for products.
297 *
298 * @param int $template_id Template ID.
299 * @return string
300 */
301 public static function option_name_by_template_id( $template_id ) {
302 $_suff = null;
303 if ( $template_id ) {
304 $_suff = '_' . $template_id;
305 }
306
307 return self::$view_template_for_products_meta . $_suff;
308 }
309
310 /**
311 * Get option name for the selected product category template.
312 *
313 * @param int|string $template_id Template ID for the category.
314 *
315 * @return string Generated option name.
316 */
317 public static function option_name_product_page_selected_cat( $template_id ) {
318 $_suff = null;
319 if ( $template_id ) {
320 $_suff = '_' . $template_id;
321 }
322
323 return self::$categories_product_page_template . $_suff;
324 }
325
326 /**
327 * Get option name for setting a specific product category template as default.
328 *
329 * @param int|string $post_id Template or post ID.
330 *
331 * @return string|null Option name or null if post ID is invalid.
332 */
333 public static function option_name_product_page_specific_cat_set_default( $post_id ) {
334 if ( ! $post_id ) {
335 return;
336 }
337
338 return 'rtsb_template_product_page_specific_category_set_default_' . $post_id;
339 }
340
341 /**
342 * Get option name for the selected product tag template.
343 *
344 * @param int|string $template_id Template ID for the tag.
345 *
346 * @return string Generated option name.
347 */
348 public static function option_name_product_page_selected_tag( $template_id ) {
349 $_suff = null;
350 if ( $template_id ) {
351 $_suff = '_' . $template_id;
352 }
353
354 return self::$tags_product_page_template . $_suff;
355 }
356 /**
357 * Get option name for setting a specific product tag template as default.
358 *
359 * @param int|string $post_id Template or post ID.
360 *
361 * @return string|null Option name or null if post ID is not provided.
362 */
363 public static function option_name_product_page_specific_tag_set_default( $post_id ) {
364 if ( ! $post_id ) {
365 return;
366 }
367
368 return 'rtsb_template_product_page_specific_tag_set_default_' . $post_id;
369 }
370 /**
371 * Get option name for the selected archive (category) template.
372 *
373 * @param int|string $template_id Template ID.
374 *
375 * @return string Generated option name.
376 */
377 public static function archive_option_name_by_template_id( $template_id ) {
378 $_suff = null;
379 if ( $template_id ) {
380 $_suff = '_' . $template_id;
381 }
382
383 return self::$view_template_for_archive_meta . $_suff;
384 }
385
386 /**
387 * Returns meta key used to store builder type.
388 *
389 * @return string
390 */
391 public static function template_type_meta_key() {
392 return self::$template_meta . '_type';
393 }
394
395 /**
396 * Get builder type.
397 *
398 * @param [type] $post_id Post id.
399 *
400 * @return string
401 */
402 public static function builder_type( $post_id ) {
403 $post_id = Fns::wpml_object_id( $post_id, self::$post_type_tb, 'default' );
404
405 if ( ! $post_id ) {
406 return;
407 }
408
409 $cache_key = 'rtsb_template_type_' . $post_id;
410 if ( ! empty( self::$cache[ $cache_key ] ) ) {
411 return self::$cache[ $cache_key ];
412 }
413
414 $type = get_post_meta( $post_id, self::template_type_meta_key(), true );
415
416 // Cache the results in a static variable for the next call.
417 self::$cache[ $cache_key ] = $type;
418
419 return $type;
420 }
421
422 /**
423 * Check if current page is builder preview.
424 *
425 * @return bool
426 */
427 public static function is_builder_preview() {
428 return is_singular( self::$post_type_tb );
429 }
430
431 /**
432 * Check if archive template should load.
433 *
434 * @return bool
435 */
436 public static function is_archive() {
437 return self::is_page_builder( 'archive', is_product_taxonomy() );
438 }
439
440 /**
441 * Check if shop template should load.
442 *
443 * @return bool
444 */
445 public static function is_shop() {
446 return self::is_page_builder( 'shop', is_shop() );
447 }
448
449 /**
450 * Check if cart template should load.
451 *
452 * @return bool
453 */
454 public static function is_cart() {
455 return self::is_page_builder( 'cart', is_cart() );
456 }
457
458 /**
459 * Check if checkout template should load.
460 *
461 * @return bool
462 */
463 public static function is_checkout() {
464 return self::is_page_builder( 'checkout', is_checkout() && ! is_wc_endpoint_url() && ! is_order_received_page() );
465 }
466
467 /**
468 * Single Page builder Detection
469 *
470 * @return boolean
471 */
472 public static function is_product() {
473 return self::is_page_builder( 'product', is_product() );
474 }
475
476 /**
477 * Check if quick view page template should load.
478 *
479 * @param bool $is_quick_view True if quick view.
480 * @return bool
481 */
482 public static function is_quick_views_page( $is_quick_view = false ) {
483 return rtsb()->has_pro() && self::is_page_builder( 'quick-view', $is_quick_view );
484 }
485
486
487 /**
488 * Check if a template should replace default WooCommerce page.
489 *
490 * @param string $type Page type.
491 * @param bool $is_page Current page status.
492 * @param bool $is_require_auth Require login.
493 * @return bool
494 */
495 public static function is_page_builder( $type, $is_page, $is_require_auth = false ) {
496 $current_builder_type = self::builder_type( get_the_ID() );
497 $post_type = get_post_type( get_the_ID() );
498 if ( $post_type === self::$post_type_tb ) {
499 if ( $current_builder_type === $type ) {
500 return true;
501 } else {
502 return false;
503 }
504 }
505 if ( $is_require_auth && ! is_user_logged_in() ) {
506 $type = 'myaccount-auth';
507 }
508 $builder_id = self::builder_page_id_by_type( $type );
509 if ( ! $builder_id ) {
510 return false;
511 }
512 $builder_type = self::builder_type( $builder_id );
513 if ( $type === $builder_type && $is_page ) {
514 return true;
515 }
516 return false;
517 }
518
519 /**
520 * Get builder content function
521 *
522 * @param [type] $template_id builder Template id.
523 * @param boolean $with_css with css.
524 *
525 * @return mixed
526 */
527 public static function get_builder_content( $template_id, $with_css = false ) {
528 // Don't Use cache: tickets ID -> 75376.
529 return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, $with_css );
530 }
531 }
532