Make WordPress Core

Ticket #37406: 37406.3.diff

File 37406.3.diff, 3.0 KB (added by birgire, 7 years ago)
  • src/wp-admin/includes/post.php

    diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php
    index 727269e..fb4db46 100644
    function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) 
    703703}
    704704
    705705/**
    706  * Determine if a post exists based on title, content, and date
     706 * Determine if a post exists based on title, content, date and type.
    707707 *
    708708 * @since 2.0.0
     709 * @since ?.?.? Added the `$type` argument to support a post type filtering.
    709710 *
    710711 * @global wpdb $wpdb WordPress database abstraction object.
    711712 *
    712  * @param string $title Post title
    713  * @param string $content Optional post content
    714  * @param string $date Optional post date
     713 * @param string $title Post title.
     714 * @param string $content Optional post content.
     715 * @param string $date Optional post date.
     716 * @param string $type Optional post type.
    715717 * @return int Post ID if post exists, 0 otherwise.
    716718 */
    717 function post_exists( $title, $content = '', $date = '' ) {
     719function post_exists( $title, $content = '', $date = '', $type = '' ) {
    718720        global $wpdb;
    719721
    720722        $post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
    721723        $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
    722724        $post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
     725        $post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
    723726
    724727        $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
    725728        $args  = array();
    function post_exists( $title, $content = '', $date = '' ) { 
    739742                $args[] = $post_content;
    740743        }
    741744
     745        if ( ! empty( $type ) ) {
     746                $query .= ' AND post_type = %s';
     747                $args[] = $post_type;
     748        }
     749
    742750        if ( ! empty( $args ) ) {
    743751                return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
    744752        }
  • tests/phpunit/tests/admin/includesPost.php

    diff --git tests/phpunit/tests/admin/includesPost.php tests/phpunit/tests/admin/includesPost.php
    index 5c080f2..7bdbf78 100644
    class Tests_Admin_Includes_Post extends WP_UnitTestCase { 
    748748                $this->assertSame( $p, post_exists( $title, $content, $date ) );
    749749        }
    750750
     751        /**
     752         * Test the post type support in post_exists().
     753         *
     754         * @ticket 37406
     755         */
     756        public function test_post_exists_should_support_post_type() {
     757                $title     = 'Foo Bar';
     758                $post_type = 'page';
     759                $post_id   = self::factory()->post->create( array(
     760                        'post_title' => $title,
     761                        'post_type'  => $post_type,
     762                ) );
     763
     764                $this->assertSame( $post_id, post_exists( $title, null, null, $post_type ) );
     765        }
     766
     767        /**
     768         * Test that post_exists() doesn't find an existing page as a post.
     769         *
     770         * @ticket 37406
     771         */
     772        public function test_post_exists_should_not_match_a_page_for_post() {
     773                $title     = 'Foo Bar';
     774                $post_type = 'page';
     775                $post_id   = self::factory()->post->create( array(
     776                        'post_title' => $title,
     777                        'post_type'  => $post_type,
     778                ) );
     779
     780                $this->assertNotSame( $post_id, post_exists( $title, null, null, 'post' ) );
     781        }
    751782}