Menus, including ones that add new pages automatically. add_filter( 'wp_get_nav_menu_items', array( $this, 'exclude_from_menus' ) ); // Links inside a block theme's Navigation block. add_filter( 'render_block_core/navigation-link', array( $this, 'exclude_navigation_link' ), 10, 2 ); // Search engines. add_filter( 'wp_robots', array( $this, 'noindex' ) ); add_filter( 'wp_sitemaps_posts_query_args', array( $this, 'exclude_from_sitemap' ), 10, 2 ); } /** * IDs of the pages the plugin creates for itself. * * @return int[] */ public static function page_ids() { return array_values( array_filter( array( (int) get_option( 'afridirect_pending_page_id', 0 ), (int) get_option( 'afridirect_receipt_page_id', 0 ), (int) get_option( 'afridirect_request_page_id', 0 ), ) ) ); } /** * Remove the plugin's pages from automatic page lists on the public site. * * @param WP_Post[] $pages Pages. * @return WP_Post[] */ public function exclude_from_page_lists( $pages ) { if ( is_admin() || ! is_array( $pages ) ) { return $pages; } $hidden = self::page_ids(); if ( empty( $hidden ) ) { return $pages; } return array_values( array_filter( $pages, static function ( $page ) use ( $hidden ) { return ! ( $page instanceof WP_Post ) || ! in_array( (int) $page->ID, $hidden, true ); } ) ); } /** * Remove the plugin's pages from menus on the public site. * * @param array $items Menu items. * @return array */ public function exclude_from_menus( $items ) { if ( is_admin() || ! is_array( $items ) ) { return $items; } $hidden = self::page_ids(); if ( empty( $hidden ) ) { return $items; } return array_values( array_filter( $items, static function ( $item ) use ( $hidden ) { return ! ( isset( $item->object, $item->object_id ) && 'page' === $item->object && in_array( (int) $item->object_id, $hidden, true ) ); } ) ); } /** * Hide a Navigation block link that points at one of the plugin's pages. * * @param string $content Rendered block. * @param array $block Parsed block. * @return string */ public function exclude_navigation_link( $content, $block ) { $attrs = isset( $block['attrs'] ) ? $block['attrs'] : array(); if ( isset( $attrs['id'], $attrs['type'] ) && 'page' === $attrs['type'] && in_array( (int) $attrs['id'], self::page_ids(), true ) ) { return ''; } return $content; } /** * Ask search engines not to list the plugin's pages. * * @param array $robots Robots directives. * @return array */ public function noindex( $robots ) { $hidden = self::page_ids(); if ( ! empty( $hidden ) && is_page( $hidden ) ) { return wp_robots_no_robots( $robots ); } return $robots; } /** * Keep the plugin's pages out of the WordPress sitemap. * * @param array $args Query arguments. * @param string $post_type Post type. * @return array */ public function exclude_from_sitemap( $args, $post_type ) { if ( 'page' === $post_type ) { $existing = isset( $args['post__not_in'] ) ? (array) $args['post__not_in'] : array(); // Only the plugin's three pages are excluded, so this exclusion costs nothing. // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in $args['post__not_in'] = array_merge( $existing, self::page_ids() ); } return $args; } }