settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); Utils::keep_hidden_settings( 'posts', $this->get_hidden_keys() ); if( empty( $this->settings['unused_pages'] ) ) { return; } if( $this->is_on( 'unused_pages_attachment' ) && $this->attachment_option_available() ) { add_filter( 'pre_option_wp_attachment_pages_enabled', [ $this, 'attachment_pages_option' ] ); } if( $this->is_on( 'unused_pages_sitemap' ) && Utils::wp_at_least( '5.5' ) ) { add_filter( 'wp_sitemaps_enabled', '__return_false' ); } if( $this->is_on( 'unused_pages_redirect_guess' ) && Utils::wp_at_least( '5.5' ) ) { add_filter( 'do_redirect_guess_404_permalink', '__return_false' ); } if( is_admin() ) { return; } add_action( 'template_redirect', [ $this, 'maybe_send_404' ], 1 ); if( $this->is_on( 'unused_pages_author' ) ) { add_filter( 'author_link', [ $this, 'author_link' ] ); } if( $this->is_on( 'unused_pages_search' ) ) { add_filter( 'request', [ $this, 'block_search_request' ] ); add_filter( 'get_search_form', '__return_empty_string' ); add_action( 'admin_bar_menu', [ $this, 'remove_admin_bar_search' ], 999 ); if( Utils::wp_at_least( '5.7' ) ) { add_filter( 'render_block_core/search', '__return_empty_string' ); } else { add_filter( 'render_block', [ $this, 'hide_search_block' ], 10, 2 ); } } } /** * Tells whether a smaller switch is on. * * @param string $key The setting key. * * @return bool */ private function is_on( string $key ): bool { return ! empty( $this->settings[ $key ] ); } /** * Checks if core has its own attachment pages option (WordPress 6.4 and newer). * * @return bool */ private function attachment_option_available(): bool { return Utils::wp_at_least( '6.4' ); } /** * Lists the setting keys hidden on this site, so a save does not wipe them. * * @return array */ private function get_hidden_keys(): array { if( Utils::wp_at_least( '5.5' ) ) { return []; } return [ 'unused_pages_sitemap', 'unused_pages_redirect_guess' ]; } /** * Builds one smaller switch. * * @param string $key The setting key, e.g. unused_pages_author. * @param string $label The translated label. * @param string $description The translated description. * @param string $risk 'safe' or 'careful'. * @param string $breaks One short translated sentence about what it can affect, or ''. * * @return array The field definition. */ private function child_field( string $key, string $label, string $description, string $risk, string $breaks = '' ): array { return [ 'type' => 'switch', 'id' => str_replace( '_', '-', $key ), 'name' => 'adminease[posts][' . $key . ']', 'value' => $this->settings[ $key ] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => $label, 'field_description' => $description, 'risk' => $risk, 'breaks' => $breaks, 'attributes' => [ 'data-parent' => 'unused-pages', ], ]; } /** * Adds the main switch and the smaller switches to the Posts tab. * * @param array $fields The settings fields keyed by tab. * * @return array The modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $children = [ $this->child_field( 'unused_pages_author', __( 'Author pages', 'adminease' ), __( 'Turns off the page WordPress makes for every author. Links to an author go to your home page.', 'adminease' ), 'careful', __( 'Bylines that link to the author now lead to the home page.', 'adminease' ) ), $this->child_field( 'unused_pages_date', __( 'Date archives', 'adminease' ), __( 'Turns off the pages that list posts by year, month or day. Posts with a date in their address keep working.', 'adminease' ), 'safe', __( 'Archive widgets and blocks that link to a month now lead to a 404 page.', 'adminease' ) ), $this->child_field( 'unused_pages_attachment', __( 'Attachment pages', 'adminease' ), __( 'Turns off the separate page WordPress makes for every uploaded file. Visitors go to the file itself.', 'adminease' ), 'safe', __( 'Browsers remember the redirect, so it can take a while to go away after you turn this off.', 'adminease' ) ), $this->child_field( 'unused_pages_search', __( 'Search', 'adminease' ), __( 'Turns off your site search, the search form and the search block.', 'adminease' ), 'careful', __( 'WooCommerce product search stops working.', 'adminease' ) ), ]; if( Utils::wp_at_least( '5.5' ) ) { $children[] = $this->child_field( 'unused_pages_sitemap', __( 'Built-in sitemap', 'adminease' ), __( 'Turns off the sitemap WordPress makes for your site. Sitemaps from SEO plugins are not affected.', 'adminease' ), 'safe' ); $children[] = $this->child_field( 'unused_pages_redirect_guess', __( '"Did you mean" redirects', 'adminease' ), __( 'Stops WordPress from guessing the right post when an address is mistyped. Visitors see a 404 page instead.', 'adminease' ), 'safe' ); } $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'unused-pages', 'name' => 'adminease[posts][unused_pages]', 'value' => $this->settings['unused_pages'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Turn off pages you don\'t use', 'adminease' ), 'description' => __( 'WordPress makes pages many sites never use. Pick the ones to switch off. Visitors who open them see your 404 page.', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Depends on which pages you switch off below.', 'adminease' ), 'child_fields' => $children, ]; return $fields; } /** * Answers 404 for the author and date pages that are switched off. * On WordPress before 6.4 it also sends attachment pages to the file. * * @return void */ public function maybe_send_404(): void { if( $this->is_on( 'unused_pages_author' ) && is_author() ) { Utils::send_404(); return; } if( $this->is_on( 'unused_pages_date' ) && is_date() ) { Utils::send_404(); return; } if( $this->is_on( 'unused_pages_attachment' ) && ! $this->attachment_option_available() && is_attachment() ) { $this->redirect_attachment(); } } /** * Sends an attachment page to the file, the way core does on WordPress 6.4 and newer. * A file attached to a post that visitors cannot see is left alone. * * @return void */ private function redirect_attachment(): void { $id = get_queried_object_id(); $url = $id ? wp_get_attachment_url( $id ) : false; if( ! $url ) { return; } $parent = (int) wp_get_post_parent_id( $id ); if( $parent && 'publish' !== get_post_status( $parent ) && ! current_user_can( 'read_post', $parent ) ) { return; } wp_redirect( $url, 302 ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- The target is the file's own address from the media library. exit; } /** * Turns attachment pages off through core's own option (WordPress 6.4 and newer). * The Media settings screen keeps the real value. * * @param mixed $pre The value before the option is read. * * @return mixed */ public function attachment_pages_option( $pre ) { global $pagenow; if( is_admin() && 'options.php' === $pagenow ) { return $pre; } return '0'; } /** * Sends author links to the home page. * * @param mixed $link The author page address. * * @return mixed */ public function author_link( $link ) { return home_url( '/' ); } /** * Turns a search request into a 404. * * @param mixed $query_vars The request query variables. * * @return mixed */ public function block_search_request( $query_vars ) { if( ! is_array( $query_vars ) || ! isset( $query_vars['s'] ) ) { return $query_vars; } unset( $query_vars['s'] ); $query_vars['error'] = '404'; return $query_vars; } /** * Hides the search block on WordPress before 5.7, which has no per block filter. * * @param mixed $content The rendered block. * @param mixed $block The parsed block. * * @return mixed */ public function hide_search_block( $content, $block = [] ) { if( is_array( $block ) && isset( $block['blockName'] ) && 'core/search' === $block['blockName'] ) { return ''; } return $content; } /** * Removes the search box from the admin bar. * * @param \WP_Admin_Bar $wp_admin_bar The admin bar. * * @return void */ public function remove_admin_bar_search( $wp_admin_bar ): void { if( $wp_admin_bar instanceof \WP_Admin_Bar ) { $wp_admin_bar->remove_node( 'search' ); } } }