PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.87
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.87
51.1.87 51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 All 41 releases
king-addons / includes / RegisterAssets.php

RegisterAssets.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.87, at includes/RegisterAssets.php

259 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 final class RegisterAssets
10 {
11 private static ?RegisterAssets $_instance = null;
12
13 public static function instance(): RegisterAssets
14 {
15 if (is_null(self::$_instance)) {
16 self::$_instance = new self();
17 }
18 return self::$_instance;
19 }
20
21 public function __construct()
22 {
23 // Register styles and scripts for Elementor widgets and features
24 self::registerElementorStyles();
25 self::registerElementorScripts();
26
27 // Register Pro assets if available
28 self::registerProAssets();
29
30 // Register general files
31 self::registerLibrariesFiles();
32 }
33
34 /**
35 * Register CSS files
36 */
37 function registerElementorStyles(): void
38 {
39 foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) {
40 foreach ($widget_array['css'] as $css) {
41 wp_register_style(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-' . $css, KING_ADDONS_URL . 'includes/widgets/' . $widget_array['php-class'] . '/' . $css . '.css', null, KING_ADDONS_VERSION);
42 }
43 }
44 }
45
46 /**
47 * Register JS files
48 */
49 function registerElementorScripts(): void
50 {
51 foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) {
52 foreach ($widget_array['js'] as $js) {
53 $script_handle = KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-' . $js;
54 $script_deps = array('jquery');
55 if ('woo-product-variations' === $widget_id) {
56 $script_deps[] = 'wc-add-to-cart-variation';
57 }
58 wp_register_script($script_handle, KING_ADDONS_URL . 'includes/widgets/' . $widget_array['php-class'] . '/' . $js . '.js', $script_deps, KING_ADDONS_VERSION, true);
59
60 // Localize script specifically for pricing-slider
61 if ($widget_id === 'pricing-slider' && $js === 'script') {
62 $localized_data = [
63 'ajax_url' => admin_url('admin-ajax.php'),
64 'view_cart_text' => esc_html__('View Cart', 'king-addons'),
65 // Note: Nonce should ideally be generated closer to where the button is rendered,
66 // but passing ajax_url here is the main fix for the AJAX call.
67 // The nonce is already passed via data attribute on the button in Pricing_Slider_Pro.php.
68 // 'nonce' => wp_create_nonce('king_addons_add_to_cart_nonce')
69 ];
70 wp_localize_script($script_handle, 'king_addons_slider_vars', $localized_data);
71 }
72
73 // Localize script for pricing-calculator
74 if ($widget_id === 'pricing-calculator' && $js === 'script') {
75 $localized_data = [
76 'ajaxurl' => admin_url('admin-ajax.php'),
77 'add_to_cart_nonce' => wp_create_nonce('king_addons_add_to_cart_nonce'),
78 'send_email_quote_nonce' => wp_create_nonce('king_addons_send_email_quote_nonce'),
79 'view_cart_text' => esc_html__('View Cart', 'king-addons')
80 ];
81 wp_localize_script($script_handle, 'king_addons_calculator_vars', $localized_data);
82 }
83
84 // Localize script for login-register-form
85 if ($widget_id === 'login-register-form' && $js === 'script') {
86 wp_localize_script($script_handle, 'king_addons_login_register_vars', [
87 'ajax_url' => admin_url('admin-ajax.php'),
88 'login_nonce' => wp_create_nonce('king_addons_login_action'),
89 'register_nonce' => wp_create_nonce('king_addons_register_action'),
90 'lostpassword_nonce' => wp_create_nonce('king_addons_lostpassword_action'),
91 'social_login_nonce' => wp_create_nonce('king_addons_social_login_action'),
92 'strings' => [
93 'loading' => esc_html__('Please wait...', 'king-addons'),
94 'login_success' => esc_html__('Login successful! Redirecting...', 'king-addons'),
95 'register_success' => esc_html__('Registration successful! Welcome!', 'king-addons'),
96 'required_fields' => esc_html__('Please fill in all required fields.', 'king-addons'),
97 'invalid_email' => esc_html__('Please enter a valid email address.', 'king-addons'),
98 'password_mismatch' => esc_html__('Passwords do not match.', 'king-addons'),
99 'network_error' => esc_html__('Network error. Please try again.', 'king-addons'),
100 ]
101 ]);
102 }
103 }
104 }
105
106 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-search-script', 'KingAddonsSearchData', [
107 'ajaxUrl' => admin_url('admin-ajax.php'),
108 'nonce' => wp_create_nonce('king_addons_search_nonce'),
109 ]);
110
111 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-mailchimp-script', 'KingAddonsMailChimpData', [
112 'ajaxUrl' => admin_url('admin-ajax.php'),
113 'nonce' => wp_create_nonce('king_addons_mailchimp_nonce'),
114 ]);
115
116 if(KING_ADDONS_WGT_FORM_BUILDER) {
117 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'KingAddonsFormBuilderData', [
118 'ajaxurl' => admin_url('admin-ajax.php'),
119 'nonce' => wp_create_nonce('king-addons-js'),
120 'input_empty' => esc_html__('Please fill out this field', 'king-addons'),
121 'select_empty' => esc_html__('Nothing selected', 'king-addons'),
122 'file_empty' => esc_html__('Please upload a file', 'king-addons'),
123 'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'),
124 'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'),
125 ]);
126 }
127
128 foreach (ModulesMap::getModulesMapArray()['features'] as $feature_id => $feature_array) {
129 foreach ($feature_array['js'] as $js) {
130 wp_register_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $feature_id . '-' . $js, KING_ADDONS_URL . 'includes/features/' . $feature_array['php-class'] . '/' . $js . '.js', null, KING_ADDONS_VERSION);
131 }
132 }
133 }
134
135 /**
136 * Register libraries files
137 */
138 function registerLibrariesFiles(): void
139 {
140 $jquery_libraries = [
141 'grid',
142 'isotope',
143 'slick',
144 'lightgallery',
145 'infinitescroll',
146 'imagesloaded',
147 'jqueryeventmove',
148 'jquerynumerator',
149 'jarallax',
150 'flipclock',
151 'fullpage',
152 'marquee',
153 'parallax',
154 'perfectscrollbar',
155 'wpcolorpicker',
156 'aos',
157 ];
158
159 foreach (LibrariesMap::getLibrariesMapArray()['libraries'] as $library_id => $library_array) {
160 foreach ($library_array['css'] as $css) {
161 wp_register_style(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $library_id . '-' . $css, KING_ADDONS_URL . 'includes/assets/libraries/' . $library_id . '/' . $css . '.css', null, KING_ADDONS_VERSION);
162 }
163 foreach ($library_array['js'] as $js) {
164 $dependencies = in_array($library_id, $jquery_libraries, true) ? ['jquery'] : [];
165
166 wp_register_script(
167 KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $library_id . '-' . $js,
168 KING_ADDONS_URL . 'includes/assets/libraries/' . $library_id . '/' . $js . '.js',
169 $dependencies,
170 KING_ADDONS_VERSION,
171 true
172 );
173 }
174 }
175
176 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [
177 'ajaxUrl' => admin_url('admin-ajax.php'),
178 'nonce' => wp_create_nonce('king_addons_grid_nonce'),
179 'viewCart' => esc_html__('View Cart', 'king-addons'),
180 'addedToCartText' => esc_html__('was added to cart', 'king-addons'),
181 'comparePageURL' => get_permalink(get_option('king_addons_compare_page')),
182 'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')),
183 ]);
184
185 }
186 /**
187 * Register Pro widget assets when premium is available
188 */
189 function registerProAssets(): void
190 {
191 // Only register Pro assets if Freemius premium is available
192 if (
193 !function_exists('king_addons_freemius') ||
194 !king_addons_freemius()->can_use_premium_code__premium_only() ||
195 !defined('KING_ADDONS_PRO_PATH') ||
196 !defined('KING_ADDONS_PRO_URL')
197 ) {
198 return;
199 }
200
201 // In Elementor editor/preview we want instant reflection of changes,
202 // even when WP_DEBUG/SCRIPT_DEBUG are off (browser cache is aggressive).
203 $is_elementor_edit_or_preview = false;
204 if (defined('ELEMENTOR_VERSION') && class_exists('\\Elementor\\Plugin')) {
205 try {
206 $elementor = \Elementor\Plugin::$instance;
207 $is_elementor_edit_or_preview = (
208 (isset($elementor->editor) && method_exists($elementor->editor, 'is_edit_mode') && $elementor->editor->is_edit_mode())
209 || (isset($elementor->preview) && method_exists($elementor->preview, 'is_preview_mode') && $elementor->preview->is_preview_mode())
210 );
211 } catch (\Throwable $e) {
212 $is_elementor_edit_or_preview = false;
213 }
214 }
215
216 // Register Pro assets for widgets that have Pro versions
217 foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) {
218 if (empty($widget_array['has-pro'])) {
219 continue;
220 }
221
222 $widget_class = $widget_array['php-class'];
223 $pro_widget_dir = KING_ADDONS_PRO_PATH . 'includes/widgets/' . $widget_class . '_Pro/';
224
225 // Register Pro CSS if exists
226 $pro_css_path = $pro_widget_dir . 'style.css';
227 if (file_exists($pro_css_path)) {
228 $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
229 $pro_css_ver = ($use_filemtime && function_exists('filemtime'))
230 ? (string) filemtime($pro_css_path)
231 : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION);
232 wp_register_style(
233 KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-style',
234 KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/style.css',
235 null,
236 $pro_css_ver
237 );
238 }
239
240 // Register Pro JS if exists
241 $pro_js_path = $pro_widget_dir . 'script.js';
242 if (file_exists($pro_js_path)) {
243 $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
244 $pro_js_ver = ($use_filemtime && function_exists('filemtime'))
245 ? (string) filemtime($pro_js_path)
246 : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION);
247 wp_register_script(
248 KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-script',
249 KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/script.js',
250 array('jquery'),
251 $pro_js_ver,
252 true
253 );
254 }
255 }
256 }
257 }
258
259 RegisterAssets::instance();