Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facilitate embedding of Embed Optimizer #1337

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

westonruter
Copy link
Member

In line with Optimization Detective and Image Prioritizer, this updates the bootstrap logic for Embed Optimizer so that it can be embedded in a theme or plugin.

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Jul 9, 2024
@westonruter westonruter added this to the embed-optimizer 0.2.0 milestone Jul 9, 2024
Copy link

github-actions bot commented Jul 9, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines +23 to +69
(
/**
* Register this copy of the plugin among other potential copies embedded in plugins or themes.
*
* @param string $global_var_name Global variable name for storing the plugin pending loading.
* @param string $version Version.
* @param Closure $load Callback that loads the plugin.
*/
static function ( string $global_var_name, string $version, Closure $load ): void {
if ( ! isset( $GLOBALS[ $global_var_name ] ) ) {
$bootstrap = static function () use ( $global_var_name ): void {
if (
isset( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] )
&&
$GLOBALS[ $global_var_name ]['load'] instanceof Closure
&&
is_string( $GLOBALS[ $global_var_name ]['version'] )
) {
call_user_func( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] );
unset( $GLOBALS[ $global_var_name ] );
}
};

// Load in the Embed Optimizer plugin hooks.
require_once __DIR__ . '/hooks.php';
// Wait until after the plugins have loaded and the theme has loaded. The after_setup_theme action is used
// because it is the first action that fires once the theme is loaded.
add_action( 'after_setup_theme', $bootstrap, PHP_INT_MIN );
}

// Register this copy of the plugin.
if (
// Register this copy if none has been registered yet.
! isset( $GLOBALS[ $global_var_name ]['version'] )
||
// Or register this copy if the version greater than what is currently registered.
version_compare( $version, $GLOBALS[ $global_var_name ]['version'], '>' )
||
// Otherwise, register this copy if it is actually the one installed in the directory for plugins.
rtrim( WP_PLUGIN_DIR, '/' ) === dirname( __DIR__ )
) {
$GLOBALS[ $global_var_name ]['version'] = $version;
$GLOBALS[ $global_var_name ]['load'] = $load;
}
}
)(
'embed_optimizer_pending_plugin',
'0.1.2',
static function ( string $version ): void {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code is a direct copy of what is in Optimization Detective's load.php.

@westonruter westonruter force-pushed the add/embeddable-embed-optimizer branch from 962a43b to 6911c56 Compare July 9, 2024 20:38
@westonruter westonruter force-pushed the add/embeddable-embed-optimizer branch from 6911c56 to 36604fe Compare July 9, 2024 20:52
@westonruter westonruter changed the base branch from add/od-embed-optimizer to trunk July 9, 2024 20:52
@westonruter westonruter added the [Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) label Jul 9, 2024
@westonruter westonruter merged commit 5bdd006 into trunk Jul 10, 2024
17 checks passed
@westonruter westonruter deleted the add/embeddable-embed-optimizer branch July 10, 2024 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) [Type] Enhancement A suggestion for improvement of an existing feature
2 participants