1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Just a sack of functions. Not actually an IXR_Server |
5
|
|
|
*/ |
6
|
|
|
class Jetpack_XMLRPC_Server { |
7
|
|
|
/** |
8
|
|
|
* The current error object |
9
|
|
|
*/ |
10
|
|
|
public $error = null; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The current user |
14
|
|
|
*/ |
15
|
|
|
public $user = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Whitelist of the XML-RPC methods available to the Jetpack Server. If the |
19
|
|
|
* user is not authenticated (->login()) then the methods are never added, |
20
|
|
|
* so they will get a "does not exist" error. |
21
|
|
|
*/ |
22
|
|
|
function xmlrpc_methods( $core_methods ) { |
23
|
|
|
$jetpack_methods = array( |
24
|
|
|
'jetpack.jsonAPI' => array( $this, 'json_api' ), |
25
|
|
|
'jetpack.verifyAction' => array( $this, 'verify_action' ), |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->user = $this->login(); |
29
|
|
|
|
30
|
|
|
if ( $this->user ) { |
31
|
|
|
$jetpack_methods = array_merge( $jetpack_methods, array( |
32
|
|
|
'jetpack.testConnection' => array( $this, 'test_connection' ), |
33
|
|
|
'jetpack.testAPIUserCode' => array( $this, 'test_api_user_code' ), |
34
|
|
|
'jetpack.featuresAvailable' => array( $this, 'features_available' ), |
35
|
|
|
'jetpack.featuresEnabled' => array( $this, 'features_enabled' ), |
36
|
|
|
'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ), |
37
|
|
|
'jetpack.unlinkUser' => array( $this, 'unlink_user' ), |
38
|
|
|
'jetpack.syncObject' => array( $this, 'sync_object' ), |
39
|
|
|
'jetpack.idcUrlValidation' => array( $this, 'validate_urls_for_idc_mitigation' ), |
40
|
|
|
) ); |
41
|
|
|
|
42
|
|
|
if ( isset( $core_methods['metaWeblog.editPost'] ) ) { |
43
|
|
|
$jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject']; |
44
|
|
|
$jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Filters the XML-RPC methods available to Jetpack for authenticated users. |
49
|
|
|
* |
50
|
|
|
* @since 1.1.0 |
51
|
|
|
* |
52
|
|
|
* @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
53
|
|
|
* @param array $core_methods Available core XML-RPC methods. |
54
|
|
|
* @param WP_User $user Information about a given WordPress user. |
55
|
|
|
*/ |
56
|
|
|
$jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $this->user ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Filters the XML-RPC methods available to Jetpack for unauthenticated users. |
61
|
|
|
* |
62
|
|
|
* @since 3.0.0 |
63
|
|
|
* |
64
|
|
|
* @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
65
|
|
|
* @param array $core_methods Available core XML-RPC methods. |
66
|
|
|
*/ |
67
|
|
|
return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Whitelist of the bootstrap XML-RPC methods |
72
|
|
|
*/ |
73
|
|
|
function bootstrap_xmlrpc_methods() { |
74
|
|
|
return array( |
75
|
|
|
'jetpack.verifyRegistration' => array( $this, 'verify_registration' ), |
76
|
|
|
'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ), |
77
|
|
|
'jetpack.remoteProvision' => array( $this, 'remote_provision' ), |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
function authorize_xmlrpc_methods() { |
82
|
|
|
return array( |
83
|
|
|
'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ), |
84
|
|
|
'jetpack.remoteProvision' => array( $this, 'remote_provision' ), |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function remote_authorize( $request ) { |
89
|
|
|
$user = get_user_by( 'id', $request['state'] ); |
90
|
|
|
JetpackTracking::record_user_event( 'jpc_remote_authorize_begin', array(), $user ); |
91
|
|
|
|
92
|
|
|
foreach( array( 'secret', 'state', 'redirect_uri', 'code' ) as $required ) { |
93
|
|
|
if ( ! isset( $request[ $required ] ) || empty( $request[ $required ] ) ) { |
94
|
|
|
return $this->error( new Jetpack_Error( 'missing_parameter', 'One or more parameters is missing from the request.', 400 ), 'jpc_remote_authorize_fail' ); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( ! $user ) { |
99
|
|
|
return $this->error( new Jetpack_Error( 'user_unknown', 'User not found.', 404 ), 'jpc_remote_authorize_fail' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( Jetpack::is_active() && Jetpack::is_user_connected( $request['state'] ) ) { |
103
|
|
|
return $this->error( new Jetpack_Error( 'already_connected', 'User already connected.', 400 ), 'jpc_remote_authorize_fail' ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$verified = $this->verify_action( array( 'authorize', $request['secret'], $request['state'] ) ); |
107
|
|
|
|
108
|
|
|
if ( is_a( $verified, 'IXR_Error' ) ) { |
109
|
|
|
return $this->error( $verified, 'jpc_remote_authorize_fail' ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
wp_set_current_user( $request['state'] ); |
113
|
|
|
|
114
|
|
|
$client_server = new Jetpack_Client_Server; |
115
|
|
|
$result = $client_server->authorize( $request ); |
116
|
|
|
|
117
|
|
|
if ( is_wp_error( $result ) ) { |
118
|
|
|
return $this->error( $result, 'jpc_remote_authorize_fail' ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
JetpackTracking::record_user_event( 'jpc_remote_authorize_success' ); |
122
|
|
|
|
123
|
|
|
$response = array( |
124
|
|
|
'result' => $result, |
125
|
|
|
); |
126
|
|
|
return $response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
131
|
|
|
* register this site so that a plan can be provisioned. |
132
|
|
|
* |
133
|
|
|
* @param array $request An array containing at minimum a nonce key and a local_username key. |
134
|
|
|
* |
135
|
|
|
* @return WP_Error|array |
136
|
|
|
*/ |
137
|
|
|
public function remote_provision( $request ) { |
138
|
|
|
if ( empty( $request['nonce'] ) ) { |
139
|
|
|
return $this->error( |
140
|
|
|
new Jetpack_Error( |
141
|
|
|
'nonce_missing', |
142
|
|
|
esc_html__( 'The required "nonce" parameter is missing.', 'jetpack' ), |
143
|
|
|
400 |
144
|
|
|
), |
145
|
|
|
'jpc_remote_provision_fail' |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( empty( $request['local_username'] ) ) { |
150
|
|
|
return $this->error( |
151
|
|
|
new Jetpack_Error( |
152
|
|
|
'local_username_missing', |
153
|
|
|
esc_html__( 'The required "local_username" parameter is missing.', 'jetpack' ), |
154
|
|
|
400 |
155
|
|
|
), |
156
|
|
|
'jpc_remote_provision_fail' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$nonce = sanitize_text_field( $request['nonce'] ); |
161
|
|
|
unset( $request['nonce'] ); |
162
|
|
|
|
163
|
|
|
$api_url = Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'partner_provision_nonce_check' ) ); |
164
|
|
|
$response = Jetpack_Client::_wp_remote_request( |
165
|
|
|
esc_url_raw( add_query_arg( 'nonce', $nonce, $api_url ) ), |
166
|
|
|
array( 'method' => 'GET' ), |
167
|
|
|
true |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
if ( |
171
|
|
|
200 !== wp_remote_retrieve_response_code( $response ) || |
172
|
|
|
'OK' !== trim( wp_remote_retrieve_body( $response ) ) |
173
|
|
|
) { |
174
|
|
|
return $this->error( |
175
|
|
|
new Jetpack_Error( |
176
|
|
|
'invalid_nonce', |
177
|
|
|
esc_html__( 'There was an issue validating this request.', 'jetpack' ), |
178
|
|
|
400 |
179
|
|
|
), |
180
|
|
|
'jpc_remote_provision_fail' |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$local_username = $request['local_username']; |
185
|
|
|
|
186
|
|
|
$user = get_user_by( 'login', $local_username ); |
187
|
|
|
|
188
|
|
|
if ( ! $user ) { |
189
|
|
|
$user = get_user_by( 'email', $local_username ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ( ! $user ) { |
193
|
|
|
return $this->error( new Jetpack_Error( 'user_unknown', 'User not found.', 404 ) ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
require_once JETPACK__PLUGIN_DIR . '_inc/class.jetpack-provision.php'; |
197
|
|
|
|
198
|
|
|
wp_set_current_user( $user->ID ); |
199
|
|
|
|
200
|
|
|
// Filter allowed parameters. |
201
|
|
|
$allowed_provision_args = array( |
202
|
|
|
'wpcom_user_id', |
203
|
|
|
'wpcom_user_email', |
204
|
|
|
'local_username', |
205
|
|
|
'plan', |
206
|
|
|
'force_register', |
207
|
|
|
'force_connect', |
208
|
|
|
'onboarding', |
209
|
|
|
'partner_tracking_id', |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$args = array_intersect_key( |
213
|
|
|
$request, |
214
|
|
|
array_flip( $allowed_provision_args ) |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$result = Jetpack_Provision::register_and_build_request_body( $args ); |
218
|
|
|
|
219
|
|
|
if ( is_wp_error( $result ) ) { |
220
|
|
|
return $this->error( $result, 'jpc_remote_provision_fail' ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$result['client_id'] = Jetpack_Options::get_option( 'id' ); |
224
|
|
|
|
225
|
|
|
return $result; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
private function tracks_record_error( $name, $error, $user = null ) { |
229
|
|
|
if ( is_wp_error( $error ) ) { |
230
|
|
|
JetpackTracking::record_user_event( $name, array( |
231
|
|
|
'error_code' => $error->get_error_code(), |
232
|
|
|
'error_message' => $error->get_error_message() |
233
|
|
|
), $user ); |
234
|
|
|
} elseif( is_a( $error, 'IXR_Error' ) ) { |
235
|
|
|
JetpackTracking::record_user_event( $name, array( |
236
|
|
|
'error_code' => $error->code, |
237
|
|
|
'error_message' => $error->message |
238
|
|
|
), $user ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $error; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Verifies that Jetpack.WordPress.com received a registration request from this site |
246
|
|
|
*/ |
247
|
|
|
function verify_registration( $data ) { |
248
|
|
|
// failure modes will be recorded in tracks in the verify_action method |
249
|
|
|
return $this->verify_action( array( 'register', $data[0], $data[1] ) ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
254
|
|
|
* |
255
|
|
|
* Possible error_codes: |
256
|
|
|
* |
257
|
|
|
* verify_secret_1_missing |
258
|
|
|
* verify_secret_1_malformed |
259
|
|
|
* verify_secrets_missing: verification secrets are not found in database |
260
|
|
|
* verify_secrets_incomplete: verification secrets are only partially found in database |
261
|
|
|
* verify_secrets_expired: verification secrets have expired |
262
|
|
|
* verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
263
|
|
|
* state_missing: required parameter of state not found |
264
|
|
|
* state_malformed: state is not a digit |
265
|
|
|
* invalid_state: state in request does not match the stored state |
266
|
|
|
* |
267
|
|
|
* The 'authorize' and 'register' actions have additional error codes |
268
|
|
|
* |
269
|
|
|
* state_missing: a state ( user id ) was not supplied |
270
|
|
|
* state_malformed: state is not the correct data type |
271
|
|
|
* invalid_state: supplied state does not match the stored state |
272
|
|
|
*/ |
273
|
|
|
function verify_action( $params ) { |
274
|
|
|
$action = $params[0]; |
275
|
|
|
$verify_secret = $params[1]; |
276
|
|
|
$state = isset( $params[2] ) ? $params[2] : ''; |
277
|
|
|
$user = get_user_by( 'id', $state ); |
278
|
|
|
JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_begin', array(), $user ); |
279
|
|
|
$tracks_failure_event_name = 'jpc_verify_' . $action . '_fail'; |
280
|
|
|
|
281
|
|
|
if ( empty( $verify_secret ) ) { |
282
|
|
|
return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
283
|
|
|
} else if ( ! is_string( $verify_secret ) ) { |
284
|
|
|
return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ), $tracks_failure_event_name, $user ); |
285
|
|
|
} else if ( empty( $state ) ) { |
286
|
|
|
return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
287
|
|
|
} else if ( ! ctype_digit( $state ) ) { |
288
|
|
|
return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ), $tracks_failure_event_name, $user ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$secrets = Jetpack::get_secrets( $action, $state ); |
292
|
|
|
|
293
|
|
|
if ( ! $secrets ) { |
294
|
|
|
Jetpack::delete_secrets( $action, $state ); |
295
|
|
|
return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification secrets not found', 400 ), $tracks_failure_event_name, $user ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ( is_wp_error( $secrets ) ) { |
299
|
|
|
Jetpack::delete_secrets( $action, $state ); |
300
|
|
|
return $this->error( new Jetpack_Error( $secrets->get_error_code(), $secrets->get_error_message(), 400 ), $tracks_failure_event_name, $user ); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if ( empty( $secrets['secret_1'] ) || empty( $secrets['secret_2'] ) || empty( $secrets['exp'] ) ) { |
304
|
|
|
Jetpack::delete_secrets( $action, $state ); |
305
|
|
|
return $this->error( new Jetpack_Error( 'verify_secrets_incomplete', 'Verification secrets are incomplete', 400 ), $tracks_failure_event_name, $user ); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ( ! hash_equals( $verify_secret, $secrets['secret_1'] ) ) { |
309
|
|
|
Jetpack::delete_secrets( $action, $state ); |
310
|
|
|
return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ), $tracks_failure_event_name, $user ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
Jetpack::delete_secrets( $action, $state ); |
314
|
|
|
|
315
|
|
|
JetpackTracking::record_user_event( 'jpc_verify_' . $action . '_success', array(), $user ); |
316
|
|
|
|
317
|
|
|
return $secrets['secret_2']; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Wrapper for wp_authenticate( $username, $password ); |
322
|
|
|
* |
323
|
|
|
* @return WP_User|bool |
324
|
|
|
*/ |
325
|
|
|
function login() { |
326
|
|
|
Jetpack::init()->require_jetpack_authentication(); |
327
|
|
|
$user = wp_authenticate( 'username', 'password' ); |
328
|
|
|
if ( is_wp_error( $user ) ) { |
329
|
|
|
if ( 'authentication_failed' == $user->get_error_code() ) { // Generic error could mean most anything. |
330
|
|
|
$this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
331
|
|
|
} else { |
332
|
|
|
$this->error = $user; |
333
|
|
|
} |
334
|
|
|
return false; |
335
|
|
|
} else if ( !$user ) { // Shouldn't happen. |
336
|
|
|
$this->error = new Jetpack_Error( 'invalid_request', 'Invalid Request', 403 ); |
337
|
|
|
return false; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $user; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Returns the current error as an IXR_Error |
345
|
|
|
* |
346
|
|
|
* @return bool|IXR_Error |
347
|
|
|
*/ |
348
|
|
|
function error( $error = null, $tracks_event_name = null, $user = null ) { |
349
|
|
|
// record using Tracks |
350
|
|
|
if ( null !== $tracks_event_name ) { |
351
|
|
|
$this->tracks_record_error( $tracks_event_name, $error, $user ); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if ( !is_null( $error ) ) { |
355
|
|
|
$this->error = $error; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if ( is_wp_error( $this->error ) ) { |
359
|
|
|
$code = $this->error->get_error_data(); |
360
|
|
|
if ( !$code ) { |
361
|
|
|
$code = -10520; |
362
|
|
|
} |
363
|
|
|
$message = sprintf( 'Jetpack: [%s] %s', $this->error->get_error_code(), $this->error->get_error_message() ); |
364
|
|
|
return new IXR_Error( $code, $message ); |
365
|
|
|
} else if ( is_a( $this->error, 'IXR_Error' ) ) { |
366
|
|
|
return $this->error; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return false; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/* API Methods */ |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Just authenticates with the given Jetpack credentials. |
376
|
|
|
* |
377
|
|
|
* @return string The current Jetpack version number |
378
|
|
|
*/ |
379
|
|
|
function test_connection() { |
380
|
|
|
return JETPACK__VERSION; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
function test_api_user_code( $args ) { |
384
|
|
|
$client_id = (int) $args[0]; |
385
|
|
|
$user_id = (int) $args[1]; |
386
|
|
|
$nonce = (string) $args[2]; |
387
|
|
|
$verify = (string) $args[3]; |
388
|
|
|
|
389
|
|
|
if ( !$client_id || !$user_id || !strlen( $nonce ) || 32 !== strlen( $verify ) ) { |
390
|
|
|
return false; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$user = get_user_by( 'id', $user_id ); |
394
|
|
|
if ( !$user || is_wp_error( $user ) ) { |
395
|
|
|
return false; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/* debugging |
399
|
|
|
error_log( "CLIENT: $client_id" ); |
400
|
|
|
error_log( "USER: $user_id" ); |
401
|
|
|
error_log( "NONCE: $nonce" ); |
402
|
|
|
error_log( "VERIFY: $verify" ); |
403
|
|
|
*/ |
404
|
|
|
|
405
|
|
|
$jetpack_token = Jetpack_Data::get_access_token( $user_id ); |
|
|
|
|
406
|
|
|
|
407
|
|
|
$api_user_code = get_user_meta( $user_id, "jetpack_json_api_$client_id", true ); |
408
|
|
|
if ( !$api_user_code ) { |
409
|
|
|
return false; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
$hmac = hash_hmac( 'md5', json_encode( (object) array( |
413
|
|
|
'client_id' => (int) $client_id, |
414
|
|
|
'user_id' => (int) $user_id, |
415
|
|
|
'nonce' => (string) $nonce, |
416
|
|
|
'code' => (string) $api_user_code, |
417
|
|
|
) ), $jetpack_token->secret ); |
418
|
|
|
|
419
|
|
|
if ( ! hash_equals( $hmac, $verify ) ) { |
420
|
|
|
return false; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
return $user_id; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Disconnect this blog from the connected wordpress.com account |
428
|
|
|
* @return boolean |
429
|
|
|
*/ |
430
|
|
|
function disconnect_blog() { |
431
|
|
|
|
432
|
|
|
// For tracking |
433
|
|
|
if ( ! empty( $this->user->ID ) ) { |
434
|
|
|
wp_set_current_user( $this->user->ID ); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
Jetpack::log( 'disconnect' ); |
438
|
|
|
Jetpack::disconnect(); |
439
|
|
|
|
440
|
|
|
return true; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Unlink a user from WordPress.com |
445
|
|
|
* |
446
|
|
|
* This will fail if called by the Master User. |
447
|
|
|
*/ |
448
|
|
|
function unlink_user() { |
449
|
|
|
Jetpack::log( 'unlink' ); |
450
|
|
|
return Jetpack::unlink_user(); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Returns any object that is able to be synced |
455
|
|
|
*/ |
456
|
|
|
function sync_object( $args ) { |
457
|
|
|
// e.g. posts, post, 5 |
458
|
|
|
list( $module_name, $object_type, $id ) = $args; |
459
|
|
|
require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-modules.php'; |
460
|
|
|
require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-sender.php'; |
461
|
|
|
|
462
|
|
|
$sync_module = Jetpack_Sync_Modules::get_module( $module_name ); |
463
|
|
|
$codec = Jetpack_Sync_Sender::get_instance()->get_codec(); |
464
|
|
|
|
465
|
|
|
return $codec->encode( $sync_module->get_object_by_id( $object_type, $id ) ); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Returns the home URL and site URL for the current site which can be used on the WPCOM side for |
470
|
|
|
* IDC mitigation to decide whether sync should be allowed if the home and siteurl values differ between WPCOM |
471
|
|
|
* and the remote Jetpack site. |
472
|
|
|
* |
473
|
|
|
* @return array |
474
|
|
|
*/ |
475
|
|
|
function validate_urls_for_idc_mitigation() { |
476
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
477
|
|
|
return array( |
478
|
|
|
'home' => Jetpack_Sync_Functions::home_url(), |
479
|
|
|
'siteurl' => Jetpack_Sync_Functions::site_url(), |
480
|
|
|
); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Returns what features are available. Uses the slug of the module files. |
485
|
|
|
* |
486
|
|
|
* @return array |
487
|
|
|
*/ |
488
|
|
View Code Duplication |
function features_available() { |
489
|
|
|
$raw_modules = Jetpack::get_available_modules(); |
490
|
|
|
$modules = array(); |
491
|
|
|
foreach ( $raw_modules as $module ) { |
492
|
|
|
$modules[] = Jetpack::get_module_slug( $module ); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
return $modules; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Returns what features are enabled. Uses the slug of the modules files. |
500
|
|
|
* |
501
|
|
|
* @return array |
502
|
|
|
*/ |
503
|
|
View Code Duplication |
function features_enabled() { |
504
|
|
|
$raw_modules = Jetpack::get_active_modules(); |
505
|
|
|
$modules = array(); |
506
|
|
|
foreach ( $raw_modules as $module ) { |
507
|
|
|
$modules[] = Jetpack::get_module_slug( $module ); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
return $modules; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
function update_attachment_parent( $args ) { |
514
|
|
|
$attachment_id = (int) $args[0]; |
515
|
|
|
$parent_id = (int) $args[1]; |
516
|
|
|
|
517
|
|
|
return wp_update_post( array( |
518
|
|
|
'ID' => $attachment_id, |
519
|
|
|
'post_parent' => $parent_id, |
520
|
|
|
) ); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
function json_api( $args = array() ) { |
524
|
|
|
$json_api_args = $args[0]; |
525
|
|
|
$verify_api_user_args = $args[1]; |
526
|
|
|
|
527
|
|
|
$method = (string) $json_api_args[0]; |
528
|
|
|
$url = (string) $json_api_args[1]; |
529
|
|
|
$post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2]; |
530
|
|
|
$user_details = (array) $json_api_args[4]; |
531
|
|
|
$locale = (string) $json_api_args[5]; |
532
|
|
|
|
533
|
|
|
if ( !$verify_api_user_args ) { |
534
|
|
|
$user_id = 0; |
535
|
|
|
} elseif ( 'internal' === $verify_api_user_args[0] ) { |
536
|
|
|
$user_id = (int) $verify_api_user_args[1]; |
537
|
|
|
if ( $user_id ) { |
538
|
|
|
$user = get_user_by( 'id', $user_id ); |
539
|
|
|
if ( !$user || is_wp_error( $user ) ) { |
540
|
|
|
return false; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
} else { |
544
|
|
|
$user_id = call_user_func( array( $this, 'test_api_user_code' ), $verify_api_user_args ); |
545
|
|
|
if ( !$user_id ) { |
546
|
|
|
return false; |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/* debugging |
551
|
|
|
error_log( "-- begin json api via jetpack debugging -- " ); |
552
|
|
|
error_log( "METHOD: $method" ); |
553
|
|
|
error_log( "URL: $url" ); |
554
|
|
|
error_log( "POST BODY: $post_body" ); |
555
|
|
|
error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) ); |
556
|
|
|
error_log( "VERIFIED USER_ID: " . (int) $user_id ); |
557
|
|
|
error_log( "-- end json api via jetpack debugging -- " ); |
558
|
|
|
*/ |
559
|
|
|
|
560
|
|
|
if ( 'en' !== $locale ) { |
561
|
|
|
// .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them. |
562
|
|
|
$new_locale = $locale; |
563
|
|
|
if ( strpos( $locale, '-' ) !== false ) { |
564
|
|
|
$locale_pieces = explode( '-', $locale ); |
565
|
|
|
$new_locale = $locale_pieces[0]; |
566
|
|
|
$new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : ''; |
567
|
|
|
} else { |
568
|
|
|
// .com might pass 'fr' because thats what our language files are named as, where core seems |
569
|
|
|
// to do fr_FR - so try that if we don't think we can load the file. |
570
|
|
|
if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) { |
571
|
|
|
$new_locale = $locale . '_' . strtoupper( $locale ); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) { |
576
|
|
|
unload_textdomain( 'default' ); |
577
|
|
|
load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' ); |
578
|
|
|
} |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
$old_user = wp_get_current_user(); |
582
|
|
|
wp_set_current_user( $user_id ); |
583
|
|
|
|
584
|
|
|
$token = Jetpack_Data::get_access_token( get_current_user_id() ); |
585
|
|
|
if ( !$token || is_wp_error( $token ) ) { |
586
|
|
|
return false; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
define( 'REST_API_REQUEST', true ); |
590
|
|
|
define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); |
591
|
|
|
|
592
|
|
|
// needed? |
593
|
|
|
require_once ABSPATH . 'wp-admin/includes/admin.php'; |
594
|
|
|
|
595
|
|
|
require_once JETPACK__PLUGIN_DIR . 'class.json-api.php'; |
596
|
|
|
$api = WPCOM_JSON_API::init( $method, $url, $post_body ); |
597
|
|
|
$api->token_details['user'] = $user_details; |
598
|
|
|
require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php'; |
599
|
|
|
|
600
|
|
|
$display_errors = ini_set( 'display_errors', 0 ); |
601
|
|
|
ob_start(); |
602
|
|
|
$content_type = $api->serve( false ); |
|
|
|
|
603
|
|
|
$output = ob_get_clean(); |
604
|
|
|
ini_set( 'display_errors', $display_errors ); |
605
|
|
|
|
606
|
|
|
$nonce = wp_generate_password( 10, false ); |
607
|
|
|
$hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); |
608
|
|
|
|
609
|
|
|
wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); |
610
|
|
|
|
611
|
|
|
return array( |
612
|
|
|
(string) $output, |
613
|
|
|
(string) $nonce, |
614
|
|
|
(string) $hmac, |
615
|
|
|
); |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
|
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: