PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
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
← All changes | app/Helpers/BuilderFns.php +200 -80 2.1.133.2.6 View file →
@@ -59,26 +59,37 @@
59 59 */
60 60 public static $view_template_for_archive_meta = 'rtsb_template_for_selected_category';
61 61
62 62 /**
63 - * Page builder id.
63 + * Template builder
64 64 *
65 - * @return int
65 + * @var string
66 66 */
67 - public static function builder_page_id_by_type( $type ) {
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
68 81 global $post;
69 82
70 83 if ( ! array_key_exists( $type, self::builder_page_types() ) ) {
71 84 return 0;
72 85 }
73 -
74 86 // Wpml Supported.
75 87 $options = self::option_name( $type );
76 88 $post_id = TemplateSettings::instance()->get_option( $options );
77 89 if ( empty( $post ) ) {
78 - return $post_id;
90 + return 0;
79 91 }
80 -
81 92 if ( empty( $post_id ) ) {
82 93 $options = self::option_name( $type, true );
83 94 $post_id = TemplateSettings::instance()->get_option( $options );
84 95 }
@@ -86,51 +97,98 @@
86 97 $cache_key = 'rtsb_builder_page_id_by_type_' . $type;
87 98 if ( isset( self::$cache[ $cache_key ] ) ) {
88 99 return self::$cache[ $cache_key ];
89 100 }
101 +
90 102 switch ( $type ) {
91 103 case 'product':
92 - if ( 'product' === $post->post_type ) {
93 - $builder_id = get_post_meta( get_the_ID(), self::$view_template_for_products_meta, true );
94 - $set_default = self::get_specific_product_as_default( $builder_id );
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 );
95 115
96 - if ( 'publish' === $post->post_status ) {
97 - $post_id = $builder_id && $set_default ? $builder_id : $post_id;
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 + }
98 135 }
99 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 );
100 153 break;
101 154 case 'archive':
102 155 $queried_object = get_queried_object();
103 -
104 156 if ( ! empty( $queried_object->taxonomy ) && 'product_cat' === $queried_object->taxonomy ) {
105 - $builder_id = get_term_meta( $queried_object->term_id, self::$view_template_for_archive_meta, true );
106 - $set_default = self::get_specific_category_as_default( $builder_id );
107 -
108 - if ( 'publish' === $post->post_status ) {
109 - $post_id = $builder_id && $set_default ? $builder_id : $post_id;
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;
110 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 + }
111 169 }
112 170 break;
113 171
114 172 default:
115 173 }
116 - if ( 'publish' === $post->post_status && self::builder_type( $post_id ) === $type ) {
174 + if ( 'publish' === get_post_status( $post_id ) && self::builder_type( $post_id ) === $type ) {
117 175 self::$cache[ $cache_key ] = $post_id;
118 176 return $post_id;
119 177 }
120 178
121 - return $post_id;
179 + return 0;
122 180 }
123 181
124 182 /**
125 - * Page builder id.
183 + * Get current page builder ID via filter.
126 184 *
127 - * @return init
185 + * @return int Template ID or 0.
128 186 */
129 187 public static function builder_page_id_by_page() {
130 188 $type = apply_filters( 'rtsb/builder/set/current/page/type', '' );
131 189 if ( ! $type ) {
132 - return;
190 + return 0;
133 191 }
134 192
135 193 return self::builder_page_id_by_type( $type );
136 194 }
@@ -136,18 +194,19 @@
136 194 }
137 195
138 196 /**
139 197 * Page builder Page for.
198 + * No need translatable Text and textdomain.
140 199 *
141 200 * @return array
142 201 */
143 202 public static function builder_page_types() {
144 203 $page_types = [
145 - 'shop' => esc_html__( 'Shop', 'shopbuilder' ),
146 - 'archive' => esc_html__( 'Category Archive', 'shopbuilder' ),
147 - 'product' => esc_html__( 'Product Page', 'shopbuilder' ),
148 - 'cart' => esc_html__( 'Cart', 'shopbuilder' ),
149 - 'checkout' => esc_html__( 'Checkout', 'shopbuilder' ),
204 + 'shop' => 'Shop',
205 + 'archive' => 'Category Archive',
206 + 'product' => 'Product Page',
207 + 'cart' => 'Cart',
208 + 'checkout' => 'Checkout',
150 209 ];
151 210
152 211 return apply_filters( 'rtsb/builder/register/page/types', $page_types );
153 212 }
@@ -152,13 +211,13 @@
152 211 return apply_filters( 'rtsb/builder/register/page/types', $page_types );
153 212 }
154 213
155 214 /**
156 - * Option name.
215 + * Get option key for template storage.
157 216 *
158 - * @param string $type Builder type.
159 - *
160 - * @return string
217 + * @param string $type Template type.
218 + * @param bool $default_lang Use default language.
219 + * @return string Option key.
161 220 */
162 221 public static function option_name( $type, $default_lang = false ) {
163 222
164 223 $type = str_replace( [ '-', ' ' ], '_', $type );
@@ -176,11 +235,12 @@
176 235 return self::$template_meta . '_default_' . $suffix;
177 236 }
178 237
179 238 /**
180 - * @param $post_id
239 + * Option name to mark product template as default.
181 240 *
182 - * @return string|void
241 + * @param int $post_id Template ID.
242 + * @return string|null
183 243 */
184 244 public static function option_name_for_specific_product_set_default( $post_id ) {
185 245 if ( ! $post_id ) {
186 246 return;
@@ -189,11 +249,12 @@
189 249 return 'rtsb_template_specific_product_set_default_' . $post_id;
190 250 }
191 251
192 252 /**
193 - * @param $post_id
253 + * Check if a specific product template is default.
194 254 *
195 - * @return false|mixed|void|null
255 + * @param int $post_id Template ID.
256 + * @return mixed
196 257 */
197 258 public static function get_specific_product_as_default( $post_id ) {
198 259 if ( ! $post_id ) {
199 260 return;
@@ -199,15 +260,16 @@
199 260 return;
200 261 }
201 262 $options = self::option_name_for_specific_product_set_default( $post_id );
202 263
203 - return TemplateSettings::instance()->get_option( $options ); // get_option( $options );
264 + return TemplateSettings::instance()->get_option( $options );
204 265 }
205 266
206 267 /**
207 - * @param $post_id
268 + * Option name for default category template.
208 269 *
209 - * @return string|void
270 + * @param int $post_id Template ID.
271 + * @return string|null
210 272 */
211 273 public static function option_name_for_specific_category_set_default( $post_id ) {
212 274 if ( ! $post_id ) {
213 275 return;
@@ -216,11 +278,12 @@
216 278 return 'rtsb_template_specific_category_set_default_' . $post_id;
217 279 }
218 280
219 281 /**
220 - * @param $post_id
282 + * Check if a specific category template is default.
221 283 *
222 - * @return false|mixed|void|null
284 + * @param int $post_id Template ID.
285 + * @return mixed
223 286 */
224 287 public static function get_specific_category_as_default( $post_id ) {
225 288 if ( ! $post_id ) {
226 289 return;
@@ -225,15 +288,15 @@
225 288 if ( ! $post_id ) {
226 289 return;
227 290 }
228 291 $options = self::option_name_for_specific_category_set_default( $post_id );
229 -
230 - return TemplateSettings::instance()->get_option( $options ); // get_option( $options );
292 + return TemplateSettings::instance()->get_option( $options );
231 293 }
232 294
233 295 /**
234 - * @param $template_id
296 + * Get meta key by template ID for products.
235 297 *
298 + * @param int $template_id Template ID.
236 299 * @return string
237 300 */
238 301 public static function option_name_by_template_id( $template_id ) {
239 302 $_suff = null;
@@ -244,12 +307,74 @@
244 307 return self::$view_template_for_products_meta . $_suff;
245 308 }
246 309
247 310 /**
248 - * @param $template_id
311 + * Get option name for the selected product category template.
249 312 *
250 - * @return string
313 + * @param int|string $template_id Template ID for the category.
314 + *
315 + * @return string Generated option name.
251 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 + */
252 377 public static function archive_option_name_by_template_id( $template_id ) {
253 378 $_suff = null;
254 379 if ( $template_id ) {
255 380 $_suff = '_' . $template_id;
@@ -258,11 +383,11 @@
258 383 return self::$view_template_for_archive_meta . $_suff;
259 384 }
260 385
261 386 /**
262 - * Template builder
387 + * Returns meta key used to store builder type.
263 388 *
264 - * @var string
389 + * @return string
265 390 */
266 391 public static function template_type_meta_key() {
267 392 return self::$template_meta . '_type';
268 393 }
@@ -274,17 +399,16 @@
274 399 *
275 400 * @return string
276 401 */
277 402 public static function builder_type( $post_id ) {
278 -
279 403 $post_id = Fns::wpml_object_id( $post_id, self::$post_type_tb, 'default' );
280 404
281 405 if ( ! $post_id ) {
282 - return;
406 + return;
283 407 }
284 408
285 409 $cache_key = 'rtsb_template_type_' . $post_id;
286 - if ( isset( self::$cache[ $cache_key ] ) ) {
410 + if ( ! empty( self::$cache[ $cache_key ] ) ) {
287 411 return self::$cache[ $cache_key ];
288 412 }
289 413
290 414 $type = get_post_meta( $post_id, self::template_type_meta_key(), true );
@@ -295,11 +419,11 @@
295 419 return $type;
296 420 }
297 421
298 422 /**
299 - * Template builder
423 + * Check if current page is builder preview.
300 424 *
301 - * @var boolean
425 + * @return bool
302 426 */
303 427 public static function is_builder_preview() {
304 428 return is_singular( self::$post_type_tb );
305 429 }
@@ -304,11 +428,11 @@
304 428 return is_singular( self::$post_type_tb );
305 429 }
306 430
307 431 /**
308 - * Template builder
432 + * Check if archive template should load.
309 433 *
310 - * @var boolean
434 + * @return bool
311 435 */
312 436 public static function is_archive() {
313 437 return self::is_page_builder( 'archive', is_product_taxonomy() );
314 438 }
@@ -313,11 +437,11 @@
313 437 return self::is_page_builder( 'archive', is_product_taxonomy() );
314 438 }
315 439
316 440 /**
317 - * Template builder
441 + * Check if shop template should load.
318 442 *
319 - * @var boolean
443 + * @return bool
320 444 */
321 445 public static function is_shop() {
322 446 return self::is_page_builder( 'shop', is_shop() );
323 447 }
@@ -322,11 +446,11 @@
322 446 return self::is_page_builder( 'shop', is_shop() );
323 447 }
324 448
325 449 /**
326 - * Template builder
450 + * Check if cart template should load.
327 451 *
328 - * @var boolean
452 + * @return bool
329 453 */
330 454 public static function is_cart() {
331 455 return self::is_page_builder( 'cart', is_cart() );
332 456 }
@@ -331,11 +455,11 @@
331 455 return self::is_page_builder( 'cart', is_cart() );
332 456 }
333 457
334 458 /**
335 - * Template builder
459 + * Check if checkout template should load.
336 460 *
337 - * @var boolean
461 + * @return bool
338 462 */
339 463 public static function is_checkout() {
340 464 return self::is_page_builder( 'checkout', is_checkout() && ! is_wc_endpoint_url() && ! is_order_received_page() );
341 465 }
@@ -349,10 +473,11 @@
349 473 return self::is_page_builder( 'product', is_product() );
350 474 }
351 475
352 476 /**
353 - * @param $is_quick_view
477 + * Check if quick view page template should load.
354 478 *
479 + * @param bool $is_quick_view True if quick view.
355 480 * @return bool
356 481 */
357 482 public static function is_quick_views_page( $is_quick_view = false ) {
358 483 return rtsb()->has_pro() && self::is_page_builder( 'quick-view', $is_quick_view );
@@ -357,43 +482,38 @@
357 482 public static function is_quick_views_page( $is_quick_view = false ) {
358 483 return rtsb()->has_pro() && self::is_page_builder( 'quick-view', $is_quick_view );
359 484 }
360 485
486 +
361 487 /**
362 - * Builder page detector.
488 + * Check if a template should replace default WooCommerce page.
363 489 *
364 - * @param string $type builder Page type.
365 - * @param boolean $is_page page status.
366 - *
367 - * @return boolean
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
368 494 */
369 495 public static function is_page_builder( $type, $is_page, $is_require_auth = false ) {
370 - // if ( 'myaccount-dashboard' === $type ) {
371 - // $current_url = esc_url_raw( home_url( add_query_arg( null, null ) ) );
372 - //
373 - // if ( wc_get_account_endpoint_url( 'dashboard' ) !== $current_url ) {
374 - // return false;
375 - // }
376 - // }
377 -
378 - if ( self::builder_type( get_the_ID() ) === $type ) {
379 - return true;
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 + }
380 504 }
381 -
382 505 if ( $is_require_auth && ! is_user_logged_in() ) {
383 506 $type = 'myaccount-auth';
384 507 }
385 -
386 508 $builder_id = self::builder_page_id_by_type( $type );
387 509 if ( ! $builder_id ) {
388 510 return false;
389 511 }
390 -
391 512 $builder_type = self::builder_type( $builder_id );
392 513 if ( $type === $builder_type && $is_page ) {
393 514 return true;
394 515 }
395 -
396 516 return false;
397 517 }
398 518
399 519 /**