class.jetpack-ixr-client.php 3.2 KB

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