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

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

613 lines 18.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 add_filter( 'rtsb/optimizer/scripts/deps', [ $this, 'extend_shared_dependencies' ], 99 );
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 * Shared dependencies.
92 *
93 * @param array $deps Dependencies.
94 *
95 * @return array
96 */
97 public function extend_shared_dependencies( $deps ) {
98 if ( Fns::is_elementor_widget_active( 'products_slider' ) ) {
99 $deps[] = 'swiper';
100 }
101 return array_unique( $deps );
102 }
103 /**
104 * Get all frontend scripts.
105 *
106 * @return void
107 */
108 private function get_public_assets() {
109 $this->get_public_styles()->get_public_scripts();
110 }
111
112 /**
113 * Get public styles.
114 *
115 * @return object
116 */
117 private function get_public_styles() {
118 $rtl_suffix = is_rtl() ? '-rtl' : '';
119 $rtl_dir = is_rtl() ? trailingslashit( 'rtl' ) : trailingslashit( 'css' );
120
121 $this->styles[] = [
122 'handle' => 'rtsb-odometer',
123 'src' => rtsb()->get_assets_uri( 'vendor/odometer/css/odometer.min.css' ),
124 ];
125
126 $this->styles[] = [
127 'handle' => 'rtsb-fonts',
128 'src' => rtsb()->get_assets_uri( 'css/frontend/rtsb-fonts.css' ),
129 ];
130 if ( rtsb()->has_pro() ) {
131 $this->styles[] = [
132 'handle' => 'rtsb-noui-slider',
133 'src' => rtsbpro()->get_assets_uri( 'vendors/nouislider/nouislider.min.css' ),
134 ];
135 }
136
137 $pro_version = defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : '0';
138 $addon_condition = version_compare( $pro_version, '1.9.0', '<' );
139
140 if ( ! rtsb()->has_pro() && Fns::is_optimization_enabled() ) {
141 $this->styles[] = AssetRegistry::instance()->load_optimized_assets();
142 } else {
143 if ( $addon_condition ) {
144 $this->styles[] = [
145 'handle' => 'rtsb-general-addons',
146 'src' => rtsb()->get_assets_uri( $rtl_dir . 'frontend/general-addons' . $rtl_suffix . '.css' ),
147 ];
148 }
149 $this->styles[] = [
150 'handle' => 'rtsb-frontend',
151 'src' => rtsb()->get_assets_uri( $rtl_dir . 'frontend/frontend' . $rtl_suffix . '.css' ),
152 ];
153 }
154 if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) {
155 $this->styles[] = [
156 'handle' => 'photoswipe',
157 'src' => plugins_url( 'assets/css/photoswipe/photoswipe.min.css', WC_PLUGIN_FILE ),
158 ];
159
160 $this->styles[] = [
161 'handle' => 'photoswipe-default-skin',
162 'src' => plugins_url( 'assets/css/photoswipe/default-skin/default-skin.min.css', WC_PLUGIN_FILE ),
163 ];
164
165 // Load only for elementor editor page and fix some issue.
166 $this->styles[] = [
167 'handle' => 'elementor-editor-style-fix',
168 'src' => rtsb()->get_assets_uri( 'css/backend/elementor-editor-style-fix.css' ),
169 ];
170 }
171
172 return $this;
173 }
174
175 /**
176 * Get public scripts.
177 *
178 * @return object
179 */
180 private function get_public_scripts() {
181 $this->scripts[] = [
182 'handle' => 'swiper',
183 'src' => esc_url( $this->get_swiper_url() ),
184 'deps' => [ 'jquery' ],
185 'footer' => true,
186 ];
187 $this->scripts[] = [
188 'handle' => 'rtsb-waypoints',
189 'src' => rtsb()->get_assets_uri( 'vendor/waypoints/js/jquery.waypoints.min.js' ),
190 'deps' => [ 'jquery' ],
191 'footer' => true,
192 ];
193 $this->scripts[] = [
194 'handle' => 'rtsb-odometer',
195 'src' => rtsb()->get_assets_uri( 'vendor/odometer/js/odometer.min.js' ),
196 'deps' => [ 'jquery' ],
197 'footer' => true,
198 ];
199
200 $this->scripts[] = [
201 'handle' => 'rtsb-tipsy',
202 'src' => rtsb()->get_assets_uri( 'vendor/tipsy/tipsy.min.js' ),
203 'deps' => [ 'jquery' ],
204 'footer' => true,
205 ];
206
207 if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) {
208 $this->scripts[] = [
209 'handle' => 'wc-flexslider',
210 'src' => plugins_url( 'assets/js/flexslider/jquery.flexslider.js', WC_PLUGIN_FILE ),
211 'deps' => [ 'jquery' ],
212 'footer' => true,
213 ];
214
215 $this->scripts[] = [
216 'handle' => 'photoswipe',
217 'src' => plugins_url( 'assets/js/photoswipe/photoswipe.js', WC_PLUGIN_FILE ),
218 'deps' => [ 'jquery' ],
219 'footer' => true,
220 ];
221
222 $this->scripts[] = [
223 'handle' => 'wc-zoom',
224 'src' => plugins_url( 'assets/js/zoom/jquery.zoom.js', WC_PLUGIN_FILE ),
225 'deps' => [ 'jquery' ],
226 'footer' => true,
227 ];
228
229 $this->scripts[] = [
230 'handle' => 'wc-photoswipe-ui-default',
231 'src' => plugins_url( 'assets/js/photoswipe/photoswipe-ui-default.js', WC_PLUGIN_FILE ),
232 'deps' => [ 'jquery', 'photoswipe' ],
233 'footer' => true,
234 ];
235
236 $this->scripts[] = [
237 'handle' => 'wc-single-product',
238 'src' => plugins_url( 'assets/js/frontend/single-product.js', WC_PLUGIN_FILE ),
239 'deps' => [ 'jquery', 'wc-flexslider', 'photoswipe', 'wc-photoswipe-ui-default', 'wc-zoom' ],
240 'footer' => true,
241 ];
242
243 }
244
245 if ( rtsb()->has_pro() ) {
246 $this->scripts[] = [
247 'handle' => 'rtsb-noui-slider',
248 'src' => rtsbpro()->get_assets_uri( 'vendors/nouislider/nouislider.min.js' ),
249 'deps' => [ 'jquery' ],
250 'footer' => true,
251 ];
252
253 $this->scripts[] = [
254 'handle' => 'rtsb-sticky-sidebar',
255 'src' => rtsbpro()->get_assets_uri( 'vendors/sticky-sidebar/sticky-sidebar.min.js' ),
256 'deps' => [ 'jquery' ],
257 'footer' => true,
258 ];
259
260 $this->scripts[] = [
261 'handle' => 'rtsb-popper',
262 'src' => rtsbpro()->get_assets_uri( 'vendors/tippy/popper.min.js' ),
263 'deps' => [ 'jquery' ],
264 'footer' => true,
265 ];
266
267 $this->scripts[] = [
268 'handle' => 'rtsb-tippy',
269 'src' => rtsbpro()->get_assets_uri( 'vendors/tippy/tippy.min.js' ),
270 'deps' => [ 'jquery', 'rtsb-popper' ],
271 'footer' => true,
272 ];
273 }
274
275 if ( ! rtsb()->has_pro() && Fns::is_optimization_enabled() ) {
276 $this->scripts[] = AssetRegistry::instance()->load_optimized_assets( 'js' );
277 } else {
278 $this->scripts[] = [
279 'handle' => 'rtsb-public',
280 'src' => rtsb()->get_assets_uri( 'js/frontend/frontend.js' ),
281 'deps' => [ 'jquery', 'imagesloaded', 'swiper' ],
282 'footer' => true,
283 ];
284 }
285
286 return $this;
287 }
288
289 /**
290 * Register public scripts.
291 *
292 * @return void
293 */
294 public function register_public_scripts() {
295 $this->get_public_assets();
296 $this->styles = array_filter( $this->styles );
297 $this->scripts = array_filter( $this->scripts );
298
299 // Register public styles.
300 foreach ( $this->styles as $style ) {
301 wp_register_style( $style['handle'], $style['src'], '', $this->version );
302 }
303
304 // Register public scripts.
305 foreach ( $this->scripts as $script ) {
306 wp_register_script( $script['handle'], $script['src'], $script['deps'], $this->version, $script['footer'] );
307 }
308 }
309
310 /**
311 * Enqueues public scripts.
312 *
313 * @return void
314 */
315 public function enqueue_public_scripts() {
316 /**
317 * Register scripts.
318 */
319 $this->register_public_scripts();
320
321 /**
322 * Enqueue scripts.
323 */
324 if ( BuilderFns::is_builder_preview() && 'elementor' === Fns::page_edit_with( get_the_ID() ) ) {
325 /**
326 * Styles.
327 */
328 wp_enqueue_style( 'photoswipe' );
329 wp_enqueue_style( 'photoswipe-default-skin' );
330 wp_enqueue_style( 'elementor-editor-style-fix' );
331 wp_enqueue_style( 'woocommerce-general' );
332
333 /**
334 * Scripts.
335 */
336 $handle = Fns::optimized_handle( 'rtsb-public' );
337
338 wp_enqueue_script( 'wc-flexslider' );
339 wp_enqueue_script( 'wc-single-product' );
340 wp_dequeue_script( $handle );
341 wp_enqueue_script( 'swiper' );
342
343 if ( Fns::is_optimization_enabled() ) {
344 Fns::enqueue_optimized_assets();
345 } else {
346 wp_enqueue_script( $handle );
347 }
348 }
349
350 wp_enqueue_script( 'rtsb-tipsy' );
351
352 if ( Fns::is_optimization_enabled() ) {
353 Fns::enqueue_optimized_assets();
354 } else {
355 wp_enqueue_style( 'rtsb-fonts' );
356 wp_enqueue_style( 'rtsb-frontend' );
357 wp_enqueue_script( 'rtsb-public' );
358 }
359
360 /**
361 * Localize script.
362 */
363 self::localizeData();
364 }
365
366 /**
367 * Localized Data.
368 *
369 * @static
370 * @return void
371 */
372 public static function localizeData() {
373 $handle = Fns::optimized_handle( 'rtsb-public' );
374
375 wp_localize_script(
376 $handle,
377 'rtsbPublicParams',
378 [
379 'ajaxUrl' => esc_url( self::$ajaxurl ),
380 'homeurl' => home_url(),
381 'wcCartUrl' => wc_get_cart_url(),
382 'addedToCartText' => esc_html__( 'Product Added', 'shopbuilder' ),
383 'singleCartToastrText' => esc_html__( 'Successfully Added', 'shopbuilder' ),
384 'singleCartBtnText' => apply_filters( 'rtsb/global/single_add_to_cart_success', esc_html__( 'Added to Cart', 'shopbuilder' ) ),
385 'browseCartText' => esc_html__( 'Browse Cart', 'shopbuilder' ),
386 'noProductsText' => apply_filters( 'rtsb/global/no_products_text', esc_html__( 'No more products to load', 'shopbuilder' ) ),
387 'isOptimizationEnabled' => Fns::is_optimization_enabled(),
388 'isLoaderEnabled' => Fns::enable_loader(),
389 'notice' => [
390 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ),
391 ],
392 rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ),
393 ]
394 );
395 }
396
397 /**
398 * Registers Admin scripts.
399 *
400 * @return void
401 */
402 public function register_backend_assets() {
403 /**
404 * Styles.
405 */
406 wp_register_style( 'rtsb-admin-app', rtsb()->get_assets_uri( 'css/backend/admin-settings.css' ), '', $this->version );
407 wp_register_style( 'rtsb-fonts', rtsb()->get_assets_uri( 'css/frontend/rtsb-fonts.css' ), '', $this->version );
408
409 $current_screen = get_current_screen();
410
411 if ( 'edit-rtsb_builder' === $current_screen->id ) {
412 if ( ! function_exists( 'woocommerce_get_asset_url' ) ) {
413 include_once WC_ABSPATH . 'includes/wc-template-functions.php';
414 }
415
416 wp_deregister_style( 'select2' );
417 wp_register_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
418 }
419 wp_register_style( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'css/backend/template-builder.css' ), '', $this->version );
420
421 /**
422 * Scripts.
423 */
424 wp_register_script( 'rtsb-admin-app', rtsb()->get_assets_uri( 'js/backend/admin-settings.js' ), '', $this->version, true );
425 wp_register_script( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'js/backend/template-builder.js' ), '', $this->version, true );
426 wp_localize_script(
427 'rtsb-admin-app',
428 'rtsbParams',
429 [
430 'ajaxurl' => esc_url( self::$ajaxurl ),
431 'homeurl' => home_url(),
432 'adminLogo' => rtsb()->get_assets_uri( 'images/icon/ShopBuilder-Logo.svg' ),
433 'restApiUrl' => esc_url_raw( rest_url() ),
434 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
435 'nonce' => wp_create_nonce( rtsb()->nonceText ),
436 'pages' => Fns::get_pages(),
437 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
438 'proVersion' => defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : '0',
439 'updateRates' => esc_html__( 'Update All Rates', 'shopbuilder' ),
440 'sections' => Settings::instance()->get_sections(),
441 'userRoles' => Fns::get_all_user_roles(),
442 'hasElementor' => defined( 'ELEMENTOR_VERSION' ),
443 'loadElementor' => Fns::should_load_elementor_scripts(),
444 'isOptimizationEnabled' => Fns::is_optimization_enabled(),
445 ]
446 );
447 wp_localize_script(
448 'rtsb-admin-app',
449 'rtsbCaAbandonment',
450 [
451 'mainTitle' => esc_html__( 'Abandoned Carts Follow Up', 'shopbuilder' ),
452 'followUpColumnTitle' => [
453 'UserName' => esc_html__( 'Name', 'shopbuilder' ),
454 'EmailTo' => esc_html__( 'Email To', 'shopbuilder' ),
455 'CartTotal' => esc_html__( 'Cart Total', 'shopbuilder' ),
456 'OrderStatus' => esc_html__( 'Order Status', 'shopbuilder' ),
457 'DateTime' => esc_html__( 'Date / Time', 'shopbuilder' ),
458 'Action' => esc_html__( 'Action', 'shopbuilder' ),
459 ],
460 ]
461 );
462 }
463
464 /**
465 * Enqueues admin scripts.
466 *
467 * @return void
468 */
469 public function enqueue_backend_scripts() {
470 ob_start(); ?>
471 #adminmenu .toplevel_page_rtsb .wp-menu-image img {
472 width: auto;
473 height: 22px;
474 padding: 4px 0;
475 box-sizing: content-box;
476 }
477 .post-type-rtsb_builder li#wp-admin-bar-WPML_ALS_all,
478 .post-type-rtsb_builder li.language_all{
479 display: none;
480 }
481
482 <?php
483 $admin_global_style = ob_get_clean();
484 // Speed Optimization.
485 wp_add_inline_style( 'admin-menu', $admin_global_style );
486
487 global $pagenow;
488
489 $whitelisted_pages = [ 'rtsb-settings', 'rtsb-get-help', 'rtsb-themes' ];
490 $current_page = ! empty( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
491
492 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && in_array( $current_page, $whitelisted_pages, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
493 /**
494 * Styles.
495 */
496 wp_enqueue_style( 'rtsb-admin-app' );
497 wp_enqueue_style( 'rtsb-fonts' );
498 }
499
500 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && 'rtsb-settings' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
501 // Elementor Console Error Fixed For "rtsb-settings" Page.
502 wp_dequeue_script( 'elementor-admin-top-bar' );
503 wp_dequeue_script( 'elementor-common' );
504 wp_dequeue_script( 'elementor-dev-tools' );
505 wp_dequeue_script( 'elementor-web-cli' );
506 wp_dequeue_script( 'elementor-import-export-admin' );
507 wp_dequeue_script( 'elementor-app-loader' );
508 wp_dequeue_script( 'elementor-admin-modules' );
509 wp_dequeue_script( 'elementor-ai-media-library' );
510 wp_dequeue_script( 'elementor-admin' );
511
512 wp_dequeue_style( 'elementor-admin-top-bar' );
513 wp_dequeue_style( 'elementor-admin' );
514 wp_dequeue_style( 'e-theme-ui-light' );
515 wp_dequeue_style( 'elementor-common' );
516
517 /**
518 * Scripts.
519 */
520 wp_enqueue_media();
521 wp_enqueue_script( 'updates' );
522 wp_enqueue_script( 'rtsb-admin-app' );
523 } else {
524 $current_screen = get_current_screen();
525
526 if ( 'edit-rtsb_builder' === $current_screen->id ) {
527 if ( ! function_exists( 'woocommerce_get_asset_url' ) ) {
528 include_once WC_ABSPATH . 'includes/wc-template-functions.php';
529 }
530 /**
531 * Styles.
532 */
533 wp_enqueue_style( 'rtsb-templatebuilder' );
534
535 wp_enqueue_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
536
537 wp_enqueue_script( 'selectWoo' );
538 /**
539 * Scripts.
540 */
541 wp_enqueue_script( 'rtsb-templatebuilder' );
542
543 wp_localize_script(
544 'rtsb-templatebuilder',
545 'rtsbParams',
546 [
547 'ajaxurl' => esc_url( self::$ajaxurl ),
548 'homeurl' => home_url(),
549 rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ),
550 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
551 ]
552 );
553 }
554 }
555 }
556
557 /**
558 * Enqueues general public scripts.
559 *
560 * @return void
561 */
562 public function enqueue_general_public_scripts() {
563 $notification_color = Fns::get_option( 'general', 'notification', 'notification_color', '#004BFF' );
564 $notification_bgcolor = Fns::get_option( 'general', 'notification', 'notification_bg', '#F5F8FF' );
565 $notification_button_color = Fns::get_option( 'general', 'notification', 'notification_btn_color', '#0039C0' );
566
567 $dynamic_css = '';
568
569 if ( ! empty( $notification_color ) ) {
570 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{color:{$notification_color}}";
571 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success:before{background-color:{$notification_color}}";
572 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-close-button{color:{$notification_color}}";
573 }
574
575 if ( ! empty( $notification_bgcolor ) ) {
576 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{background-color:{$notification_bgcolor}}";
577 }
578
579 if ( ! empty( $notification_button_color ) ) {
580 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success a{color:{$notification_button_color}}";
581 }
582
583 if ( ! empty( $dynamic_css ) ) {
584 wp_add_inline_style( Fns::optimized_handle( 'rtsb-frontend' ), $dynamic_css );
585 }
586 }
587
588 /**
589 * Get the appropriate Swiper JS file URL.
590 *
591 * @return string
592 */
593 private function get_swiper_url() {
594 $default_swiper_url = rtsb()->get_assets_uri( 'vendor/swiper/js/swiper-bundle.min.js' );
595
596 if ( defined( 'ELEMENTOR_ASSETS_PATH' ) && defined( 'ELEMENTOR_ASSETS_URL' ) ) {
597 $experiment = get_option( 'elementor_experiment-e_swiper_latest' );
598
599 $el_relative_path = ( 'active' === $experiment || 'default' === $experiment )
600 ? 'lib/swiper/v8/swiper.min.js'
601 : 'lib/swiper/swiper.min.js';
602
603 $el_full_path = ELEMENTOR_ASSETS_PATH . $el_relative_path;
604
605 if ( file_exists( $el_full_path ) ) {
606 return ELEMENTOR_ASSETS_URL . $el_relative_path;
607 }
608 }
609
610 return $default_swiper_url;
611 }
612 }
613