PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.1.2
ShopBuilder – WooCommerce Builder For Elementor v3.1.2
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.2, at app/Controllers/AssetsController.php

598 lines 17.5 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' => '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' => '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' => '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', 'flexslider', 'photoswipe', 'photoswipe-ui-default', '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( '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 }
427
428 /**
429 * Enqueues admin scripts.
430 *
431 * @return void
432 */
433 public function enqueue_backend_scripts() {
434 ob_start(); ?>
435 #adminmenu .toplevel_page_rtsb .wp-menu-image img {
436 width: auto;
437 height: 22px;
438 padding: 4px 0;
439 box-sizing: content-box;
440 }
441 .post-type-rtsb_builder li#wp-admin-bar-WPML_ALS_all,
442 .post-type-rtsb_builder li.language_all{
443 display: none;
444 }
445
446 <?php
447 $admin_global_style = ob_get_clean();
448 // Speed Optimization.
449 wp_add_inline_style( 'admin-menu', $admin_global_style );
450
451 global $pagenow;
452
453 $whitelisted_pages = [ 'rtsb-settings', 'rtsb-get-help', 'rtsb-themes' ];
454 $current_page = ! empty( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
455
456 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && in_array( $current_page, $whitelisted_pages, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
457 /**
458 * Styles.
459 */
460 wp_enqueue_style( 'rtsb-admin-app' );
461 wp_enqueue_style( 'rtsb-fonts' );
462 }
463
464 if ( 'admin.php' === $pagenow && ! empty( $_GET['page'] ) && 'rtsb-settings' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
465 // Elementor Console Error Fixed For "rtsb-settings" Page.
466 wp_dequeue_script( 'elementor-admin-top-bar' );
467 wp_dequeue_script( 'elementor-common' );
468 wp_dequeue_script( 'elementor-dev-tools' );
469 wp_dequeue_script( 'elementor-web-cli' );
470 wp_dequeue_script( 'elementor-import-export-admin' );
471 wp_dequeue_script( 'elementor-app-loader' );
472 wp_dequeue_script( 'elementor-admin-modules' );
473 wp_dequeue_script( 'elementor-ai-media-library' );
474 wp_dequeue_script( 'elementor-admin' );
475
476 wp_dequeue_style( 'elementor-admin-top-bar' );
477 wp_dequeue_style( 'elementor-admin' );
478 wp_dequeue_style( 'e-theme-ui-light' );
479 wp_dequeue_style( 'elementor-common' );
480
481 /**
482 * Scripts.
483 */
484 wp_enqueue_media();
485 wp_enqueue_script( 'updates' );
486 wp_enqueue_script( 'rtsb-admin-app' );
487 wp_localize_script(
488 'rtsb-admin-app',
489 'rtsbParams',
490 [
491 'ajaxurl' => esc_url( self::$ajaxurl ),
492 'homeurl' => home_url(),
493 'adminLogo' => rtsb()->get_assets_uri( 'images/icon/ShopBuilder-Logo.svg' ),
494 'restApiUrl' => esc_url_raw( rest_url() ),
495 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
496 'nonce' => wp_create_nonce( rtsb()->nonceText ),
497 'pages' => Fns::get_pages(),
498 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
499 'proVersion' => defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : '0',
500 'updateRates' => esc_html__( 'Update All Rates', 'shopbuilder' ),
501 'sections' => Settings::instance()->get_sections(),
502 'userRoles' => Fns::get_all_user_roles(),
503 'hasElementor' => defined( 'ELEMENTOR_VERSION' ),
504 'loadElementor' => Fns::should_load_elementor_scripts(),
505 'isOptimizationEnabled' => Fns::is_optimization_enabled(),
506 ]
507 );
508 } else {
509 $current_screen = get_current_screen();
510
511 if ( 'edit-rtsb_builder' === $current_screen->id ) {
512 if ( ! function_exists( 'woocommerce_get_asset_url' ) ) {
513 include_once WC_ABSPATH . 'includes/wc-template-functions.php';
514 }
515 /**
516 * Styles.
517 */
518 wp_enqueue_style( 'rtsb-templatebuilder' );
519
520 wp_enqueue_style( 'select2', plugins_url( 'assets/css/select2.css', WC_PLUGIN_FILE ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
521
522 wp_enqueue_script( 'selectWoo' );
523 /**
524 * Scripts.
525 */
526 wp_enqueue_script( 'rtsb-templatebuilder' );
527
528 wp_localize_script(
529 'rtsb-templatebuilder',
530 'rtsbParams',
531 [
532 'ajaxurl' => esc_url( self::$ajaxurl ),
533 'homeurl' => home_url(),
534 rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ),
535 'hasPro' => rtsb()->has_pro() ? 'yes' : 'no',
536 ]
537 );
538 }
539 }
540 }
541
542 /**
543 * Enqueues general public scripts.
544 *
545 * @return void
546 */
547 public function enqueue_general_public_scripts() {
548 $notification_color = Fns::get_option( 'general', 'notification', 'notification_color', '#004BFF' );
549 $notification_bgcolor = Fns::get_option( 'general', 'notification', 'notification_bg', '#F5F8FF' );
550 $notification_button_color = Fns::get_option( 'general', 'notification', 'notification_btn_color', '#0039C0' );
551
552 $dynamic_css = '';
553
554 if ( ! empty( $notification_color ) ) {
555 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{color:{$notification_color}}";
556 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success:before{background-color:{$notification_color}}";
557 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-close-button{color:{$notification_color}}";
558 }
559
560 if ( ! empty( $notification_bgcolor ) ) {
561 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success{background-color:{$notification_bgcolor}}";
562 }
563
564 if ( ! empty( $notification_button_color ) ) {
565 $dynamic_css .= ".rtsb-shopbuilder-plugin #toast-container .toast-success a{color:{$notification_button_color}}";
566 }
567
568 if ( ! empty( $dynamic_css ) ) {
569 wp_add_inline_style( Fns::optimized_handle( 'rtsb-frontend' ), $dynamic_css );
570 }
571 }
572
573 /**
574 * Get the appropriate Swiper JS file URL.
575 *
576 * @return string
577 */
578 private function get_swiper_url() {
579 $default_swiper_url = rtsb()->get_assets_uri( 'vendor/swiper/js/swiper-bundle.min.js' );
580
581 if ( defined( 'ELEMENTOR_ASSETS_PATH' ) && defined( 'ELEMENTOR_ASSETS_URL' ) ) {
582 $experiment = get_option( 'elementor_experiment-e_swiper_latest' );
583
584 $el_relative_path = ( 'active' === $experiment || 'default' === $experiment )
585 ? 'lib/swiper/v8/swiper.min.js'
586 : 'lib/swiper/swiper.min.js';
587
588 $el_full_path = ELEMENTOR_ASSETS_PATH . $el_relative_path;
589
590 if ( file_exists( $el_full_path ) ) {
591 return ELEMENTOR_ASSETS_URL . $el_relative_path;
592 }
593 }
594
595 return $default_swiper_url;
596 }
597 }
598