Make WordPress Core

Ticket #34012: 34012.2.diff

File 34012.2.diff, 2.9 KB (added by brettshumaker, 2 years ago)

Updates patch and adds relevant unit tests.

  • src/wp-admin/includes/post.php

    diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
    index 20a3fb1354..a9f488a791 100644
    a b function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) 
    766766 * @param string $content Optional post content.
    767767 * @param string $date    Optional post date.
    768768 * @param string $type    Optional post type.
     769 * @param string $status  Optional post status.
    769770 * @return int Post ID if post exists, 0 otherwise.
    770771 */
    771 function post_exists( $title, $content = '', $date = '', $type = '' ) {
     772function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
    772773        global $wpdb;
    773774
    774775        $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    775776        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    776777        $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    777778        $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
     779        $post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
    778780
    779781        $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    780782        $args  = array();
    function post_exists( $title, $content = '', $date = '', $type = '' ) { 
    799801                $args[] = $post_type;
    800802        }
    801803
     804        if ( ! empty( $status ) ) {
     805                $query .= ' AND post_status = %s';
     806                $args[] = $post_status;
     807        }
     808
    802809        if ( ! empty( $args ) ) {
    803810                return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
    804811        }
  • tests/phpunit/tests/admin/includesPost.php

    diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php
    index 366bfefcd7..27f036c280 100644
    a b class Tests_Admin_Includes_Post extends WP_UnitTestCase { 
    901901                );
    902902                $this->assertSame( 0, post_exists( $title, null, null, 'post' ) );
    903903        }
     904
     905        /**
     906         * Test the status support in post_exists()
     907         *
     908         * @ticket 34012
     909         */
     910        public function test_post_exists_should_support_post_status() {
     911                $title       = 'Foo Bar';
     912                $post_type   = 'post';
     913                $post_status = 'publish';
     914                $post_id   = self::factory()->post->create(
     915                        array(
     916                                'post_title'  => $title,
     917                                'post_type'   => $post_type,
     918                                'post_status' => $post_status,
     919                        )
     920                );
     921                $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) );
     922        }
     923
     924        /**
     925         * Test that post_exists() doesn't find an existing draft post when looking for publish
     926         *
     927         * @ticket 34012
     928         */
     929        public function test_post_exists_should_only_match_correct_post_status() {
     930                $title      = 'Foo Bar';
     931                $post_type  = 'post';
     932                $post_status = 'draft';
     933                $post_id   = self::factory()->post->create(
     934                        array(
     935                                'post_title'  => $title,
     936                                'post_type'   => $post_type,
     937                                'post_status' => $post_status,
     938                        )
     939                );
     940                $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) );
     941        }
    904942}