PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.89
WindPress – Tailwind CSS integration for WordPress v3.2.89
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / src / Core / Cache.php

Cache.php in WindPress – Tailwind CSS integration for WordPress 3.2.89, at src/Core/Cache.php

122 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Each provider should have `id`, `name`, `description`, and `callback` keys.
37 * Providers supporting stable source identities opt in with `source_index` => 1.
38 */
39 return apply_filters('f!windpress/core/cache:compile.providers', []);
40 }
41 public static function get_cache_path(string $file_path = ''): string
42 {
43 return wp_upload_dir()['basedir'] . WIND_PRESS::CACHE_DIR . $file_path;
44 }
45 public static function get_cache_url(string $file_path = ''): string
46 {
47 return wp_upload_dir()['baseurl'] . WIND_PRESS::CACHE_DIR . $file_path;
48 }
49 public static function save_cache(string $payload)
50 {
51 try {
52 Common::save_file($payload, self::get_cache_path(self::CSS_CACHE_FILE));
53 } catch (\Throwable $throwable) {
54 throw $throwable;
55 }
56 do_action('a!windpress/core/cache:save_cache.after', $payload);
57 UtilsCache::flush_cache_plugin();
58 }
59 public static function save_sourcemap(string $payload)
60 {
61 try {
62 Common::save_file($payload, self::get_cache_path(self::CSS_SOURCEMAP_FILE));
63 } catch (\Throwable $throwable) {
64 throw $throwable;
65 }
66 do_action('a!windpress/core/cache:save_sourcemap.after', $payload);
67 UtilsCache::flush_cache_plugin();
68 }
69 public static function save_theme_json(string $payload)
70 {
71 try {
72 Common::save_file($payload, self::get_cache_path(self::THEME_JSON_FILE));
73 } catch (\Throwable $throwable) {
74 throw $throwable;
75 }
76 do_action('a!windpress/core/cache:save_theme_json.after', $payload);
77 UtilsCache::flush_cache_plugin();
78 }
79 public static function fetch_contents($callback, $metadata = [])
80 {
81 // if class has an "__invoke" method.
82 if (is_string($callback) && class_exists($callback) && method_exists($callback, '__invoke')) {
83 $callback = new $callback();
84 }
85 try {
86 $result = call_user_func($callback, $metadata);
87 if (!is_array($result)) {
88 throw new Exception(__('The callback should return an array', 'windpress'));
89 }
90 $_metadata = array_key_exists('metadata', $result) ? $result['metadata'] : [];
91 $_contents = array_key_exists('contents', $result) ? $result['contents'] : $result;
92 if (!is_array($_metadata) || !is_array($_contents)) {
93 throw new Exception(__('The provider metadata and contents must be arrays.', 'windpress'));
94 }
95 $_contents = array_map(static function ($content) {
96 if (!is_array($content) || !array_key_exists('content', $content)) {
97 throw new Exception(__('A scan source is missing its content.', 'windpress'));
98 }
99 $content_type = $content['type'] ?? null;
100 if (is_array($content['content']) || is_object($content['content'])) {
101 $content['content'] = wp_json_encode($content['content']);
102 $content['type'] = 'json';
103 } elseif (is_string($content['content']) && $content_type !== 'json') {
104 // Check if the string is already valid JSON
105 $decoded = json_decode($content['content'], \true);
106 if (json_last_error() === \JSON_ERROR_NONE && ($decoded !== null || $content['content'] === 'null')) {
107 $content['type'] = 'json';
108 }
109 }
110 if (!is_string($content['content'])) {
111 throw new Exception(__('A scan source could not be encoded as text.', 'windpress'));
112 }
113 $content['content'] = base64_encode($content['content']);
114 return $content;
115 }, $_contents);
116 } catch (\Throwable $throwable) {
117 throw $throwable;
118 }
119 return ['metadata' => $_metadata, 'contents' => $_contents];
120 }
121 }
122