wp/wp-includes/class-wp-http-ixr-client.php
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 17:39:30 +0200
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
permissions -rw-r--r--
resynchronize code repo with production
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WP_HTTP_IXR_Client
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
class WP_HTTP_IXR_Client extends IXR_Client {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
	public $scheme;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
	 * @var IXR_Error
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
	public $error;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    16
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    17
	 * @param string $server
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    18
	 * @param string|bool $path
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
	 * @param int|bool $port
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
	 * @param int $timeout
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
	public function __construct($server, $path = false, $port = false, $timeout = 15) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		if ( ! $path ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
			// Assume we have been given a URL instead
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
			$bits = parse_url($server);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			$this->scheme = $bits['scheme'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
			$this->server = $bits['host'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
			$this->port = isset($bits['port']) ? $bits['port'] : $port;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			$this->path = !empty($bits['path']) ? $bits['path'] : '/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
			// Make absolutely sure we have a path
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
			if ( ! $this->path ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
				$this->path = '/';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
			if ( ! empty( $bits['query'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
				$this->path .= '?' . $bits['query'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			$this->scheme = 'http';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
			$this->server = $server;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
			$this->path = $path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			$this->port = $port;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		$this->useragent = 'The Incutio XML-RPC PHP Library';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		$this->timeout = $timeout;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    49
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    50
	 * @return bool
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    51
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
	public function query() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		$args = func_get_args();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		$method = array_shift($args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		$request = new IXR_Request($method, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		$xml = $request->getXml();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		$port = $this->port ? ":$this->port" : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		$url = $this->scheme . '://' . $this->server . $port . $this->path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		$args = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
			'headers'    => array('Content-Type' => 'text/xml'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
			'user-agent' => $this->useragent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			'body'       => $xml,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		// Merge Custom headers ala #8145
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    67
		foreach ( $this->headers as $header => $value ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			$args['headers'][$header] = $value;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    69
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    71
		/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    72
		 * Filters the headers collection to be sent to the XML-RPC server.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    73
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    74
		 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    75
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    76
		 * @param array $headers Array of headers to be sent.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    77
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    78
		$args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    79
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    80
		if ( $this->timeout !== false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			$args['timeout'] = $this->timeout;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    82
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		// Now send the request
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    85
		if ( $this->debug ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			echo '<pre class="ixr_request">' . htmlspecialchars($xml) . "\n</pre>\n\n";
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    87
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		$response = wp_remote_post($url, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		if ( is_wp_error($response) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			$errno    = $response->get_error_code();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
			$errorstr = $response->get_error_message();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			$this->error = new IXR_Error(-32300, "transport error: $errno $errorstr");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
			$this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   103
		if ( $this->debug ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   105
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		// Now parse what we've got back
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		$this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		if ( ! $this->message->parse() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
			// XML error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
			$this->error = new IXR_Error(-32700, 'parse error. not well formed');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		// Is the message a fault?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		if ( $this->message->messageType == 'fault' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
			$this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		// Message must be OK
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
}