| 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 general files |
| 28 |
self::registerLibrariesFiles(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register CSS files |
| 33 |
*/ |
| 34 |
function registerElementorStyles(): void |
| 35 |
{ |
| 36 |
foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) { |
| 37 |
foreach ($widget_array['css'] as $css) { |
| 38 |
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); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register JS files |
| 45 |
*/ |
| 46 |
function registerElementorScripts(): void |
| 47 |
{ |
| 48 |
foreach (ModulesMap::getModulesMapArray()['widgets'] as $widget_id => $widget_array) { |
| 49 |
foreach ($widget_array['js'] as $js) { |
| 50 |
$script_handle = KING_ADDONS_ASSETS_UNIQUE_KEY . '-' . $widget_id . '-' . $js; |
| 51 |
wp_register_script($script_handle, KING_ADDONS_URL . 'includes/widgets/' . $widget_array['php-class'] . '/' . $js . '.js', array('jquery'), KING_ADDONS_VERSION, true); |
| 52 |
|
| 53 |
// Localize script specifically for pricing-slider |
| 54 |
if ($widget_id === 'pricing-slider' && $js === 'script') { |
| 55 |
$localized_data = [ |
| 56 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 57 |
'view_cart_text' => esc_html__('View Cart', 'king-addons'), |
| 58 |
// Note: Nonce should ideally be generated closer to where the button is rendered, |
| 59 |
// but passing ajax_url here is the main fix for the AJAX call. |
| 60 |
// The nonce is already passed via data attribute on the button in Pricing_Slider_Pro.php. |
| 61 |
// 'nonce' => wp_create_nonce('king_addons_add_to_cart_nonce') |
| 62 |
]; |
| 63 |
wp_localize_script($script_handle, 'king_addons_slider_vars', $localized_data); |
| 64 |
} |
| 65 |
|
| 66 |
// Localize script for pricing-calculator |
| 67 |
if ($widget_id === 'pricing-calculator' && $js === 'script') { |
| 68 |
$localized_data = [ |
| 69 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 70 |
'add_to_cart_nonce' => wp_create_nonce('king_addons_add_to_cart_nonce'), |
| 71 |
'send_email_quote_nonce' => wp_create_nonce('king_addons_send_email_quote_nonce'), |
| 72 |
'view_cart_text' => esc_html__('View Cart', 'king-addons') |
| 73 |
]; |
| 74 |
wp_localize_script($script_handle, 'king_addons_calculator_vars', $localized_data); |
| 75 |
} |
| 76 |
|
| 77 |
// Localize script for login-register-form |
| 78 |
if ($widget_id === 'login-register-form' && $js === 'script') { |
| 79 |
wp_localize_script($script_handle, 'king_addons_login_register_vars', [ |
| 80 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 81 |
'login_nonce' => wp_create_nonce('king_addons_login_action'), |
| 82 |
'register_nonce' => wp_create_nonce('king_addons_register_action'), |
| 83 |
'lostpassword_nonce' => wp_create_nonce('king_addons_lostpassword_action'), |
| 84 |
'social_login_nonce' => wp_create_nonce('king_addons_social_login_action'), |
| 85 |
'strings' => [ |
| 86 |
'loading' => esc_html__('Please wait...', 'king-addons'), |
| 87 |
'login_success' => esc_html__('Login successful! Redirecting...', 'king-addons'), |
| 88 |
'register_success' => esc_html__('Registration successful! Welcome!', 'king-addons'), |
| 89 |
'required_fields' => esc_html__('Please fill in all required fields.', 'king-addons'), |
| 90 |
'invalid_email' => esc_html__('Please enter a valid email address.', 'king-addons'), |
| 91 |
'password_mismatch' => esc_html__('Passwords do not match.', 'king-addons'), |
| 92 |
'network_error' => esc_html__('Network error. Please try again.', 'king-addons'), |
| 93 |
] |
| 94 |
]); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-search-script', 'KingAddonsSearchData', [ |
| 100 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 101 |
'nonce' => wp_create_nonce('king_addons_search_nonce'), |
| 102 |
]); |
| 103 |
|
| 104 |
wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-mailchimp-script', 'KingAddonsMailChimpData', [ |
| 105 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 106 |
'nonce' => wp_create_nonce('king_addons_mailchimp_nonce'), |
| 107 |
]); |
| 108 |
|
| 109 |
if(KING_ADDONS_WGT_FORM_BUILDER) { |
| 110 |
wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-form-builder-script', 'KingAddonsFormBuilderData', [ |
| 111 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 112 |
// Security fix: Remove public nonce exposure - generate dynamically in AJAX handlers |
| 113 |
'input_empty' => esc_html__('Please fill out this field', 'king-addons'), |
| 114 |
'select_empty' => esc_html__('Nothing selected', 'king-addons'), |
| 115 |
'file_empty' => esc_html__('Please upload a file', 'king-addons'), |
| 116 |
'recaptcha_v3_site_key' => get_option('king_addons_recaptcha_v3_site_key'), |
| 117 |
'recaptcha_error' => esc_html__('Recaptcha Error', 'king-addons'), |
| 118 |
]); |
| 119 |
} |
| 120 |
|
| 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 |
foreach (ModulesMap::getModulesMapArray()['features'] as $feature_id => $feature_array) { |
| 136 |
foreach ($feature_array['js'] as $js) { |
| 137 |
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 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register libraries files |
| 144 |
*/ |
| 145 |
function registerLibrariesFiles(): void |
| 146 |
{ |
| 147 |
foreach (LibrariesMap::getLibrariesMapArray()['libraries'] as $library_id => $library_array) { |
| 148 |
foreach ($library_array['css'] as $css) { |
| 149 |
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 |
} |
| 151 |
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); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [ |
| 157 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 158 |
'nonce' => wp_create_nonce('king_addons_grid_nonce'), |
| 159 |
'viewCart' => esc_html__('View Cart', 'king-addons'), |
| 160 |
'addedToCartText' => esc_html__('was added to cart', 'king-addons'), |
| 161 |
'comparePageURL' => get_permalink(get_option('king_addons_compare_page')), |
| 162 |
'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')), |
| 163 |
]); |
| 164 |
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
RegisterAssets::instance(); |