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