PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.1
WindPress – Tailwind CSS integration for WordPress v3.0.1
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 / Runtime.php

Runtime.php in WindPress – Tailwind CSS integration for WordPress 3.0.1, at src/Core/Runtime.php

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