Completed
Push — master ( fdb3a7...cde0c6 )
by Stephen
20:18
created

WP_Ajax_Upgrader_Skin::error()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 27
Code Lines 17

Duplication

Lines 12
Ratio 44.44 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 8
nop 1
dl 12
loc 27
rs 6.7272
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
 */
19
class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
20
21
	/**
22
	 * Holds the WP_Error object.
23
	 *
24
	 * @since 4.6.0
25
	 * @access protected
26
	 * @var null|WP_Error
27
	 */
28
	protected $errors = null;
29
30
	/**
31
	 * Constructor.
32
	 *
33
	 * @since 4.6.0
34
	 * @access public
35
	 *
36
	 * @param array $args Options for the upgrader, see WP_Upgrader_Skin::__construct().
37
	 */
38
	public function __construct( $args = array() ) {
39
		parent::__construct( $args );
40
41
		$this->errors = new WP_Error();
42
	}
43
44
	/**
45
	 * Retrieves the list of errors.
46
	 *
47
	 * @since 4.6.0
48
	 * @access public
49
	 *
50
	 * @return WP_Error Errors during an upgrade.
51
	 */
52
	public function get_errors() {
53
		return $this->errors;
54
	}
55
56
	/**
57
	 * Retrieves a string for error messages.
58
	 *
59
	 * @since 4.6.0
60
	 * @access public
61
	 *
62
	 * @return string Error messages during an upgrade.
63
	 */
64
	public function get_error_messages() {
65
		$messages = array();
66
67
		foreach ( $this->errors->get_error_codes() as $error_code ) {
68
			if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
69
				$messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
70
			} else {
71
				$messages[] = $this->errors->get_error_message( $error_code );
72
			}
73
		}
74
75
		return implode( ', ', $messages );
76
	}
77
78
	/**
79
	 * Stores a log entry for an error.
80
	 *
81
	 * @since 4.6.0
82
	 * @access public
83
	 *
84
	 * @param string|WP_Error $errors Errors.
85
	 */
86
	public function error( $errors ) {
87
		if ( is_string( $errors ) ) {
88
			$string = $errors;
89
			if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
90
				$string = $this->upgrader->strings[ $string ];
91
			}
92
93 View Code Duplication
			if ( false !== strpos( $string, '%' ) ) {
94
				$args = func_get_args();
95
				$args = array_splice( $args, 1 );
96
				if ( ! empty( $args ) ) {
97
					$string = vsprintf( $string, $args );
98
				}
99
			}
100
101
			// Count existing errors to generate an unique error code.
102
			$errors_count = count( $errors->get_error_codes() );
0 ignored issues
show
Bug introduced by
The method get_error_codes cannot be called on $errors (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
103
			$this->errors->add( 'unknown_upgrade_error_' . $errors_count + 1 , $string );
104 View Code Duplication
		} elseif ( is_wp_error( $errors ) ) {
105
			foreach ( $errors->get_error_codes() as $error_code ) {
106
				$this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
107
			}
108
		}
109
110
		$args = func_get_args();
111
		call_user_func_array( array( $this, 'parent::error' ), $args );
112
	}
113
114
	/**
115
	 * Stores a log entry.
116
	 *
117
	 * @since 4.6.0
118
	 * @access public
119
	 *
120
	 * @param string|array|WP_Error $data Log entry data.
121
	 */
122
	public function feedback( $data ) {
123 View Code Duplication
		if ( is_wp_error( $data ) ) {
124
			foreach ( $data->get_error_codes() as $error_code ) {
125
				$this->errors->add( $error_code, $data->get_error_message( $error_code ), $data->get_error_data( $error_code ) );
0 ignored issues
show
Bug introduced by
It seems like $data is not always an object, but can also be of type string|array. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
126
			}
127
		}
128
129
		$args = func_get_args();
130
		call_user_func_array( array( $this, 'parent::feedback' ), $args );
131
	}
132
}
133