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

305 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 /**
208 * Min. WP 5.3 required for block editor plugin due to useSelect
209 * Likely the older version of react didn't support hooks.
210 * The post meta-box works with compat mode vice-versa...
211 */
212 if ( version_compare( get_bloginfo( 'version' ), '5.3', '>=' ) ) {
213 wp_register_script(
214 'powered-cache-editor',
215 script_url( 'editor', 'admin' ),
216 [
217 'jquery',
218 'lodash',
219 'wp-i18n',
220 'wp-edit-post',
221 'wp-components',
222 'wp-compose',
223 'wp-data',
224 'wp-edit-post',
225 'wp-element',
226 'wp-plugins',
227 ],
228 POWERED_CACHE_VERSION,
229 true
230 );
231
232 wp_enqueue_script( 'powered-cache-editor' );
233
234 wp_set_script_translations(
235 'powered-cache-editor',
236 'powered-cache',
237 plugin_dir_path( POWERED_CACHE_PLUGIN_FILE ) . 'languages'
238 );
239
240 }
241 }
242
243 /**
244 * Enqueue styles for admin.
245 *
246 * @return void
247 */
248 function admin_styles() {
249 // load on the powered cache page only
250 if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
251 return;
252 }
253
254 wp_enqueue_style(
255 'powered-cache-admin',
256 style_url( 'admin-style', 'admin' ),
257 [],
258 POWERED_CACHE_VERSION
259 );
260
261 }
262
263 /**
264 * Add async/defer attributes to enqueued scripts that have the specified script_execution flag.
265 *
266 * @link https://core.trac.wordpress.org/ticket/12009
267 *
268 * @param string $tag The script tag.
269 * @param string $handle The script handle.
270 *
271 * @return string
272 */
273 function script_loader_tag( $tag, $handle ) {
274 $script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
275
276 if ( ! $script_execution ) {
277 return $tag;
278 }
279
280 if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
281 return $tag;
282 }
283
284 // Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
285 foreach ( wp_scripts()->registered as $script ) {
286 if ( in_array( $handle, $script->deps, true ) ) {
287 return $tag;
288 }
289 }
290
291 // Add the attribute if it hasn't already been added.
292 if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
293 $tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
294 }
295
296 return $tag;
297 }
298
299 /**
300 * Invoke async classes
301 */
302 function register_async_process() {
303 DatabaseOptimizer::factory();
304 }
305