1<?php
2/**
3 * Upgrader API: WP_Ajax_Upgrader_Skin class
4 *
5 * @package WordPress
6 * @subpackage Upgrader
7 * @since 4.6.0
8 */
9
10/**
11 * Upgrader Skin for Ajax WordPress upgrades.
12 *
13 * This skin is designed to be used for Ajax updates.
14 *
15 * @since 4.6.0
16 *
17 * @see Automatic_Upgrader_Skin
18 */
19class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
20
21 /**
22 * Plugin info.
23 *
24 * The Plugin_Upgrader::bulk_upgrade() method will fill this in
25 * with info retrieved from the get_plugin_data() function.
26 *
27 * @var array Plugin data. Values will be empty if not supplied by the plugin.
28 */
29 public $plugin_info = array();
30
31 /**
32 * Theme info.
33 *
34 * The Theme_Upgrader::bulk_upgrade() method will fill this in
35 * with info retrieved from the Theme_Upgrader::theme_info() method,
36 * which in turn calls the wp_get_theme() function.
37 *
38 * @var WP_Theme|false The theme's info object, or false.
39 */
40 public $theme_info = false;
41
42 /**
43 * Holds the WP_Error object.
44 *
45 * @since 4.6.0
46 *
47 * @var null|WP_Error
48 */
49 protected $errors = null;
50
51 /**
52 * Constructor.
53 *
54 * Sets up the WordPress Ajax upgrader skin.
55 *
56 * @since 4.6.0
57 *
58 * @see WP_Upgrader_Skin::__construct()
59 *
60 * @param array $args Optional. The WordPress Ajax upgrader skin arguments to
61 * override default options. See WP_Upgrader_Skin::__construct().
62 * Default empty array.
63 */
64 public function __construct( $args = array() ) {
65 parent::__construct( $args );
66
67 $this->errors = new WP_Error();
68 }
69
70 /**
71 * Retrieves the list of errors.
72 *
73 * @since 4.6.0
74 *
75 * @return WP_Error Errors during an upgrade.
76 */
77 public function get_errors() {
78 return $this->errors;
79 }
80
81 /**
82 * Retrieves a string for error messages.
83 *
84 * @since 4.6.0
85 *
86 * @return string Error messages during an upgrade.
87 */
88 public function get_error_messages() {
89 $messages = array();
90
91 foreach ( $this->errors->get_error_codes() as $error_code ) {
92 $error_data = $this->errors->get_error_data( $error_code );
93
94 if ( $error_data && is_string( $error_data ) ) {
95 $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
96 } else {
97 $messages[] = $this->errors->get_error_message( $error_code );
98 }
99 }
100
101 return implode( ', ', $messages );
102 }
103
104 /**
105 * Stores an error message about the upgrade.
106 *
107 * @since 4.6.0
108 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
109 * to the function signature.
110 *
111 * @param string|WP_Error $errors Errors.
112 * @param mixed ...$args Optional text replacements.
113 */
114 public function error( $errors, ...$args ) {
115 if ( is_string( $errors ) ) {
116 $string = $errors;
117 if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
118 $string = $this->upgrader->strings[ $string ];
119 }
120
121 if ( str_contains( $string, '%' ) ) {
122 if ( ! empty( $args ) ) {
123 $string = vsprintf( $string, $args );
124 }
125 }
126
127 // Count existing errors to generate a unique error code.
128 $errors_count = count( $this->errors->get_error_codes() );
129 $this->errors->add( 'unknown_upgrade_error_' . ( $errors_count + 1 ), $string );
130 } elseif ( is_wp_error( $errors ) ) {
131 foreach ( $errors->get_error_codes() as $error_code ) {
132 $this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
133 }
134 }
135
136 parent::error( $errors, ...$args );
137 }
138
139 /**
140 * Stores a message about the upgrade.
141 *
142 * @since 4.6.0
143 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
144 * to the function signature.
145 * @since 5.9.0 Renamed `$data` to `$feedback` for PHP 8 named parameter support.
146 *
147 * @param string|array|WP_Error $feedback Message data.
148 * @param mixed ...$args Optional text replacements.
149 */
150 public function feedback( $feedback, ...$args ) {
151 if ( is_wp_error( $feedback ) ) {
152 foreach ( $feedback->get_error_codes() as $error_code ) {
153 $this->errors->add( $error_code, $feedback->get_error_message( $error_code ), $feedback->get_error_data( $error_code ) );
154 }
155 }
156
157 parent::feedback( $feedback, ...$args );
158 }
159}
160