| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets up the Connection XML-RPC methods. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-connection |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Connection; |
| 9 |
|
| 10 |
use IXR_Error; |
| 11 |
|
| 12 |
/** |
| 13 |
* Registers the XML-RPC methods for Connections. |
| 14 |
* |
| 15 |
* @phan-constructor-used-for-side-effects |
| 16 |
*/ |
| 17 |
class XMLRPC_Connector { |
| 18 |
/** |
| 19 |
* The Connection Manager. |
| 20 |
* |
| 21 |
* @var Manager |
| 22 |
*/ |
| 23 |
private $connection; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @param Manager $connection The Connection Manager. |
| 29 |
*/ |
| 30 |
public function __construct( Manager $connection ) { |
| 31 |
$this->connection = $connection; |
| 32 |
|
| 33 |
// Adding the filter late to avoid being overwritten by Jetpack's XMLRPC server. |
| 34 |
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ), 20 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Attached to the `xmlrpc_methods` filter. |
| 39 |
* |
| 40 |
* @param array $methods The already registered XML-RPC methods. |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function xmlrpc_methods( $methods ) { |
| 44 |
return array_merge( |
| 45 |
$methods, |
| 46 |
array( |
| 47 |
'jetpack.verifyRegistration' => array( $this, 'verify_registration' ), |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Handles verification that a site is registered. |
| 54 |
* |
| 55 |
* @param array $registration_data The data sent by the XML-RPC client: |
| 56 |
* [ $secret_1, $user_id ]. |
| 57 |
* |
| 58 |
* @return string|IXR_Error |
| 59 |
*/ |
| 60 |
public function verify_registration( $registration_data ) { |
| 61 |
return $this->output( $this->connection->handle_registration( $registration_data ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Normalizes output for XML-RPC. |
| 66 |
* |
| 67 |
* @param mixed $data The data to output. |
| 68 |
*/ |
| 69 |
private function output( $data ) { |
| 70 |
if ( is_wp_error( $data ) ) { |
| 71 |
$code = $data->get_error_data(); |
| 72 |
if ( ! $code ) { |
| 73 |
$code = -10520; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! class_exists( IXR_Error::class ) ) { |
| 77 |
require_once ABSPATH . WPINC . '/class-IXR.php'; |
| 78 |
} |
| 79 |
return new IXR_Error( |
| 80 |
$code, |
| 81 |
sprintf( 'Jetpack: [%s] %s', $data->get_error_code(), $data->get_error_message() ) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
return $data; |
| 86 |
} |
| 87 |
} |
| 88 |
|