Make WordPress Core


Ignore:
Timestamp:
10/06/2025 08:11:21 PM (2 months ago)
Author:
westonruter
Message:

Posts, Post Types: Short-circuit wp_post_delete() when $post_id arg is not above zero after casting to int.

The casting to int ensures that the action callbacks for post deletion can safely use the int type hint for the $post_id argument, as otherwise a fatal error occurs when an integer string is passed. This function also originally had casting of the argument to an integer, going back to at least WP 1.5.0, since it was passed directly into an SQL query. The casting was removed in [6180] with the introduction of prepared SQL statements.

The wp_delete_post() function had $post_id = 0 defined as its argument, also going back at least to WP 1.5.0, perhaps as a way to indicate the type of the argument as being an integer before there was PHPDoc. Unlike with functions like get_post() which have $post = null as the default argument to fall back to getting the global post, no such fallback logic was added to wp_delete_post(), meaning that passing no argument would always result in a DB query to locate the post with an ID of 0, which will never happen. So this introduces a _doing_it_wrong() in case 0 is passed, and yet the default value of 0 is not removed from the function signature to not introduce a fatal error in case any existing code is not supplying the $post_id parameter (however unlikely this may be).

Unit tests have been fleshed out for wp_delete_post() to add coverage for what was previously missing.

Props SirLouen, kkmuffme, fakhriaz, sajjad67, siliconforks, peterwilsoncc, westonruter.
Fixes #63975.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r60792 r60906  
    503503    }
    504504
    505     public function test_wp_delete_post_reassign_hierarchical_post_type() {
    506         $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
    507         $parent_page_id      = self::factory()->post->create(
    508             array(
    509                 'post_type'   => 'page',
    510                 'post_parent' => $grandparent_page_id,
    511             )
    512         );
    513         $page_id             = self::factory()->post->create(
    514             array(
    515                 'post_type'   => 'page',
    516                 'post_parent' => $parent_page_id,
    517             )
    518         );
    519 
    520         $this->assertSame( $parent_page_id, get_post( $page_id )->post_parent );
    521 
    522         wp_delete_post( $parent_page_id, true );
    523         $this->assertSame( $grandparent_page_id, get_post( $page_id )->post_parent );
    524 
    525         wp_delete_post( $grandparent_page_id, true );
    526         $this->assertSame( 0, get_post( $page_id )->post_parent );
    527     }
    528 
    529505    /**
    530506     * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in
Note: See TracChangeset for help on using the changeset viewer.