PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.4
WindPress – Tailwind CSS integration for WordPress v3.0.4
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 3.0.7 All 142 releases
windpress / src / Core / Runtime.php

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

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