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: fix the fatal that occurs during plugin update #16279

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 0 additions & 40 deletions packages/autoloader/src/class-plugins-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,44 +156,4 @@ private function get_plugins_activating_via_request() {
public function get_current_plugin_dir() {
return explode( '/', plugin_basename( __FILE__ ) )[0];
}

/**
* Resets the autoloader after a plugin update.
*
* @param bool $response Installation response.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @param array $result Installation result data.
*
* @return bool The passed in $response param.
*/
public function reset_maps_after_update( $response, $hook_extra, $result ) {
global $jetpack_autoloader_latest_version;
global $jetpack_packages_classmap;

if ( isset( $hook_extra['plugin'] ) ) {
$plugin = $hook_extra['plugin'];

if ( ! $this->is_directory_plugin( $plugin ) ) {
// Single-file plugins don't use packages, so bail.
return $response;
}

if ( ! is_plugin_active( $plugin ) ) {
// The updated plugin isn't active, so bail.
return $response;
}

$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . trailingslashit( explode( '/', $plugin )[0] );

if ( is_readable( $plugin_path . 'vendor/autoload_functions.php' ) ) {
// The plugin has a v2.x autoloader, so reset it.
$jetpack_autoloader_latest_version = null;
$jetpack_packages_classmap = array();

require $plugin_path . 'vendor/autoload_packages.php';
}
}

return $response;
}
}
50 changes: 49 additions & 1 deletion packages/autoloader/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,55 @@ function set_up_autoloader() {
if ( empty( $jetpack_packages_classmap ) && $current_autoloader_version === $jetpack_autoloader_latest_version ) {
enqueue_files( $plugins_handler, $version_selector );
$autoloader_handler->update_autoloader_chain();
add_filter( 'upgrader_post_install', array( $plugins_handler, 'reset_maps_after_update' ), 0, 3 );
add_filter( 'upgrader_post_install', __NAMESPACE__ . '\reset_maps_after_update', 0, 3 );
}
}

/**
* Resets the autoloader after a plugin update.
*
* @param bool $response Installation response.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @param array $result Installation result data.
*
* @return bool The passed in $response param.
*/
function reset_maps_after_update( $response, $hook_extra, $result ) {
global $jetpack_autoloader_latest_version;
global $jetpack_packages_classmap;

if ( isset( $hook_extra['plugin'] ) ) {
/*
* $hook_extra['plugin'] is the path to the plugin file relative to the plugins directory:
* https://core.trac.wordpress.org/browser/tags/5.4/src/wp-admin/includes/class-wp-upgrader.php#L701
*/
$plugin = $hook_extra['plugin'];

if ( false === strpos( $plugin, '/', 1 ) ) {
// Single-file plugins don't use packages, so bail.
return $response;
}

if ( ! is_plugin_active( $plugin ) ) {
// The updated plugin isn't active, so bail.
return $response;
}

/*
* $plugin is the path to the plugin file relative to the plugins directory.
* What if this plugin is not in the plugins directory, for example an mu plugin?
*/
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . trailingslashit( explode( '/', $plugin )[0] );

if ( is_readable( $plugin_path . 'vendor/autoload_functions.php' ) ) {
// The plugin has a v2.x autoloader, so reset it.
$jetpack_autoloader_latest_version = null;
$jetpack_packages_classmap = array();

set_up_autoloader();
}
}

return $response;
}