| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Plugin Name: Embed Optimizer |
|---|
| 4 | * Plugin URI: https://github.com/WordPress/performance/tree/trunk/plugins/embed-optimizer |
|---|
| 5 | * Description: Optimizes the performance of embeds through lazy-loading, adding dns-prefetch links, and reserving space to reduce layout shifts. |
|---|
| 6 | * Requires at least: 6.6 |
|---|
| 7 | * Requires PHP: 7.2 |
|---|
| 8 | * Version: 1.0.0-beta5 |
|---|
| 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: embed-optimizer |
|---|
| 14 | * |
|---|
| 15 | * @package embed-optimizer |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | // @codeCoverageIgnoreStart |
|---|
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
|---|
| 20 | exit; // Exit if accessed directly. |
|---|
| 21 | } |
|---|
| 22 | // @codeCoverageIgnoreEnd |
|---|
| 23 | |
|---|
| 24 | ( |
|---|
| 25 | /** |
|---|
| 26 | * Register this copy of the plugin among other potential copies embedded in plugins or themes. |
|---|
| 27 | * |
|---|
| 28 | * @param string $global_var_name Global variable name for storing the plugin pending loading. |
|---|
| 29 | * @param string $version Version. |
|---|
| 30 | * @param Closure $load Callback that loads the plugin. |
|---|
| 31 | */ |
|---|
| 32 | static function ( string $global_var_name, string $version, Closure $load ): void { |
|---|
| 33 | if ( ! isset( $GLOBALS[ $global_var_name ] ) ) { |
|---|
| 34 | $bootstrap = static function () use ( $global_var_name ): void { |
|---|
| 35 | if ( |
|---|
| 36 | isset( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ) |
|---|
| 37 | && |
|---|
| 38 | $GLOBALS[ $global_var_name ]['load'] instanceof Closure |
|---|
| 39 | && |
|---|
| 40 | is_string( $GLOBALS[ $global_var_name ]['version'] ) |
|---|
| 41 | ) { |
|---|
| 42 | call_user_func( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ); |
|---|
| 43 | unset( $GLOBALS[ $global_var_name ] ); |
|---|
| 44 | } |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | /* |
|---|
| 48 | * Wait until after the plugins have loaded and the theme has loaded. The after_setup_theme action could be |
|---|
| 49 | * used since it is the first action that fires once the theme is loaded. However, plugins may embed this |
|---|
| 50 | * logic inside a module which initializes even later at the init action. The earliest action that this |
|---|
| 51 | * plugin has hooks for is the init action at the default priority of 10 (which includes the rest_api_init |
|---|
| 52 | * action), so this is why it gets initialized at priority 9. |
|---|
| 53 | */ |
|---|
| 54 | add_action( 'init', $bootstrap, 9 ); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | // Register this copy of the plugin. |
|---|
| 58 | if ( |
|---|
| 59 | // Register this copy if none has been registered yet. |
|---|
| 60 | ! isset( $GLOBALS[ $global_var_name ]['version'] ) |
|---|
| 61 | || |
|---|
| 62 | // Or register this copy if the version greater than what is currently registered. |
|---|
| 63 | version_compare( $version, $GLOBALS[ $global_var_name ]['version'], '>' ) |
|---|
| 64 | || |
|---|
| 65 | // Otherwise, register this copy if it is actually the one installed in the directory for plugins. |
|---|
| 66 | rtrim( WP_PLUGIN_DIR, '/' ) === dirname( __DIR__ ) |
|---|
| 67 | ) { |
|---|
| 68 | $GLOBALS[ $global_var_name ]['version'] = $version; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- It is prefixed. |
|---|
| 69 | $GLOBALS[ $global_var_name ]['load'] = $load; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- It is prefixed. |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | )( |
|---|
| 73 | 'embed_optimizer_pending_plugin', |
|---|
| 74 | '1.0.0-beta5', |
|---|
| 75 | static function ( string $version ): void { |
|---|
| 76 | if ( defined( 'EMBED_OPTIMIZER_VERSION' ) ) { |
|---|
| 77 | return; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | define( 'EMBED_OPTIMIZER_VERSION', $version ); |
|---|
| 81 | |
|---|
| 82 | require_once __DIR__ . '/helper.php'; |
|---|
| 83 | require_once __DIR__ . '/hooks.php'; |
|---|
| 84 | } |
|---|
| 85 | ); |
|---|