Make WordPress Core

Ticket #37406: 37406.diff

File 37406.diff, 3.0 KB (added by swissspidy, 6 years ago)
  • src/wp-admin/includes/post.php

    diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php
    index 0e19876e51..2f087ccb51 100644
    function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) 
    731731}
    732732
    733733/**
    734  * Determine if a post exists based on title, content, and date
     734 * Determines if a post exists based on title, content, date and type.
    735735 *
    736736 * @since 2.0.0
     737 * @since 5.2.0 Added the `$type` parameter.
    737738 *
    738739 * @global wpdb $wpdb WordPress database abstraction object.
    739740 *
    740  * @param string $title Post title
    741  * @param string $content Optional post content
    742  * @param string $date Optional post date
     741 * @param string $title   Post title.
     742 * @param string $content Optional post content.
     743 * @param string $date    Optional post date.
     744 * @param string $type    Optional post type.
    743745 * @return int Post ID if post exists, 0 otherwise.
    744746 */
    745 function post_exists( $title, $content = '', $date = '' ) {
     747function post_exists( $title, $content = '', $date = '', $type = '' ) {
    746748        global $wpdb;
    747749
    748750        $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    749751        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    750752        $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
     753        $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
    751754
    752755        $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    753756        $args  = array();
    function post_exists( $title, $content = '', $date = '' ) { 
    767770                $args[] = $post_content;
    768771        }
    769772
     773        if ( ! empty( $type ) ) {
     774                $query .= ' AND post_type = %s';
     775                $args[] = $post_type;
     776        }
     777
    770778        if ( ! empty( $args ) ) {
    771779                return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
    772780        }
  • tests/phpunit/tests/admin/includesPost.php

    diff --git tests/phpunit/tests/admin/includesPost.php tests/phpunit/tests/admin/includesPost.php
    index d4cc606dcd..e1418f397e 100644
    class Tests_Admin_Includes_Post extends WP_UnitTestCase { 
    851851                $this->assertNotFalse( add_meta( $p ) );
    852852                $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) );
    853853        }
     854
     855        /**
     856         * Test the post type support in post_exists().
     857         *
     858         * @ticket 37406
     859         */
     860        public function test_post_exists_should_support_post_type() {
     861                $title     = 'Foo Bar';
     862                $post_type = 'page';
     863                $post_id   = self::factory()->post->create( array(
     864                        'post_title' => $title,
     865                        'post_type'  => $post_type,
     866                ) );
     867                $this->assertSame( $post_id, post_exists( $title, null, null, $post_type ) );
     868        }
     869
     870        /**
     871         * Test that post_exists() doesn't find an existing page as a post.
     872         *
     873         * @ticket 37406
     874         */
     875        public function test_post_exists_should_not_match_a_page_for_post() {
     876                $title     = 'Foo Bar';
     877                $post_type = 'page';
     878                $post_id   = self::factory()->post->create( array(
     879                        'post_title' => $title,
     880                        'post_type'  => $post_type,
     881                ) );
     882                $this->assertNotSame( $post_id, post_exists( $title, null, null, 'post' ) );
     883        }
    854884}