PluginProbe
Performance Lab / 3.9.0
Performance Lab v3.9.0
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / load.php

load.php in Performance Lab 3.9.0, at load.php

347 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Performance Lab
4 * Plugin URI: https://github.com/WordPress/performance
5 * Description: Performance plugin from the WordPress Performance Team, which is a collection of standalone performance features.
6 * Requires at least: 6.6
7 * Requires PHP: 7.2
8 * Version: 3.9.0
9 * Author: WordPress Performance Team
10 * Author URI: https://make.wordpress.org/performance/
11 * License: GPLv2 or later
12 * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
13 * Text Domain: performance-lab
14 *
15 * @package performance-lab
16 */
17
18 // @codeCoverageIgnoreStart
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // Exit if accessed directly.
21 }
22 // @codeCoverageIgnoreEnd
23
24 define( 'PERFLAB_VERSION', '3.9.0' );
25 define( 'PERFLAB_MAIN_FILE', __FILE__ );
26 define( 'PERFLAB_PLUGIN_DIR_PATH', plugin_dir_path( PERFLAB_MAIN_FILE ) );
27 define( 'PERFLAB_SCREEN', 'performance-lab' );
28
29 // If the constant isn't defined yet, it means the Performance Lab object cache file is not loaded.
30 if ( ! defined( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION' ) ) {
31 define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', false );
32 }
33 define( 'PERFLAB_OBJECT_CACHE_DROPIN_LATEST_VERSION', 3 );
34
35 // Load server-timing API.
36 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/class-perflab-server-timing-metric.php';
37 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/class-perflab-server-timing.php';
38 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/load.php';
39 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/defaults.php';
40
41 // Load site health checks.
42 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/site-health/load.php';
43
44 /**
45 * Gets the content attribute for the generator tag for the Performance Lab plugin.
46 *
47 * This attribute is then used in {@see perflab_render_generator()}.
48 *
49 * @since 1.1.0
50 * @since 2.9.0 The generator tag now includes the active standalone plugin slugs.
51 * @since 3.0.0 The generator tag no longer includes module slugs.
52 */
53 function perflab_get_generator_content(): string {
54 $active_plugins = array();
55 foreach ( perflab_get_standalone_plugin_version_constants() as $plugin_slug => $constant_name ) {
56 if ( defined( $constant_name ) && ! str_starts_with( constant( $constant_name ), 'Performance Lab ' ) ) {
57 $active_plugins[] = $plugin_slug;
58 }
59 }
60
61 return sprintf(
62 // Use the plugin slug as it is immutable.
63 'performance-lab %1$s; plugins: %2$s',
64 PERFLAB_VERSION,
65 implode( ', ', $active_plugins )
66 );
67 }
68
69 /**
70 * Displays the HTML generator tag for the Performance Lab plugin.
71 *
72 * See {@see 'wp_head'}.
73 *
74 * @since 1.1.0
75 */
76 function perflab_render_generator(): void {
77 $content = perflab_get_generator_content();
78
79 echo '<meta name="generator" content="' . esc_attr( $content ) . '">' . "\n";
80 }
81 add_action( 'wp_head', 'perflab_render_generator' );
82
83 /**
84 * Gets the standalone plugins and their data.
85 *
86 * @since 3.0.0
87 *
88 * @return array<string, array{'constant': string, 'experimental'?: bool}> Associative array of $plugin_slug => $plugin_data pairs.
89 */
90 function perflab_get_standalone_plugin_data(): array {
91 /*
92 * Alphabetically sorted list of plugin slugs and their data.
93 * Supported keys per plugin are:
94 * - 'constant' (string, required)
95 * - 'experimental' (boolean, optional)
96 */
97 return array(
98 'auto-sizes' => array(
99 'constant' => 'IMAGE_AUTO_SIZES_VERSION',
100 'experimental' => true,
101 ),
102 'dominant-color-images' => array(
103 'constant' => 'DOMINANT_COLOR_IMAGES_VERSION',
104 ),
105 'embed-optimizer' => array(
106 'constant' => 'EMBED_OPTIMIZER_VERSION',
107 'experimental' => false,
108 ),
109 'image-prioritizer' => array(
110 'constant' => 'IMAGE_PRIORITIZER_VERSION',
111 'experimental' => false,
112 ),
113 'performant-translations' => array(
114 'constant' => 'PERFORMANT_TRANSLATIONS_VERSION',
115 ),
116 'speculation-rules' => array(
117 'constant' => 'SPECULATION_RULES_VERSION',
118 ),
119 'web-worker-offloading' => array(
120 'constant' => 'WEB_WORKER_OFFLOADING_VERSION',
121 'experimental' => true,
122 ),
123 'webp-uploads' => array(
124 'constant' => 'WEBP_UPLOADS_VERSION',
125 ),
126 );
127 }
128
129 /**
130 * Gets the standalone plugin constants used for each available standalone plugin.
131 *
132 * @since 2.9.0
133 * @since 3.0.0 The $source parameter was removed.
134 *
135 * @return array<string, string> Map of plugin slug and the version constant used.
136 */
137 function perflab_get_standalone_plugin_version_constants(): array {
138 return wp_list_pluck( perflab_get_standalone_plugin_data(), 'constant' );
139 }
140
141 /**
142 * Places the Performance Lab's object cache drop-in in the drop-ins folder.
143 *
144 * This only runs in WP Admin to not have any potential performance impact on
145 * the frontend.
146 *
147 * This function will short-circuit if at least one of the constants
148 * 'PERFLAB_DISABLE_SERVER_TIMING' or 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is
149 * set as true.
150 *
151 * @since 1.8.0
152 * @since 2.1.0 No longer attempts to use two of the drop-ins together.
153 *
154 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
155 */
156 function perflab_maybe_set_object_cache_dropin(): void {
157 global $wp_filesystem;
158
159 // Bail if Server-Timing is disabled entirely.
160 if ( defined( 'PERFLAB_DISABLE_SERVER_TIMING' ) && PERFLAB_DISABLE_SERVER_TIMING ) {
161 return;
162 }
163
164 // Bail if disabled via constant.
165 if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) {
166 return;
167 }
168
169 /**
170 * Filters whether the Perflab server timing drop-in should be set.
171 *
172 * @since 2.0.0
173 *
174 * @param bool $disabled Whether to disable the server timing drop-in. Default false.
175 */
176 if ( apply_filters( 'perflab_disable_object_cache_dropin', false ) ) {
177 return;
178 }
179
180 /**
181 * Filters the value of the `object-cache.php` drop-in constant.
182 *
183 * This filter should not be used outside of tests.
184 *
185 * @since 3.0.0
186 * @internal
187 *
188 * @param int|bool $current_dropin_version The drop-in version as defined by the
189 * `PERFLAB_OBJECT_CACHE_DROPIN_VERSION` constant.
190 */
191 $current_dropin_version = apply_filters( 'perflab_object_cache_dropin_version', PERFLAB_OBJECT_CACHE_DROPIN_VERSION );
192
193 // Bail if already placed in the latest version or newer.
194 if ( null !== $current_dropin_version && $current_dropin_version >= PERFLAB_OBJECT_CACHE_DROPIN_LATEST_VERSION ) {
195 return;
196 }
197
198 // Bail if already attempted before timeout has been completed.
199 // This is present in case placing the file fails for some reason, to avoid
200 // excessively retrying to place it on every request.
201 $timeout = get_transient( 'perflab_set_object_cache_dropin' );
202 if ( false !== $timeout ) {
203 return;
204 }
205
206 if ( $wp_filesystem instanceof WP_Filesystem_Base || true === WP_Filesystem() ) {
207 $dropin_path = WP_CONTENT_DIR . '/object-cache.php';
208
209 /*
210 * If there is an actual object-cache.php file, it is most likely from
211 * a third party, or it may be an older version of the Performance Lab
212 * object-cache.php. If it's from a third party, do not replace it.
213 *
214 * Previous versions of the Performance Lab plugin were renaming the
215 * original object-cache.php file and then loading both. However, due
216 * to other plugins eagerly checking file headers, this caused too many
217 * problems across sites, so it was decided to remove this layer.
218 * Only placing the drop-in file if no other one exists yet is the
219 * safest solution.
220 */
221 if ( $wp_filesystem->exists( $dropin_path ) ) {
222 // If this constant evaluates to `false`, the existing file is for sure from a third party.
223 if ( false === $current_dropin_version ) {
224 // Set timeout of 1 day before retrying again (only in case the file already exists).
225 set_transient( 'perflab_set_object_cache_dropin', true, DAY_IN_SECONDS );
226 return;
227 }
228
229 // Otherwise, verify that it's actually the Performance Lab drop-in.
230 $test_content = "<?php\n/**\n * Plugin Name: Performance Lab Server Timing Object Cache Drop-In\n";
231 if ( ! str_starts_with( $wp_filesystem->get_contents( $dropin_path ), $test_content ) ) {
232 // Set timeout of 1 day before retrying again (only in case the file already exists).
233 set_transient( 'perflab_set_object_cache_dropin', true, DAY_IN_SECONDS );
234 return;
235 }
236
237 /*
238 * If this logic is reached, the existing file is an older version
239 * of the Performance Lab drop-in, so it can be safely deleted, and
240 * then be replaced below.
241 */
242 $wp_filesystem->delete( $dropin_path );
243 }
244
245 $wp_filesystem->copy( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php', $dropin_path );
246 }
247
248 // Set timeout of 1 hour before retrying again (only relevant in case the above failed).
249 set_transient( 'perflab_set_object_cache_dropin', true, HOUR_IN_SECONDS );
250 }
251 add_action( 'admin_init', 'perflab_maybe_set_object_cache_dropin' );
252
253 /**
254 * Removes the Performance Lab's object cache drop-in from the drop-ins folder.
255 *
256 * This function should be run on plugin deactivation. For backward compatibility with
257 * an earlier implementation of `perflab_maybe_set_object_cache_dropin()`, this function
258 * checks whether there is an object-cache-plst-orig.php file, and if so restores it.
259 *
260 * This function will short-circuit if the constant
261 * 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is set as true.
262 *
263 * @since 1.8.0
264 *
265 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
266 */
267 function perflab_maybe_remove_object_cache_dropin(): void {
268 global $wp_filesystem;
269
270 // Bail if disabled via constant.
271 if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) {
272 return;
273 }
274
275 // Bail if custom drop-in not present anyway.
276 if ( ! PERFLAB_OBJECT_CACHE_DROPIN_VERSION ) {
277 return;
278 }
279
280 if ( $wp_filesystem instanceof WP_Filesystem_Base || true === WP_Filesystem() ) {
281 $dropin_path = WP_CONTENT_DIR . '/object-cache.php';
282 $dropin_backup_path = WP_CONTENT_DIR . '/object-cache-plst-orig.php';
283
284 /**
285 * If there is an object-cache-plst-orig.php file, restore it and
286 * override the Performance Lab file. This is only relevant for
287 * backward-compatibility with previous Performance Lab versions
288 * which were backing up the file and then loading both.
289 * Otherwise, just delete the Performance Lab file.
290 */
291 if ( $wp_filesystem->exists( $dropin_backup_path ) ) {
292 $wp_filesystem->move( $dropin_backup_path, $dropin_path, true );
293 } else {
294 $wp_filesystem->delete( $dropin_path );
295 }
296 }
297
298 // Delete transient for drop-in check in case the plugin is reactivated shortly after.
299 delete_transient( 'perflab_set_object_cache_dropin' );
300 }
301 register_deactivation_hook( __FILE__, 'perflab_maybe_remove_object_cache_dropin' );
302
303 /**
304 * Redirects legacy module page to the performance feature page.
305 *
306 * @since 3.0.0
307 *
308 * @global $plugin_page
309 */
310 function perflab_no_access_redirect_module_to_performance_feature_page(): void {
311 global $plugin_page;
312
313 if ( 'perflab-modules' !== $plugin_page ) {
314 return;
315 }
316
317 if (
318 current_user_can( 'manage_options' ) &&
319 wp_safe_redirect( add_query_arg( 'page', PERFLAB_SCREEN ) )
320 ) {
321 exit;
322 }
323 }
324 add_action( 'admin_page_access_denied', 'perflab_no_access_redirect_module_to_performance_feature_page' );
325
326 /**
327 * Cleanup function to delete legacy 'perflab_modules_settings' option if present.
328 *
329 * @since 3.0.0
330 */
331 function perflab_cleanup_option(): void {
332 if ( current_user_can( 'manage_options' ) ) {
333 delete_option( 'perflab_modules_settings' );
334 }
335 }
336 add_action( 'admin_init', 'perflab_cleanup_option' );
337
338 // Only load admin integration when in admin.
339 if ( is_admin() ) {
340 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/load.php';
341 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/server-timing.php';
342 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/plugins.php';
343 }
344
345 // Load REST API.
346 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/rest-api.php';
347