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

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