| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Admin Asset Enqueuing |
| 8 |
* |
| 9 |
* Registers and enqueues all admin-side CSS and JavaScript files. |
| 10 |
* |
| 11 |
* @package Metasync |
| 12 |
* @subpackage Metasync/includes |
| 13 |
*/ |
| 14 |
class Metasync_Admin_Assets |
| 15 |
{ |
| 16 |
private static $instance = null; |
| 17 |
|
| 18 |
public static function instance() |
| 19 |
{ |
| 20 |
if (null === self::$instance) { |
| 21 |
self::$instance = new self(); |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
private function __construct() {} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the stylesheets for the admin area. |
| 30 |
* |
| 31 |
* @param string $plugin_name The plugin identifier. |
| 32 |
* @param string $version The plugin version. |
| 33 |
* @param string $admin_dir_url URL to the admin/ directory (trailing slash). |
| 34 |
*/ |
| 35 |
public function enqueue_styles($plugin_name, $version, $admin_dir_url) |
| 36 |
{ |
| 37 |
wp_enqueue_style( |
| 38 |
$plugin_name, |
| 39 |
$admin_dir_url . 'css/metasync-admin.css', |
| 40 |
array(), |
| 41 |
$version, |
| 42 |
'all' |
| 43 |
); |
| 44 |
|
| 45 |
wp_enqueue_style( |
| 46 |
$plugin_name . '-dashboard', |
| 47 |
$admin_dir_url . 'css/metasync-dashboard.css', |
| 48 |
array($plugin_name), |
| 49 |
$version, |
| 50 |
'all' |
| 51 |
); |
| 52 |
|
| 53 |
if (isset($_GET['page']) && strpos($_GET['page'], '-setup-wizard') !== false) { |
| 54 |
wp_enqueue_style( |
| 55 |
$plugin_name . '-setup-wizard', |
| 56 |
$admin_dir_url . 'css/metasync-setup-wizard.css', |
| 57 |
array($plugin_name . '-dashboard'), |
| 58 |
$version, |
| 59 |
'all' |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Register the JavaScript for the admin area. |
| 66 |
* |
| 67 |
* @param string $plugin_name The plugin identifier. |
| 68 |
* @param string $version The plugin version. |
| 69 |
* @param string $admin_dir_url URL to the admin/ directory (trailing slash). |
| 70 |
*/ |
| 71 |
public function enqueue_scripts($plugin_name, $version, $admin_dir_url) |
| 72 |
{ |
| 73 |
wp_enqueue_media(); |
| 74 |
|
| 75 |
wp_enqueue_script( |
| 76 |
$plugin_name, |
| 77 |
$admin_dir_url . 'js/metasync-admin.js', |
| 78 |
array('jquery'), |
| 79 |
$version, |
| 80 |
false |
| 81 |
); |
| 82 |
|
| 83 |
wp_enqueue_script( |
| 84 |
$plugin_name . '-dashboard', |
| 85 |
$admin_dir_url . 'js/metasync-dashboard.js', |
| 86 |
array('jquery', $plugin_name), |
| 87 |
$version, |
| 88 |
true |
| 89 |
); |
| 90 |
|
| 91 |
wp_enqueue_script( |
| 92 |
$plugin_name . '-theme-switcher', |
| 93 |
$admin_dir_url . 'js/metasync-theme-switcher.js', |
| 94 |
array('jquery'), |
| 95 |
$version, |
| 96 |
true |
| 97 |
); |
| 98 |
|
| 99 |
wp_localize_script( |
| 100 |
$plugin_name . '-theme-switcher', |
| 101 |
'metasyncThemeData', |
| 102 |
array( |
| 103 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 104 |
'nonce' => wp_create_nonce('metasync_theme_nonce'), |
| 105 |
'currentTheme' => get_option('metasync_theme', 'dark') |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
wp_enqueue_script( |
| 110 |
$plugin_name . '-color-palette', |
| 111 |
$admin_dir_url . 'js/metasync-color-palette.js', |
| 112 |
array('jquery'), |
| 113 |
$version, |
| 114 |
true |
| 115 |
); |
| 116 |
|
| 117 |
$options = Metasync::get_option('general'); |
| 118 |
$general_settings = Metasync::get_option('general'); |
| 119 |
$searchatlas_api_key = isset($general_settings['searchatlas_api_key']) ? $general_settings['searchatlas_api_key'] : ''; |
| 120 |
$otto_pixel_uuid = isset($general_settings['otto_pixel_uuid']) ? $general_settings['otto_pixel_uuid'] : ''; |
| 121 |
|
| 122 |
// SECURITY FIX (CVE-2025-14386): Only generate Search Atlas connect nonce for administrators |
| 123 |
$sa_connect_nonce = ''; |
| 124 |
if (current_user_can('manage_options')) { |
| 125 |
$sa_connect_nonce = wp_create_nonce('metasync_sa_connect_nonce'); |
| 126 |
} |
| 127 |
|
| 128 |
$heartbeat_state = Metasync_Heartbeat_Manager::instance()->get_heartbeat_state(); |
| 129 |
wp_localize_script( $plugin_name, 'metaSync', array( |
| 130 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 131 |
'admin_url'=>admin_url('admin.php'), |
| 132 |
'sa_connect_nonce' => $sa_connect_nonce, |
| 133 |
'reset_auth_nonce' => wp_create_nonce('metasync_reset_auth_nonce'), |
| 134 |
'burst_ping_nonce' => wp_create_nonce('metasync_burst_ping'), |
| 135 |
'heartbeat_state' => $heartbeat_state, |
| 136 |
'dashboard_domain' => Metasync_Admin::get_effective_dashboard_domain(), |
| 137 |
'support_email' => Metasync::SUPPORT_EMAIL, |
| 138 |
'documentation_domain' => Metasync::DOCUMENTATION_DOMAIN, |
| 139 |
'debug_enabled' => WP_DEBUG || (defined('METASYNC_DEBUG') && constant('METASYNC_DEBUG')), |
| 140 |
'searchatlas_api_key' => !empty($searchatlas_api_key), |
| 141 |
'otto_pixel_uuid' => $otto_pixel_uuid, |
| 142 |
'is_connected' => (bool)Metasync_Heartbeat_Manager::instance()->is_heartbeat_connected() |
| 143 |
)); |
| 144 |
|
| 145 |
wp_enqueue_script('wp-util'); |
| 146 |
|
| 147 |
$inline_script = " |
| 148 |
if (typeof ajaxurl === 'undefined') { |
| 149 |
var ajaxurl = '" . esc_js(admin_url('admin-ajax.php')) . "'; |
| 150 |
} |
| 151 |
|
| 152 |
// Add Plugin Auth Token refresh functionality |
| 153 |
jQuery(document).ready(function($) { |
| 154 |
$('#refresh-plugin-auth-token').click(function() { |
| 155 |
var button = $(this); |
| 156 |
var originalText = button.text(); |
| 157 |
|
| 158 |
if (confirm('Are you sure you want to refresh the Plugin Auth Token? This will generate a new token and update the heartbeat API.')) { |
| 159 |
// Disable button and show loading |
| 160 |
button.prop('disabled', true).text('🔄 Refreshing...'); |
| 161 |
|
| 162 |
$.post(ajaxurl, { |
| 163 |
action: 'metasync_refresh_plugin_auth_token', |
| 164 |
nonce: '" . wp_create_nonce('metasync_refresh_plugin_auth_token') . "' |
| 165 |
}) |
| 166 |
.done(function(response) { |
| 167 |
if (response.success && response.data && response.data.new_token) { |
| 168 |
// Update the field value immediately |
| 169 |
$('#apikey').val(response.data.new_token); |
| 170 |
|
| 171 |
// Visual feedback with green border |
| 172 |
$('#apikey').css('border', '2px solid #28a745').animate({borderColor: '#ddd'}, 2000); |
| 173 |
|
| 174 |
alert('� |
| 175 |
Plugin Auth Token refreshed successfully!\\n\\nNew token: ' + response.data.new_token.substring(0, 8) + '...'); |
| 176 |
} else { |
| 177 |
alert('❌ Error refreshing token: ' + (response.data ? response.data.message : 'Unknown error')); |
| 178 |
} |
| 179 |
}) |
| 180 |
.fail(function() { |
| 181 |
alert('❌ Network error while refreshing token'); |
| 182 |
}) |
| 183 |
.always(function() { |
| 184 |
// Re-enable button |
| 185 |
button.prop('disabled', false).text(originalText); |
| 186 |
}); |
| 187 |
} |
| 188 |
}); |
| 189 |
}); |
| 190 |
"; |
| 191 |
wp_add_inline_script($plugin_name, $inline_script); |
| 192 |
|
| 193 |
if (isset($_GET['page']) && strpos($_GET['page'], '-setup-wizard') !== false) { |
| 194 |
wp_enqueue_script( |
| 195 |
$plugin_name . '-setup-wizard', |
| 196 |
$admin_dir_url . 'js/metasync-setup-wizard.js', |
| 197 |
array('jquery'), |
| 198 |
$version, |
| 199 |
true |
| 200 |
); |
| 201 |
|
| 202 |
wp_localize_script($plugin_name . '-setup-wizard', 'metasyncWizardData', array( |
| 203 |
'nonce' => wp_create_nonce('metasync_wizard'), |
| 204 |
'ssoNonce' => wp_create_nonce('metasync_sso_nonce'), |
| 205 |
'importNonce' => wp_create_nonce('metasync_import_external_data'), |
| 206 |
'dashboardUrl' => admin_url('admin.php?page=' . Metasync_Admin::$page_slug . '-dashboard'), |
| 207 |
'currentStep' => 1, |
| 208 |
'totalSteps' => 6, |
| 209 |
'pluginName' => Metasync::get_effective_plugin_name() |
| 210 |
)); |
| 211 |
} |
| 212 |
|
| 213 |
wp_enqueue_script('heartbeat'); |
| 214 |
} |
| 215 |
} |
| 216 |
|