Ping IndexNow for newly 404ing URLs. (#160)

Signed-off-by: Peter Wilson <git-commit-by@peterwilson.cc>
Signed-off-by: Ryan McCue <me@ryanmccue.info>
Co-authored-by: Ryan McCue <me@ryanmccue.info>
This commit is contained in:
Peter Wilson 2025-07-13 11:09:25 +10:00 committed by GitHub
parent ffbc7559a7
commit 966c82949e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -109,18 +109,51 @@ function handle_key_file_request() {
* @param WP_Post $post Post object. * @param WP_Post $post Post object.
*/ */
function ping_indexnow( $new_status, $old_status, $post ) : void { function ping_indexnow( $new_status, $old_status, $post ) : void {
// Only ping for published posts. /*
if ( 'publish' !== $new_status ) { * Skip if post type isn't viewable.
*
* The post type shouldn't change under normal circumstances,
* so it's safe to assume that both the old and new post are
* not viewable.
*/
if ( ! is_post_type_viewable( $post->post_type ) ) {
return; return;
} }
// Skip revisions and autosaves. /*
* Skip for revisions and autosaves.
*
* The IndexNow ping for revisions will be handled by the
* parent post's transition_post_status hook.
*/
if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) { if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) {
return; return;
} }
// Skip non-public post types. /*
if ( ! is_post_type_viewable( $post->post_type ) ) { * Skip if both old and new statuses are private.
*
* The page will have been a 404 before and after.
*
* For pages that are newly a 404, still ping IndexNow
* to encourage removal of the URL from search engines.
*/
if (
! is_post_status_viewable( $new_status )
&& ! is_post_status_viewable( $old_status )
) {
return;
}
/*
* Prevent double pings for block editor legacy meta boxes.
*/
if (
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
isset( $_GET['meta-box-loader'] )
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.PHP.StrictComparisons.LooseComparison -- form input.
&& '1' == $_GET['meta-box-loader']
) {
return; return;
} }