PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.6.1
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.6.1
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 3.6.1, at includes/core.php

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