-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.php
More file actions
539 lines (478 loc) · 16.3 KB
/
load.php
File metadata and controls
539 lines (478 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
* Plugin Name: Performance Lab
* Plugin URI: https://github.com/WordPress/performance
* Description: Performance plugin from the WordPress Performance Team, which is a collection of standalone performance modules.
* Requires at least: 6.1
* Requires PHP: 5.6
* Version: 2.6.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: performance-lab
*
* @package performance-lab
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'PERFLAB_VERSION', '2.6.0' );
define( 'PERFLAB_MAIN_FILE', __FILE__ );
define( 'PERFLAB_PLUGIN_DIR_PATH', plugin_dir_path( PERFLAB_MAIN_FILE ) );
define( 'PERFLAB_MODULES_SETTING', 'perflab_modules_settings' );
define( 'PERFLAB_MODULES_SCREEN', 'perflab-modules' );
// If the constant isn't defined yet, it means the Performance Lab object cache file is not loaded.
if ( ! defined( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION' ) ) {
define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', false );
}
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/class-perflab-server-timing-metric.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/class-perflab-server-timing.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/load.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'server-timing/defaults.php';
/**
* Registers the performance modules setting.
*
* @since 1.0.0
*/
function perflab_register_modules_setting() {
register_setting(
PERFLAB_MODULES_SCREEN,
PERFLAB_MODULES_SETTING,
array(
'type' => 'object',
'sanitize_callback' => 'perflab_sanitize_modules_setting',
'default' => perflab_get_modules_setting_default(),
)
);
}
add_action( 'init', 'perflab_register_modules_setting' );
/**
* Gets the default value for the performance modules setting.
*
* @since 1.0.0
*
* @return array Associative array of module settings keyed by module slug.
*/
function perflab_get_modules_setting_default() {
// Since the default relies on some minimal logic that includes requiring an additional file,
// the result is "cached" in a static variable.
static $default_option = null;
if ( null === $default_option ) {
// To set the default value for which modules are enabled, rely on this generated file.
$default_enabled_modules = require PERFLAB_PLUGIN_DIR_PATH . 'default-enabled-modules.php';
$default_option = array_reduce(
$default_enabled_modules,
static function( $module_settings, $module_dir ) {
$module_settings[ $module_dir ] = array( 'enabled' => true );
return $module_settings;
},
array()
);
}
return $default_option;
}
/**
* Sanitizes the performance modules setting.
*
* @since 1.0.0
*
* @param mixed $value Modules setting value.
* @return array Sanitized modules setting value.
*/
function perflab_sanitize_modules_setting( $value ) {
if ( ! is_array( $value ) ) {
return array();
}
// Ensure that every element is an array with an 'enabled' key.
return array_filter(
array_map(
static function( $module_settings ) {
if ( ! is_array( $module_settings ) ) {
return array();
}
return array_merge(
array( 'enabled' => false ),
$module_settings
);
},
$value
)
);
}
/**
* Gets the performance module settings.
*
* @since 1.0.0
*
* @return array Associative array of module settings keyed by module slug.
*/
function perflab_get_module_settings() {
// Even though a default value is registered for this setting, the default must be explicitly
// passed here, to support scenarios where this function is called before the 'init' action,
// for example when loading the active modules.
$module_settings = (array) get_option( PERFLAB_MODULES_SETTING, perflab_get_modules_setting_default() );
$legacy_module_slugs = array(
'site-health/audit-autoloaded-options' => 'database/audit-autoloaded-options',
'site-health/audit-enqueued-assets' => 'js-and-css/audit-enqueued-assets',
'site-health/webp-support' => 'images/webp-support',
'images/dominant-color' => 'images/dominant-color-images',
);
foreach ( $legacy_module_slugs as $legacy_slug => $current_slug ) {
if ( isset( $module_settings[ $legacy_slug ] ) ) {
$module_settings[ $current_slug ] = $module_settings[ $legacy_slug ];
unset( $module_settings[ $legacy_slug ] );
}
}
return $module_settings;
}
/**
* Gets the active performance modules.
*
* @since 1.0.0
*
* @return array List of active module slugs.
*/
function perflab_get_active_modules() {
$modules = array_keys(
array_filter(
perflab_get_module_settings(),
static function( $module_settings ) {
return isset( $module_settings['enabled'] ) && $module_settings['enabled'];
}
)
);
/**
* Filters active modules to allow programmatically control which modules are active.
*
* @since 1.0.0
*
* @param array $modules An array of the currently active modules.
*/
$modules = apply_filters( 'perflab_active_modules', $modules );
return $modules;
}
/**
* Gets the active and valid performance modules.
*
* @since 1.3.0
* @since 2.2.0 Adds an additional check for standalone plugins.
*
* @param string $module Slug of the module.
* @return bool True if the module is active and valid, otherwise false.
*/
function perflab_is_valid_module( $module ) {
if ( empty( $module ) ) {
return false;
}
// Do not load the module if it can be loaded by a separate plugin.
if ( perflab_is_standalone_plugin_loaded( $module ) ) {
return false;
}
// Do not load module if no longer exists.
$module_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/load.php';
if ( ! file_exists( $module_file ) ) {
return false;
}
// Do not load module if it cannot be loaded, e.g. if it was already merged and is available in WordPress core.
return perflab_can_load_module( $module );
}
/**
* Gets the content attribute for the generator tag for the Performance Lab plugin.
*
* This attribute is then used in {@see perflab_render_generator()}.
*
* @since 1.1.0
*/
function perflab_get_generator_content() {
$active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
return sprintf(
'Performance Lab %1$s; modules: %2$s',
PERFLAB_VERSION,
implode( ', ', $active_and_valid_modules )
);
}
/**
* Displays the HTML generator tag for the Performance Lab plugin.
*
* See {@see 'wp_head'}.
*
* @since 1.1.0
*/
function perflab_render_generator() {
$content = perflab_get_generator_content();
echo '<meta name="generator" content="' . esc_attr( $content ) . '">' . "\n";
}
add_action( 'wp_head', 'perflab_render_generator' );
/**
* Checks whether the given module can be loaded in the current environment.
*
* @since 1.3.0
*
* @param string $module Slug of the module.
* @return bool Whether the module can be loaded or not.
*/
function perflab_can_load_module( $module ) {
$module_load_file = PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/can-load.php';
// If the `can-load.php` file does not exist, assume the module can be loaded.
if ( ! file_exists( $module_load_file ) ) {
return true;
}
// Require the file to get the closure for whether the module can load.
$module = require $module_load_file;
// If the `can-load.php` file is invalid and does not return a closure, assume the module can be loaded.
if ( ! is_callable( $module ) ) {
return true;
}
// Call the closure to determine whether the module can be loaded.
return (bool) $module();
}
/**
* Checks whether the given module has already been loaded by a separate plugin.
*
* @since 2.2.0
*
* @param string $module Slug of the module.
* @return bool Whether the module has already been loaded by a separate plugin.
*/
function perflab_is_standalone_plugin_loaded( $module ) {
$standalone_plugins_constants = perflab_get_standalone_plugins_constants();
if (
isset( $standalone_plugins_constants[ $module ] ) &&
defined( $standalone_plugins_constants[ $module ] ) &&
! str_starts_with( constant( $standalone_plugins_constants[ $module ] ), 'Performance Lab ' )
) {
return true;
}
return false;
}
/**
* Gets the standalone plugin constants used for each module / plugin.
*
* @since 2.2.0
*
* @return array Map of module path to version constant used.
*/
function perflab_get_standalone_plugins_constants() {
return array(
'images/dominant-color-images' => 'DOMINANT_COLOR_IMAGES_VERSION',
'images/fetchpriority' => 'FETCHPRIORITY_VERSION',
'images/webp-uploads' => 'WEBP_UPLOADS_VERSION',
);
}
/**
* Loads the active and valid performance modules.
*
* @since 1.0.0
* @since 1.3.0 Renamed to perflab_load_active_and_valid_modules().
*/
function perflab_load_active_and_valid_modules() {
$active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
foreach ( $active_and_valid_modules as $module ) {
require_once PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module . '/load.php';
}
}
add_action( 'plugins_loaded', 'perflab_load_active_and_valid_modules' );
/**
* Places the Performance Lab's object cache drop-in in the drop-ins folder.
*
* This only runs in WP Admin to not have any potential performance impact on
* the frontend.
*
* This function will short-circuit if at least one of the constants
* 'PERFLAB_DISABLE_SERVER_TIMING' or 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is
* set as true.
*
* @since 1.8.0
* @since 2.1.0 No longer attempts to use two of the drop-ins together.
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*/
function perflab_maybe_set_object_cache_dropin() {
global $wp_filesystem;
// Bail if Server-Timing is disabled entirely.
if ( defined( 'PERFLAB_DISABLE_SERVER_TIMING' ) && PERFLAB_DISABLE_SERVER_TIMING ) {
return;
}
// Bail if disabled via constant.
if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) {
return;
}
// Bail if already placed.
if ( PERFLAB_OBJECT_CACHE_DROPIN_VERSION ) {
return;
}
/**
* Filters whether the Perflab server timing drop-in should be set.
*
* @since 2.0.0
*
* @param bool $disabled Whether to disable the server timing drop-in. Default false.
*/
if ( apply_filters( 'perflab_disable_object_cache_dropin', false ) ) {
return;
}
// Bail if already attempted before timeout has been completed.
// This is present in case placing the file fails for some reason, to avoid
// excessively retrying to place it on every request.
$timeout = get_transient( 'perflab_set_object_cache_dropin' );
if ( false !== $timeout ) {
return;
}
if ( $wp_filesystem || WP_Filesystem() ) {
$dropin_path = WP_CONTENT_DIR . '/object-cache.php';
/**
* If there is an actual object-cache.php file, do not replace it.
* Previous versions of the Performance Lab plugin were renaming the
* original object-cache.php file and then loading both. However, due
* to other plugins eagerly checking file headers, this caused too many
* problems across sites so it was decided to remove this layer.
* Only placing the drop-in file if no other one exists yet is the
* safest solution.
*/
if ( $wp_filesystem->exists( $dropin_path ) ) {
// Set timeout of 1 day before retrying again (only in case the file already exists).
set_transient( 'perflab_set_object_cache_dropin', true, DAY_IN_SECONDS );
return;
}
$wp_filesystem->copy( PERFLAB_PLUGIN_DIR_PATH . 'server-timing/object-cache.copy.php', $dropin_path );
}
// Set timeout of 1 hour before retrying again (only relevant in case the above failed).
set_transient( 'perflab_set_object_cache_dropin', true, HOUR_IN_SECONDS );
}
add_action( 'admin_init', 'perflab_maybe_set_object_cache_dropin' );
/**
* Removes the Performance Lab's object cache drop-in from the drop-ins folder.
*
* This function should be run on plugin deactivation. For backward compatibility with
* an earlier implementation of `perflab_maybe_set_object_cache_dropin()`, this function
* checks whether there is an object-cache-plst-orig.php file, and if so restores it.
*
* This function will short-circuit if the constant
* 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' is set as true.
*
* @since 1.8.0
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*/
function perflab_maybe_remove_object_cache_dropin() {
global $wp_filesystem;
// Bail if disabled via constant.
if ( defined( 'PERFLAB_DISABLE_OBJECT_CACHE_DROPIN' ) && PERFLAB_DISABLE_OBJECT_CACHE_DROPIN ) {
return;
}
// Bail if custom drop-in not present anyway.
if ( ! PERFLAB_OBJECT_CACHE_DROPIN_VERSION ) {
return;
}
if ( $wp_filesystem || WP_Filesystem() ) {
$dropin_path = WP_CONTENT_DIR . '/object-cache.php';
$dropin_backup_path = WP_CONTENT_DIR . '/object-cache-plst-orig.php';
/**
* If there is an object-cache-plst-orig.php file, restore it and
* override the Performance Lab file. This is only relevant for
* backward-compatibility with previous Performance Lab versions
* which were backing up the file and then loading both.
* Otherwise just delete the Performance Lab file.
*/
if ( $wp_filesystem->exists( $dropin_backup_path ) ) {
$wp_filesystem->move( $dropin_backup_path, $dropin_path, true );
} else {
$wp_filesystem->delete( $dropin_path );
}
}
// Delete transient for drop-in check in case the plugin is reactivated shortly after.
delete_transient( 'perflab_set_object_cache_dropin' );
}
register_deactivation_hook( __FILE__, 'perflab_maybe_remove_object_cache_dropin' );
// Only load admin integration when in admin.
if ( is_admin() ) {
require_once PERFLAB_PLUGIN_DIR_PATH . 'admin/load.php';
require_once PERFLAB_PLUGIN_DIR_PATH . 'admin/server-timing.php';
}
/**
* Trigger actions when a module gets activated or deactivated.
*
* @since 1.8.0
*
* @param mixed $old_value Old value of the option.
* @param mixed $value New value of the option.
*/
function perflab_run_module_activation_deactivation( $old_value, $value ) {
$old_value = (array) $old_value;
$value = (array) $value;
// Get the list of modules that were activated, and load the activate.php files if they exist.
if ( ! empty( $value ) ) {
foreach ( $value as $module => $module_settings ) {
if ( ! empty( $module_settings['enabled'] ) && ( empty( $old_value[ $module ] ) || empty( $old_value[ $module ]['enabled'] ) ) ) {
perflab_activate_module( PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module );
}
}
}
// Get the list of modules that were deactivated, and load the deactivate.php files if they exist.
if ( ! empty( $old_value ) ) {
foreach ( $old_value as $module => $module_settings ) {
if ( ! empty( $module_settings['enabled'] ) && ( empty( $value[ $module ] ) || empty( $value[ $module ]['enabled'] ) ) ) {
perflab_deactivate_module( PERFLAB_PLUGIN_DIR_PATH . 'modules/' . $module );
}
}
}
return $value;
}
/**
* Activate a module.
*
* Runs the activate.php file if it exists.
*
* @since 1.8.0
*
* @param string $module_dir_path The module's directory path.
*/
function perflab_activate_module( $module_dir_path ) {
$module_activation_file = $module_dir_path . '/activate.php';
if ( ! file_exists( $module_activation_file ) ) {
return;
}
$module = require $module_activation_file;
if ( ! is_callable( $module ) ) {
return;
}
$module();
}
/**
* Deactivate a module.
*
* Runs the deactivate.php file if it exists.
*
* @since 1.8.0
*
* @param string $module_dir_path The module's directory path.
*/
function perflab_deactivate_module( $module_dir_path ) {
$module_deactivation_file = $module_dir_path . '/deactivate.php';
if ( ! file_exists( $module_deactivation_file ) ) {
return;
}
$module = require $module_deactivation_file;
if ( ! is_callable( $module ) ) {
return;
}
$module();
}
// Run the module activation & deactivation actions when the option is updated.
add_action( 'update_option_' . PERFLAB_MODULES_SETTING, 'perflab_run_module_activation_deactivation', 10, 2 );
// Run the module activation & deactivation actions when the option is added.
add_action(
'add_option_' . PERFLAB_MODULES_SETTING,
/**
* Fires after the option has been added.
*
* @param string $option Name of the option to add.
* @param mixed $value Value of the option.
*/
static function( $option, $value ) {
perflab_run_module_activation_deactivation( perflab_get_modules_setting_default(), $value );
},
10,
2
);