Completed
Push — try/instant-search-rendering ( a9fac4...f284a4 )
by
unknown
06:33
created

Jetpack_IXR_Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B query() 0 42 6
A get_jetpack_error() 0 18 4
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
	function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
20
		$defaults = array(
21
			'url' => Jetpack::xmlrpc_api_url(),
22
			'user_id' => 0,
23
		);
24
25
		$args = wp_parse_args( $args, $defaults );
26
27
		$this->jetpack_args = $args;
28
29
		$this->IXR_Client( $args['url'], $path, $port, $timeout );
30
	}
31
32
	function query() {
33
		$args = func_get_args();
34
		$method = array_shift( $args );
35
		$request = new IXR_Request( $method, $args );
36
		$xml = trim( $request->getXml() );
37
38
		$response = Client::remote_request( $this->jetpack_args, $xml );
39
40
		if ( is_wp_error( $response ) ) {
41
			$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...
42
			return false;
43
		}
44
45
		if ( !$response ) {
46
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
47
			return false;
48
		}
49
50
		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
51
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
52
			return false;
53
		}
54
55
		$content = wp_remote_retrieve_body( $response );
56
57
		// Now parse what we've got back
58
		$this->message = new IXR_Message( $content );
59
		if ( !$this->message->parse() ) {
60
			// XML error
61
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
62
			return false;
63
		}
64
65
		// Is the message a fault?
66
		if ( $this->message->messageType == 'fault' ) {
67
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
68
			return false;
69
		}
70
71
		// Message must be OK
72
		return true;
73
	}
74
75
	function get_jetpack_error( $fault_code = null, $fault_string = null ) {
76
		if ( is_null( $fault_code ) ) {
77
			$fault_code = $this->error->code;
78
		}
79
80
		if ( is_null( $fault_string ) ) {
81
			$fault_string = $this->error->message;
82
		}
83
84
		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
85
			$code    = $match[1];
86
			$message = $match[2];
87
			$status  = $fault_code;
88
			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...
89
		}
90
91
		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...
92
	}
93
}
94
95
/**
96
 * IXR_ClientMulticall
97
 *
98
 * @package IXR
99
 * @since 1.5
100
 */
101
class Jetpack_IXR_ClientMulticall extends Jetpack_IXR_Client {
102
	public $calls = array();
103
104
	function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
105
		parent::__construct( $args, $path, $port, $timeout );
106
	}
107
108
	function addCall() {
109
		$args = func_get_args();
110
		$methodName = array_shift( $args );
111
		$struct = array(
112
			'methodName' => $methodName,
113
			'params' => $args
114
		);
115
		$this->calls[] = $struct;
116
	}
117
118
	function query() {
119
		usort( $this->calls, array( $this, 'sort_calls' ) );
120
121
		// Prepare multicall, then call the parent::query() method
122
		return parent::query( 'system.multicall', $this->calls );
123
	}
124
125
	// Make sure syncs are always done first
126
	function sort_calls( $a, $b ) {
127
		if ( 'jetpack.syncContent' == $a['methodName'] ) {
128
			return -1;
129
		}
130
131
		if ( 'jetpack.syncContent' == $b['methodName'] ) {
132
			return 1;
133
		}
134
135
		return 0;
136
	}
137
}
138