| 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 WP_REST_Request; |
| 16 |
use WP_REST_Response; |
| 17 |
use WP_REST_Server; |
| 18 |
use WindPress\WindPress\Api\AbstractApi; |
| 19 |
use WindPress\WindPress\Api\ApiInterface; |
| 20 |
use WindPress\WindPress\Core\Cache as CoreCache; |
| 21 |
use WindPress\WindPress\Utils\Common; |
| 22 |
use WindPress\WindPress\Utils\Debug; |
| 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, '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 |
} |
| 48 |
return new WP_REST_Response(['cache' => $cache]); |
| 49 |
} |
| 50 |
public function store(WP_REST_Request $wprestRequest) : WP_REST_Response |
| 51 |
{ |
| 52 |
$payload = $wprestRequest->get_json_params(); |
| 53 |
try { |
| 54 |
$content = \sprintf("/*! %s v%s | %s | %s */\n%s", \strtolower(Common::plugin_data('Name')), WIND_PRESS::VERSION, \gmdate('Y-m-d H:i:s', \time()), \strtolower(Common::plugin_data('PluginURI')), \base64_decode($payload['content'])); |
| 55 |
CoreCache::save_cache($content); |
| 56 |
} catch (\Throwable $throwable) { |
| 57 |
return new WP_REST_Response(['status' => 'KO', 'message' => 'Save cache error: ' . $throwable->getMessage()], 500); |
| 58 |
} |
| 59 |
return $this->index($wprestRequest); |
| 60 |
} |
| 61 |
public function providers(WP_REST_Request $wprestRequest) : WP_REST_Response |
| 62 |
{ |
| 63 |
return new WP_REST_Response(['providers' => CoreCache::get_providers()]); |
| 64 |
} |
| 65 |
public function providers_scan(WP_REST_Request $wprestRequest) : WP_REST_Response |
| 66 |
{ |
| 67 |
$stopwatch = Debug::stopwatch(); |
| 68 |
$provider_id = $wprestRequest->get_param('provider_id'); |
| 69 |
$metadata = $wprestRequest->get_param('metadata') ?? []; |
| 70 |
$provider = \array_filter(CoreCache::get_providers(), static fn($provider) => $provider['id'] === $provider_id); |
| 71 |
if ($provider === []) { |
| 72 |
return new WP_REST_Response(['status' => 'KO', 'message' => 'The provider not found'], 404); |
| 73 |
} |
| 74 |
$provider = \array_shift($provider); |
| 75 |
$stopwatch->start('cache-provider:' . $provider['id'], 'cache-provider-scan'); |
| 76 |
try { |
| 77 |
$result = CoreCache::fetch_contents($provider['callback'], $metadata); |
| 78 |
} catch (\Throwable $throwable) { |
| 79 |
return new WP_REST_Response(['status' => 'KO', 'message' => 'Provider error: ' . $throwable->getMessage()], 500); |
| 80 |
} |
| 81 |
$event = $stopwatch->stop('cache-provider:' . $provider['id']); |
| 82 |
return new WP_REST_Response(['metadata' => ['provider_id' => $provider['id'], 'provider_name' => $provider['name'], 'profiling' => ['unix_timestamp' => \time(), 'duration' => $event->getDuration() . ' ms', 'memory' => \sprintf('%.2F MiB', $event->getMemory() / 1024 / 1024)], 'next_batch' => $result['metadata']['next_batch'] ?? \false], 'contents' => $result['contents'], 'status' => 'OK']); |
| 83 |
} |
| 84 |
} |
| 85 |
|