false, 'keep_woo_reviews' => true, 'emojis' => true, 'embeds' => false, 'head_links' => true, 'self_pingbacks' => true, 'dashicons' => false, 'feeds' => false, 'heartbeat' => 'default', 'xmlrpc' => false, 'app_passwords' => false, 'generator' => true, 'user_enumeration' => false, 'file_editor' => false, 'security_headers' => false, 'hsts' => false, ); } /** * Settings fields. * * @return array */ public function get_fields() { $checkbox = static function ( $label, $text ) { return array( 'type' => 'checkbox', 'label' => $label, 'checkbox_label' => $text, ); }; return array( 'speed_heading' => array( 'type' => 'heading', 'label' => __( 'Speed', 'toolvane' ), ), 'emojis' => $checkbox( __( 'Emojis', 'toolvane' ), __( 'Remove the emoji script and styles (saves a request on every page; emojis still show natively).', 'toolvane' ) ), 'embeds' => $checkbox( __( 'oEmbed host', 'toolvane' ), __( 'Stop other sites from embedding your posts and remove wp-embed.js. You can still embed YouTube and others.', 'toolvane' ) ), 'head_links' => $checkbox( __( 'Head clutter', 'toolvane' ), __( 'Remove the RSD, Windows Live Writer and shortlink tags from the page head.', 'toolvane' ) ), 'self_pingbacks' => $checkbox( __( 'Self pingbacks', 'toolvane' ), __( 'Do not ping your own posts when you link to them.', 'toolvane' ) ), 'dashicons' => $checkbox( __( 'Dashicons', 'toolvane' ), __( 'Do not load the Dashicons font for logged-out visitors (keep it if your theme uses Dashicons).', 'toolvane' ) ), 'heartbeat' => array( 'type' => 'select', 'label' => __( 'Heartbeat API', 'toolvane' ), 'options' => array( 'default' => __( 'WordPress default', 'toolvane' ), 'slow' => __( 'Slow down to every 60 seconds', 'toolvane' ), 'frontend' => __( 'Disable on the front end, slow down in wp-admin', 'toolvane' ), ), 'description' => __( 'Heartbeat keeps post locking and autosave working. It is never disabled inside the editors.', 'toolvane' ), ), 'content_heading' => array( 'type' => 'heading', 'label' => __( 'Content', 'toolvane' ), ), 'comments' => $checkbox( __( 'Comments', 'toolvane' ), __( 'Disable comments site-wide: front end, admin menu, REST API and toolbar.', 'toolvane' ) ), 'keep_woo_reviews' => $checkbox( __( 'WooCommerce reviews', 'toolvane' ), __( 'Keep product reviews working when comments are disabled.', 'toolvane' ) ), 'feeds' => $checkbox( __( 'RSS feeds', 'toolvane' ), __( 'Disable RSS and Atom feeds and redirect them to the matching page.', 'toolvane' ) ), 'security_heading' => array( 'type' => 'heading', 'label' => __( 'Security', 'toolvane' ), ), 'xmlrpc' => $checkbox( __( 'XML-RPC', 'toolvane' ), __( 'Disable XML-RPC and pingbacks. Leave it on if you use Jetpack or the WordPress mobile app.', 'toolvane' ) ), 'app_passwords' => $checkbox( __( 'Application passwords', 'toolvane' ), __( 'Disable application passwords if no external app needs REST API access.', 'toolvane' ) ), 'generator' => $checkbox( __( 'Version number', 'toolvane' ), __( 'Hide the WordPress version from the page source and feeds.', 'toolvane' ) ), 'user_enumeration' => $checkbox( __( 'User enumeration', 'toolvane' ), __( 'Block ?author=N scans and the public REST API users endpoint for visitors. Logged-in users are not affected.', 'toolvane' ) ), 'file_editor' => $checkbox( __( 'File editor', 'toolvane' ), __( 'Disable the theme and plugin file editor in wp-admin.', 'toolvane' ) ), 'security_headers' => $checkbox( __( 'Security headers', 'toolvane' ), __( 'Send X-Content-Type-Options, X-Frame-Options (same origin), Referrer-Policy and Permissions-Policy headers. Your site can then no longer be shown in frames on other domains.', 'toolvane' ) ), 'hsts' => $checkbox( __( 'HSTS', 'toolvane' ), __( 'Tell browsers to always use HTTPS for one year (Strict-Transport-Security). Only enable this when your whole site works over HTTPS.', 'toolvane' ) ), ); } /** * Registers hooks. */ public function init() { if ( $this->get( 'comments' ) ) { $this->disable_comments(); } if ( $this->get( 'emojis' ) ) { add_action( 'init', array( $this, 'disable_emojis' ) ); } if ( $this->get( 'xmlrpc' ) ) { add_filter( 'xmlrpc_enabled', '__return_false' ); add_filter( 'xmlrpc_methods', '__return_empty_array' ); add_filter( 'wp_headers', array( $this, 'remove_pingback_header' ) ); add_filter( 'pings_open', '__return_false', 20 ); remove_action( 'wp_head', 'rsd_link' ); if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { add_action( 'init', array( $this, 'block_xmlrpc' ), 0 ); } } if ( $this->get( 'app_passwords' ) ) { add_filter( 'wp_is_application_passwords_available', '__return_false' ); } if ( $this->get( 'embeds' ) ) { add_action( 'init', array( $this, 'disable_embeds' ), 9999 ); } if ( $this->get( 'generator' ) ) { remove_action( 'wp_head', 'wp_generator' ); add_filter( 'the_generator', '__return_empty_string' ); } if ( $this->get( 'head_links' ) ) { remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' ); remove_action( 'wp_head', 'wp_shortlink_wp_head' ); remove_action( 'template_redirect', 'wp_shortlink_header', 11 ); } if ( $this->get( 'self_pingbacks' ) ) { add_action( 'pre_ping', array( $this, 'no_self_ping' ) ); } if ( $this->get( 'user_enumeration' ) ) { add_filter( 'rest_endpoints', array( $this, 'hide_user_endpoints' ) ); add_action( 'template_redirect', array( $this, 'block_author_scan' ), 1 ); } if ( $this->get( 'file_editor' ) && ! defined( 'DISALLOW_FILE_EDIT' ) ) { define( 'DISALLOW_FILE_EDIT', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- WordPress core constant. } if ( $this->get( 'dashicons' ) ) { add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_dashicons' ), 100 ); } if ( $this->get( 'feeds' ) ) { $this->disable_feeds(); } if ( 'default' !== $this->get( 'heartbeat', 'default' ) ) { add_filter( 'heartbeat_settings', array( $this, 'heartbeat_settings' ) ); if ( 'frontend' === $this->get( 'heartbeat' ) ) { add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_heartbeat' ), 100 ); } } if ( $this->get( 'security_headers' ) || $this->get( 'hsts' ) ) { add_action( 'send_headers', array( $this, 'send_security_headers' ) ); add_action( 'login_init', array( $this, 'send_security_headers' ) ); add_action( 'admin_init', array( $this, 'send_security_headers' ) ); } } /* * Comments. */ /** * Registers all comment-disabling hooks. */ private function disable_comments() { add_filter( 'comments_open', array( $this, 'filter_open' ), 20, 2 ); add_filter( 'pings_open', array( $this, 'filter_open' ), 20, 2 ); add_filter( 'comments_array', array( $this, 'filter_comments_array' ), 20, 2 ); add_filter( 'get_comments_number', array( $this, 'filter_comments_number' ), 20, 2 ); add_action( 'admin_init', array( $this, 'comments_admin' ) ); add_action( 'admin_menu', array( $this, 'comments_menu' ), 999 ); add_action( 'admin_bar_menu', array( $this, 'comments_admin_bar' ), 999 ); add_filter( 'rest_endpoints', array( $this, 'comments_rest' ) ); add_filter( 'feed_links_show_comments_feed', '__return_false' ); add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_comment_reply' ), 100 ); add_action( 'widgets_init', array( $this, 'unregister_comment_widget' ) ); } /** * Whether WooCommerce reviews are kept. * * @return bool */ private function keep_reviews() { return $this->get( 'keep_woo_reviews' ) && class_exists( 'WooCommerce' ); } /** * Whether comments stay on for this post (WooCommerce reviews). * * @param int|\WP_Post|null $post Post. * @return bool */ private function is_exempt( $post ) { return $this->keep_reviews() && 'product' === get_post_type( $post ); } /** * Closes comments and pings. * * @param bool $open Whether open. * @param int $post_id Post ID. * @return bool */ public function filter_open( $open, $post_id ) { return $this->is_exempt( $post_id ) ? $open : false; } /** * Hides existing comments. * * @param array $comments Comments. * @param int $post_id Post ID. * @return array */ public function filter_comments_array( $comments, $post_id ) { return $this->is_exempt( $post_id ) ? $comments : array(); } /** * Shows zero comments. * * @param int $count Count. * @param int $post_id Post ID. * @return int */ public function filter_comments_number( $count, $post_id ) { return $this->is_exempt( $post_id ) ? $count : 0; } /** * Removes comment support and admin screens. */ public function comments_admin() { global $pagenow; foreach ( get_post_types() as $type ) { if ( 'product' === $type && $this->keep_reviews() ) { continue; } if ( post_type_supports( $type, 'comments' ) ) { remove_post_type_support( $type, 'comments' ); remove_post_type_support( $type, 'trackbacks' ); } } remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); if ( in_array( $pagenow, array( 'edit-comments.php', 'options-discussion.php' ), true ) && ! $this->keep_reviews() ) { wp_safe_redirect( admin_url() ); exit; } } /** * Removes the comments menu. */ public function comments_menu() { remove_menu_page( 'edit-comments.php' ); if ( ! $this->keep_reviews() ) { remove_submenu_page( 'options-general.php', 'options-discussion.php' ); } } /** * Removes the toolbar comments bubble. * * @param \WP_Admin_Bar $bar Toolbar. */ public function comments_admin_bar( $bar ) { $bar->remove_node( 'comments' ); } /** * Removes REST comment routes unless WooCommerce reviews are kept. * * @param array $endpoints Endpoints. * @return array */ public function comments_rest( $endpoints ) { if ( $this->keep_reviews() ) { return $endpoints; } foreach ( array_keys( $endpoints ) as $route ) { if ( 0 === strpos( $route, '/wp/v2/comments' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /** * Removes the comment reply script. */ public function dequeue_comment_reply() { if ( ! is_singular() || ! $this->is_exempt( get_queried_object_id() ) ) { wp_dequeue_script( 'comment-reply' ); } } /** * Removes the Recent Comments widget. */ public function unregister_comment_widget() { unregister_widget( 'WP_Widget_Recent_Comments' ); } /* * Feeds. */ /** * Registers feed-disabling hooks. */ private function disable_feeds() { foreach ( array( 'do_feed', 'do_feed_rdf', 'do_feed_rss', 'do_feed_rss2', 'do_feed_atom', 'do_feed_rss2_comments', 'do_feed_atom_comments' ) as $hook ) { add_action( $hook, array( $this, 'redirect_feed' ), 1 ); } remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'feed_links_extra', 3 ); } /** * Redirects feed requests to the matching page. */ public function redirect_feed() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); $target = preg_replace( '#/(feed|rss|rss2|rdf|atom)(/(rss|rss2|rdf|atom))?/?$#', '/', $path ); $target = ( $target && $target !== $path ) ? $target : home_url( '/' ); wp_safe_redirect( $target, 301 ); exit; } /* * Emojis, embeds and other features. */ /** * Removes emoji scripts and styles. */ public function disable_emojis() { remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( 'embed_head', 'print_emoji_detection_script' ); remove_action( 'wp_print_styles', 'print_emoji_styles' ); remove_action( 'admin_print_styles', 'print_emoji_styles' ); remove_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' ); remove_action( 'admin_enqueue_scripts', 'wp_enqueue_emoji_styles' ); remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); add_filter( 'tiny_mce_plugins', array( $this, 'remove_tinymce_emoji' ) ); add_filter( 'wp_resource_hints', array( $this, 'remove_emoji_dns_prefetch' ), 10, 2 ); } /** * Removes the TinyMCE emoji plugin. * * @param array $plugins TinyMCE plugins. * @return array */ public function remove_tinymce_emoji( $plugins ) { return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array(); } /** * Removes the emoji CDN DNS prefetch hint. * * @param array $urls URLs. * @param string $relation Relation type. * @return array */ public function remove_emoji_dns_prefetch( $urls, $relation ) { if ( 'dns-prefetch' !== $relation ) { return $urls; } return array_filter( $urls, static function ( $url ) { // The emoji CDN hint ends with the core emoji image folder. return ! is_string( $url ) || false === strpos( $url, '/core/emoji/' ); } ); } /** * Disables the oEmbed host features. */ public function disable_embeds() { remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); remove_action( 'wp_head', 'wp_oembed_add_host_js' ); remove_action( 'rest_api_init', 'wp_oembed_register_route' ); remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); add_action( 'wp_footer', array( $this, 'dequeue_embed_script' ) ); } /** * Removes wp-embed.js. */ public function dequeue_embed_script() { wp_dequeue_script( 'wp-embed' ); } /** * Rejects every XML-RPC request, including system.multicall brute-force attempts. */ public function block_xmlrpc() { status_header( 403 ); nocache_headers(); header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); echo 'faultCode403faultString' . esc_html__( 'XML-RPC services are disabled on this site.', 'toolvane' ) . ''; exit; } /** * Removes the X-Pingback header. * * @param array $headers Headers. * @return array */ public function remove_pingback_header( $headers ) { unset( $headers['X-Pingback'] ); return $headers; } /** * Removes links to this site from the ping list. * * @param array $links Links (by reference). */ public function no_self_ping( &$links ) { $home = home_url(); foreach ( $links as $i => $link ) { if ( 0 === strpos( $link, $home ) ) { unset( $links[ $i ] ); } } } /** * Hides the users REST endpoints from visitors. * * @param array $endpoints Endpoints. * @return array */ public function hide_user_endpoints( $endpoints ) { if ( is_user_logged_in() ) { return $endpoints; } foreach ( array_keys( $endpoints ) as $route ) { if ( 0 === strpos( $route, '/wp/v2/users' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /** * Blocks ?author=N scans. */ public function block_author_scan() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check. if ( ! is_user_logged_in() && isset( $_GET['author'] ) && is_numeric( $_GET['author'] ) ) { wp_safe_redirect( home_url( '/' ), 301 ); exit; } } /** * Removes Dashicons for visitors. */ public function dequeue_dashicons() { if ( ! is_user_logged_in() ) { wp_dequeue_style( 'dashicons' ); } } /** * Heartbeat interval. * * @param array $settings Heartbeat settings. * @return array */ public function heartbeat_settings( $settings ) { $settings['interval'] = 60; return $settings; } /** * Removes Heartbeat on the front end, except in page builder previews. */ public function dequeue_heartbeat() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only detection of editor previews. if ( is_customize_preview() || isset( $_GET['elementor-preview'] ) || isset( $_GET['fl_builder'] ) || isset( $_GET['et_fb'] ) || isset( $_GET['bricks'] ) ) { return; } wp_deregister_script( 'heartbeat' ); } /** * Sends security headers. */ public function send_security_headers() { if ( headers_sent() ) { return; } if ( $this->get( 'security_headers' ) ) { header( 'X-Content-Type-Options: nosniff' ); header( 'X-Frame-Options: SAMEORIGIN' ); header( 'Referrer-Policy: strict-origin-when-cross-origin' ); header( 'Permissions-Policy: camera=(), microphone=(), geolocation=(self), payment=(self)' ); } if ( $this->get( 'hsts' ) && is_ssl() ) { header( 'Strict-Transport-Security: max-age=31536000' ); } } }