| @@ -23,8 +23,11 @@ | ||
| 23 | 23 | // Register styles and scripts for Elementor widgets and features |
| 24 | 24 | self::registerElementorStyles(); |
| 25 | 25 | self::registerElementorScripts(); |
| 26 | 26 | |
| 27 | + // Register Pro assets if available | |
| 28 | + self::registerProAssets(); | |
| 29 | + | |
| 27 | 30 | // Register general files |
| 28 | 31 | self::registerLibrariesFiles(); |
| 29 | 32 | } |
| 30 | 33 | |
| @@ -108,9 +111,9 @@ | ||
| 108 | 111 | |
| 109 | 112 | if(KING_ADDONS_WGT_FORM_BUILDER) { |
| 110 | 113 | wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'KingAddonsFormBuilderData', [ |
| 111 | 114 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 112 | - // Security fix: Remove public nonce exposure - generate dynamically in AJAX handlers | |
| 115 | + 'nonce' => wp_create_nonce('king-addons-js'), | |
| 113 | 116 | 'input_empty' => esc_html__('Please fill out this field', 'king-addons'), |
| 114 | 117 | 'select_empty' => esc_html__('Nothing selected', 'king-addons'), |
| 115 | 118 | 'file_empty' => esc_html__('Please upload a file', 'king-addons'), |
| 116 | 119 | 'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'), |
| @@ -117,22 +120,8 @@ | ||
| 117 | 120 | 'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'), |
| 118 | 121 | ]); |
| 119 | 122 | } |
| 120 | 123 | |
| 121 | - // Localize form builder script - Security fix: Remove public nonce exposure | |
| 122 | - if (KING_ADDONS_WGT_FORM_BUILDER) { | |
| 123 | - wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'king_addons_form_builder_vars', [ | |
| 124 | - 'ajax_url' => admin_url('admin-ajax.php'), | |
| 125 | - // Security fix: Remove public nonce exposure - generate dynamically in AJAX handlers | |
| 126 | - 'required_text' => esc_html__('This field is required', 'king-addons'), | |
| 127 | - 'email_text' => esc_html__('Enter a valid email', 'king-addons'), | |
| 128 | - 'select_empty' => esc_html__('Nothing selected', 'king-addons'), | |
| 129 | - 'file_empty' => esc_html__('Please upload a file', 'king-addons'), | |
| 130 | - 'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'), | |
| 131 | - 'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'), | |
| 132 | - ]); | |
| 133 | - } | |
| 134 | - | |
| 135 | 124 | foreach (ModulesMap::getModulesMapArray()['features'] as $feature_id => $feature_array) { |
| 136 | 125 | foreach ($feature_array['js'] as $js) { |
| 137 | 126 | 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); |
| 138 | 127 | } |
| @@ -143,14 +132,41 @@ | ||
| 143 | 132 | * Register libraries files |
| 144 | 133 | */ |
| 145 | 134 | function registerLibrariesFiles(): void |
| 146 | 135 | { |
| 136 | + $jquery_libraries = [ | |
| 137 | + 'grid', | |
| 138 | + 'isotope', | |
| 139 | + 'slick', | |
| 140 | + 'lightgallery', | |
| 141 | + 'infinitescroll', | |
| 142 | + 'imagesloaded', | |
| 143 | + 'jqueryeventmove', | |
| 144 | + 'jquerynumerator', | |
| 145 | + 'jarallax', | |
| 146 | + 'flipclock', | |
| 147 | + 'fullpage', | |
| 148 | + 'marquee', | |
| 149 | + 'parallax', | |
| 150 | + 'perfectscrollbar', | |
| 151 | + 'wpcolorpicker', | |
| 152 | + 'aos', | |
| 153 | + ]; | |
| 154 | + | |
| 147 | 155 | foreach (LibrariesMap::getLibrariesMapArray()['libraries'] as $library_id => $library_array) { |
| 148 | 156 | foreach ($library_array['css'] as $css) { |
| 149 | 157 | 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); |
| 150 | 158 | } |
| 151 | 159 | foreach ($library_array['js'] as $js) { |
| 152 | - 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); | |
| 160 | + $dependencies = in_array($library_id, $jquery_libraries, true) ? ['jquery'] : []; | |
| 161 | + | |
| 162 | + wp_register_script( | |
| 163 | + KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $library_id . '-' . $js, | |
| 164 | + KING_ADDONS_URL . 'includes/assets/libraries/' . $library_id . '/' . $js . '.js', | |
| 165 | + $dependencies, | |
| 166 | + KING_ADDONS_VERSION, | |
| 167 | + true | |
| 168 | + ); | |
| 153 | 169 | } |
| 154 | 170 | } |
| 155 | 171 | |
| 156 | 172 | wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [ |
| @@ -161,8 +177,79 @@ | ||
| 161 | 177 | 'comparePageURL' => get_permalink(get_option('king_addons_compare_page')), |
| 162 | 178 | 'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')), |
| 163 | 179 | ]); |
| 164 | 180 | |
| 181 | + } | |
| 182 | +/** | |
| 183 | + * Register Pro widget assets when premium is available | |
| 184 | + */ | |
| 185 | + function registerProAssets(): void | |
| 186 | + { | |
| 187 | + // Only register Pro assets if Freemius premium is available | |
| 188 | + if ( | |
| 189 | + !function_exists('king_addons_freemius') || | |
| 190 | + !king_addons_freemius()->can_use_premium_code__premium_only() || | |
| 191 | + !defined('KING_ADDONS_PRO_PATH') || | |
| 192 | + !defined('KING_ADDONS_PRO_URL') | |
| 193 | + ) { | |
| 194 | + return; | |
| 195 | + } | |
| 196 | + | |
| 197 | + // In Elementor editor/preview we want instant reflection of changes, | |
| 198 | + // even when WP_DEBUG/SCRIPT_DEBUG are off (browser cache is aggressive). | |
| 199 | + $is_elementor_edit_or_preview = false; | |
| 200 | + if (defined('ELEMENTOR_VERSION') && class_exists('\\Elementor\\Plugin')) { | |
| 201 | + try { | |
| 202 | + $elementor = \Elementor\Plugin::$instance; | |
| 203 | + $is_elementor_edit_or_preview = ( | |
| 204 | + (isset($elementor->editor) && method_exists($elementor->editor, 'is_edit_mode') && $elementor->editor->is_edit_mode()) | |
| 205 | + || (isset($elementor->preview) && method_exists($elementor->preview, 'is_preview_mode') && $elementor->preview->is_preview_mode()) | |
| 206 | + ); | |
| 207 | + } catch (\Throwable $e) { | |
| 208 | + $is_elementor_edit_or_preview = false; | |
| 209 | + } | |
| 210 | + } | |
| 211 | + | |
| 212 | + // Register Pro assets for widgets that have Pro versions | |
| 213 | + foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) { | |
| 214 | + if (empty($widget_array['has-pro'])) { | |
| 215 | + continue; | |
| 216 | + } | |
| 217 | + | |
| 218 | + $widget_class = $widget_array['php-class']; | |
| 219 | + $pro_widget_dir = KING_ADDONS_PRO_PATH . 'includes/widgets/' . $widget_class . '_Pro/'; | |
| 220 | + | |
| 221 | + // Register Pro CSS if exists | |
| 222 | + $pro_css_path = $pro_widget_dir . 'style.css'; | |
| 223 | + if (file_exists($pro_css_path)) { | |
| 224 | + $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG); | |
| 225 | + $pro_css_ver = ($use_filemtime && function_exists('filemtime')) | |
| 226 | + ? (string) filemtime($pro_css_path) | |
| 227 | + : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION); | |
| 228 | + wp_register_style( | |
| 229 | + KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-style', | |
| 230 | + KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/style.css', | |
| 231 | + null, | |
| 232 | + $pro_css_ver | |
| 233 | + ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + // Register Pro JS if exists | |
| 237 | + $pro_js_path = $pro_widget_dir . 'script.js'; | |
| 238 | + if (file_exists($pro_js_path)) { | |
| 239 | + $use_filemtime = $is_elementor_edit_or_preview || (defined('WP_DEBUG') && WP_DEBUG) || (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG); | |
| 240 | + $pro_js_ver = ($use_filemtime && function_exists('filemtime')) | |
| 241 | + ? (string) filemtime($pro_js_path) | |
| 242 | + : (defined('KING_ADDONS_PRO_VERSION') ? KING_ADDONS_PRO_VERSION : KING_ADDONS_VERSION); | |
| 243 | + wp_register_script( | |
| 244 | + KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-pro-script', | |
| 245 | + KING_ADDONS_PRO_URL . 'includes/widgets/' . $widget_class . '_Pro/script.js', | |
| 246 | + array('jquery'), | |
| 247 | + $pro_js_ver, | |
| 248 | + true | |
| 249 | + ); | |
| 250 | + } | |
| 251 | + } | |
| 165 | 252 | } |
| 166 | 253 | } |
| 167 | 254 | |
| 168 | 255 | RegisterAssets::instance(); |