wp/wp-includes/class-wp-http-ixr-client.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    11 	 * @var IXR_Error
    11 	 * @var IXR_Error
    12 	 */
    12 	 */
    13 	public $error;
    13 	public $error;
    14 
    14 
    15 	/**
    15 	/**
    16 	 * @param string $server
    16 	 * @param string      $server
    17 	 * @param string|bool $path
    17 	 * @param string|bool $path
    18 	 * @param int|bool $port
    18 	 * @param int|bool    $port
    19 	 * @param int $timeout
    19 	 * @param int         $timeout
    20 	 */
    20 	 */
    21 	public function __construct( $server, $path = false, $port = false, $timeout = 15 ) {
    21 	public function __construct( $server, $path = false, $port = false, $timeout = 15 ) {
    22 		if ( ! $path ) {
    22 		if ( ! $path ) {
    23 			// Assume we have been given a URL instead
    23 			// Assume we have been given a URL instead.
    24 			$bits         = parse_url( $server );
    24 			$bits         = parse_url( $server );
    25 			$this->scheme = $bits['scheme'];
    25 			$this->scheme = $bits['scheme'];
    26 			$this->server = $bits['host'];
    26 			$this->server = $bits['host'];
    27 			$this->port   = isset( $bits['port'] ) ? $bits['port'] : $port;
    27 			$this->port   = isset( $bits['port'] ) ? $bits['port'] : $port;
    28 			$this->path   = ! empty( $bits['path'] ) ? $bits['path'] : '/';
    28 			$this->path   = ! empty( $bits['path'] ) ? $bits['path'] : '/';
    29 
    29 
    30 			// Make absolutely sure we have a path
    30 			// Make absolutely sure we have a path.
    31 			if ( ! $this->path ) {
    31 			if ( ! $this->path ) {
    32 				$this->path = '/';
    32 				$this->path = '/';
    33 			}
    33 			}
    34 
    34 
    35 			if ( ! empty( $bits['query'] ) ) {
    35 			if ( ! empty( $bits['query'] ) ) {
    44 		$this->useragent = 'The Incutio XML-RPC PHP Library';
    44 		$this->useragent = 'The Incutio XML-RPC PHP Library';
    45 		$this->timeout   = $timeout;
    45 		$this->timeout   = $timeout;
    46 	}
    46 	}
    47 
    47 
    48 	/**
    48 	/**
       
    49 	 * @since 3.1.0
       
    50 	 * @since 5.5.0 Formalized the existing `...$args` parameter by adding it
       
    51 	 *              to the function signature.
       
    52 	 *
    49 	 * @return bool
    53 	 * @return bool
    50 	 */
    54 	 */
    51 	public function query() {
    55 	public function query( ...$args ) {
    52 		$args    = func_get_args();
       
    53 		$method  = array_shift( $args );
    56 		$method  = array_shift( $args );
    54 		$request = new IXR_Request( $method, $args );
    57 		$request = new IXR_Request( $method, $args );
    55 		$xml     = $request->getXml();
    58 		$xml     = $request->getXml();
    56 
    59 
    57 		$port = $this->port ? ":$this->port" : '';
    60 		$port = $this->port ? ":$this->port" : '';
    60 			'headers'    => array( 'Content-Type' => 'text/xml' ),
    63 			'headers'    => array( 'Content-Type' => 'text/xml' ),
    61 			'user-agent' => $this->useragent,
    64 			'user-agent' => $this->useragent,
    62 			'body'       => $xml,
    65 			'body'       => $xml,
    63 		);
    66 		);
    64 
    67 
    65 		// Merge Custom headers ala #8145
    68 		// Merge Custom headers ala #8145.
    66 		foreach ( $this->headers as $header => $value ) {
    69 		foreach ( $this->headers as $header => $value ) {
    67 			$args['headers'][ $header ] = $value;
    70 			$args['headers'][ $header ] = $value;
    68 		}
    71 		}
    69 
    72 
    70 		/**
    73 		/**
    74 		 *
    77 		 *
    75 		 * @param string[] $headers Associative array of headers to be sent.
    78 		 * @param string[] $headers Associative array of headers to be sent.
    76 		 */
    79 		 */
    77 		$args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
    80 		$args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
    78 
    81 
    79 		if ( $this->timeout !== false ) {
    82 		if ( false !== $this->timeout ) {
    80 			$args['timeout'] = $this->timeout;
    83 			$args['timeout'] = $this->timeout;
    81 		}
    84 		}
    82 
    85 
    83 		// Now send the request
    86 		// Now send the request.
    84 		if ( $this->debug ) {
    87 		if ( $this->debug ) {
    85 			echo '<pre class="ixr_request">' . htmlspecialchars( $xml ) . "\n</pre>\n\n";
    88 			echo '<pre class="ixr_request">' . htmlspecialchars( $xml ) . "\n</pre>\n\n";
    86 		}
    89 		}
    87 
    90 
    88 		$response = wp_remote_post( $url, $args );
    91 		$response = wp_remote_post( $url, $args );
   101 
   104 
   102 		if ( $this->debug ) {
   105 		if ( $this->debug ) {
   103 			echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
   106 			echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
   104 		}
   107 		}
   105 
   108 
   106 		// Now parse what we've got back
   109 		// Now parse what we've got back.
   107 		$this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
   110 		$this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
   108 		if ( ! $this->message->parse() ) {
   111 		if ( ! $this->message->parse() ) {
   109 			// XML error
   112 			// XML error.
   110 			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
   113 			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
   111 			return false;
   114 			return false;
   112 		}
   115 		}
   113 
   116 
   114 		// Is the message a fault?
   117 		// Is the message a fault?
   115 		if ( $this->message->messageType == 'fault' ) {
   118 		if ( 'fault' === $this->message->messageType ) {
   116 			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
   119 			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
   117 			return false;
   120 			return false;
   118 		}
   121 		}
   119 
   122 
   120 		// Message must be OK
   123 		// Message must be OK.
   121 		return true;
   124 		return true;
   122 	}
   125 	}
   123 }
   126 }