settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['disable_rest_api'] ) ) { 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_filter( 'rest_authentication_errors', [ $this, 'check_rest_access' ], 110 ); } if( !empty( $this->settings['hide_rest_index'] ) ) { add_filter( 'rest_authentication_errors', [ $this, 'hide_rest_index' ], 110 ); } } /** * 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' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Disable REST API', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Plugins, themes and apps that use the REST API can stop working.', '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' ), 'conflicts' => [ 'contact-form-7/wp-contact-form-7.php' => __( 'Contact Form 7 sends its forms through the REST API. Turning this on can stop the forms from sending.', 'adminease' ), 'woocommerce/woocommerce.php' => __( 'WooCommerce uses the REST API for parts of the admin and the block checkout. Turning this on can break them.', 'adminease' ), 'jetpack/jetpack.php' => __( 'Jetpack uses the REST API to connect to WordPress.com. Turning this on can disconnect it.', 'adminease' ), ], 'child_fields' => [ [ 'type' => 'switch', 'id' => 'enable-rest-api-loggedin', 'name' => 'adminease[security][enable_rest_api_loggedin]', 'value' => $this->settings['enable_rest_api_loggedin'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Also allow other logged-in users (customers, members)', 'adminease' ), 'description' => __( 'Allows logged-in users to keep using the REST API while all anonymous requests remain blocked. Outgoing communication with wordpress.org — plugin and theme updates, version checks — is unaffected by this setting either way.', 'adminease' ), 'field_description' => __( "Note: Users who can edit posts always keep REST API access, so Gutenberg, Elementor and the media library keep working in wp-admin. Enable this option to let every other logged-in user, such as customers or members, keep using it too.", 'adminease' ), 'attributes' => [ 'data-parent' => 'disable-rest-api', ], ], ], ]; $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'hide-rest-index', 'name' => 'adminease[security][hide_rest_index]', 'value' => $this->settings['hide_rest_index'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Hide the REST API route list', 'adminease' ), 'description' => __( "Hides the list of all API addresses at /wp-json/ from visitors who are not logged in, so they can't see which plugins you use. Each plugin's own API keeps working. Not needed when Disable REST API is on.", 'adminease' ), ]; return $fields; } /** * Checks and manages access to the WordPress REST API based on plugin settings and user authentication. * * @param mixed $result The current REST API access result. Default null or pre-existing authentication error. * * @return mixed The modified access result, which may be a WP_Error if access is disabled, or the original result if no restrictions apply. */ public function check_rest_access( $result ) { if( is_wp_error( $result ) ) { return $result; } if( apply_filters( 'adminease_disable_rest_api_bypass', false ) ) { return $result; } if( current_user_can( 'edit_posts' ) ) { return $result; } if( !empty( $this->settings['enable_rest_api_loggedin'] ) && is_user_logged_in() ) { return $result; } if( !empty( $this->settings['disable_rest_api'] ) ) { return new WP_Error( 'rest_api_disabled', esc_html__( 'REST API is disabled.', 'adminease' ), [ 'status' => 403 ] ); } return $result; } /** * Answers the REST API index, the list of every route, with a 401 for visitors who are not logged in. * * @param mixed $result The current REST API access result. Default null or pre-existing authentication error. * * @return mixed A WP_Error for a visitor asking for the index, otherwise the result unchanged. */ public function hide_rest_index( $result ) { if( is_wp_error( $result ) || is_user_logged_in() ) { return $result; } if( '/' !== Utils::current_rest_route() ) { return $result; } return new WP_Error( 'adminease_rest_index_disabled', esc_html__( 'The REST API route list is not available.', 'adminease' ), [ 'status' => 401 ] ); } }