class-wp-ajax-upgrader-skin.php 3.18 KB
Newer Older
Pham Huy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?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
	 * @var null|WP_Error
	 */
	protected $errors = null;

	/**
	 * Constructor.
	 *
	 * @since 4.6.0
	 *
	 * @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
	 *
	 * @return WP_Error Errors during an upgrade.
	 */
	public function get_errors() {
		return $this->errors;
	}

	/**
	 * Retrieves a string for error messages.
	 *
	 * @since 4.6.0
	 *
	 * @return string Error messages during an upgrade.
	 */
	public function get_error_messages() {
		$messages = array();

		foreach ( $this->errors->get_error_codes() as $error_code ) {
			$error_data = $this->errors->get_error_data( $error_code );

			if ( $error_data && is_string( $error_data ) ) {
				$messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
			} else {
				$messages[] = $this->errors->get_error_message( $error_code );
			}
		}

		return implode( ', ', $messages );
	}

	/**
	 * Stores a log entry for an error.
	 *
	 * @since 4.6.0
Pham Huy committed
80 81
	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
	 *              to the function signature.
Pham Huy committed
82
	 *
Pham Huy committed
83 84
	 * @param string|WP_Error $errors  Errors.
	 * @param mixed           ...$args Optional text replacements.
Pham Huy committed
85
	 */
Pham Huy committed
86
	public function error( $errors, ...$args ) {
Pham Huy committed
87 88 89 90 91 92 93 94 95 96 97 98
		if ( is_string( $errors ) ) {
			$string = $errors;
			if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
				$string = $this->upgrader->strings[ $string ];
			}

			if ( false !== strpos( $string, '%' ) ) {
				if ( ! empty( $args ) ) {
					$string = vsprintf( $string, $args );
				}
			}

Pham Huy committed
99
			// Count existing errors to generate a unique error code.
Pham Huy committed
100
			$errors_count = count( $this->errors->get_error_codes() );
Pham Huy committed
101
			$this->errors->add( 'unknown_upgrade_error_' . ( $errors_count + 1 ), $string );
Pham Huy committed
102 103 104 105 106 107
		} 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 ) );
			}
		}

Pham Huy committed
108
		parent::error( $errors, ...$args );
Pham Huy committed
109 110 111 112 113 114
	}

	/**
	 * Stores a log entry.
	 *
	 * @since 4.6.0
Pham Huy committed
115 116
	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
	 *              to the function signature.
Pham Huy committed
117
	 *
Pham Huy committed
118 119
	 * @param string|array|WP_Error $data    Log entry data.
	 * @param mixed                 ...$args Optional text replacements.
Pham Huy committed
120
	 */
Pham Huy committed
121
	public function feedback( $data, ...$args ) {
Pham Huy committed
122 123 124 125 126 127
		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 ) );
			}
		}

Pham Huy committed
128
		parent::feedback( $data, ...$args );
Pham Huy committed
129 130
	}
}