| 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\AssetVite; |
| 17 |
use WindPress\WindPress\Utils\Common; |
| 18 |
use WindPress\WindPress\Utils\Config; |
| 19 |
/** |
| 20 |
* @since 3.0.0 |
| 21 |
* |
| 22 |
* TODO: This class is not yet implemented, it's just temporary for Universal Editor testing purpose. |
| 23 |
*/ |
| 24 |
class Runtime |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Stores the instance, implementing a Singleton pattern. |
| 28 |
*/ |
| 29 |
private static self $instance; |
| 30 |
/** |
| 31 |
* The Singleton's constructor should always be private to prevent direct |
| 32 |
* construction calls with the `new` operator. |
| 33 |
*/ |
| 34 |
private function __construct() |
| 35 |
{ |
| 36 |
} |
| 37 |
/** |
| 38 |
* Singletons should not be cloneable. |
| 39 |
*/ |
| 40 |
private function __clone() |
| 41 |
{ |
| 42 |
} |
| 43 |
/** |
| 44 |
* Singletons should not be restorable from strings. |
| 45 |
* |
| 46 |
* @throws Exception Cannot unserialize a singleton. |
| 47 |
*/ |
| 48 |
public function __wakeup() |
| 49 |
{ |
| 50 |
throw new Exception('Cannot unserialize a singleton.'); |
| 51 |
} |
| 52 |
/** |
| 53 |
* This is the static method that controls the access to the singleton |
| 54 |
* instance. On the first run, it creates a singleton object and places it |
| 55 |
* into the static property. On subsequent runs, it returns the client existing |
| 56 |
* object stored in the static property. |
| 57 |
*/ |
| 58 |
public static function get_instance() : self |
| 59 |
{ |
| 60 |
if (!isset(self::$instance)) { |
| 61 |
self::$instance = new self(); |
| 62 |
} |
| 63 |
return self::$instance; |
| 64 |
} |
| 65 |
public function init() |
| 66 |
{ |
| 67 |
if (!\is_admin()) { |
| 68 |
$is_prevent_load = \apply_filters('f!windpress/core/runtime:is_prevent_load', \false); |
| 69 |
if ($is_prevent_load) { |
| 70 |
return; |
| 71 |
} |
| 72 |
$this->append_header(); |
| 73 |
} |
| 74 |
} |
| 75 |
public function append_header() |
| 76 |
{ |
| 77 |
$is_cache_enabled = Config::get('performance.cache.enabled', \false); |
| 78 |
$is_cache_enabled = \apply_filters('f!windpress/core/runtime:append_header.cache_enabled', $is_cache_enabled); |
| 79 |
$is_exclude_admin = Config::get('performance.cache.exclude_admin', \false) && \current_user_can('manage_options'); |
| 80 |
$is_exclude_admin = \apply_filters('f!windpress/core/runtime:append_header.exclude_admin', $is_exclude_admin); |
| 81 |
if ($is_cache_enabled && $this->is_cache_exists() && !$is_exclude_admin) { |
| 82 |
\add_action('wp_head', fn() => $this->enqueue_css_cache(), 1000001); |
| 83 |
} else { |
| 84 |
\add_action('wp_head', fn() => $this->enqueue_play_cdn(), 1000001); |
| 85 |
} |
| 86 |
if (Config::get('general.ubiquitous-panel.enabled', \true) && \current_user_can('manage_options') && !\apply_filters('f!windpress/core/runtime:append_header.ubiquitous_panel.is_prevent_load', \false)) { |
| 87 |
\add_action('wp_head', fn() => $this->enqueue_front_panel(), 1000001); |
| 88 |
} |
| 89 |
} |
| 90 |
public function is_cache_exists() |
| 91 |
{ |
| 92 |
return \file_exists(\WindPress\WindPress\Core\Cache::get_cache_path(\WindPress\WindPress\Core\Cache::CSS_CACHE_FILE)) && \is_readable(\WindPress\WindPress\Core\Cache::get_cache_path(\WindPress\WindPress\Core\Cache::CSS_CACHE_FILE)); |
| 93 |
} |
| 94 |
public function enqueue_css_cache() |
| 95 |
{ |
| 96 |
if (!$this->is_cache_exists()) { |
| 97 |
return; |
| 98 |
} |
| 99 |
$handle = WIND_PRESS::WP_OPTION . '-cached'; |
| 100 |
if (Config::get('performance.cache.inline_load', \false)) { |
| 101 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file |
| 102 |
$css_clean = \file_get_contents(\WindPress\WindPress\Core\Cache::get_cache_path(\WindPress\WindPress\Core\Cache::CSS_CACHE_FILE)); |
| 103 |
if ($css_clean === \false) { |
| 104 |
return; |
| 105 |
} |
| 106 |
\wp_register_style($handle, \false, [], \false); |
| 107 |
\wp_add_inline_style($handle, \wp_strip_all_tags($css_clean)); |
| 108 |
\wp_print_styles($handle); |
| 109 |
} else { |
| 110 |
$version = (string) \filemtime(\WindPress\WindPress\Core\Cache::get_cache_path(\WindPress\WindPress\Core\Cache::CSS_CACHE_FILE)); |
| 111 |
\wp_register_style($handle, \WindPress\WindPress\Core\Cache::get_cache_url(\WindPress\WindPress\Core\Cache::CSS_CACHE_FILE), [], $version); |
| 112 |
\wp_print_styles($handle); |
| 113 |
} |
| 114 |
} |
| 115 |
public function enqueue_play_cdn($display = \true) |
| 116 |
{ |
| 117 |
$volumeEntries = \array_reduce(\WindPress\WindPress\Core\Volume::get_entries(), fn($carry, $entry) => $carry + ['/' . $entry['relative_path'] => $entry['content']], []); |
| 118 |
// Script content are base64 encoded to prevent it from being executed by the browser. |
| 119 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 120 |
echo \sprintf('<script id="windpress:vfs" type="text/tailwindcss">%s</script>', \base64_encode(\wp_json_encode($volumeEntries))); |
| 121 |
AssetVite::get_instance()->enqueue_asset('assets/packages/core/tailwind/play/autocomplete.js', ['handle' => WIND_PRESS::WP_OPTION . ':autocomplete', 'in-footer' => \true]); |
| 122 |
AssetVite::get_instance()->enqueue_asset('assets/packages/core/tailwind/play/sort.js', ['handle' => WIND_PRESS::WP_OPTION . ':sort', 'in-footer' => \true]); |
| 123 |
AssetVite::get_instance()->enqueue_asset('assets/packages/core/tailwind/play/classname-to-css.js', ['handle' => WIND_PRESS::WP_OPTION . ':classname-to-css', 'in-footer' => \true]); |
| 124 |
AssetVite::get_instance()->enqueue_asset('assets/packages/core/tailwind/play/observer.js', ['handle' => WIND_PRESS::WP_OPTION . ':observer', 'in-footer' => \true]); |
| 125 |
} |
| 126 |
public function enqueue_front_panel() |
| 127 |
{ |
| 128 |
$handle = WIND_PRESS::WP_OPTION . ':admin'; |
| 129 |
AssetVite::get_instance()->enqueue_asset('assets/apps/dashboard/main.js', ['handle' => $handle, 'in_footer' => \true, 'dependencies' => ['wp-i18n', 'wp-hooks']]); |
| 130 |
\wp_set_script_translations($handle, 'windpress'); |
| 131 |
\wp_localize_script($handle, 'windpress', ['_version' => WIND_PRESS::VERSION, '_via_wp_org' => !Common::is_updater_library_available(), '_wpnonce' => \wp_create_nonce(WIND_PRESS::WP_OPTION), 'rest_api' => ['nonce' => \wp_create_nonce('wp_rest'), 'root' => \esc_url_raw(\rest_url()), 'namespace' => WIND_PRESS::REST_NAMESPACE, 'url' => \esc_url_raw(\rest_url(WIND_PRESS::REST_NAMESPACE))], 'assets' => ['url' => AssetVite::asset_base_url()], 'site_meta' => ['name' => \get_bloginfo('name'), 'site_url' => \get_site_url()], 'is_universal' => \true]); |
| 132 |
// do enqueue scripts manually as it already runned before |
| 133 |
\wp_enqueue_scripts(); |
| 134 |
} |
| 135 |
} |
| 136 |
|