settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['disable_rest_api'] ) ) { // Remove REST API info from head and headers remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' ); remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); remove_action( 'template_redirect', 'rest_output_link_header', 11 ); // Add authentication check add_filter( 'rest_authentication_errors', [ $this, 'check_rest_access' ] ); } } /** * Modifies or appends custom settings fields to the existing settings array. * * @param array $fields The existing array of settings fields. * * @return array The updated array of settings fields with modifications or additions. */ public function adminease_settings_fields( array $fields ): array { $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-rest-api', 'name' => 'adminease[security][disable_rest_api]', 'value' => $this->settings['disable_rest_api'] ?? false, 'label_class' => 'adminease-switch', 'input_class' => 'form-control toggle-field', 'label' => __( 'Disable REST API', 'adminease' ), 'description' => __( "The WordPress REST API allows external applications and services to interact with your website’s data, such as posts, users, and settings. While it’s useful for developers building custom features or apps, it can also expose sensitive information—especially on sites that don’t need it. Disabling the REST API helps reduce potential security risks and protect user privacy. You can disable it completely or restrict access to logged-in users using code or a security plugin.", 'adminease' ), ]; return $fields; } /** * Checks access to the REST API based on the configured security settings. * Determines whether to allow or deny access based on the 'disable_rest_api' and 'disable_rest_api_allow_ips' options. * If REST API access is completely disabled, an error response is returned. * * @param mixed $result The current authentication result passed from other filters. If not empty, it bypasses further checks. * * @return mixed|\WP_Error The original result if not empty, or a WP_Error if access is denied, otherwise null. */ public function check_rest_access( $result ) { // Pass through other authentication errors if( !empty( $result ) ) { return $result; } // Mode: Complete Disable if( !empty( $this->settings['disable_rest_api'] ) && empty( $this->settings['disable_rest_api_allow_ips'] ) ) { return new \WP_Error( 'rest_api_disabled', esc_html__( 'REST API is disabled.', 'adminease' ), [ 'status' => 403 ] ); } return $result; } }