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

309 lines 7.1 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', 'classic-editor' ];
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 * @param string $hook Current hook.
157 *
158 * @return void
159 */
160 function admin_scripts( $hook ) {
161
162 $classic_editor_hooks = [ 'post-new.php', 'post.php' ];
163
164 if ( in_array( $hook, $classic_editor_hooks, true ) ) {
165 wp_enqueue_script(
166 'powered-cache-classic-editor',
167 script_url( 'classic-editor', 'classic-editor' ),
168 [
169 'jquery',
170 ],
171 POWERED_CACHE_VERSION,
172 true
173 );
174 }
175
176 if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
177 return;
178 }
179
180 wp_enqueue_script(
181 'powered-cache-admin',
182 script_url( 'admin', 'admin' ),
183 [
184 'jquery',
185 'lodash',
186 'wp-i18n',
187 ],
188 POWERED_CACHE_VERSION,
189 true
190 );
191
192 wp_set_script_translations(
193 'powered-cache-admin',
194 'powered-cache',
195 plugin_dir_path( POWERED_CACHE_PLUGIN_FILE ) . 'languages'
196 );
197
198 }
199
200 /**
201 * Enqueue Block Editor assets
202 *
203 * @since 2.0
204 */
205 function block_editor_assets() {
206
207 if ( ! current_user_can( 'edit_others_posts' ) ) {
208 return;
209 }
210
211 /**
212 * Min. WP 5.3 required for block editor plugin due to useSelect
213 * Likely the older version of react didn't support hooks.
214 * The post meta-box works with compat mode vice-versa...
215 */
216 if ( version_compare( get_bloginfo( 'version' ), '5.3', '>=' ) ) {
217 wp_register_script(
218 'powered-cache-editor',
219 script_url( 'editor', 'admin' ),
220 [
221 'jquery',
222 'lodash',
223 'wp-i18n',
224 'wp-edit-post',
225 'wp-components',
226 'wp-compose',
227 'wp-data',
228 'wp-edit-post',
229 'wp-element',
230 'wp-plugins',
231 ],
232 POWERED_CACHE_VERSION,
233 true
234 );
235
236 wp_enqueue_script( 'powered-cache-editor' );
237
238 wp_set_script_translations(
239 'powered-cache-editor',
240 'powered-cache',
241 plugin_dir_path( POWERED_CACHE_PLUGIN_FILE ) . 'languages'
242 );
243
244 }
245 }
246
247 /**
248 * Enqueue styles for admin.
249 *
250 * @return void
251 */
252 function admin_styles() {
253 // load on the powered cache page only
254 if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
255 return;
256 }
257
258 wp_enqueue_style(
259 'powered-cache-admin',
260 style_url( 'admin-style', 'admin' ),
261 [],
262 POWERED_CACHE_VERSION
263 );
264
265 }
266
267 /**
268 * Add async/defer attributes to enqueued scripts that have the specified script_execution flag.
269 *
270 * @link https://core.trac.wordpress.org/ticket/12009
271 *
272 * @param string $tag The script tag.
273 * @param string $handle The script handle.
274 *
275 * @return string
276 */
277 function script_loader_tag( $tag, $handle ) {
278 $script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
279
280 if ( ! $script_execution ) {
281 return $tag;
282 }
283
284 if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
285 return $tag;
286 }
287
288 // Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
289 foreach ( wp_scripts()->registered as $script ) {
290 if ( in_array( $handle, $script->deps, true ) ) {
291 return $tag;
292 }
293 }
294
295 // Add the attribute if it hasn't already been added.
296 if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
297 $tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
298 }
299
300 return $tag;
301 }
302
303 /**
304 * Invoke async classes
305 */
306 function register_async_process() {
307 DatabaseOptimizer::factory();
308 }
309