Completed
Push — send-disconnecting-userid ( ec9e8c...78775a )
by
unknown
16:20 queued 09:39
created

Jetpack_IXR_Client::add_additional_parameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
defined( 'ABSPATH' ) or die( 'No direct access, please.' );
4
5
use Automattic\Jetpack\Connection\Client;
6
7
require_once( ABSPATH . WPINC . '/class-IXR.php' );
8
9
/**
10
 * IXR_Client
11
 *
12
 * @package IXR
13
 * @since 1.5
14
 *
15
 */
16
class Jetpack_IXR_Client extends IXR_Client {
17
	public $jetpack_args = null;
18
19
	/**
20
	 * @var array Array of additional parameters
21
	 */
22
	protected $additional_parameters;
23
24
	function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
25
		$defaults = array(
26
			'url' => Jetpack::xmlrpc_api_url(),
27
			'user_id' => 0,
28
		);
29
30
		$args = wp_parse_args( $args, $defaults );
31
32
		$this->jetpack_args = $args;
33
34
		$this->IXR_Client( $args['url'], $path, $port, $timeout );
35
	}
36
37
	function query() {
38
		$args = func_get_args();
39
		$method = array_shift( $args );
40
		if ( is_array( $this->additional_parameters ) ) {
41
			$args = array_merge( $args, $this->additional_parameters );
42
		}
43
44
		$request = new IXR_Request( $method, $args );
45
		$xml = trim( $request->getXml() );
46
47
		$response = Client::remote_request( $this->jetpack_args, $xml );
48
49
		if ( is_wp_error( $response ) ) {
50
			$this->error = new IXR_Error( -10520, sprintf( 'Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message() ) );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
					return false;
52
		}
53
54
		if ( !$response ) {
55
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
56
			return false;
57
		}
58
59
		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
60
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
61
			return false;
62
		}
63
64
		$content = wp_remote_retrieve_body( $response );
65
66
		// Now parse what we've got back
67
		$this->message = new IXR_Message( $content );
68
		if ( !$this->message->parse() ) {
69
			// XML error
70
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
71
			return false;
72
		}
73
74
		// Is the message a fault?
75
		if ( $this->message->messageType == 'fault' ) {
76
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
77
			return false;
78
		}
79
80
		// Message must be OK
81
		return true;
82
	}
83
84
	function get_jetpack_error( $fault_code = null, $fault_string = null ) {
85
		if ( is_null( $fault_code ) ) {
86
			$fault_code = $this->error->code;
87
		}
88
89
		if ( is_null( $fault_string ) ) {
90
			$fault_string = $this->error->message;
91
		}
92
93
		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
94
			$code    = $match[1];
95
			$message = $match[2];
96
			$status  = $fault_code;
97
			return new Jetpack_Error( $code, $message, $status );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with $code.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
98
		}
99
100
		return new Jetpack_Error( "IXR_{$fault_code}", $fault_string );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with "IXR_{$fault_code}".

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
	}
102
103
	function add_additional_parameters( $additional_parameters ) {
104
		$this->additional_parameters = $additional_parameters;
105
	}
106
}
107
108
/**
109
 * IXR_ClientMulticall
110
 *
111
 * @package IXR
112
 * @since 1.5
113
 */
114
class Jetpack_IXR_ClientMulticall extends Jetpack_IXR_Client {
115
	public $calls = array();
116
117
	function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
118
		parent::__construct( $args, $path, $port, $timeout );
119
	}
120
121
	function addCall() {
122
		$args = func_get_args();
123
		$methodName = array_shift( $args );
124
		$struct = array(
125
			'methodName' => $methodName,
126
			'params' => $args
127
		);
128
		$this->calls[] = $struct;
129
	}
130
131
	function query() {
132
		usort( $this->calls, array( $this, 'sort_calls' ) );
133
134
		// Prepare multicall, then call the parent::query() method
135
		return parent::query( 'system.multicall', $this->calls );
136
	}
137
138
	// Make sure syncs are always done first
139
	function sort_calls( $a, $b ) {
140
		if ( 'jetpack.syncContent' == $a['methodName'] ) {
141
			return -1;
142
		}
143
144
		if ( 'jetpack.syncContent' == $b['methodName'] ) {
145
			return 1;
146
		}
147
148
		return 0;
149
	}
150
}
151