diff --git a/projects/plugins/beta/changelog/fix-jetpack-beta-plugin-installation b/projects/plugins/beta/changelog/fix-jetpack-beta-plugin-installation new file mode 100644 index 0000000000000..77aaedcd2cc90 --- /dev/null +++ b/projects/plugins/beta/changelog/fix-jetpack-beta-plugin-installation @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Use WordPress core's `Plugin_Upgrader` to install plugins, as it handles edge cases better. diff --git a/projects/plugins/beta/src/class-plugin.php b/projects/plugins/beta/src/class-plugin.php index 32d9de1eaebad..26a5d752d2156 100644 --- a/projects/plugins/beta/src/class-plugin.php +++ b/projects/plugins/beta/src/class-plugin.php @@ -9,6 +9,8 @@ use Composer\Semver\Comparator as Semver; use InvalidArgumentException; +use Plugin_Upgrader; +use WP_Ajax_Upgrader_Skin; use WP_Error; /** @@ -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: %1$s - 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;