PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.1.0
ShopBuilder – WooCommerce Builder For Elementor v3.1.0
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 / Controllers / AssetsController.php

AssetsController.php in ShopBuilder – WooCommerce Builder For Elementor 3.1.0, at app/Controllers/AssetsController.php

585 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Assets Controller Class
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Controllers;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SB\Models\Settings;
12 use RadiusTheme\SB\Helpers\BuilderFns;
13 use RadiusTheme\SB\Traits\SingletonTrait;
14
15 // Do not allow directly accessing this file.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit( 'This script cannot be accessed directly.' );
18 }
19
20 /**
21 * Assets Controller Class
22 */
23 class AssetsController {
24
25 use SingletonTrait;
26
27 /**
28 * Plugin version
29 *
30 * @var string
31 */
32 private $version;
33
34 /**
35 * Ajax URL
36 *
37 * @var string
38 */
39 private static $ajaxurl;
40
41 /**
42 * Styles.
43 *
44 * @var array
45 */
46 private $styles = [];
47
48 /**
49 * Scripts.
50 *
51 * @var array
52 */
53 private $scripts = [];
54
55 /**
56 * Cached bundled assets.
57 *
58 * @var array|null
59 */
60 private $cached_bundled_assets = null;
61
62 /**
63 * Class Constructor
64 */
65 public function __construct() {
66 $this->version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : RTSB_VERSION;
67
68 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
69 if ( in_array( 'sitepress-multilingual-cms/sitepress.php', get_option( 'active_plugins' ) ) ) {
70 self::$ajaxurl = admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE );
71 } else {
72 self::$ajaxurl = admin_url( 'admin-ajax.php' );
73 }
74
75 /**
76 * Admin scripts.
77 */
78 add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_assets' ], 1 );
79 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_backend_scripts' ], 15 );
80
81 /**
82 * Public scripts.
83 */
84 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public_scripts' ], 25 );
85 /**
86 * General scripts.
87 */
88 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_general_public_scripts' ], 30 );
89 }
90
91 /**
92 * Get all frontend scripts.
93 *
94 * @return void
95 */
96 private function get_public_assets() {
97 $this->get_public_styles()->get_public_scripts();
98 }
99
100 /**
101 * Get public styles.
102 *
103 * @return object
104 */
105 private function get_public_styles() {
106 $rtl_suffix = is_rtl() ? '-rtl' : '';
107 $rtl_dir = is_rtl() ? trailingslashit( 'rtl' ) : trailingslashit( 'css' );
108
109 $this->styles[] = [
110 'handle' => 'rtsb-odometer',
111 'src' => rtsb()->get_assets_uri( 'vendor/odometer/css/odometer.min.css' ),
112 ];
113
114 $this->styles[] = [
115 'handle' => 'rtsb-fonts',
116 'src' => rtsb()->get_assets_uri( 'css/frontend/rtsb-fonts.css' ),
117 ];
118 if ( rtsb()->has_pro() ) {
119 $this->styles[] = [
120 'handle' => 'rtsb-noui-slider',
121 'src' => rtsbpro()->get_assets_uri( 'vendors/nouislider/nouislider.min.css' ),
122 ];
123 }
124
125 $pro_version = defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : '0';
126 $addon_condition = version_compare( $pro_version, '1.9.0', '<' );
127
128 if ( ! rtsb()->has_pro() && Fns::is_optimization_enabled() ) {
129 $this->styles[] = AssetRegistry::instance()->load_optimized_assets();
130 } else {
131 if ( $addon_condition ) {
132 $this->styles[] = [
133 'handle' => 'rtsb-general-addons',
134 'src' => rtsb()->get_assets_uri( $rtl_dir . 'frontend/general-addons' . $rtl_suffix . '.css' ),
135 ];
136 }
137 $this->styles[] = [
138 'handle' => 'rtsb-frontend',
139 'src' => rtsb()->get_assets_uri( $rtl_dir . 'frontend/frontend' . $rtl_suffix . '.css' ),
140 ];
141 }
142 if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) {
143 $this->styles[] = [
144 'handle' => 'photoswipe',
145 'src' => plugins_url( 'assets/css/photoswipe/photoswipe.min.css', WC_PLUGIN_FILE ),
146 ];
147
148 $this->styles[] = [
149 'handle' => 'photoswipe-default-skin',
150 'src' => plugins_url( 'assets/css/photoswipe/default-skin/default-skin.min.css', WC_PLUGIN_FILE ),
151 ];
152
153 // Load only for elementor editor page and fix some issue.
154 $this->styles[] = [
155 'handle' => 'elementor-editor-style-fix',
156 'src' => rtsb()->get_assets_uri( 'css/backend/elementor-editor-style-fix.css' ),
157 ];
158 }
159
160 return $this;
161 }
162
163 /**
164 * Get public scripts.
165 *
166 * @return object
167 */
168 private function get_public_scripts() {
169 $this->scripts[] = [
170 'handle' => 'swiper',
171 'src' => esc_url( $this->get_swiper_url() ),
172 'deps' => [ 'jquery' ],
173 'footer' => true,
174 ];
175 $this->scripts[] = [
176 'handle' => 'rtsb-waypoints',
177 'src' => rtsb()->get_assets_uri( 'vendor/waypoints/js/jquery.waypoints.min.js' ),
178 'deps' => [ 'jquery' ],
179 'footer' => true,
180 ];
181 $this->scripts[] = [
182 'handle' => 'rtsb-odometer',
183 'src' => rtsb()->get_assets_uri( 'vendor/odometer/js/odometer.min.js' ),
184 'deps' => [ 'jquery' ],
185 'footer' => true,
186 ];
187
188 $this->scripts[] = [
189 'handle' => 'rtsb-tipsy',
190 'src' => rtsb()->get_assets_uri( 'vendor/tipsy/tipsy.min.js' ),
191 'deps' => [ 'jquery' ],
192 'footer' => true,
193 ];
194
195 if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) {
196 $this->scripts[] = [
197 'handle' => 'flexslider',
198 'src' => plugins_url( 'assets/js/flexslider/jquery.flexslider.js', WC_PLUGIN_FILE ),
199 'deps' => [ 'jquery' ],
200 'footer' => true,
201 ];
202
203 $this->scripts[] = [
204 'handle' => 'photoswipe',
205 'src' => plugins_url( 'assets/js/photoswipe/photoswipe.js', WC_PLUGIN_FILE ),
206 'deps' => [ 'jquery' ],
207 'footer' => true,
208 ];
209
210 $this->scripts[] = [
211 'handle' => 'zoom',
212 'src' => plugins_url( 'assets/js/zoom/jquery.zoom.js', WC_PLUGIN_FILE ),
213 'deps' => [ 'jquery' ],
214 'footer' => true,
215 ];
216
217 $this->scripts[] = [
218 'handle' => 'photoswipe-ui-default',
219 'src' => plugins_url( 'assets/js/photoswipe/photoswipe-ui-default.js', WC_PLUGIN_FILE ),
220 'deps' => [ 'jquery', 'photoswipe' ],
221 'footer' => true,
222 ];
223
224 $this->scripts[] = [
225 'handle' => 'wc-single-product',
226 'src' => plugins_url( 'assets/js/frontend/single-product.js', WC_PLUGIN_FILE ),
227 'deps' => [ 'jquery', 'flexslider', 'photoswipe', 'photoswipe-ui-default', 'zoom' ],
228 'footer' => true,
229 ];
230
231 }
232
233 if ( rtsb()->has_pro() ) {
234 $this->scripts[] = [
235 'handle' => 'rtsb-noui-slider',
236 'src' => rtsbpro()->get_assets_uri( 'vendors/nouislider/nouislider.min.js' ),
237 'deps' => [ 'jquery' ],
238 'footer' => true,
239 ];
240
241 $this->scripts[] = [
242 'handle' => 'rtsb-sticky-sidebar',
243 'src' => rtsbpro()->get_assets_uri( 'vendors/sticky-sidebar/sticky-sidebar.min.js' ),
244 'deps' => [ 'jquery' ],
245 'footer' => true,
246 ];
247
248 $this->scripts[] = [
249 'handle' => 'rtsb-popper',
250 'src' => rtsbpro()->get_assets_uri( 'vendors/tippy/popper.min.js' ),
251 'deps' => [ 'jquery' ],
252 'footer' => true,
253 ];
254
255 $this->scripts[] = [
256 'handle' => 'rtsb-tippy',
257 'src' => rtsbpro()->get_assets_uri( 'vendors/tippy/tippy.min.js' ),
258 'deps' => [ 'jquery', 'rtsb-popper' ],
259 'footer' => true,
260 ];
261 }
262
263 if ( ! rtsb()->has_pro() && Fns::is_optimization_enabled() ) {
264 $this->scripts[] = AssetRegistry::instance()->load_optimized_assets( 'js' );
265 } else {
266 $this->scripts[] = [
267 'handle' => 'rtsb-public',
268 'src' => rtsb()->get_assets_uri( 'js/frontend/frontend.js' ),
269 'deps' => [ 'jquery', 'imagesloaded', 'swiper' ],
270 'footer' => true,
271 ];
272 }
273
274 return $this;
275 }
276
277 /**
278 * Register public scripts.
279 *
280 * @return void
281 */
282 public function register_public_scripts() {
283 $this->get_public_assets();
284 $this->styles = array_filter( $this->styles );
285 $this->scripts = array_filter( $this->scripts );
286
287 // Register public styles.
288 foreach ( $this->styles as $style ) {
289 wp_register_style( $style['handle'], $style['src'], '', $this->version );
290 }
291
292 // Register public scripts.
293 foreach ( $this->scripts as $script ) {
294 wp_register_script( $script['handle'], $script['src'], $script['deps'], $this->version, $script['footer'] );
295 }
296 }
297
298 /**
299 * Enqueues public scripts.
300 *
301 * @return void
302 */
303 public function enqueue_public_scripts() {
304 /**
305 * Register scripts.
306 */
307 $this->register_public_scripts();
308
309 /**
310 * Enqueue scripts.
311 */
312 if ( BuilderFns::is_builder_preview() && 'elementor' === Fns::page_edit_with( get_the_ID() ) ) {
313 /**
314 * Styles.
315 */
316 wp_enqueue_style( 'photoswipe' );
317 wp_enqueue_style( 'photoswipe-default-skin' );
318 wp_enqueue_style( 'elementor-editor-style-fix' );
319 wp_enqueue_style( 'woocommerce-general' );
320
321 /**
322 * Scripts.
323 */
324 $handle = Fns::optimized_handle( 'rtsb-public' );
325
326 wp_enqueue_script( 'flexslider' );
327 wp_enqueue_script( 'wc-single-product' );
328 wp_dequeue_script( $handle );
329 wp_enqueue_script( 'swiper' );
330
331 if ( Fns::is_optimization_enabled() ) {
332 Fns::enqueue_optimized_assets();
333 } else {
334 wp_enqueue_script( $handle );
335 }
336 }
337
338 wp_enqueue_script( 'rtsb-tipsy' );
339
340 if ( Fns::is_optimization_enabled() ) {
341 Fns::enqueue_optimized_assets();
342 } else {
343 wp_enqueue_style( 'rtsb-fonts' );
344 wp_enqueue_style( 'rtsb-frontend' );
345 wp_enqueue_script( 'rtsb-public' );
346 }
347
348 /**
349 * Localize script.
350 */
351 self::localizeData();
352 }
353
354 /**
355 * Localized Data.
356 *
357 * @static
358 * @return void
359 */
360 public static function localizeData() {
361 $handle = Fns::optimized_handle( 'rtsb-public' );
362
363 wp_localize_script(
364 $handle,
365 'rtsbPublicParams',
366 [
367 'ajaxUrl' => esc_url( self::$ajaxurl ),
368 'homeurl' => home_url(),
369 'wcCartUrl' => wc_get_cart_url(),
370 'addedToCartText' => esc_html__( 'Product Added', 'shopbuilder' ),
371 'singleCartToastrText' => esc_html__( 'Successfully Added', 'shopbuilder' ),
372 'singleCartBtnText' => apply_filters( 'rtsb/global/single_add_to_cart_success', esc_html__( 'Added to Cart', 'shopbuilder' ) ),
373 'browseCartText' => esc_html__( 'Browse Cart', 'shopbuilder' ),
374 'noProductsText' => apply_filters( 'rtsb/global/no_products_text', esc_html__( 'No more products to load', 'shopbuilder' ) ),
375 'isOptimizationEnabled' => Fns::is_optimization_enabled(),
376 'notice' => [
377 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ),
378 ],
379 rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ),
380 ]
381 );
382 }
383
384 /**
385 * Registers Admin scripts.
386 *
387 * @return void
388 */
389 public function register_backend_assets() {
390 /**
391 * Styles.
392 */
393 wp_register_style( 'rtsb-admin-app', rtsb()->get_assets_uri( 'css/backend/admin-settings.css' ), '', $this->version );
394 wp_register_style( 'rtsb-fonts', rtsb()->get_assets_uri( 'css/frontend/rtsb-fonts.css' ), '', $this->version );
395
396 $current_screen = get_current_screen();
397
398 if ( 'edit-rtsb_builder' === $current_screen->id ) {
399 if ( ! function_exists( 'woocommerce_get_asset_url' ) ) {
400 include_once WC_ABSPATH . 'includes/wc-template-functions.php';
401 }
402
403 wp_deregister_style( 'select2' );
404 wp_register_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
405 }
406 wp_register_style( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'css/backend/template-builder.css' ), '', $this->version );
407
408 /**
409 * Scripts.
410 */
411 wp_register_script( 'rtsb-admin-app', rtsb()->get_assets_uri( 'js/backend/admin-settings.js' ), '', $this->version, true );
412 wp_register_script( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'js/backend/template-builder.js' ), '', $this->version, true );
413 }
414
415 /**
416 * Enqueues admin scripts.
417 *
418 * @return void
419 */
420 public function enqueue_backend_scripts() {
421 ob_start(); ?>
422 #adminmenu .toplevel_page_rtsb .wp-menu-image img {
423 width: auto;
424 height: 22px;
425 padding: 4px 0;
426 box-sizing: content-box;
427 }
428 .post-type-rtsb_builder li#wp-admin-bar-WPML_ALS_all,
429 .post-type-rtsb_builder li.language_all{
430 display: none;
431 }
432
433 <?php
434 $admin_global_style = ob_get_clean();
435 // Speed Optimization.
436 wp_add_inline_style( 'admin-menu', $admin_global_style );
437
438 global $pagenow;
439
440 $whitelisted_pages = [ 'rtsb-settings', 'rtsb-get-help', 'rtsb-themes' ];
441 $current_page = ! empty( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
442
443 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && in_array( $current_page, $whitelisted_pages, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
444 /**
445 * Styles.
446 */
447 wp_enqueue_style( 'rtsb-admin-app' );
448 wp_enqueue_style( 'rtsb-fonts' );
449 }
450
451 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && 'rtsb-settings' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
452 // Elementor Console Error Fixed For "rtsb-settings" Page.
453 wp_dequeue_script( 'elementor-admin-top-bar' );
454 wp_dequeue_script( 'elementor-common' );
455 wp_dequeue_script( 'elementor-dev-tools' );
456 wp_dequeue_script( 'elementor-web-cli' );
457 wp_dequeue_script( 'elementor-import-export-admin' );
458 wp_dequeue_script( 'elementor-app-loader' );
459 wp_dequeue_script( 'elementor-admin-modules' );
460 wp_dequeue_script( 'elementor-ai-media-library' );
461 wp_dequeue_script( 'elementor-admin' );
462
463 wp_dequeue_style( 'elementor-admin-top-bar' );
464 wp_dequeue_style( 'elementor-admin' );
465 wp_dequeue_style( 'e-theme-ui-light' );
466 wp_dequeue_style( 'elementor-common' );
467
468 /**
469 * Scripts.
470 */
471 wp_enqueue_media();
472 wp_enqueue_script( 'updates' );
473 wp_enqueue_script( 'rtsb-admin-app' );
474 wp_localize_script(
475 'rtsb-admin-app',
476 'rtsbParams',
477 [
478 'ajaxurl' => esc_url( self::$ajaxurl ),
479 'homeurl' => home_url(),
480 'adminLogo' => rtsb()->get_assets_uri( 'images/icon/ShopBuilder-Logo.svg' ),
481 'restApiUrl' => esc_url_raw( rest_url() ),
482 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
483 'nonce' => wp_create_nonce( rtsb()->nonceText ),
484 'pages' => Fns::get_pages(),
485 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
486 'proVersion' => defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : '0',
487 'updateRates' => esc_html__( 'Update All Rates', 'shopbuilder' ),
488 'sections' => Settings::instance()->get_sections(),
489 'userRoles' => Fns::get_all_user_roles(),
490 'hasElementor' => defined( 'ELEMENTOR_VERSION' ),
491 'loadElementor' => Fns::should_load_elementor_scripts(),
492 'isOptimizationEnabled' => Fns::is_optimization_enabled(),
493 ]
494 );
495 } else {
496 $current_screen = get_current_screen();
497
498 if ( 'edit-rtsb_builder' === $current_screen->id ) {
499 if ( ! function_exists( 'woocommerce_get_asset_url' ) ) {
500 include_once WC_ABSPATH . 'includes/wc-template-functions.php';
501 }
502 /**
503 * Styles.
504 */
505 wp_enqueue_style( 'rtsb-templatebuilder' );
506
507 wp_enqueue_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
508
509 wp_enqueue_script( 'selectWoo' );
510 /**
511 * Scripts.
512 */
513 wp_enqueue_script( 'rtsb-templatebuilder' );
514
515 wp_localize_script(
516 'rtsb-templatebuilder',
517 'rtsbParams',
518 [
519 'ajaxurl' => esc_url( self::$ajaxurl ),
520 'homeurl' => home_url(),
521 rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ),
522 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
523 ]
524 );
525 }
526 }
527 }
528
529 /**
530 * Enqueues general public scripts.
531 *
532 * @return void
533 */
534 public function enqueue_general_public_scripts() {
535 $notification_color = Fns::get_option( 'general', 'notification', 'notification_color', '#004BFF' );
536 $notification_bgcolor = Fns::get_option( 'general', 'notification', 'notification_bg', '#F5F8FF' );
537 $notification_button_color = Fns::get_option( 'general', 'notification', 'notification_btn_color', '#0039C0' );
538
539 $dynamic_css = '';
540
541 if ( ! empty( $notification_color ) ) {
542 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{color:{$notification_color}}";
543 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success:before{background-color:{$notification_color}}";
544 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-close-button{color:{$notification_color}}";
545 }
546
547 if ( ! empty( $notification_bgcolor ) ) {
548 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{background-color:{$notification_bgcolor}}";
549 }
550
551 if ( ! empty( $notification_button_color ) ) {
552 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success a{color:{$notification_button_color}}";
553 }
554
555 if ( ! empty( $dynamic_css ) ) {
556 wp_add_inline_style( Fns::optimized_handle( 'rtsb-frontend' ), $dynamic_css );
557 }
558 }
559
560 /**
561 * Get the appropriate Swiper JS file URL.
562 *
563 * @return string
564 */
565 private function get_swiper_url() {
566 $default_swiper_url = rtsb()->get_assets_uri( 'vendor/swiper/js/swiper-bundle.min.js' );
567
568 if ( defined( 'ELEMENTOR_ASSETS_PATH' ) && defined( 'ELEMENTOR_ASSETS_URL' ) ) {
569 $experiment = get_option( 'elementor_experiment-e_swiper_latest' );
570
571 $el_relative_path = ( 'active' === $experiment || 'default' === $experiment )
572 ? 'lib/swiper/v8/swiper.min.js'
573 : 'lib/swiper/swiper.min.js';
574
575 $el_full_path = ELEMENTOR_ASSETS_PATH . $el_relative_path;
576
577 if ( file_exists( $el_full_path ) ) {
578 return ELEMENTOR_ASSETS_URL . $el_relative_path;
579 }
580 }
581
582 return $default_swiper_url;
583 }
584 }
585