` 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' ] ); // PHP-layer gate for servers that never read .htaccess, matching the pattern used // by BlockSpecificBots::php_block_bots(). if( !empty( $this->settings['disable_xmlrpc'] ) && !Utils::is_apache_compatible() ) { add_action( 'init', [ $this, 'php_block_xmlrpc' ], 1 ); } } /** * 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' ), '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' ), ]; // Added only when there is something to say, so Apache users get no extra line. $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 { // Servers that ignore .htaccess are handled by php_block_xmlrpc() instead. The rule // is still cleared rather than skipped, so a site moved from Apache to Nginx does // not keep a stale block in a file nothing reads any more. The file is never // created just to empty it. 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(). An earlier hook is * not usable: pluggable.php has not loaded during 'plugins_loaded'. * * @return void */ public function php_block_xmlrpc(): void { if( !defined( 'XMLRPC_REQUEST' ) || !XMLRPC_REQUEST ) { return; } // Pro supplies the allowlist here so it applies on every server type, not just on // the Apache path it writes rewrite rules for. $allow_ips = (string) apply_filters( 'adminease_disable_xmlrpc_allowed_ips', '' ); if( '' !== $allow_ips && Utils::ip_in_allowlist( Utils::get_client_ip(), $allow_ips ) ) { return; } nocache_headers(); header( 'Content-Type: text/plain; charset=utf-8' ); status_header( 403 ); echo 'Forbidden'; exit; } }