Skip to content

Commit

Permalink
Jetpack Beta: Use WP core Plugin_Upgrader to install plugins (#33216)
Browse files Browse the repository at this point in the history
WordPress core's `Plugin_Upgrader` class can be passed a URL to download
and install, and the zips we're installing are structured properly.
Let's use that instead of rolling our own logic that doesn't handle as
many edge cases.
  • Loading branch information
anomiex authored Sep 20, 2023
1 parent 9d9143b commit ce11df0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Use WordPress core's `Plugin_Upgrader` to install plugins, as it handles edge cases better.
46 changes: 23 additions & 23 deletions projects/plugins/beta/src/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Composer\Semver\Comparator as Semver;
use InvalidArgumentException;
use Plugin_Upgrader;
use WP_Ajax_Upgrader_Skin;
use WP_Error;

/**
Expand Down Expand Up @@ -838,35 +840,33 @@ private function get_which_and_info( $source, $id ) {
* @return null|WP_Error
*/
private function install( $which, $info ) {
// Download the required version of the plugin.
$temp_path = download_url( $info->download_url );
if ( is_wp_error( $temp_path ) ) {
return new WP_Error(
'download_error',
// translators: %1$s: download url, %2$s: error message.
sprintf( __( 'Error Downloading: <a href="%1$s">%1$s</a> - Error: %2$s', 'jetpack-beta' ), $info->download_url, $temp_path->get_error_message() )
);
}

// Init the WP_Filesystem API.
require_once ABSPATH . 'wp-admin/includes/file.php';
$creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
if ( ! WP_Filesystem( $creds ) ) {
return new WP_Error( 'fs_api_error', __( 'Jetpack Beta: No File System access', 'jetpack-beta' ) );
}
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$upgrader->init();
$result = $upgrader->install(
$info->download_url,
array(
'overwrite_package' => true,
)
);

// Unzip the downloaded plugin.
global $wp_filesystem;
$plugin_path = str_replace( ABSPATH, $wp_filesystem->abspath(), WP_PLUGIN_DIR );
$result = unzip_file( $temp_path, $plugin_path );
if ( is_wp_error( $result ) ) {
// translators: %1$s: error message.
return new WP_Error( 'unzip_error', sprintf( __( 'Error Unziping file: Error: %1$s', 'jetpack-beta' ), $result->get_error_message() ) );
return $result;
}
$errors = $upgrader->skin->get_errors();
if ( is_wp_error( $errors ) && $errors->get_error_code() ) {
return $errors;
}
if ( $result === false ) {
return new WP_Error( 'install_error', __( 'There was an error installing your plugin.', 'jetpack-beta' ) );
}

// Record the source info, if it's a dev version.
if ( 'dev' === $which ) {
$wp_filesystem->put_contents( "$plugin_path/{$this->dev_plugin_slug()}/.jpbeta.json", wp_json_encode( $info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
global $wp_filesystem; // Should have been set up by the upgrader already.
$wp_filesystem->put_contents( WP_PLUGIN_DIR . '/' . $this->dev_plugin_slug() . '/.jpbeta.json', wp_json_encode( $info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
}

return null;
Expand Down

0 comments on commit ce11df0

Please sign in to comment.