| 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\Core; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use WIND_PRESS; |
| 16 |
use WindPress\WindPress\Utils\Cache as UtilsCache; |
| 17 |
use WindPress\WindPress\Utils\Common; |
| 18 |
/** |
| 19 |
* @since 3.0.0 |
| 20 |
*/ |
| 21 |
class Cache |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public const CSS_CACHE_FILE = 'tailwind.css'; |
| 27 |
/** |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public const CSS_SOURCEMAP_FILE = 'tailwind.css.map'; |
| 31 |
public const THEME_JSON_FILE = 'theme.json'; |
| 32 |
public static function get_providers(): array |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Register cache providers. |
| 36 |
* @param array $providers The list of cache providers. Each provider should have `id`, `name`, `description`, and `callback` keys. |
| 37 |
*/ |
| 38 |
return apply_filters('f!windpress/core/cache:compile.providers', []); |
| 39 |
} |
| 40 |
public static function get_cache_path(string $file_path = ''): string |
| 41 |
{ |
| 42 |
return wp_upload_dir()['basedir'] . WIND_PRESS::CACHE_DIR . $file_path; |
| 43 |
} |
| 44 |
public static function get_cache_url(string $file_path = ''): string |
| 45 |
{ |
| 46 |
return wp_upload_dir()['baseurl'] . WIND_PRESS::CACHE_DIR . $file_path; |
| 47 |
} |
| 48 |
public static function save_cache(string $payload) |
| 49 |
{ |
| 50 |
try { |
| 51 |
Common::save_file($payload, self::get_cache_path(self::CSS_CACHE_FILE)); |
| 52 |
} catch (\Throwable $throwable) { |
| 53 |
throw $throwable; |
| 54 |
} |
| 55 |
do_action('a!windpress/core/cache:save_cache.after', $payload); |
| 56 |
UtilsCache::flush_cache_plugin(); |
| 57 |
} |
| 58 |
public static function save_sourcemap(string $payload) |
| 59 |
{ |
| 60 |
try { |
| 61 |
Common::save_file($payload, self::get_cache_path(self::CSS_SOURCEMAP_FILE)); |
| 62 |
} catch (\Throwable $throwable) { |
| 63 |
throw $throwable; |
| 64 |
} |
| 65 |
do_action('a!windpress/core/cache:save_sourcemap.after', $payload); |
| 66 |
UtilsCache::flush_cache_plugin(); |
| 67 |
} |
| 68 |
public static function save_theme_json(string $payload) |
| 69 |
{ |
| 70 |
try { |
| 71 |
Common::save_file($payload, self::get_cache_path(self::THEME_JSON_FILE)); |
| 72 |
} catch (\Throwable $throwable) { |
| 73 |
throw $throwable; |
| 74 |
} |
| 75 |
do_action('a!windpress/core/cache:save_theme_json.after', $payload); |
| 76 |
UtilsCache::flush_cache_plugin(); |
| 77 |
} |
| 78 |
public static function fetch_contents($callback, $metadata = []) |
| 79 |
{ |
| 80 |
// if class has an "__invoke" method. |
| 81 |
if (is_string($callback) && class_exists($callback) && method_exists($callback, '__invoke')) { |
| 82 |
$callback = new $callback(); |
| 83 |
} |
| 84 |
try { |
| 85 |
$result = call_user_func($callback, $metadata); |
| 86 |
if (!is_array($result)) { |
| 87 |
throw new Exception(__('The callback should return an array', 'windpress')); |
| 88 |
} |
| 89 |
$_metadata = array_key_exists('metadata', $result) ? $result['metadata'] : []; |
| 90 |
$_contents = array_key_exists('contents', $result) ? $result['contents'] : $result; |
| 91 |
$_contents = array_map(static function ($content) { |
| 92 |
$content_type = $content['type'] ?? null; |
| 93 |
if (is_array($content['content']) || is_object($content['content'])) { |
| 94 |
$content['content'] = wp_json_encode($content['content']); |
| 95 |
$content['type'] = 'json'; |
| 96 |
} elseif (is_string($content['content']) && $content_type !== 'json') { |
| 97 |
// Check if the string is already valid JSON |
| 98 |
$decoded = json_decode($content['content'], \true); |
| 99 |
if (json_last_error() === \JSON_ERROR_NONE && ($decoded !== null || $content['content'] === 'null')) { |
| 100 |
$content['type'] = 'json'; |
| 101 |
} |
| 102 |
} |
| 103 |
$content['content'] = is_string($content['content']) ? base64_encode($content['content']) : null; |
| 104 |
return $content; |
| 105 |
}, $_contents); |
| 106 |
} catch (\Throwable $throwable) { |
| 107 |
throw $throwable; |
| 108 |
} |
| 109 |
return ['metadata' => $_metadata, 'contents' => $_contents]; |
| 110 |
} |
| 111 |
} |
| 112 |
|