WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/04/2016 10:17:44 PM (3 years ago)
Author:
ocean90
Message:

Upgrade/Install: Make some install/update failures more verbose.

An upgrader class is used in conjunction with an upgrader skin class. A skin class handles the logging for an upgrade and informs a user about the progress and failures.
The current Ajax install/update handlers are using the Automatic_Upgrader_Skin class because during an Ajax request no output is intended. The difference between Ajax updates and automatic updates is that you will see the full log (usually by email) while Ajax updates focus only on success or failure. For that Automatic_Upgrader_Skin has one disadvantage: It doesn't provide a way to retrieve failure messages which were passed through WP_Upgrader_Skin::error() by the upgrader.
To solve this issue a new skin WP_Ajax_Upgrader_Skin has been introduced. The skin extends Automatic_Upgrader_Skin and overrides the error() and feedback() methods to intercept all errors, which can be a WP_Error object or a string.

This updates all four Ajax handler for installing/updating themes/plugins to use the new skin. They now also check the skin for any intercepted errors and pass them on to the user.

Props flixos90, obenland, ocean90.
Props DrewAPicture, pento for review.
Fixes #37531.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r38168 r38199  
    33463346    }
    33473347
    3348     $upgrader = new Theme_Upgrader( new Automatic_Upgrader_Skin() );
     3348    $skin     = new WP_Ajax_Upgrader_Skin();
     3349    $upgrader = new Theme_Upgrader( $skin );
    33493350    $result   = $upgrader->install( $api->download_link );
    33503351
    33513352    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
    3352         $status['debug'] = $upgrader->skin->get_upgrade_messages();
     3353        $status['debug'] = $skin->get_upgrade_messages();
    33533354    }
    33543355
    33553356    if ( is_wp_error( $result ) ) {
     3357        $status['errorCode']    = $result->get_error_code();
    33563358        $status['errorMessage'] = $result->get_error_message();
     3359        wp_send_json_error( $status );
     3360    } elseif ( is_wp_error( $skin->result ) ) {
     3361        $status['errorCode']    = $skin->result->get_error_code();
     3362        $status['errorMessage'] = $skin->result->get_error_message();
     3363        wp_send_json_error( $status );
     3364    } elseif ( $skin->get_errors()->get_error_code() ) {
     3365        $status['errorMessage'] = $skin->get_error_messages();
    33573366        wp_send_json_error( $status );
    33583367    } elseif ( is_null( $result ) ) {
     
    34383447    }
    34393448
    3440     $upgrader = new Theme_Upgrader( new Automatic_Upgrader_Skin() );
     3449    $skin     = new WP_Ajax_Upgrader_Skin();
     3450    $upgrader = new Theme_Upgrader( $skin );
    34413451    $result   = $upgrader->bulk_upgrade( array( $stylesheet ) );
    34423452
    34433453    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
    3444         $status['debug'] = $upgrader->skin->get_upgrade_messages();
    3445     }
    3446 
    3447     if ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) {
     3454        $status['debug'] = $skin->get_upgrade_messages();
     3455    }
     3456
     3457    if ( is_wp_error( $skin->result ) ) {
     3458        $status['errorCode']    = $skin->result->get_error_code();
     3459        $status['errorMessage'] = $skin->result->get_error_message();
     3460        wp_send_json_error( $status );
     3461    } elseif ( $skin->get_errors()->get_error_code() ) {
     3462        $status['errorMessage'] = $skin->get_error_messages();
     3463        wp_send_json_error( $status );
     3464    } elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) {
    34483465
    34493466        // Theme is already at the latest version.
     
    34593476
    34603477        wp_send_json_success( $status );
    3461     } elseif ( is_wp_error( $upgrader->skin->result ) ) {
    3462         $status['errorCode']    = $upgrader->skin->result->get_error_code();
    3463         $status['errorMessage'] = $upgrader->skin->result->get_error_message();
    3464         wp_send_json_error( $status );
    34653478    } elseif ( false === $result ) {
    34663479        global $wp_filesystem;
     
    35953608    $status['pluginName'] = $api->name;
    35963609
    3597     $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
     3610    $skin     = new WP_Ajax_Upgrader_Skin();
     3611    $upgrader = new Plugin_Upgrader( $skin );
    35983612    $result   = $upgrader->install( $api->download_link );
    35993613
    36003614    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
    3601         $status['debug'] = $upgrader->skin->get_upgrade_messages();
     3615        $status['debug'] = $skin->get_upgrade_messages();
    36023616    }
    36033617
    36043618    if ( is_wp_error( $result ) ) {
     3619        $status['errorCode']    = $result->get_error_code();
    36053620        $status['errorMessage'] = $result->get_error_message();
     3621        wp_send_json_error( $status );
     3622    } elseif ( is_wp_error( $skin->result ) ) {
     3623        $status['errorCode']    = $skin->result->get_error_code();
     3624        $status['errorMessage'] = $skin->result->get_error_message();
     3625        wp_send_json_error( $status );
     3626    } elseif ( $skin->get_errors()->get_error_code() ) {
     3627        $status['errorMessage'] = $skin->get_error_messages();
    36063628        wp_send_json_error( $status );
    36073629    } elseif ( is_null( $result ) ) {
     
    36813703    wp_update_plugins();
    36823704
    3683     $skin     = new Automatic_Upgrader_Skin();
     3705    $skin     = new WP_Ajax_Upgrader_Skin();
    36843706    $upgrader = new Plugin_Upgrader( $skin );
    36853707    $result   = $upgrader->bulk_upgrade( array( $plugin ) );
    36863708
    36873709    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
    3688         $status['debug'] = $upgrader->skin->get_upgrade_messages();
    3689     }
    3690 
    3691     if ( is_array( $result ) && empty( $result[ $plugin ] ) && is_wp_error( $skin->result ) ) {
    3692         $result = $skin->result;
    3693     }
    3694 
    3695     if ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
     3710        $status['debug'] = $skin->get_upgrade_messages();
     3711    }
     3712
     3713    if ( is_wp_error( $skin->result ) ) {
     3714        $status['errorCode']    = $skin->result->get_error_code();
     3715        $status['errorMessage'] = $skin->result->get_error_message();
     3716        wp_send_json_error( $status );
     3717    } elseif ( $skin->get_errors()->get_error_code() ) {
     3718        $status['errorMessage'] = $skin->get_error_messages();
     3719        wp_send_json_error( $status );
     3720    } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
    36963721        $plugin_update_data = current( $result );
    36973722
     
    37173742        }
    37183743        wp_send_json_success( $status );
    3719     } elseif ( is_wp_error( $result ) ) {
    3720         $status['errorMessage'] = $result->get_error_message();
    3721         wp_send_json_error( $status );
    37223744    } elseif ( false === $result ) {
    37233745        global $wp_filesystem;
Note: See TracChangeset for help on using the changeset viewer.