| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
$js_urls = $request->get_param('js_urls'); |
| 7 |
|
| 8 |
if (empty($js_urls)) { |
| 9 |
return new WP_REST_Response('No JavaScript URLs provided.', 400); |
| 10 |
} |
| 11 |
|
| 12 |
$return_url = []; |
| 13 |
|
| 14 |
foreach ($js_urls as $js_url) { |
| 15 |
// $response = wp_remote_get($js_url); |
| 16 |
$response = bwp_wp_remote_get($js_url); |
| 17 |
|
| 18 |
// Download and save the content of each JS file |
| 19 |
$content = file_get_contents($js_url); |
| 20 |
$file_name = basename(parse_url($js_url, PHP_URL_PATH)); // Remove GET parameters |
| 21 |
$file_path = optifer_cache . 'js/' . md5($js_url) . '_' . $file_name; |
| 22 |
|
| 23 |
// Create the directory if it doesn't exist |
| 24 |
if (!file_exists(optifer_cache . 'js/')) { |
| 25 |
mkdir(optifer_cache . 'js/', 0755, true); |
| 26 |
} |
| 27 |
|
| 28 |
// Save the content to the cache directory |
| 29 |
file_put_contents($file_path, $content); |
| 30 |
|
| 31 |
unset($content, $response); |
| 32 |
|
| 33 |
$return_url[md5($js_url)] = get_site_url() . '/wp-content/cache/berqwp/js/' . md5($js_url) . '_' . $file_name; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
echo wp_json_encode( |
| 38 |
[ |
| 39 |
"status" => "success", |
| 40 |
"urls" => $return_url |
| 41 |
] |
| 42 |
); |
| 43 |
exit; |