|
14 | 14 | * options, etc. |
15 | 15 | * |
16 | 16 | * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled |
17 | | - * via the {@see 'xmlrpc_enabled'} filter found in wp_xmlrpc_server::login(). |
| 17 | + * via the {@see 'xmlrpc_enabled'} filter found in wp_xmlrpc_server::set_is_enabled(). |
18 | 18 | * |
19 | 19 | * @since 1.5.0 |
20 | 20 | * |
@@ -49,6 +49,13 @@ class wp_xmlrpc_server extends IXR_Server { |
49 | 49 | */ |
50 | 50 | protected $auth_failed = false; |
51 | 51 |
|
| 52 | + /** |
| 53 | + * Flags that XML-RPC is enabled |
| 54 | + * |
| 55 | + * @var bool |
| 56 | + */ |
| 57 | + private $is_enabled; |
| 58 | + |
52 | 59 | /** |
53 | 60 | * Registers all of the XMLRPC methods that XMLRPC server understands. |
54 | 61 | * |
@@ -164,6 +171,51 @@ public function __construct() { |
164 | 171 | * @param string[] $methods An array of XML-RPC methods, keyed by their methodName. |
165 | 172 | */ |
166 | 173 | $this->methods = apply_filters( 'xmlrpc_methods', $this->methods ); |
| 174 | + |
| 175 | + $this->set_is_enabled(); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Set wp_xmlrpc_server::$is_enabled property. |
| 180 | + * |
| 181 | + * Determine whether the xmlrpc server is enabled on this WordPress install |
| 182 | + * and set the is_enabled property accordingly. |
| 183 | + * |
| 184 | + * @since 5.7.3 |
| 185 | + */ |
| 186 | + private function set_is_enabled() { |
| 187 | + /* |
| 188 | + * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc' |
| 189 | + * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead. |
| 190 | + */ |
| 191 | + $is_enabled = apply_filters( 'pre_option_enable_xmlrpc', false ); |
| 192 | + if ( false === $is_enabled ) { |
| 193 | + $is_enabled = apply_filters( 'option_enable_xmlrpc', true ); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Filters whether XML-RPC methods requiring authentication are enabled. |
| 198 | + * |
| 199 | + * Contrary to the way it's named, this filter does not control whether XML-RPC is *fully* |
| 200 | + * enabled, rather, it only controls whether XML-RPC methods requiring authentication - such |
| 201 | + * as for publishing purposes - are enabled. |
| 202 | + * |
| 203 | + * Further, the filter does not control whether pingbacks or other custom endpoints that don't |
| 204 | + * require authentication are enabled. This behavior is expected, and due to how parity was matched |
| 205 | + * with the `enable_xmlrpc` UI option the filter replaced when it was introduced in 3.5. |
| 206 | + * |
| 207 | + * To disable XML-RPC methods that require authentication, use: |
| 208 | + * |
| 209 | + * add_filter( 'xmlrpc_enabled', '__return_false' ); |
| 210 | + * |
| 211 | + * For more granular control over all XML-RPC methods and requests, see the {@see 'xmlrpc_methods'} |
| 212 | + * and {@see 'xmlrpc_element_limit'} hooks. |
| 213 | + * |
| 214 | + * @since 3.5.0 |
| 215 | + * |
| 216 | + * @param bool $is_enabled Whether XML-RPC is enabled. Default true. |
| 217 | + */ |
| 218 | + $this->is_enabled = apply_filters( 'xmlrpc_enabled', $is_enabled ); |
167 | 219 | } |
168 | 220 |
|
169 | 221 | /** |
@@ -231,40 +283,7 @@ public function addTwoNumbers( $args ) { |
231 | 283 | * @return WP_User|false WP_User object if authentication passed, false otherwise |
232 | 284 | */ |
233 | 285 | public function login( $username, $password ) { |
234 | | - /* |
235 | | - * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc' |
236 | | - * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead. |
237 | | - */ |
238 | | - $enabled = apply_filters( 'pre_option_enable_xmlrpc', false ); |
239 | | - if ( false === $enabled ) { |
240 | | - $enabled = apply_filters( 'option_enable_xmlrpc', true ); |
241 | | - } |
242 | | - |
243 | | - /** |
244 | | - * Filters whether XML-RPC methods requiring authentication are enabled. |
245 | | - * |
246 | | - * Contrary to the way it's named, this filter does not control whether XML-RPC is *fully* |
247 | | - * enabled, rather, it only controls whether XML-RPC methods requiring authentication - such |
248 | | - * as for publishing purposes - are enabled. |
249 | | - * |
250 | | - * Further, the filter does not control whether pingbacks or other custom endpoints that don't |
251 | | - * require authentication are enabled. This behavior is expected, and due to how parity was matched |
252 | | - * with the `enable_xmlrpc` UI option the filter replaced when it was introduced in 3.5. |
253 | | - * |
254 | | - * To disable XML-RPC methods that require authentication, use: |
255 | | - * |
256 | | - * add_filter( 'xmlrpc_enabled', '__return_false' ); |
257 | | - * |
258 | | - * For more granular control over all XML-RPC methods and requests, see the {@see 'xmlrpc_methods'} |
259 | | - * and {@see 'xmlrpc_element_limit'} hooks. |
260 | | - * |
261 | | - * @since 3.5.0 |
262 | | - * |
263 | | - * @param bool $enabled Whether XML-RPC is enabled. Default true. |
264 | | - */ |
265 | | - $enabled = apply_filters( 'xmlrpc_enabled', $enabled ); |
266 | | - |
267 | | - if ( ! $enabled ) { |
| 286 | + if ( ! $this->is_enabled ) { |
268 | 287 | $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) ); |
269 | 288 | return false; |
270 | 289 | } |
@@ -335,6 +354,30 @@ public function escape( &$data ) { |
335 | 354 | } |
336 | 355 | } |
337 | 356 |
|
| 357 | + /** |
| 358 | + * Send error response to client. |
| 359 | + * |
| 360 | + * Send an XML error response to the client. If the endpoint is enabled |
| 361 | + * an HTTP 200 response is always sent per the XML-RPC specification. |
| 362 | + * |
| 363 | + * @since 5.7.3 |
| 364 | + * |
| 365 | + * @param IXR_Error|string $error Error code or an error object. |
| 366 | + * @param false $message Error message. Optional. |
| 367 | + */ |
| 368 | + public function error( $error, $message = false ) { |
| 369 | + // Accepts either an error object or an error code and message |
| 370 | + if ( $message && ! is_object( $error ) ) { |
| 371 | + $error = new IXR_Error( $error, $message ); |
| 372 | + } |
| 373 | + |
| 374 | + if ( ! $this->is_enabled ) { |
| 375 | + status_header( $error->code ); |
| 376 | + } |
| 377 | + |
| 378 | + $this->output( $error->getXml() ); |
| 379 | + } |
| 380 | + |
338 | 381 | /** |
339 | 382 | * Retrieve custom fields for post. |
340 | 383 | * |
|
0 commit comments