From b44313a1d791a9556b1560c930916e11186bb92f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 Apr 2024 16:39:31 -0700 Subject: [PATCH 1/8] Facilitate embedding Speculative Loading in other plugins/themes --- bin/phpstan/constants.php | 9 ++++ phpstan.neon.dist | 2 +- plugins/speculation-rules/load.php | 74 ++++++++++++++++++++++++++---- 3 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 bin/phpstan/constants.php diff --git a/bin/phpstan/constants.php b/bin/phpstan/constants.php new file mode 100644 index 0000000000..f55f8935e2 --- /dev/null +++ b/bin/phpstan/constants.php @@ -0,0 +1,9 @@ +' ) + || + // 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; + } + } +)( + 'plsr_pending_plugin_info', + '1.2.1', + static function ( string $version ) { + + // Define the constant. + if ( defined( 'SPECULATION_RULES_VERSION' ) ) { + return; + } -define( 'SPECULATION_RULES_VERSION', '1.2.1' ); -define( 'SPECULATION_RULES_MAIN_FILE', plugin_basename( __FILE__ ) ); + define( 'SPECULATION_RULES_VERSION', $version ); + define( 'SPECULATION_RULES_MAIN_FILE', plugin_basename( __FILE__ ) ); -require_once __DIR__ . '/class-plsr-url-pattern-prefixer.php'; -require_once __DIR__ . '/helper.php'; -require_once __DIR__ . '/hooks.php'; -require_once __DIR__ . '/settings.php'; + require_once __DIR__ . '/class-plsr-url-pattern-prefixer.php'; + require_once __DIR__ . '/helper.php'; + require_once __DIR__ . '/hooks.php'; + require_once __DIR__ . '/settings.php'; + } +); From 7ae4e449a2804354bdfbfdf24f0a984c86301030 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 18 Apr 2024 09:19:11 -0700 Subject: [PATCH 2/8] Use 0.0.0 as SPECULATION_RULES_VERSION for PHPStan --- bin/phpstan/constants.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/phpstan/constants.php b/bin/phpstan/constants.php index f55f8935e2..71adeef274 100644 --- a/bin/phpstan/constants.php +++ b/bin/phpstan/constants.php @@ -5,5 +5,5 @@ * @package speculation-rules */ -define( 'SPECULATION_RULES_VERSION', '1.2.1' ); +define( 'SPECULATION_RULES_VERSION', '0.0.0' ); define( 'SPECULATION_RULES_MAIN_FILE', 'plugins/speculation-rules/load.php' ); From bdf96a652d3f6de3a4e26cc52055289327c87977 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 18 Apr 2024 11:52:34 -0700 Subject: [PATCH 3/8] Eliminate passing version into load callback --- plugins/speculation-rules/load.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index f8fe0ac64f..b817e033b3 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -32,13 +32,11 @@ static function ( string $global_var_name, string $version, Closure $load ) { if ( ! isset( $GLOBALS[ $global_var_name ] ) ) { $bootstrap = static function () use ( $global_var_name ) { if ( - isset( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ) + isset( $GLOBALS[ $global_var_name ]['load'] ) && $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'] ); + call_user_func( $GLOBALS[ $global_var_name ]['load'] ); unset( $GLOBALS[ $global_var_name ] ); } }; @@ -70,14 +68,14 @@ static function ( string $global_var_name, string $version, Closure $load ) { )( 'plsr_pending_plugin_info', '1.2.1', - static function ( string $version ) { + static function () { // Define the constant. if ( defined( 'SPECULATION_RULES_VERSION' ) ) { return; } - define( 'SPECULATION_RULES_VERSION', $version ); + define( 'SPECULATION_RULES_VERSION', '1.2.1' ); define( 'SPECULATION_RULES_MAIN_FILE', plugin_basename( __FILE__ ) ); require_once __DIR__ . '/class-plsr-url-pattern-prefixer.php'; From 7b3ee06278dd0286aa41d774d8d5a0b5b96117f8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 19 Apr 2024 14:06:05 -0700 Subject: [PATCH 4/8] Set loaded flag to prevent theme from attempting to load --- plugins/speculation-rules/load.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index d68782bd77..0dc0e4a29f 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -34,10 +34,12 @@ static function ( string $global_var_name, string $version, Closure $load ) { if ( isset( $GLOBALS[ $global_var_name ]['load'] ) && + ! isset( $GLOBALS[ $global_var_name ]['loaded'] ) + && $GLOBALS[ $global_var_name ]['load'] instanceof Closure ) { call_user_func( $GLOBALS[ $global_var_name ]['load'] ); - unset( $GLOBALS[ $global_var_name ] ); + $GLOBALS[ $global_var_name ]['loaded'] = true; } }; From 27284ab3b05f1872858333898a9a51eaaf376ee3 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 19 Apr 2024 14:13:34 -0700 Subject: [PATCH 5/8] Delay loading plugin until after_setup_theme --- plugins/speculation-rules/load.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index 0dc0e4a29f..388e83aa0f 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -34,21 +34,14 @@ static function ( string $global_var_name, string $version, Closure $load ) { if ( isset( $GLOBALS[ $global_var_name ]['load'] ) && - ! isset( $GLOBALS[ $global_var_name ]['loaded'] ) - && $GLOBALS[ $global_var_name ]['load'] instanceof Closure ) { call_user_func( $GLOBALS[ $global_var_name ]['load'] ); - $GLOBALS[ $global_var_name ]['loaded'] = true; + unset( $GLOBALS[ $global_var_name ] ); } }; - // Handle either where the plugin is installed as a regular plugin or is embedded in another plugin or in a theme. - if ( ! did_action( 'plugins_loaded' ) ) { - add_action( 'plugins_loaded', $bootstrap, 0 ); - } - - // Handle case where plugin is embedded in a theme. + // Wait until after the plugins have loaded and the theme has loaded. add_action( 'after_setup_theme', $bootstrap, 0 ); } From 224a4715f1117563b3eb36b480b0d6349241f96e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 22 Apr 2024 17:54:47 -0700 Subject: [PATCH 6/8] Explain why after_setup_theme action is used --- plugins/speculation-rules/load.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index 388e83aa0f..6cc342a73c 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -22,7 +22,7 @@ ( /** - * Register this copy of the plugin among other potential copies. + * 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. @@ -41,7 +41,8 @@ static function ( string $global_var_name, string $version, Closure $load ) { } }; - // Wait until after the plugins have loaded and the theme has loaded. + // 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, 0 ); } From ee853513162d7c5e31af946a798ec74874d18eed Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 22 Apr 2024 17:55:14 -0700 Subject: [PATCH 7/8] Use PHP_INT_MIN instead of 0 --- plugins/speculation-rules/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index 6cc342a73c..b78a2fe69b 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -43,7 +43,7 @@ static function ( string $global_var_name, string $version, Closure $load ) { // 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, 0 ); + add_action( 'after_setup_theme', $bootstrap, PHP_INT_MIN ); } // Register this copy of the plugin. From 36fbaf4da0723ce61810fd9507f935e143d6c517 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 22 Apr 2024 18:01:12 -0700 Subject: [PATCH 8/8] Revert "Eliminate passing version into load callback" This reverts commit bdf96a652d3f6de3a4e26cc52055289327c87977. --- plugins/speculation-rules/load.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index b78a2fe69b..f2cf78c498 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -32,11 +32,13 @@ static function ( string $global_var_name, string $version, Closure $load ) { if ( ! isset( $GLOBALS[ $global_var_name ] ) ) { $bootstrap = static function () use ( $global_var_name ) { if ( - isset( $GLOBALS[ $global_var_name ]['load'] ) + 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'] ); + call_user_func( $GLOBALS[ $global_var_name ]['load'], $GLOBALS[ $global_var_name ]['version'] ); unset( $GLOBALS[ $global_var_name ] ); } }; @@ -64,14 +66,14 @@ static function ( string $global_var_name, string $version, Closure $load ) { )( 'plsr_pending_plugin_info', '1.2.2', - static function () { + static function ( string $version ) { // Define the constant. if ( defined( 'SPECULATION_RULES_VERSION' ) ) { return; } - define( 'SPECULATION_RULES_VERSION', '1.2.1' ); + define( 'SPECULATION_RULES_VERSION', $version ); define( 'SPECULATION_RULES_MAIN_FILE', plugin_basename( __FILE__ ) ); require_once __DIR__ . '/class-plsr-url-pattern-prefixer.php';