| 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: 4.1.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', '4.1.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' => false, |
| 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 |
'nocache-bfcache' => array( |
| 117 |
'constant' => 'WestonRuter\NocacheBFCache\VERSION', |
| 118 |
), |
| 119 |
'speculation-rules' => array( |
| 120 |
'constant' => 'SPECULATION_RULES_VERSION', |
| 121 |
), |
| 122 |
'view-transitions' => array( |
| 123 |
'constant' => 'VIEW_TRANSITIONS_VERSION', |
| 124 |
'experimental' => true, |
| 125 |
), |
| 126 |
'webp-uploads' => array( |
| 127 |
'constant' => 'WEBP_UPLOADS_VERSION', |
| 128 |
), |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Gets the standalone plugin constants used for each available standalone plugin. |
| 134 |
* |
| 135 |
* @since 2.9.0 |
| 136 |
* @since 3.0.0 The $source parameter was removed. |
| 137 |
* |
| 138 |
* @return array<string, string> Map of plugin slug and the version constant used. |
| 139 |
*/ |
| 140 |
function perflab_get_standalone_plugin_version_constants(): array { |
| 141 |
return wp_list_pluck( perflab_get_standalone_plugin_data(), 'constant' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Places the Performance Lab's object cache drop-in in the drop-ins folder. |
| 146 |
* |
| 147 |
* This only runs in WP Admin to not have any potential performance impact on |
| 148 |
* the frontend. |
| 149 |
* |
| 150 |
* This function will short-circuit if at least one of the constants |
| 151 |
* 'PERFLAB_DISABLE_SERVER_TIMING' or |
| 152 |
* 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is set as true or if the |
| 153 |
* 'PERFLAB_PLACE_OBJECT_CACHE_DROPIN' constant is not set to a truthy value. |
| 154 |
* |
| 155 |
* @since 1.8.0 |
| 156 |
* @since 2.1.0 No longer attempts to use two of the drop-ins together. |
| 157 |
* @since 4.0.0 No longer places the drop-in on new sites by default, unless the `PERFLAB_PLACE_OBJECT_CACHE_DROPIN` constant is set to true. |
| 158 |
* |
| 159 |
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| 160 |
*/ |
| 161 |
function perflab_maybe_set_object_cache_dropin(): void { |
| 162 |
global $wp_filesystem; |
| 163 |
|
| 164 |
// Bail if Server-Timing is disabled entirely. |
| 165 |
if ( defined( 'PERFLAB_DISABLE_SERVER_TIMING' ) && PERFLAB_DISABLE_SERVER_TIMING ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
// Bail if the drop-in is not enabled. |
| 170 |
if ( ! defined( 'PERFLAB_PLACE_OBJECT_CACHE_DROPIN' ) || ! PERFLAB_PLACE_OBJECT_CACHE_DROPIN ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// Bail if disabled via constant. |
| 175 |
// This constant is maintained only for backward compatibility and should not be relied upon in new implementations. |
| 176 |
// Use the 'PERFLAB_PLACE_OBJECT_CACHE_DROPIN' constant instead to control drop-in placement. |
| 177 |
if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Filters whether the Perflab server timing drop-in should be set. |
| 183 |
* |
| 184 |
* @since 2.0.0 |
| 185 |
* |
| 186 |
* @param bool $disabled Whether to disable the server timing drop-in. Default false. |
| 187 |
*/ |
| 188 |
if ( apply_filters( 'perflab_disable_object_cache_dropin', false ) ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Filters the value of the `object-cache.php` drop-in constant. |
| 194 |
* |
| 195 |
* This filter should not be used outside of tests. |
| 196 |
* |
| 197 |
* @since 3.0.0 |
| 198 |
* @internal |
| 199 |
* |
| 200 |
* @param int|bool $current_dropin_version The drop-in version as defined by the |
| 201 |
* `PERFLAB_OBJECT_CACHE_DROPIN_VERSION` constant. |
| 202 |
*/ |
| 203 |
$current_dropin_version = apply_filters( 'perflab_object_cache_dropin_version', PERFLAB_OBJECT_CACHE_DROPIN_VERSION ); |
| 204 |
|
| 205 |
// Bail if already placed in the latest version or newer. |
| 206 |
if ( null !== $current_dropin_version && $current_dropin_version >= PERFLAB_OBJECT_CACHE_DROPIN_LATEST_VERSION ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// Bail if already attempted before timeout has been completed. |
| 211 |
// This is present in case placing the file fails for some reason, to avoid |
| 212 |
// excessively retrying to place it on every request. |
| 213 |
$timeout = get_transient( 'perflab_set_object_cache_dropin' ); |
| 214 |
if ( false !== $timeout ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if ( $wp_filesystem instanceof WP_Filesystem_Base || true === WP_Filesystem() ) { |
| 219 |
$dropin_path = WP_CONTENT_DIR . '/object-cache.php'; |
| 220 |
|
| 221 |
/* |
| 222 |
* If there is an actual object-cache.php file, it is most likely from |
| 223 |
* a third party, or it may be an older version of the Performance Lab |
| 224 |
* object-cache.php. If it's from a third party, do not replace it. |
| 225 |
* |
| 226 |
* Previous versions of the Performance Lab plugin were renaming the |
| 227 |
* original object-cache.php file and then loading both. However, due |
| 228 |
* to other plugins eagerly checking file headers, this caused too many |
| 229 |
* problems across sites, so it was decided to remove this layer. |
| 230 |
* Only placing the drop-in file if no other one exists yet is the |
| 231 |
* safest solution. |
| 232 |
*/ |
| 233 |
if ( $wp_filesystem->exists( $dropin_path ) ) { |
| 234 |
// If this constant evaluates to `false`, the existing file is for sure from a third party. |
| 235 |
if ( false === $current_dropin_version ) { |
| 236 |
// Set timeout of 1 day before retrying again (only in case the file already exists). |
| 237 |
set_transient( 'perflab_set_object_cache_dropin', true, DAY_IN_SECONDS ); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
// Otherwise, verify that it's actually the Performance Lab drop-in. |
| 242 |
$test_content = "<?php\n/**\n * Plugin Name: Performance Lab Server Timing Object Cache Drop-In\n"; |
| 243 |
if ( ! str_starts_with( $wp_filesystem->get_contents( $dropin_path ), $test_content ) ) { |
| 244 |
// Set timeout of 1 day before retrying again (only in case the file already exists). |
| 245 |
set_transient( 'perflab_set_object_cache_dropin', true, DAY_IN_SECONDS ); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* If this logic is reached, the existing file is an older version |
| 251 |
* of the Performance Lab drop-in, so it can be safely deleted, and |
| 252 |
* then be replaced below. |
| 253 |
*/ |
| 254 |
$wp_filesystem->delete( $dropin_path ); |
| 255 |
} |
| 256 |
|
| 257 |
$wp_filesystem->copy( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php', $dropin_path ); |
| 258 |
} |
| 259 |
|
| 260 |
// Set timeout of 1 hour before retrying again (only relevant in case the above failed). |
| 261 |
set_transient( 'perflab_set_object_cache_dropin', true, HOUR_IN_SECONDS ); |
| 262 |
} |
| 263 |
add_action( 'admin_init', 'perflab_maybe_set_object_cache_dropin' ); |
| 264 |
|
| 265 |
/** |
| 266 |
* Removes the Performance Lab's object cache drop-in from the drop-ins folder. |
| 267 |
* |
| 268 |
* This function should be run on plugin deactivation. For backward compatibility with |
| 269 |
* an earlier implementation of `perflab_maybe_set_object_cache_dropin()`, this function |
| 270 |
* checks whether there is an object-cache-plst-orig.php file, and if so restores it. |
| 271 |
* |
| 272 |
* This function will short-circuit if the constant |
| 273 |
* 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is set as true. |
| 274 |
* |
| 275 |
* @since 1.8.0 |
| 276 |
* |
| 277 |
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| 278 |
*/ |
| 279 |
function perflab_maybe_remove_object_cache_dropin(): void { |
| 280 |
global $wp_filesystem; |
| 281 |
|
| 282 |
// Bail if disabled via constant. |
| 283 |
if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
// Bail if custom drop-in not present anyway. |
| 288 |
if ( ! PERFLAB_OBJECT_CACHE_DROPIN_VERSION ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
if ( $wp_filesystem instanceof WP_Filesystem_Base || true === WP_Filesystem() ) { |
| 293 |
$dropin_path = WP_CONTENT_DIR . '/object-cache.php'; |
| 294 |
$dropin_backup_path = WP_CONTENT_DIR . '/object-cache-plst-orig.php'; |
| 295 |
|
| 296 |
/** |
| 297 |
* If there is an object-cache-plst-orig.php file, restore it and |
| 298 |
* override the Performance Lab file. This is only relevant for |
| 299 |
* backward-compatibility with previous Performance Lab versions |
| 300 |
* which were backing up the file and then loading both. |
| 301 |
* Otherwise, just delete the Performance Lab file. |
| 302 |
*/ |
| 303 |
if ( $wp_filesystem->exists( $dropin_backup_path ) ) { |
| 304 |
$wp_filesystem->move( $dropin_backup_path, $dropin_path, true ); |
| 305 |
} else { |
| 306 |
$wp_filesystem->delete( $dropin_path ); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// Delete transient for drop-in check in case the plugin is reactivated shortly after. |
| 311 |
delete_transient( 'perflab_set_object_cache_dropin' ); |
| 312 |
} |
| 313 |
register_deactivation_hook( __FILE__, 'perflab_maybe_remove_object_cache_dropin' ); |
| 314 |
|
| 315 |
/** |
| 316 |
* Redirects legacy module page to the performance feature page. |
| 317 |
* |
| 318 |
* @since 3.0.0 |
| 319 |
* |
| 320 |
* @global $plugin_page |
| 321 |
*/ |
| 322 |
function perflab_no_access_redirect_module_to_performance_feature_page(): void { |
| 323 |
global $plugin_page; |
| 324 |
|
| 325 |
if ( 'perflab-modules' !== $plugin_page ) { |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
if ( |
| 330 |
current_user_can( 'manage_options' ) && |
| 331 |
wp_safe_redirect( add_query_arg( 'page', PERFLAB_SCREEN ) ) |
| 332 |
) { |
| 333 |
exit; |
| 334 |
} |
| 335 |
} |
| 336 |
add_action( 'admin_page_access_denied', 'perflab_no_access_redirect_module_to_performance_feature_page' ); |
| 337 |
|
| 338 |
/** |
| 339 |
* Cleanup function to delete legacy 'perflab_modules_settings' option if present. |
| 340 |
* |
| 341 |
* @since 3.0.0 |
| 342 |
*/ |
| 343 |
function perflab_cleanup_option(): void { |
| 344 |
if ( current_user_can( 'manage_options' ) ) { |
| 345 |
delete_option( 'perflab_modules_settings' ); |
| 346 |
} |
| 347 |
} |
| 348 |
add_action( 'admin_init', 'perflab_cleanup_option' ); |
| 349 |
|
| 350 |
// Only load admin integration when in admin. |
| 351 |
if ( is_admin() ) { |
| 352 |
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/load.php'; |
| 353 |
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/server-timing.php'; |
| 354 |
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/plugins.php'; |
| 355 |
} |
| 356 |
|
| 357 |
// Load REST API. |
| 358 |
require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/rest-api.php'; |
| 359 |
|