Make WordPress Core

Changeset 44959


Ignore:
Timestamp:
03/21/2019 12:43:48 PM (8 years ago)
Author:
desrosj
Message:

Posts, Post Types: Add type parameter to post_exists().

This allows post exists checks scoped to a specific post type.

Props sgarza, birgire, swissspidy.
Fixes #37406.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/post.php

    r44938 r44959  
    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
    … …  
    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";
    … …  
    766769                $query .= ' AND post_content = %s';
    767770                $args[] = $post_content;
     771        }
     772
     773        if ( ! empty( $type ) ) {
     774                $query .= ' AND post_type = %s';
     775                $args[] = $post_type;
    768776        }
    769777
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r44672 r44959  
    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(
     864                        array(
     865                                'post_title' => $title,
     866                                'post_type'  => $post_type,
     867                        )
     868                );
     869                $this->assertSame( $post_id, post_exists( $title, null, null, $post_type ) );
     870        }
     871
     872        /**
     873         * Test that post_exists() doesn't find an existing page as a post.
     874         *
     875         * @ticket 37406
     876         */
     877        public function test_post_exists_should_not_match_a_page_for_post() {
     878                $title     = 'Foo Bar';
     879                $post_type = 'page';
     880                $post_id   = self::factory()->post->create(
     881                        array(
     882                                'post_title' => $title,
     883                                'post_type'  => $post_type,
     884                        )
     885                );
     886                $this->assertSame( 0, post_exists( $title, null, null, 'post' ) );
     887        }
    854888}
Note: See TracChangeset for help on using the changeset viewer.