summaryrefslogtreecommitdiff
blob: 19f119c52c2b073418d5e51303871c5118504e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

/**
 * IXR_Client
 *
 * @package IXR
 * @since 1.5
 *
 */
class Jetpack_IXR_Client extends IXR_Client {
	var $jetpack_args = null;

	function Jetpack_IXR_Client( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
		$defaults = array(
			'url' => Jetpack::xmlrpc_api_url(),
			'user_id' => 0,
		);

		$args = wp_parse_args( $args, $defaults );

		$args['user_id'] = (int) $args['user_id'];

		$this->jetpack_args = $args;

		$this->IXR_Client( $args['url'], $path, $port, $timeout );
	}

	function query() {
		$args = func_get_args();
		$method = array_shift( $args );
		$request = new IXR_Request( $method, $args );
		$xml = trim( $request->getXml() );

		$headers = array(
			'Content-Type' => 'text/xml',
		);

		$response = Jetpack_Client::remote_request( $this->jetpack_args, $xml );

		if ( is_wp_error( $response ) ) {
			$this->error = new IXR_Error( -10520, sprintf( 'Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message() ) );
			return false;
		}

		if ( !$response ) {
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
			return false;
		}

		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
			return false;
		}
		
		$content = wp_remote_retrieve_body( $response );

		// Now parse what we've got back
		$this->message = new IXR_Message( $content );
		if ( !$this->message->parse() ) {
			// XML error
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
			return false;
		}

		// Is the message a fault?
		if ( $this->message->messageType == 'fault' ) {
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
			return false;
		}

		// Message must be OK
		return true;
	}

	function get_jetpack_error( $fault_code = null, $fault_string = null ) {
		if ( is_null( $fault_code ) ) {
			$fault_code = $this->error->code;
		}

		if ( is_null( $fault_string ) ) {
			$fault_string = $this->error->message;
		}

		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
			$code    = $match[1];
			$message = $match[2];
			$status  = $fault_code;
			return new Jetpack_Error( $code, $message, $status );
		}

		return new Jetpack_Error( "IXR_{$fault_code}", $fault_string );
	}
}

/**
 * IXR_ClientMulticall
 *
 * @package IXR
 * @since 1.5
 */
class Jetpack_IXR_ClientMulticall extends Jetpack_IXR_Client {
	var $calls = array();

	function Jetpack_IXR_ClientMulticall( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
		parent::Jetpack_IXR_Client( $args, $path, $port, $timeout );
	}

	function addCall() {
		$args = func_get_args();
		$methodName = array_shift( $args );
		$struct = array(
			'methodName' => $methodName,
			'params' => $args
		);
		$this->calls[] = $struct;
	}

	function query() {
		usort( $this->calls, array( $this, 'sort_calls' ) );

		// Prepare multicall, then call the parent::query() method
		return parent::query( 'system.multicall', $this->calls );
	}

	// Make sure syncs are always done first
	function sort_calls( $a, $b ) {
		if ( 'jetpack.syncContent' == $a['methodName'] ) {
			return -1;
		}

		if ( 'jetpack.syncContent' == $b['methodName'] ) {
			return 1;
		}

		return 0;
	}
}