Make WordPress Core

Ticket #34012: 44314.diff

File 44314.diff, 4.3 KB (added by peterwilsoncc, 4 years ago)
  • src/wp-admin/includes/post.php

    diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
    index 1a0fef056e..58711ef674 100644
    a b function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) 
    759759 *
    760760 * @since 2.0.0
    761761 * @since 5.2.0 Added the `$type` parameter.
     762 * @since 5.8.0 Added the `$status` parameter.
    762763 *
    763764 * @global wpdb $wpdb WordPress database abstraction object.
    764765 *
    function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) 
    766767 * @param string $content Optional post content.
    767768 * @param string $date    Optional post date.
    768769 * @param string $type    Optional post type.
     770 * @param string $status  Optional post status.
    769771 * @return int Post ID if post exists, 0 otherwise.
    770772 */
    771 function post_exists( $title, $content = '', $date = '', $type = '' ) {
     773function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
    772774        global $wpdb;
    773775
    774776        $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    775777        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    776778        $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
    777779        $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
     780        $post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
    778781
    779782        $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    780783        $args  = array();
    function post_exists( $title, $content = '', $date = '', $type = '' ) { 
    799802                $args[] = $post_type;
    800803        }
    801804
     805        if ( ! empty( $status ) ) {
     806                $query .= ' AND post_status = %s';
     807                $args[] = $post_status;
     808        }
     809
    802810        if ( ! empty( $args ) ) {
    803811                return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
    804812        }
  • tests/phpunit/tests/admin/includesPost.php

    diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php
    index 4dffffd0d0..4bf9f5bd6a 100644
    a b class Tests_Admin_Includes_Post extends WP_UnitTestCase { 
    902902                );
    903903                $this->assertSame( 0, post_exists( $title, null, null, 'post' ) );
    904904        }
     905
     906        /**
     907         * Test the status support in post_exists()
     908         *
     909         * @ticket 34012
     910         */
     911        public function test_post_exists_should_support_post_status() {
     912                $title       = 'Foo Bar';
     913                $post_type   = 'post';
     914                $post_status = 'publish';
     915                $post_id     = self::factory()->post->create(
     916                        array(
     917                                'post_title'  => $title,
     918                                'post_type'   => $post_type,
     919                                'post_status' => $post_status,
     920                        )
     921                );
     922                $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) );
     923        }
     924
     925
     926        /**
     927         * Test the type and status query in post_exists()
     928         *
     929         * @ticket 34012
     930         */
     931        public function test_post_exists_should_support_post_type_status_combined() {
     932                $title       = 'Foo Bar';
     933                $post_type   = 'post';
     934                $post_status = 'publish';
     935                $post_id     = self::factory()->post->create(
     936                        array(
     937                                'post_title'  => $title,
     938                                'post_type'   => $post_type,
     939                                'post_status' => $post_status,
     940                        )
     941                );
     942                $this->assertSame( $post_id, post_exists( $title, null, null, $post_type, $post_status ) );
     943        }
     944
     945        /**
     946         * Test that post_exists() doesn't find an existing draft post when looking for publish
     947         *
     948         * @ticket 34012
     949         */
     950        public function test_post_exists_should_only_match_correct_post_status() {
     951                $title       = 'Foo Bar';
     952                $post_type   = 'post';
     953                $post_status = 'draft';
     954                $post_id     = self::factory()->post->create(
     955                        array(
     956                                'post_title'  => $title,
     957                                'post_type'   => $post_type,
     958                                'post_status' => $post_status,
     959                        )
     960                );
     961                $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) );
     962        }
     963
     964        /**
     965         * Test the status support in post_exists()
     966         *
     967         * @ticket 34012
     968         */
     969        public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() {
     970                $title       = 'Foo Bar';
     971                $post_type   = 'post';
     972                $post_status = 'publish';
     973                $post_id     = self::factory()->post->create(
     974                        array(
     975                                'post_title'  => $title,
     976                                'post_type'   => $post_type,
     977                                'post_status' => $post_status,
     978                        )
     979                );
     980
     981                $this->assertSame( 0, post_exists( $title, null, null, $post_type, 'draft' ) );
     982                $this->assertSame( 0, post_exists( $title, null, null, 'wp_tests', $post_status ) );
     983        }
    905984}