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

Block Directory: Activate deactivated blocks if already installed #22286

Merged
merged 4 commits into from
May 21, 2020
Merged
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
63 changes: 32 additions & 31 deletions lib/class-wp-rest-block-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,46 @@ public function install_block( $request ) {
)
);

if ( is_wp_error( $api ) ) {
return WP_Error( $api->get_error_code(), $api->get_error_message() );
}
// Check if the plugin is already installed.
$installed_plugins = get_plugins( '/' . $api->slug );

$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
if ( empty( $installed_plugins ) ) {

$filesystem_method = get_filesystem_method();
if ( is_wp_error( $api ) ) {
return WP_Error( $api->get_error_code(), $api->get_error_message() );
}

if ( 'direct' !== $filesystem_method ) {
return WP_Error( null, 'Only direct FS_METHOD is supported.' );
}
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );

$result = $upgrader->install( $api->download_link );
$filesystem_method = get_filesystem_method();

if ( is_wp_error( $result ) ) {
return WP_Error( $result->get_error_code(), $result->get_error_message() );
}
if ( 'direct' !== $filesystem_method ) {
return WP_Error( null, 'Only direct FS_METHOD is supported.' );
}

if ( is_wp_error( $skin->result ) ) {
return WP_Error( $skin->$result->get_error_code(), $skin->$result->get_error_message() );
}
$result = $upgrader->install( $api->download_link );

if ( $skin->get_errors()->has_errors() ) {
return WP_Error( $skin->$result->get_error_code(), $skin->$result->get_error_messages() );
}
if ( is_wp_error( $result ) ) {
return WP_Error( $result->get_error_code(), $result->get_error_message() );
}

if ( is_wp_error( $skin->result ) ) {
return WP_Error( $skin->$result->get_error_code(), $skin->$result->get_error_message() );
}

if ( is_null( $result ) ) {
global $wp_filesystem;
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return WP_Error( 'unable_to_connect_to_filesystem', esc_html( $wp_filesystem->errors->get_error_message() ) );
if ( $skin->get_errors()->has_errors() ) {
return WP_Error( $skin->$result->get_error_code(), $skin->$result->get_error_messages() );
}

if ( is_null( $result ) ) {
global $wp_filesystem;
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return WP_Error( 'unable_to_connect_to_filesystem', esc_html( $wp_filesystem->errors->get_error_message() ) );
}
return WP_Error( 'unable_to_connect_to_filesystem', __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'gutenberg' ) );
}
return WP_Error( 'unable_to_connect_to_filesystem', __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'gutenberg' ) );
}

$install_status = install_plugin_install_status( $api );
Expand Down Expand Up @@ -251,12 +257,7 @@ public function get_items( $request ) {
$result = array();

foreach ( $response['plugins'] as $plugin ) {
$installed_plugins = get_plugins( '/' . $plugin['slug'] );

// Only show uninstalled blocks.
if ( empty( $installed_plugins ) ) {
$result[] = self::parse_block_metadata( $plugin );
}
$result[] = self::parse_block_metadata( $plugin );
}

return rest_ensure_response( $result );
Expand Down