| 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 |
'nonce' => wp_create_nonce('king-addons-js'), |
| 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 |
foreach (ModulesMap::getModulesMapArray()['features'] as $feature_id => $feature_array) { |
| 125 |
foreach ($feature_array['js'] as $js) { |
| 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); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Register libraries files |
| 133 |
*/ |
| 134 |
function registerLibrariesFiles(): void |
| 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 |
|
| 155 |
foreach (LibrariesMap::getLibrariesMapArray()['libraries'] as $library_id => $library_array) { |
| 156 |
foreach ($library_array['css'] as $css) { |
| 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); |
| 158 |
} |
| 159 |
foreach ($library_array['js'] as $js) { |
| 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 |
); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [ |
| 173 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 174 |
'nonce' => wp_create_nonce('king_addons_grid_nonce'), |
| 175 |
'viewCart' => esc_html__('View Cart', 'king-addons'), |
| 176 |
'addedToCartText' => esc_html__('was added to cart', 'king-addons'), |
| 177 |
'comparePageURL' => get_permalink(get_option('king_addons_compare_page')), |
| 178 |
'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')), |
| 179 |
]); |
| 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 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
RegisterAssets::instance(); |