| 1 |
<?php |
| 2 |
/** |
| 3 |
* Frontend Assets Handler |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit(); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Helper function to detect localhost |
| 12 |
*/ |
| 13 |
function maxi_is_localhost() { |
| 14 |
$server_name = strtolower(isset($_SERVER['SERVER_NAME']) ? sanitize_text_field($_SERVER['SERVER_NAME']) : ''); |
| 15 |
$remote_addr = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : ''; |
| 16 |
|
| 17 |
return in_array($server_name, ['localhost', '127.0.0.1', '::1']) || |
| 18 |
strpos($remote_addr, '127.0.') === 0 || |
| 19 |
$remote_addr === '::1' || |
| 20 |
strpos($server_name, '.local') !== false || |
| 21 |
strpos($server_name, '.test') !== false; |
| 22 |
} |
| 23 |
|
| 24 |
// Add both admin and non-admin AJAX handlers |
| 25 |
function maxi_register_frontend_assets_handlers() |
| 26 |
{ |
| 27 |
add_action('wp_ajax_maxi_get_frontend_assets', 'maxi_get_frontend_assets_handler'); |
| 28 |
add_action('wp_ajax_nopriv_maxi_get_frontend_assets', 'maxi_get_frontend_assets_handler'); |
| 29 |
} |
| 30 |
add_action('init', 'maxi_register_frontend_assets_handlers'); |
| 31 |
|
| 32 |
function maxi_get_frontend_assets_handler() |
| 33 |
{ |
| 34 |
|
| 35 |
// Verify nonce first |
| 36 |
if (!check_ajax_referer('maxi_get_frontend_assets', 'nonce', false)) { |
| 37 |
wp_send_json_error(['message' => 'Invalid security token']); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
try { |
| 42 |
// Get the homepage URL |
| 43 |
$home_url = home_url(); |
| 44 |
|
| 45 |
// Make a request to the frontend |
| 46 |
$response = wp_remote_get($home_url, [ |
| 47 |
'timeout' => 30, |
| 48 |
'sslverify' => !maxi_is_localhost(), |
| 49 |
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' |
| 50 |
]); |
| 51 |
|
| 52 |
if (is_wp_error($response)) { |
| 53 |
wp_send_json_error(['message' => 'Failed to fetch frontend']); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$html = wp_remote_retrieve_body($response); |
| 58 |
|
| 59 |
// Parse HTML to find assets |
| 60 |
$css_files = []; |
| 61 |
$js_files = []; |
| 62 |
|
| 63 |
// Use DOMDocument to parse HTML |
| 64 |
$dom = new DOMDocument(); |
| 65 |
libxml_use_internal_errors(true); |
| 66 |
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
| 67 |
libxml_clear_errors(); |
| 68 |
|
| 69 |
// Get all CSS files |
| 70 |
$links = $dom->getElementsByTagName('link'); |
| 71 |
foreach ($links as $link) { |
| 72 |
if ($link->getAttribute('rel') === 'stylesheet') { |
| 73 |
$css_files[] = $link->getAttribute('href'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Get all JS files |
| 78 |
$scripts = $dom->getElementsByTagName('script'); |
| 79 |
foreach ($scripts as $script) { |
| 80 |
$src = $script->getAttribute('src'); |
| 81 |
if ($src) { |
| 82 |
$js_files[] = $src; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Clean up URLs |
| 87 |
$css_files = array_values(array_filter(array_unique($css_files))); |
| 88 |
$js_files = array_values(array_filter(array_unique($js_files))); |
| 89 |
|
| 90 |
wp_send_json_success([ |
| 91 |
'css' => $css_files ?: [__('No CSS files found', 'maxi-blocks')], |
| 92 |
'js' => $js_files ?: [__('No JavaScript files found', 'maxi-blocks')] |
| 93 |
]); |
| 94 |
|
| 95 |
} catch (Exception $e) { |
| 96 |
wp_send_json_error(['message' => 'Internal error']); |
| 97 |
} |
| 98 |
} |
| 99 |
|