| 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 WIND_PRESS; |
| 15 |
use WindPress\WindPress\Utils\Cache as UtilsCache; |
| 16 |
use WindPress\WindPress\Utils\Common; |
| 17 |
/** |
| 18 |
* @since 3.0.0 |
| 19 |
*/ |
| 20 |
class Cache |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
public const CSS_CACHE_FILE = 'tailwind.css'; |
| 26 |
public static function get_providers() : array |
| 27 |
{ |
| 28 |
/** |
| 29 |
* Register cache providers. |
| 30 |
* @param array $providers The list of cache providers. Each provider should have `id`, `name`, `description`, and `callback` keys. |
| 31 |
*/ |
| 32 |
return \apply_filters('f!windpress/core/cache:compile.providers', []); |
| 33 |
} |
| 34 |
public static function get_cache_path(string $file_path = '') : string |
| 35 |
{ |
| 36 |
return \wp_upload_dir()['basedir'] . WIND_PRESS::CACHE_DIR . $file_path; |
| 37 |
} |
| 38 |
public static function get_cache_url(string $file_path = '') : string |
| 39 |
{ |
| 40 |
return \wp_upload_dir()['baseurl'] . WIND_PRESS::CACHE_DIR . $file_path; |
| 41 |
} |
| 42 |
public static function save_cache(string $payload) |
| 43 |
{ |
| 44 |
try { |
| 45 |
Common::save_file($payload, self::get_cache_path(self::CSS_CACHE_FILE)); |
| 46 |
} catch (\Throwable $throwable) { |
| 47 |
throw $throwable; |
| 48 |
} |
| 49 |
UtilsCache::flush_cache_plugin(); |
| 50 |
} |
| 51 |
public static function fetch_contents($callback, $metadata = []) |
| 52 |
{ |
| 53 |
// if class has an "__invoke" method. |
| 54 |
if (\is_string($callback) && \class_exists($callback) && \method_exists($callback, '__invoke')) { |
| 55 |
$callback = new $callback(); |
| 56 |
} |
| 57 |
try { |
| 58 |
$result = \call_user_func($callback, $metadata); |
| 59 |
if (!\is_array($result)) { |
| 60 |
throw new \Exception('The callback should return an array'); |
| 61 |
} |
| 62 |
$_metadata = \array_key_exists('metadata', $result) ? $result['metadata'] : []; |
| 63 |
$_contents = \array_key_exists('contents', $result) ? $result['contents'] : $result; |
| 64 |
$_contents = \array_map(static function ($content) { |
| 65 |
if (\is_array($content['content']) || \is_object($content['content'])) { |
| 66 |
$content['content'] = \wp_json_encode($content['content']); |
| 67 |
$content['type'] = 'json'; |
| 68 |
} |
| 69 |
$content['content'] = \is_string($content['content']) ? \base64_encode($content['content']) : null; |
| 70 |
return $content; |
| 71 |
}, $_contents); |
| 72 |
} catch (\Throwable $throwable) { |
| 73 |
throw $throwable; |
| 74 |
} |
| 75 |
return ['metadata' => $_metadata, 'contents' => $_contents]; |
| 76 |
} |
| 77 |
} |
| 78 |
|