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