` deny block * written to .htaccess, which refuses the request before PHP is reached. * - Nginx, native-mode LiteSpeed, IIS: a PHP gate on 'init' that answers 403 and exits. * Without it the toggle would save cleanly and leave xmlrpc.php fully reachable. * * Both paths refuse everything except the IP allowlist Pro supplies through * `adminease_disable_xmlrpc_allowed_ips`, so the feature behaves the same either way. */ class DisableXmlRpc { private array $settings; public function __construct() { $this->settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); add_action( 'adminease_settings_saved', [ $this, 'adminease_settings_saved' ] ); if( !empty( $this->settings['disable_xmlrpc'] ) && !Utils::is_apache_compatible() ) { add_action( 'init', [ $this, 'php_block_xmlrpc' ], 1 ); } if( !empty( $this->settings['disable_xmlrpc'] ) ) { add_filter( 'xmlrpc_enabled', [ $this, 'xmlrpc_enabled' ], 99 ); add_filter( 'xmlrpc_methods', [ $this, 'xmlrpc_methods' ], 99 ); } } /** * Turns XML-RPC off through WordPress, unless the caller is on the allowlist. * Untyped parameter: other plugins may pass anything. * * @param mixed $enabled Whether XML-RPC is enabled. * * @return mixed False for blocked callers, the incoming value otherwise. */ public function xmlrpc_enabled( $enabled ) { if( $this->is_allowed_request() ) { return $enabled; } return false; } /** * Removes every XML-RPC method, including pingback.ping, unless the caller is on * the allowlist. Untyped parameter: other plugins may pass anything. * * @param mixed $methods The registered XML-RPC methods. * * @return mixed An empty array for blocked callers, the incoming value otherwise. */ public function xmlrpc_methods( $methods ) { if( $this->is_allowed_request() ) { return $methods; } return []; } /** * Tells whether the caller's IP is on the allowlist Pro supplies. * * @return bool True when the caller may use XML-RPC. */ private function is_allowed_request(): bool { $allow_ips = (string) apply_filters( 'adminease_disable_xmlrpc_allowed_ips', '' ); return '' !== $allow_ips && Utils::ip_in_allowlist( Utils::get_client_ip(), $allow_ips ); } /** * Adds a new settings field for disabling XML-RPC functionality to the provided fields array. * The setting is included in the security section with various configuration options, allowing users * to enable or disable XML-RPC based on their needs to enhance site security. * * @param array $fields The array of existing settings fields to which the new field will be added. * * @return array The modified array of settings fields including the new XML-RPC disable option. */ public function adminease_settings_fields( array $fields ): array { $field = [ 'type' => 'switch', 'id' => 'disable-xmlrpc', 'name' => 'adminease[security][disable_xmlrpc]', 'value' => $this->settings['disable_xmlrpc'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Disable XML-RPC', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'The mobile app, Jetpack and some remote publishing tools stop working.', 'adminease' ), 'description' => __( "

XML-RPC is a feature in WordPress that allows external apps and services to connect to your site-for example, to publish posts remotely or manage content via mobile apps. However, it's also a common target for brute force and DDoS attacks. If you're not using any tools that require XML-RPC, it's a good idea to disable it to improve your site's security.

If you need XML-RPC access from specific locations, you can add their IP addresses. One IP address per line, supporting both IPv4 and IPv6 formats.

Examples:
IPv4: 192.168.1.1
IPv6: 2001:db8:85a3::8a2e:370:7334

Important: When XML-RPC is disabled, only requests from these IPs will be allowed, while all other requests will be denied. Leave empty to block all access to xmlrpc.php.

", 'adminease' ), ]; $field['conflicts'] = [ 'jetpack/jetpack.php' => __( 'Jetpack is active and needs XML-RPC to connect to WordPress.com. Turning this on can disconnect it.', 'adminease' ), ]; $php_notice = Utils::get_php_enforcement_notice(); if( '' !== $php_notice ) { $field['field_description'] = $php_notice; } $fields['security']['fields'][] = $field; return $fields; } /** * Processes and saves the admin ease settings, specifically managing the XML-RPC access rules. * * @param array $sanitized_settings An associative array containing the sanitized settings. * Expects 'disable_xmlrpc' key to determine whether XML-RPC should be disabled. * * @return void */ public function adminease_settings_saved( array $sanitized_settings ): void { if( !Utils::is_apache_compatible() ) { if( file_exists( ABSPATH . '.htaccess' ) ) { FileHandler::get_instance()->stack_htaccess_rule( 'DISABLE_XMLRPC', '' ); } return; } if( !empty( $sanitized_settings['security']['disable_xmlrpc'] ) ) { $code = "\n"; $code .= "order deny,allow\n"; $code .= "deny from all\n"; $code .= ""; } else { $code = ''; } $code = apply_filters( 'adminease_disable_xmlrpc_code', $code ); FileHandler::get_instance()->stack_htaccess_rule( 'DISABLE_XMLRPC', $code ); } /** * PHP-layer XML-RPC gate for servers that do not read .htaccess. * * Mirrors what the .htaccess block does on Apache: every request to xmlrpc.php is * refused with a 403 unless the caller's IP is on the allowlist. xmlrpc.php defines * XMLRPC_REQUEST before it loads WordPress and only calls serve_request() afterwards, * so testing that constant is exact and 'init' is early enough to stop the request. * * Runs at priority 1 to match BlockSpecificBots::php_block_bots(). 'init' is used * because the request and the allowlist filter are ready by then. * * @return void */ public function php_block_xmlrpc(): void { if( !defined( 'XMLRPC_REQUEST' ) || !XMLRPC_REQUEST ) { return; } if( $this->is_allowed_request() ) { return; } nocache_headers(); header( 'Content-Type: text/plain; charset=utf-8' ); status_header( 403 ); echo 'Forbidden'; exit; } }