PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.13
ShopBuilder – WooCommerce Builder For Elementor v2.1.13
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 2.1.13, at app/Helpers/BuilderFns.php

412 lines 8.9 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 * Page builder id.
64 *
65 * @return int
66 */
67 public static function builder_page_id_by_type( $type ) {
68 global $post;
69
70 if ( ! array_key_exists( $type, self::builder_page_types() ) ) {
71 return 0;
72 }
73
74 // Wpml Supported.
75 $options = self::option_name( $type );
76 $post_id = TemplateSettings::instance()->get_option( $options );
77 if ( empty( $post ) ) {
78 return $post_id;
79 }
80
81 if ( empty( $post_id ) ) {
82 $options = self::option_name( $type, true );
83 $post_id = TemplateSettings::instance()->get_option( $options );
84 }
85
86 $cache_key = 'rtsb_builder_page_id_by_type_' . $type;
87 if ( isset( self::$cache[ $cache_key ] ) ) {
88 return self::$cache[ $cache_key ];
89 }
90 switch ( $type ) {
91 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 );
95
96 if ( 'publish' === $post->post_status ) {
97 $post_id = $builder_id && $set_default ? $builder_id : $post_id;
98 }
99 }
100 break;
101 case 'archive':
102 $queried_object = get_queried_object();
103
104 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;
110 }
111 }
112 break;
113
114 default:
115 }
116 if ( 'publish' === $post->post_status && self::builder_type( $post_id ) === $type ) {
117 self::$cache[ $cache_key ] = $post_id;
118 return $post_id;
119 }
120
121 return $post_id;
122 }
123
124 /**
125 * Page builder id.
126 *
127 * @return init
128 */
129 public static function builder_page_id_by_page() {
130 $type = apply_filters( 'rtsb/builder/set/current/page/type', '' );
131 if ( ! $type ) {
132 return;
133 }
134
135 return self::builder_page_id_by_type( $type );
136 }
137
138 /**
139 * Page builder Page for.
140 *
141 * @return array
142 */
143 public static function builder_page_types() {
144 $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' ),
150 ];
151
152 return apply_filters( 'rtsb/builder/register/page/types', $page_types );
153 }
154
155 /**
156 * Option name.
157 *
158 * @param string $type Builder type.
159 *
160 * @return string
161 */
162 public static function option_name( $type, $default_lang = false ) {
163
164 $type = str_replace( [ '-', ' ' ], '_', $type );
165 $suffix = $type;
166
167 if ( $default_lang ) {
168 return self::$template_meta . '_default_' . $suffix;
169 }
170 $current_lang = Fns::wpml_current_language();
171
172 if ( ! empty( $current_lang ) ) {
173 $suffix = $type . '_' . $current_lang;
174 }
175
176 return self::$template_meta . '_default_' . $suffix;
177 }
178
179 /**
180 * @param $post_id
181 *
182 * @return string|void
183 */
184 public static function option_name_for_specific_product_set_default( $post_id ) {
185 if ( ! $post_id ) {
186 return;
187 }
188
189 return 'rtsb_template_specific_product_set_default_' . $post_id;
190 }
191
192 /**
193 * @param $post_id
194 *
195 * @return false|mixed|void|null
196 */
197 public static function get_specific_product_as_default( $post_id ) {
198 if ( ! $post_id ) {
199 return;
200 }
201 $options = self::option_name_for_specific_product_set_default( $post_id );
202
203 return TemplateSettings::instance()->get_option( $options ); // get_option( $options );
204 }
205
206 /**
207 * @param $post_id
208 *
209 * @return string|void
210 */
211 public static function option_name_for_specific_category_set_default( $post_id ) {
212 if ( ! $post_id ) {
213 return;
214 }
215
216 return 'rtsb_template_specific_category_set_default_' . $post_id;
217 }
218
219 /**
220 * @param $post_id
221 *
222 * @return false|mixed|void|null
223 */
224 public static function get_specific_category_as_default( $post_id ) {
225 if ( ! $post_id ) {
226 return;
227 }
228 $options = self::option_name_for_specific_category_set_default( $post_id );
229
230 return TemplateSettings::instance()->get_option( $options ); // get_option( $options );
231 }
232
233 /**
234 * @param $template_id
235 *
236 * @return string
237 */
238 public static function option_name_by_template_id( $template_id ) {
239 $_suff = null;
240 if ( $template_id ) {
241 $_suff = '_' . $template_id;
242 }
243
244 return self::$view_template_for_products_meta . $_suff;
245 }
246
247 /**
248 * @param $template_id
249 *
250 * @return string
251 */
252 public static function archive_option_name_by_template_id( $template_id ) {
253 $_suff = null;
254 if ( $template_id ) {
255 $_suff = '_' . $template_id;
256 }
257
258 return self::$view_template_for_archive_meta . $_suff;
259 }
260
261 /**
262 * Template builder
263 *
264 * @var string
265 */
266 public static function template_type_meta_key() {
267 return self::$template_meta . '_type';
268 }
269
270 /**
271 * Get builder type.
272 *
273 * @param [type] $post_id Post id.
274 *
275 * @return string
276 */
277 public static function builder_type( $post_id ) {
278
279 $post_id = Fns::wpml_object_id( $post_id, self::$post_type_tb, 'default' );
280
281 if ( ! $post_id ) {
282 return;
283 }
284
285 $cache_key = 'rtsb_template_type_' . $post_id;
286 if ( isset( self::$cache[ $cache_key ] ) ) {
287 return self::$cache[ $cache_key ];
288 }
289
290 $type = get_post_meta( $post_id, self::template_type_meta_key(), true );
291
292 // Cache the results in a static variable for the next call.
293 self::$cache[ $cache_key ] = $type;
294
295 return $type;
296 }
297
298 /**
299 * Template builder
300 *
301 * @var boolean
302 */
303 public static function is_builder_preview() {
304 return is_singular( self::$post_type_tb );
305 }
306
307 /**
308 * Template builder
309 *
310 * @var boolean
311 */
312 public static function is_archive() {
313 return self::is_page_builder( 'archive', is_product_taxonomy() );
314 }
315
316 /**
317 * Template builder
318 *
319 * @var boolean
320 */
321 public static function is_shop() {
322 return self::is_page_builder( 'shop', is_shop() );
323 }
324
325 /**
326 * Template builder
327 *
328 * @var boolean
329 */
330 public static function is_cart() {
331 return self::is_page_builder( 'cart', is_cart() );
332 }
333
334 /**
335 * Template builder
336 *
337 * @var boolean
338 */
339 public static function is_checkout() {
340 return self::is_page_builder( 'checkout', is_checkout() && ! is_wc_endpoint_url() && ! is_order_received_page() );
341 }
342
343 /**
344 * Single Page builder Detection
345 *
346 * @return boolean
347 */
348 public static function is_product() {
349 return self::is_page_builder( 'product', is_product() );
350 }
351
352 /**
353 * @param $is_quick_view
354 *
355 * @return bool
356 */
357 public static function is_quick_views_page( $is_quick_view = false ) {
358 return rtsb()->has_pro() && self::is_page_builder( 'quick-view', $is_quick_view );
359 }
360
361 /**
362 * Builder page detector.
363 *
364 * @param string $type builder Page type.
365 * @param boolean $is_page page status.
366 *
367 * @return boolean
368 */
369 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;
380 }
381
382 if ( $is_require_auth && ! is_user_logged_in() ) {
383 $type = 'myaccount-auth';
384 }
385
386 $builder_id = self::builder_page_id_by_type( $type );
387 if ( ! $builder_id ) {
388 return false;
389 }
390
391 $builder_type = self::builder_type( $builder_id );
392 if ( $type === $builder_type && $is_page ) {
393 return true;
394 }
395
396 return false;
397 }
398
399 /**
400 * Get builder content function
401 *
402 * @param [type] $template_id builder Template id.
403 * @param boolean $with_css with css.
404 *
405 * @return mixed
406 */
407 public static function get_builder_content( $template_id, $with_css = false ) {
408 // Don't Use cache: tickets ID -> 75376.
409 return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, $with_css );
410 }
411 }
412