| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Optimization Detective |
| 4 |
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/plugins/optimization-detective |
| 5 |
* Description: Provides a framework for leveraging real user metrics to detect optimizations for improving page performance. |
| 6 |
* Requires at least: 6.9 |
| 7 |
* Requires PHP: 7.4 |
| 8 |
* Version: 1.0.0-beta7 |
| 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: optimization-detective |
| 14 |
* |
| 15 |
* @package optimization-detective |
| 16 |
*/ |
| 17 |
|
| 18 |
declare( strict_types = 1 ); |
| 19 |
|
| 20 |
// @codeCoverageIgnoreStart |
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; // Exit if accessed directly. |
| 23 |
} |
| 24 |
// @codeCoverageIgnoreEnd |
| 25 |
|
| 26 |
( |
| 27 |
/** |
| 28 |
* Register this copy of the plugin among other potential copies embedded in plugins or themes. |
| 29 |
* |
| 30 |
* @param string $global_var_name Global variable name for storing the plugin pending loading. |
| 31 |
* @param string $version Version. |
| 32 |
* @param Closure $load Callback that loads the plugin. |
| 33 |
*/ |
| 34 |
static function ( string $global_var_name, string $version, Closure $load ): void { |
| 35 |
if ( ! isset( $GLOBALS[ $global_var_name ] ) ) { |
| 36 |
$bootstrap = static function () use ( $global_var_name ): void { |
| 37 |
if ( |
| 38 |
isset( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ) |
| 39 |
&& |
| 40 |
$GLOBALS[ $global_var_name ]['load'] instanceof Closure |
| 41 |
&& |
| 42 |
is_string( $GLOBALS[ $global_var_name ]['version'] ) |
| 43 |
) { |
| 44 |
call_user_func( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ); |
| 45 |
unset( $GLOBALS[ $global_var_name ] ); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
/* |
| 50 |
* Wait until after the plugins have loaded and the theme has loaded. The after_setup_theme action could be |
| 51 |
* used since it is the first action that fires once the theme is loaded. However, plugins may embed this |
| 52 |
* logic inside a module which initializes even later at the init action. The earliest action that this |
| 53 |
* plugin has hooks for is the init action at the default priority of 10 (which includes the rest_api_init |
| 54 |
* action), so this is why it gets initialized at priority 9. |
| 55 |
*/ |
| 56 |
add_action( 'init', $bootstrap, 9 ); |
| 57 |
} |
| 58 |
|
| 59 |
// Register this copy of the plugin. |
| 60 |
if ( |
| 61 |
// Register this copy if none has been registered yet. |
| 62 |
! isset( $GLOBALS[ $global_var_name ]['version'] ) |
| 63 |
|| |
| 64 |
// Or register this copy if the version greater than what is currently registered. |
| 65 |
version_compare( $version, $GLOBALS[ $global_var_name ]['version'], '>' ) |
| 66 |
|| |
| 67 |
// Otherwise, register this copy if it is actually the one installed in the directory for plugins. |
| 68 |
rtrim( WP_PLUGIN_DIR, '/' ) === dirname( __DIR__ ) |
| 69 |
) { |
| 70 |
$GLOBALS[ $global_var_name ]['version'] = $version; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- It is prefixed. |
| 71 |
$GLOBALS[ $global_var_name ]['load'] = $load; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- It is prefixed. |
| 72 |
} |
| 73 |
} |
| 74 |
)( |
| 75 |
'optimization_detective_pending_plugin', |
| 76 |
'1.0.0-beta7', |
| 77 |
static function ( string $version ): void { |
| 78 |
if ( defined( 'OPTIMIZATION_DETECTIVE_VERSION' ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if ( |
| 83 |
( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) && |
| 84 |
! file_exists( __DIR__ . '/build/web-vitals.asset.php' ) |
| 85 |
) { |
| 86 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 87 |
trigger_error( |
| 88 |
esc_html( |
| 89 |
sprintf( |
| 90 |
/* translators: 1: File path. 2: CLI command. */ |
| 91 |
'[Optimization Detective] ' . __( 'Unable to load %1$s. Please make sure you have run %2$s.', 'optimization-detective' ), |
| 92 |
'build/web-vitals.asset.php', |
| 93 |
'`npm install && npm run build:plugin:optimization-detective`' |
| 94 |
) |
| 95 |
), |
| 96 |
E_USER_ERROR |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
define( 'OPTIMIZATION_DETECTIVE_VERSION', $version ); |
| 101 |
|
| 102 |
require_once __DIR__ . '/helper.php'; |
| 103 |
|
| 104 |
// Core infrastructure classes. |
| 105 |
require_once __DIR__ . '/class-od-data-validation-exception.php'; |
| 106 |
require_once __DIR__ . '/class-od-html-tag-processor.php'; |
| 107 |
require_once __DIR__ . '/class-od-url-metric.php'; |
| 108 |
require_once __DIR__ . '/class-od-element.php'; |
| 109 |
require_once __DIR__ . '/class-od-strict-url-metric.php'; |
| 110 |
require_once __DIR__ . '/class-od-url-metric-group.php'; |
| 111 |
require_once __DIR__ . '/class-od-url-metric-group-collection.php'; |
| 112 |
|
| 113 |
// Storage logic. |
| 114 |
require_once __DIR__ . '/storage/class-od-url-metrics-post-type.php'; |
| 115 |
require_once __DIR__ . '/storage/class-od-storage-lock.php'; |
| 116 |
require_once __DIR__ . '/storage/data.php'; |
| 117 |
require_once __DIR__ . '/storage/class-od-rest-url-metrics-store-endpoint.php'; |
| 118 |
require_once __DIR__ . '/storage/class-od-url-metric-store-request-context.php'; |
| 119 |
|
| 120 |
// Detection logic. |
| 121 |
require_once __DIR__ . '/detection.php'; |
| 122 |
|
| 123 |
// Optimization logic. |
| 124 |
require_once __DIR__ . '/class-od-template-optimization-context.php'; |
| 125 |
require_once __DIR__ . '/class-od-link-collection.php'; |
| 126 |
require_once __DIR__ . '/class-od-tag-visitor-registry.php'; |
| 127 |
require_once __DIR__ . '/class-od-visited-tag-state.php'; |
| 128 |
require_once __DIR__ . '/class-od-tag-visitor-context.php'; |
| 129 |
require_once __DIR__ . '/optimization.php'; |
| 130 |
|
| 131 |
// Add hooks for the above requires. |
| 132 |
require_once __DIR__ . '/hooks.php'; |
| 133 |
|
| 134 |
// Load site health checks. |
| 135 |
require_once __DIR__ . '/site-health.php'; |
| 136 |
} |
| 137 |
); |
| 138 |
|