PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.65
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.65
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 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / RegisterAssets.php

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

242 lines 12.0 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 wp_register_script($script_handle, KING_ADDONS_URL . 'includes/widgets/' . $widget_array['php-class'] . '/' . $js . '.js', array('jquery'), KING_ADDONS_VERSION, true);
55
56 // Localize script specifically for pricing-slider
57 if ($widget_id === 'pricing-slider' && $js === 'script') {
58 $localized_data = [
59 'ajax_url' => admin_url('admin-ajax.php'),
60 'view_cart_text' => esc_html__('View Cart', 'king-addons'),
61 // Note: Nonce should ideally be generated closer to where the button is rendered,
62 // but passing ajax_url here is the main fix for the AJAX call.
63 // The nonce is already passed via data attribute on the button in Pricing_Slider_Pro.php.
64 // 'nonce' => wp_create_nonce('king_addons_add_to_cart_nonce')
65 ];
66 wp_localize_script($script_handle, 'king_addons_slider_vars', $localized_data);
67 }
68
69 // Localize script for pricing-calculator
70 if ($widget_id === 'pricing-calculator' && $js === 'script') {
71 $localized_data = [
72 'ajaxurl' => admin_url('admin-ajax.php'),
73 'add_to_cart_nonce' => wp_create_nonce('king_addons_add_to_cart_nonce'),
74 'send_email_quote_nonce' => wp_create_nonce('king_addons_send_email_quote_nonce'),
75 'view_cart_text' => esc_html__('View Cart', 'king-addons')
76 ];
77 wp_localize_script($script_handle, 'king_addons_calculator_vars', $localized_data);
78 }
79
80 // Localize script for login-register-form
81 if ($widget_id === 'login-register-form' && $js === 'script') {
82 wp_localize_script($script_handle, 'king_addons_login_register_vars', [
83 'ajax_url' => admin_url('admin-ajax.php'),
84 'login_nonce' => wp_create_nonce('king_addons_login_action'),
85 'register_nonce' => wp_create_nonce('king_addons_register_action'),
86 'lostpassword_nonce' => wp_create_nonce('king_addons_lostpassword_action'),
87 'social_login_nonce' => wp_create_nonce('king_addons_social_login_action'),
88 'strings' => [
89 'loading' => esc_html__('Please wait...', 'king-addons'),
90 'login_success' => esc_html__('Login successful! Redirecting...', 'king-addons'),
91 'register_success' => esc_html__('Registration successful! Welcome!', 'king-addons'),
92 'required_fields' => esc_html__('Please fill in all required fields.', 'king-addons'),
93 'invalid_email' => esc_html__('Please enter a valid email address.', 'king-addons'),
94 'password_mismatch' => esc_html__('Passwords do not match.', 'king-addons'),
95 'network_error' => esc_html__('Network error. Please try again.', 'king-addons'),
96 ]
97 ]);
98 }
99 }
100 }
101
102 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-search-script', 'KingAddonsSearchData', [
103 'ajaxUrl' => admin_url('admin-ajax.php'),
104 'nonce' => wp_create_nonce('king_addons_search_nonce'),
105 ]);
106
107 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-mailchimp-script', 'KingAddonsMailChimpData', [
108 'ajaxUrl' => admin_url('admin-ajax.php'),
109 'nonce' => wp_create_nonce('king_addons_mailchimp_nonce'),
110 ]);
111
112 if(KING_ADDONS_WGT_FORM_BUILDER) {
113 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'KingAddonsFormBuilderData', [
114 'ajaxurl' => admin_url('admin-ajax.php'),
115 // Security fix: Remove public nonce exposure - generate dynamically in AJAX handlers
116 'input_empty' => esc_html__('Please fill out this field', 'king-addons'),
117 'select_empty' => esc_html__('Nothing selected', 'king-addons'),
118 'file_empty' => esc_html__('Please upload a file', 'king-addons'),
119 'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'),
120 'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'),
121 ]);
122 }
123
124 // Localize form builder script - Security fix: Remove public nonce exposure
125 if (KING_ADDONS_WGT_FORM_BUILDER) {
126 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'king_addons_form_builder_vars', [
127 'ajax_url' => admin_url('admin-ajax.php'),
128 // Security fix: Remove public nonce exposure - generate dynamically in AJAX handlers
129 'required_text' => esc_html__('This field is required', 'king-addons'),
130 'email_text' => esc_html__('Enter a valid email', 'king-addons'),
131 'select_empty' => esc_html__('Nothing selected', 'king-addons'),
132 'file_empty' => esc_html__('Please upload a file', 'king-addons'),
133 'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'),
134 'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'),
135 ]);
136 }
137
138 foreach (ModulesMap::getModulesMapArray()['features'] as $feature_id => $feature_array) {
139 foreach ($feature_array['js'] as $js) {
140 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);
141 }
142 }
143 }
144
145 /**
146 * Register libraries files
147 */
148 function registerLibrariesFiles(): void
149 {
150 foreach (LibrariesMap::getLibrariesMapArray()['libraries'] as $library_id => $library_array) {
151 foreach ($library_array['css'] as $css) {
152 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);
153 }
154 foreach ($library_array['js'] as $js) {
155 wp_register_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $library_id . '-' . $js, KING_ADDONS_URL . 'includes/assets/libraries/' . $library_id . '/' . $js . '.js', null, KING_ADDONS_VERSION);
156 }
157 }
158
159 wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [
160 'ajaxUrl' => admin_url('admin-ajax.php'),
161 'nonce' => wp_create_nonce('king_addons_grid_nonce'),
162 'viewCart' => esc_html__('View Cart', 'king-addons'),
163 'addedToCartText' => esc_html__('was added to cart', 'king-addons'),
164 'comparePageURL' => get_permalink(get_option('king_addons_compare_page')),
165 'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')),
166 ]);
167
168 }
169 /**
170 * Register Pro widget assets when premium is available
171 */
172 function registerProAssets(): void
173 {
174 // Only register Pro assets if Freemius premium is available
175 if (
176 !function_exists('king_addons_freemius') ||
177 !king_addons_freemius()->can_use_premium_code__premium_only() ||
178 !defined('KING_ADDONS_PRO_PATH') ||
179 !defined('KING_ADDONS_PRO_URL')
180 ) {
181 return;
182 }
183
184 // In Elementor editor/preview we want instant reflection of changes,
185 // even when WP_DEBUG/SCRIPT_DEBUG are off (browser cache is aggressive).
186 $is_elementor_edit_or_preview = false;
187 if (defined('ELEMENTOR_VERSION') && class_exists('\\Elementor\\Plugin')) {
188 try {
189 $elementor = \Elementor\Plugin::$instance;
190 $is_elementor_edit_or_preview = (
191 (isset($elementor->editor) && method_exists($elementor->editor, 'is_edit_mode') && $elementor->editor->is_edit_mode())
192 || (isset($elementor->preview) && method_exists($elementor->preview, 'is_preview_mode') && $elementor->preview->is_preview_mode())
193 );
194 } catch (\Throwable $e) {
195 $is_elementor_edit_or_preview = false;
196 }
197 }
198
199 // Register Pro assets for widgets that have Pro versions
200 foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) {
201 if (empty($widget_array['has-pro'])) {
202 continue;
203 }
204
205 $widget_class = $widget_array['php-class'];
206 $pro_widget_dir = KING_ADDONS_PRO_PATH . 'includes/widgets/' . $widget_class . '_Pro/';
207
208 // Register Pro CSS if exists
209 $pro_css_path = $pro_widget_dir . 'style.css';
210 if (file_exists($pro_css_path)) {
211 $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
212 $pro_css_ver = ($use_filemtime && function_exists('filemtime'))
213 ? (string) filemtime($pro_css_path)
214 : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION);
215 wp_register_style(
216 KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-style',
217 KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/style.css',
218 null,
219 $pro_css_ver
220 );
221 }
222
223 // Register Pro JS if exists
224 $pro_js_path = $pro_widget_dir . 'script.js';
225 if (file_exists($pro_js_path)) {
226 $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG);
227 $pro_js_ver = ($use_filemtime && function_exists('filemtime'))
228 ? (string) filemtime($pro_js_path)
229 : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION);
230 wp_register_script(
231 KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-script',
232 KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/script.js',
233 array('jquery'),
234 $pro_js_ver,
235 true
236 );
237 }
238 }
239 }
240 }
241
242 RegisterAssets::instance();