add_action( 'wp_trash_post', $this, 'on_removal', 10, 1 ); $loader->add_action( 'before_delete_post', $this, 'on_removal', 10, 1 ); $loader->add_action( 'admin_notices', $this, 'maybe_notice' ); } /** * A post is about to be trashed or deleted: remember its address if it * was public. * * @param int $post_id Post ID. */ public function on_removal( $post_id ): void { $post = get_post( (int) $post_id ); if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status || ! is_post_type_viewable( $post->post_type ) ) { return; } $permalink = (string) get_permalink( $post ); // Plain "?p=123" addresses carry no path a rule could match. if ( '' === $permalink || false !== strpos( $permalink, '?' ) ) { return; } $home = ABDS_SEO_Redirect_Matcher::normalize_path( (string) wp_parse_url( home_url(), PHP_URL_PATH ) ); $path = ABDS_SEO_Redirect_Matcher::normalize_path( $permalink ); if ( '/' !== $home && 0 === strpos( $path, $home . '/' ) ) { $path = ABDS_SEO_Redirect_Matcher::normalize_path( substr( $path, strlen( $home ) ) ); } if ( '/' === $path || is_array( call_user_func( ( new ABDS_SEO_Redirect_Repository() )->finder(), $path ) ) ) { return; // The home page, or an address a rule already handles. } self::add( $path, wp_strip_all_tags( get_the_title( $post ) ) ); } /** * Adds (or refreshes) a suggestion. * * @param string $path Normalized old path. * @param string $title Title of the removed page. */ public static function add( string $path, string $title ): void { $list = array_filter( self::all(), static fn( array $entry ): bool => $entry['path'] !== $path ); array_unshift( $list, array( 'id' => md5( $path ), 'path' => $path, 'title' => $title, 'time' => current_time( 'mysql' ), ) ); update_option( self::OPTION, array_slice( array_values( $list ), 0, self::MAX ), false ); } /** * All suggestions, newest first. * * @return array */ public static function all(): array { $stored = get_option( self::OPTION, array() ); return is_array( $stored ) ? array_values( $stored ) : array(); } /** * Drops a suggestion. * * @param string $id Suggestion ID (md5 of the path). */ public static function dismiss( string $id ): void { update_option( self::OPTION, array_values( array_filter( self::all(), static fn( array $entry ): bool => $entry['id'] !== $id ) ), false ); } /** * Notices: removed pages on the content lists, frequently requested * missing addresses on the Site Audit screen. */ public function maybe_notice(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $url = admin_url( 'admin.php?page=abds-seo-indexing#redirects' ); if ( $screen && 'edit' === $screen->base ) { $count = count( self::all() ); if ( $count > 0 ) { printf( '

%s %s

', esc_html( sprintf( /* translators: %d: number of removed pages. */ _n( '%d removed page used to have a public address. Visitors and search engines asking for it will find nothing unless you redirect it.', '%d removed pages used to have a public address. Visitors and search engines asking for them will find nothing unless you redirect them.', $count, 'abds-seo' ), $count ) ), esc_url( $url ), esc_html__( 'Review in Redirects', 'abds-seo' ) ); } return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen check, no state change. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; if ( 'abds-seo-audit' === $page ) { $frequent = ( new ABDS_SEO_Not_Found_Log() )->frequent( 1 ); if ( array() !== $frequent ) { $total = count( ( new ABDS_SEO_Not_Found_Log() )->frequent( 1000 ) ); printf( '

%s %s

', esc_html( sprintf( /* translators: 1: number of missing addresses, 2: how many requests the most requested one has had. */ _n( 'The site has %1$d address that is requested often but does not exist (the most requested: %2$d times).', 'The site has %1$d addresses that are requested often but do not exist (the most requested: %2$d times).', $total, 'abds-seo' ), $total, $frequent[0]['hits'] ) ), esc_url( $url ), esc_html__( 'Open the 404 log', 'abds-seo' ) ); } } } }