WordPress/wp-admin/includes/class-wp-ajax-upgrader-skin.php
Dominik Schilling 58d772a02f 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.
Built from https://develop.svn.wordpress.org/trunk@38199


git-svn-id: http://core.svn.wordpress.org/trunk@38140 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-08-04 22:18:30 +00:00

133 lines
3.1 KiB
PHP

<?php
/**
* Upgrader API: WP_Ajax_Upgrader_Skin class
*
* @package WordPress
* @subpackage Upgrader
* @since 4.6.0
*/
/**
* Upgrader Skin for Ajax WordPress upgrades.
*
* This skin is designed to be used for Ajax updates.
*
* @since 4.6.0
*
* @see Automatic_Upgrader_Skin
*/
class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
/**
* Holds the WP_Error object.
*
* @since 4.6.0
* @access protected
* @var null|WP_Error
*/
protected $errors = null;
/**
* Constructor.
*
* @since 4.6.0
* @access public
*
* @param array $args Options for the upgrader, see WP_Upgrader_Skin::__construct().
*/
public function __construct( $args = array() ) {
parent::__construct( $args );
$this->errors = new WP_Error();
}
/**
* Retrieves the list of errors.
*
* @since 4.6.0
* @access public
*
* @return WP_Error Errors during an upgrade.
*/
public function get_errors() {
return $this->errors;
}
/**
* Retrieves a string for error messages.
*
* @since 4.6.0
* @access public
*
* @return string Error messages during an upgrade.
*/
public function get_error_messages() {
$messages = array();
foreach ( $this->errors->get_error_codes() as $error_code ) {
if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
$messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
} else {
$messages[] = $this->errors->get_error_message( $error_code );
}
}
return implode( ', ', $messages );
}
/**
* Stores a log entry for an error.
*
* @since 4.6.0
* @access public
*
* @param string|WP_Error $errors Errors.
*/
public function error( $errors ) {
if ( is_string( $errors ) ) {
$string = $errors;
if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
$string = $this->upgrader->strings[ $string ];
}
if ( false !== strpos( $string, '%' ) ) {
$args = func_get_args();
$args = array_splice( $args, 1 );
if ( ! empty( $args ) ) {
$string = vsprintf( $string, $args );
}
}
// Count existing errors to generate an unique error code.
$errors_count = count( $errors->get_error_codes() );
$this->errors->add( 'unknown_upgrade_error_' . $errors_count + 1 , $string );
} elseif ( is_wp_error( $errors ) ) {
foreach ( $errors->get_error_codes() as $error_code ) {
$this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
}
}
$args = func_get_args();
call_user_func_array( array( $this, 'parent::error' ), $args );
}
/**
* Stores a log entry.
*
* @since 4.6.0
* @access public
*
* @param string|array|WP_Error $data Log entry data.
*/
public function feedback( $data ) {
if ( is_wp_error( $data ) ) {
foreach ( $data->get_error_codes() as $error_code ) {
$this->errors->add( $error_code, $data->get_error_message( $error_code ), $data->get_error_data( $error_code ) );
}
}
$args = func_get_args();
call_user_func_array( array( $this, 'parent::feedback' ), $args );
}
}