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/wpInsertPost.php

    r57680 r60906  
    567567
    568568    /**
    569      * "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly."
    570      *
    571      * @ticket 5364
    572      * @covers ::wp_delete_post
    573      */
    574     public function test_delete_future_post_cron() {
    575         $future_date = strtotime( '+1 day' );
    576 
    577         $data = array(
    578             'post_status'  => 'publish',
    579             'post_content' => 'content',
    580             'post_title'   => 'title',
    581             'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    582         );
    583 
    584         // Insert a post and make sure the ID is OK.
    585         $post_id = wp_insert_post( $data );
    586 
    587         // Check that there's a publish_future_post job scheduled at the right time.
    588         $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    589 
    590         // Now delete the post and make sure the cron entry is removed.
    591         wp_delete_post( $post_id );
    592 
    593         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    594     }
    595 
    596     /**
    597569     * Bug: permalink doesn't work if post title is empty.
    598570     *
Note: See TracChangeset for help on using the changeset viewer.