Completed
Push — try/jetpack-stories-block-mobi... ( 2fea66 )
by
unknown
126:35 queued 116:47
created

connection/legacy/class-jetpack-ixr-client.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * IXR_Client
4
 *
5
 * @package automattic/jetpack-connection
6
 *
7
 * @since 1.5
8
 * @since 7.7 Moved to the jetpack-connection package.
9
 */
10
11
use Automattic\Jetpack\Connection\Client;
12
use Automattic\Jetpack\Connection\Manager;
13
14
/**
15
 * A Jetpack implementation of the WordPress core IXR client.
16
 */
17
class Jetpack_IXR_Client extends IXR_Client {
18
	/**
19
	 * Jetpack args, used for the remote requests.
20
	 *
21
	 * @var array
22
	 */
23
	public $jetpack_args = null;
24
25
	/**
26
	 * Constructor.
27
	 * Initialize a new Jetpack IXR client instance.
28
	 *
29
	 * @param array       $args    Jetpack args, used for the remote requests.
30
	 * @param string|bool $path    Path to perform the reuqest to.
31
	 * @param int         $port    Port number.
32
	 * @param int         $timeout The connection timeout, in seconds.
33
	 */
34
	public function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
35
		$connection = new Manager();
36
37
		$defaults = array(
38
			'url'     => $connection->xmlrpc_api_url(),
39
			'user_id' => 0,
40
			'headers' => array(),
41
		);
42
43
		$args            = wp_parse_args( $args, $defaults );
0 ignored issues
show
$defaults is of type array<string,string|inte...er","headers":"array"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
		$args['headers'] = array_merge( array( 'Content-Type' => 'text/xml' ), (array) $args['headers'] );
45
46
		$this->jetpack_args = $args;
47
48
		$this->IXR_Client( $args['url'], $path, $port, $timeout );
49
	}
50
51
	/**
52
	 * Perform the IXR request.
53
	 *
54
	 * @param string[] ...$args IXR args.
55
	 *
56
	 * @return bool True if request succeeded, false otherwise.
57
	 */
58
	public function query( ...$args ) {
59
		$method  = array_shift( $args );
60
		$request = new IXR_Request( $method, $args );
61
		$xml     = trim( $request->getXml() );
62
63
		$response = Client::remote_request( $this->jetpack_args, $xml );
64
65
		if ( is_wp_error( $response ) ) {
66
			$this->error = new IXR_Error( -10520, sprintf( 'Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message() ) );
67
			return false;
68
		}
69
70
		if ( ! $response ) {
71
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
72
			return false;
73
		}
74
75
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
76
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
77
			return false;
78
		}
79
80
		$content = wp_remote_retrieve_body( $response );
81
82
		// Now parse what we've got back.
83
		$this->message = new IXR_Message( $content );
84
		if ( ! $this->message->parse() ) {
85
			// XML error.
86
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
87
			return false;
88
		}
89
90
		// Is the message a fault?
91
		if ( 'fault' === $this->message->messageType ) {
92
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
93
			return false;
94
		}
95
96
		// Message must be OK.
97
		return true;
98
	}
99
100
	/**
101
	 * Retrieve the Jetpack error from the result of the last request.
102
	 *
103
	 * @param int    $fault_code   Fault code.
104
	 * @param string $fault_string Fault string.
105
	 * @return WP_Error Error object.
106
	 */
107
	public function get_jetpack_error( $fault_code = null, $fault_string = null ) {
108
		if ( is_null( $fault_code ) ) {
109
			$fault_code = $this->error->code;
110
		}
111
112
		if ( is_null( $fault_string ) ) {
113
			$fault_string = $this->error->message;
114
		}
115
116
		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
117
			$code    = $match[1];
118
			$message = $match[2];
119
			$status  = $fault_code;
120
			return new \WP_Error( $code, $message, $status );
121
		}
122
123
		return new \WP_Error( "IXR_{$fault_code}", $fault_string );
124
	}
125
}
126