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

autoloader: Preload classes on plugin upgrade to avoid mid-upgrade fatals #23039

Merged
merged 13 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

On plugin update, pre-load all (non-PSR-4) classes from the plugin to avoid mid-upgrade fatals.
2 changes: 1 addition & 1 deletion projects/packages/autoloader/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"link-template": "https://github.com/Automattic/jetpack-autoloader/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-master": "2.10.x-dev"
"dev-master": "2.11.x-dev"
}
}
}
7 changes: 7 additions & 0 deletions projects/packages/autoloader/src/class-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public static function init( $container = null ) {
// Register a shutdown handler to clean up the autoloader.
$hook_manager->add_action( 'shutdown', new Shutdown_Handler( $plugins_handler, $cached_plugins, $was_included_by_autoloader ) );

// Register an upgrade handler to handle plugin upgrades.
$upgrade_handler = new Upgrade_Handler();
$hook_manager->add_filter( 'upgrader_pre_install', array( $upgrade_handler, 'upgrader_pre_install' ), 10, 2 );
// wp-cli never fires `delete_plugin` but always fires `pre_uninstall_plugin`, while core only conditionally fires `pre_uninstall_plugin` but always fires `delete_plugin`.
$hook_manager->add_action( 'delete_plugin', array( $upgrade_handler, 'delete_plugin' ), 10, 1 );
$hook_manager->add_action( 'pre_uninstall_plugin', array( $upgrade_handler, 'delete_plugin' ), 10, 1 );

// phpcs:enable Generic.Commenting.DocComment.MissingShort
}
}
1 change: 1 addition & 0 deletions projects/packages/autoloader/src/class-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private function register_dependencies() {
// Register any classes that we will use elsewhere.
require_once __DIR__ . '/class-version-loader.php';
require_once __DIR__ . '/class-shutdown-handler.php';
require_once __DIR__ . '/class-upgrade-handler.php';
}

/**
Expand Down
52 changes: 52 additions & 0 deletions projects/packages/autoloader/src/class-upgrade-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/* HEADER */ // phpcs:ignore

/**
* This class handles plugin upgrades and uninstalls.
*/
class Upgrade_Handler {

/**
* Handles a plugin upgrade.
*
* @param bool|WP_Error $response Response.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @return bool|WP_Error Response
*/
public function upgrader_pre_install( $response, $hook_extra ) {
if ( isset( $hook_extra['plugin'] ) ) {
$this->load_plugin_classes( $hook_extra['plugin'] );
}
return $response;
}

/**
* Handles a plugin deletion.
*
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
*/
public function delete_plugin( $plugin_file ) {
$this->load_plugin_classes( $plugin_file );
}

/**
* Load classes for a plugin.
*
* The intention here is to avoid errors on upgrade when a file is deleted,
* by loading everything the autoloader knows about before the upgrade
* happens.
*
* Note this can't load classes autoloaded by PSR-0 or PSR-4 unless an
* optimized autoloader was used.
*
* See p9dueE-4c4-p2 for internal discussion.
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
*/
private function load_plugin_classes( $plugin ) {
global $jetpack_autoloader_loader;
if ( isset( $jetpack_autoloader_loader ) && defined( 'WP_PLUGIN_DIR' ) ) {
$jetpack_autoloader_loader->load_classes_in_path( WP_PLUGIN_DIR . '/' . dirname( $plugin ) );
}
}
}
17 changes: 17 additions & 0 deletions projects/packages/autoloader/src/class-version-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ public function load_filemap() {
}
}

/**
* Load all of the (non-PSR-4) classes located under a path.
*
* @param string $path Path.
*/
public function load_classes_in_path( $path ) {
$path = rtrim( $path, '/' ) . '/';
$l = strlen( $path );
foreach ( $this->classmap as $classname => $data ) {
if ( substr( $data['path'], 0, $l ) === $path && file_exists( $data['path'] ) ) {
// Use the autoloader instead of loading directly in case there's a PSR-4 class or
// something masking it.
class_exists( $classname, true );
}
}
}

/**
* Compares different class sources and returns the newest.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Updated package dependencies.
2 changes: 1 addition & 1 deletion projects/plugins/backup/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"automattic/jetpack-assets": "1.17.x-dev",
"automattic/jetpack-admin-ui": "0.2.x-dev",
"automattic/jetpack-autoloader": "2.10.x-dev",
"automattic/jetpack-autoloader": "2.11.x-dev",
"automattic/jetpack-backup": "1.2.x-dev",
"automattic/jetpack-composer-plugin": "1.1.x-dev",
"automattic/jetpack-config": "1.6.x-dev",
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Updated package dependencies.
2 changes: 1 addition & 1 deletion projects/plugins/beta/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"issues": "https://github.com/Automattic/jetpack/issues"
},
"require": {
"automattic/jetpack-autoloader": "2.10.x-dev",
"automattic/jetpack-autoloader": "2.11.x-dev",
"composer/semver": "3.2.7",
"erusev/parsedown": "1.7.4"
},
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/beta/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Updated package dependencies.
2 changes: 1 addition & 1 deletion projects/plugins/boost/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ext-json": "*",
"automattic/jetpack-assets": "1.17.x-dev",
"automattic/jetpack-admin-ui": "0.2.x-dev",
"automattic/jetpack-autoloader": "2.10.x-dev",
"automattic/jetpack-autoloader": "2.11.x-dev",
"automattic/jetpack-composer-plugin": "1.1.x-dev",
"automattic/jetpack-config": "1.6.x-dev",
"automattic/jetpack-connection": "1.37.x-dev",
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/boost/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Updated package dependencies.
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"automattic/jetpack-a8c-mc-stats": "1.4.x-dev",
"automattic/jetpack-abtest": "1.9.x-dev",
"automattic/jetpack-assets": "1.17.x-dev",
"automattic/jetpack-autoloader": "2.10.x-dev",
"automattic/jetpack-autoloader": "2.11.x-dev",
"automattic/jetpack-backup": "1.2.x-dev",
"automattic/jetpack-blocks": "1.4.x-dev",
"automattic/jetpack-compat": "1.6.x-dev",
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Updated package dependencies.
2 changes: 1 addition & 1 deletion projects/plugins/vaultpress/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"require": {
"automattic/jetpack-logo": "1.5.x-dev",
"automattic/jetpack-autoloader": "2.10.x-dev"
"automattic/jetpack-autoloader": "2.11.x-dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.0.3",
Expand Down
6 changes: 3 additions & 3 deletions projects/plugins/vaultpress/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tools/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,8 @@ async function buildProject( t ) {
}

// HACK: Create stubs to avoid upgrade errors. See https://github.com/Automattic/jetpack/pull/22431.
// Ideally we'll have fixed the upgrade errors by the time something else breaks, in which case this should be removed instead of extended.
// This should no longer be needed for upgrades when jetpack-autoloader 2.11+ is in use, but we should probably
// keep it around for a few versions for people still upgrading from older versions.
if ( t.project === 'plugins/jetpack' || t.project === 'plugins/backup' ) {
t.output( '\n=== Stubbing old vendor files for backward compatibility ===\n\n' );
const files = [
Expand Down