| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Api\Admin\Settings; |
| 13 |
|
| 14 |
use WIND_PRESS; |
| 15 |
use WindPress\WindPress\Api\AbstractApi; |
| 16 |
use WindPress\WindPress\Api\ApiInterface; |
| 17 |
use WindPress\WindPress\Core\Cache as CoreCache; |
| 18 |
use WindPress\WindPress\Utils\Common; |
| 19 |
use WindPress\WindPress\Utils\Debug; |
| 20 |
use WP_REST_Request; |
| 21 |
use WP_REST_Response; |
| 22 |
use WP_REST_Server; |
| 23 |
class Cache extends AbstractApi implements ApiInterface |
| 24 |
{ |
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
} |
| 28 |
public function get_prefix(): string |
| 29 |
{ |
| 30 |
return 'admin/settings/cache'; |
| 31 |
} |
| 32 |
public function register_custom_endpoints(): void |
| 33 |
{ |
| 34 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 35 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/store', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->store($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 36 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/providers', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->providers($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 37 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/providers/scan', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->providers_scan($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 38 |
} |
| 39 |
public function index(WP_REST_Request $wprestRequest): WP_REST_Response |
| 40 |
{ |
| 41 |
$cache_path = CoreCache::get_cache_path(CoreCache::CSS_CACHE_FILE); |
| 42 |
$cache = ['last_generated' => null, 'last_full_build' => null, 'file_url' => null, 'file_size' => \false]; |
| 43 |
if (file_exists($cache_path) && is_readable($cache_path)) { |
| 44 |
$cache['file_url'] = CoreCache::get_cache_url(CoreCache::CSS_CACHE_FILE); |
| 45 |
$cache['last_generated'] = filemtime($cache_path); |
| 46 |
$cache['file_size'] = filesize($cache_path); |
| 47 |
$last_full_build = wp_cache_get('last_full_build', 'windpress', \true); |
| 48 |
if ($last_full_build) { |
| 49 |
$cache['last_full_build'] = $last_full_build; |
| 50 |
} |
| 51 |
} |
| 52 |
return new WP_REST_Response(['cache' => $cache]); |
| 53 |
} |
| 54 |
public function store(WP_REST_Request $wprestRequest): WP_REST_Response |
| 55 |
{ |
| 56 |
$payload = $wprestRequest->get_json_params(); |
| 57 |
try { |
| 58 |
$content = base64_decode($payload['content']); |
| 59 |
if (isset($payload['sourcemap']) && $payload['sourcemap']) { |
| 60 |
$sourcemapContent = base64_decode($payload['sourcemap']); |
| 61 |
CoreCache::save_sourcemap($sourcemapContent); |
| 62 |
$content .= "\n/*# sourceMappingURL=" . CoreCache::CSS_SOURCEMAP_FILE . " */"; |
| 63 |
} else { |
| 64 |
// add a comment at the top of the file |
| 65 |
$comment = sprintf("/*! %s v%s | %s | %s */\n", strtolower(Common::plugin_data('Name')), WIND_PRESS::VERSION, gmdate('Y-m-d H:i:s', time()), strtolower(Common::plugin_data('PluginURI'))); |
| 66 |
$content = $comment . $content; |
| 67 |
} |
| 68 |
CoreCache::save_cache($content); |
| 69 |
} catch (\Throwable $throwable) { |
| 70 |
return new WP_REST_Response(['status' => 'KO', 'message' => __('Save cache error: ', 'windpress') . $throwable->getMessage()], 500); |
| 71 |
} |
| 72 |
if (isset($payload['full_build']) && $payload['full_build']) { |
| 73 |
wp_cache_set('last_full_build', $payload['full_build'], 'windpress'); |
| 74 |
} |
| 75 |
return $this->index($wprestRequest); |
| 76 |
} |
| 77 |
public function providers(WP_REST_Request $wprestRequest): WP_REST_Response |
| 78 |
{ |
| 79 |
$providers = CoreCache::get_providers(); |
| 80 |
// Add installation status to each provider |
| 81 |
foreach ($providers as &$provider) { |
| 82 |
if (isset($provider['is_installed_active']) && is_callable($provider['is_installed_active'])) { |
| 83 |
$provider['is_installed_active'] = $provider['is_installed_active'](); |
| 84 |
} |
| 85 |
} |
| 86 |
return new WP_REST_Response(['providers' => $providers]); |
| 87 |
} |
| 88 |
public function providers_scan(WP_REST_Request $wprestRequest): WP_REST_Response |
| 89 |
{ |
| 90 |
$stopwatch = Debug::stopwatch(); |
| 91 |
$provider_id = $wprestRequest->get_param('provider_id'); |
| 92 |
$metadata = $wprestRequest->get_param('metadata') ?? []; |
| 93 |
$provider = array_filter(CoreCache::get_providers(), static fn($provider) => $provider['id'] === $provider_id); |
| 94 |
if ($provider === []) { |
| 95 |
return new WP_REST_Response(['status' => 'KO', 'message' => __('The provider not found', 'windpress')], 404); |
| 96 |
} |
| 97 |
$provider = array_shift($provider); |
| 98 |
$stopwatch->start('cache-provider:' . $provider['id'], 'cache-provider-scan'); |
| 99 |
try { |
| 100 |
$result = CoreCache::fetch_contents($provider['callback'], $metadata); |
| 101 |
} catch (\Throwable $throwable) { |
| 102 |
return new WP_REST_Response(['status' => 'KO', 'message' => __('Provider error: ', 'windpress') . $throwable->__toString()], 500); |
| 103 |
} |
| 104 |
$stopwatchEvent = $stopwatch->stop('cache-provider:' . $provider['id']); |
| 105 |
return new WP_REST_Response(['metadata' => ['provider_id' => $provider['id'], 'provider_name' => $provider['name'], 'profiling' => ['unix_timestamp' => time(), 'duration' => $stopwatchEvent->getDuration() . ' ms', 'memory' => sprintf('%.2F MiB', $stopwatchEvent->getMemory() / 1024 / 1024)], 'next_batch' => $result['metadata']['next_batch'] ?? \false, 'total_batches' => $result['metadata']['total_batches'] ?? \false], 'contents' => $result['contents'], 'status' => 'OK']); |
| 106 |
} |
| 107 |
} |
| 108 |
|