PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.0
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.0
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / core.php

core.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 2.0, at includes/core.php

272 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Core plugin functionality.
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache\Core;
9
10 use PoweredCache\Async\CachePreloader;
11 use PoweredCache\Async\DatabaseOptimizer;
12 use PoweredCache\Config;
13 use const PoweredCache\Constants\DB_CLEANUP_COUNT_CACHE_KEY;
14 use const PoweredCache\Constants\MENU_SLUG;
15 use PoweredCache\Optimizer\JS;
16 use \WP_Error as WP_Error;
17
18 /**
19 * Default setup routine
20 *
21 * @return void
22 */
23 function setup() {
24 add_action( 'init', __NAMESPACE__ . '\\i18n' );
25 add_action( 'init', __NAMESPACE__ . '\\init' );
26 add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_scripts' );
27 add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_styles' );
28 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\block_editor_assets' );
29 add_action( 'plugins_loaded', __NAMESPACE__ . '\\register_async_process' );
30
31 // Hook to allow async or defer on asset loading.
32 add_filter( 'script_loader_tag', __NAMESPACE__ . '\\script_loader_tag', 10, 2 );
33
34 /**
35 * Fires after powered cache loaded
36 *
37 * @hook powered_cache_loaded
38 *
39 * @since 2.0
40 */
41 do_action( 'powered_cache_loaded' );
42 }
43
44 /**
45 * Registers the default textdomain.
46 *
47 * @return void
48 */
49 function i18n() {
50 $locale = apply_filters( 'plugin_locale', get_locale(), 'powered-cache' ); // This filter is documented in /wp-includes/l10n.php.
51 load_textdomain( 'powered-cache', WP_LANG_DIR . '/powered-cache/powered-cache-' . $locale . '.mo' );
52 load_plugin_textdomain( 'powered-cache', false, plugin_basename( POWERED_CACHE_PATH ) . '/languages/' );
53 }
54
55 /**
56 * Initializes the plugin and fires an action other plugins can hook into.
57 *
58 * @return void
59 */
60 function init() {
61 /**
62 * Fires during init
63 *
64 * @hook powered_cache_init
65 *
66 * @since 2.0
67 */
68 do_action( 'powered_cache_init' );
69 }
70
71 /**
72 * Activate the plugin
73 * `POWERED_CACHE_IS_NETWORK` useless on networkwide activation at first
74 *
75 * @param bool $network_wide Whether network-wide configuration or not
76 *
77 * @return void
78 */
79 function activate( $network_wide ) {
80 $settings = \PoweredCache\Utils\get_settings( $network_wide );
81 Config::factory()->save_configuration( $settings, $network_wide );
82 }
83
84 /**
85 * Deactivate the plugin
86 *
87 * Uninstall routines should be in uninstall.php
88 *
89 * @param bool $network_wide Whether network-wide configuration or not
90 *
91 * @return void
92 */
93 function deactivate( $network_wide ) {
94 Config::factory()->clean_up();
95
96 // cancel async jobs
97 $cache_preloader = CachePreloader::factory();
98 $cache_preloader->cancel_process();
99 $db_optimizer = DatabaseOptimizer::factory();
100 $db_optimizer->cancel_process();
101
102 // cleanup transients
103 delete_site_transient( DB_CLEANUP_COUNT_CACHE_KEY );
104 delete_transient( DB_CLEANUP_COUNT_CACHE_KEY );
105 }
106
107
108 /**
109 * The list of knows contexts for enqueuing scripts/styles.
110 *
111 * @return array
112 */
113 function get_enqueue_contexts() {
114 return [ 'admin', 'frontend', 'shared' ];
115 }
116
117 /**
118 * Generate an URL to a script, taking into account whether SCRIPT_DEBUG is enabled.
119 *
120 * @param string $script Script file name (no .js extension)
121 * @param string $context Context for the script ('admin', 'frontend', or 'shared')
122 *
123 * @return string|WP_Error URL
124 */
125 function script_url( $script, $context ) {
126
127 if ( ! in_array( $context, get_enqueue_contexts(), true ) ) {
128 return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in PoweredCache script loader.' );
129 }
130
131 return POWERED_CACHE_URL . "dist/js/${script}.js";
132
133 }
134
135 /**
136 * Generate an URL to a stylesheet, taking into account whether SCRIPT_DEBUG is enabled.
137 *
138 * @param string $stylesheet Stylesheet file name (no .css extension)
139 * @param string $context Context for the script ('admin', 'frontend', or 'shared')
140 *
141 * @return string URL
142 */
143 function style_url( $stylesheet, $context ) {
144
145 if ( ! in_array( $context, get_enqueue_contexts(), true ) ) {
146 return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in PoweredCache stylesheet loader.' );
147 }
148
149 return POWERED_CACHE_URL . "dist/css/${stylesheet}.css";
150
151 }
152
153 /**
154 * Enqueue scripts for admin.
155 *
156 * @return void
157 */
158 function admin_scripts() {
159 if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
160 return;
161 }
162
163 wp_enqueue_script(
164 'powered_cache_admin',
165 script_url( 'admin', 'admin' ),
166 [
167 'jquery',
168 'lodash',
169 'wp-i18n',
170 ],
171 POWERED_CACHE_VERSION,
172 true
173 );
174 }
175
176 /**
177 * Enqueue Block Editor assets
178 *
179 * @since 2.0
180 */
181 function block_editor_assets() {
182
183 /**
184 * Min. WP 5.3 required for block editor plugin due to useSelect
185 * Likely the older version of react didn't support hooks.
186 * The post meta-box works with compat mode vice-versa...
187 */
188 if ( version_compare( get_bloginfo( 'version' ), '5.3', '>=' ) ) {
189 wp_enqueue_script(
190 'powered_cache_admin',
191 script_url( 'editor', 'admin' ),
192 [
193 'jquery',
194 'lodash',
195 'wp-i18n',
196 'wp-edit-post',
197 'wp-components',
198 'wp-compose',
199 'wp-data',
200 'wp-edit-post',
201 'wp-element',
202 'wp-plugins',
203 ],
204 POWERED_CACHE_VERSION,
205 true
206 );
207 }
208 }
209
210 /**
211 * Enqueue styles for admin.
212 *
213 * @return void
214 */
215 function admin_styles() {
216 // load on the powered cache page only
217 if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
218 return;
219 }
220
221 wp_enqueue_style(
222 'powered_cache_admin',
223 style_url( 'admin-style', 'admin' ),
224 [],
225 POWERED_CACHE_VERSION
226 );
227
228 }
229
230 /**
231 * Add async/defer attributes to enqueued scripts that have the specified script_execution flag.
232 *
233 * @link https://core.trac.wordpress.org/ticket/12009
234 *
235 * @param string $tag The script tag.
236 * @param string $handle The script handle.
237 *
238 * @return string
239 */
240 function script_loader_tag( $tag, $handle ) {
241 $script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
242
243 if ( ! $script_execution ) {
244 return $tag;
245 }
246
247 if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
248 return $tag;
249 }
250
251 // Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
252 foreach ( wp_scripts()->registered as $script ) {
253 if ( in_array( $handle, $script->deps, true ) ) {
254 return $tag;
255 }
256 }
257
258 // Add the attribute if it hasn't already been added.
259 if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
260 $tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
261 }
262
263 return $tag;
264 }
265
266 /**
267 * Invoke async classes
268 */
269 function register_async_process() {
270 DatabaseOptimizer::factory();
271 }
272