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 ) |
731 | 731 | } |
732 | 732 | |
733 | 733 | /** |
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. |
735 | 735 | * |
736 | 736 | * @since 2.0.0 |
| 737 | * @since 5.2.0 Added the `$type` parameter. |
737 | 738 | * |
738 | 739 | * @global wpdb $wpdb WordPress database abstraction object. |
739 | 740 | * |
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. |
743 | 745 | * @return int Post ID if post exists, 0 otherwise. |
744 | 746 | */ |
745 | | function post_exists( $title, $content = '', $date = '' ) { |
| 747 | function post_exists( $title, $content = '', $date = '', $type = '' ) { |
746 | 748 | global $wpdb; |
747 | 749 | |
748 | 750 | $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); |
749 | 751 | $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); |
750 | 752 | $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' ) ); |
751 | 754 | |
752 | 755 | $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; |
753 | 756 | $args = array(); |
… |
… |
function post_exists( $title, $content = '', $date = '' ) { |
767 | 770 | $args[] = $post_content; |
768 | 771 | } |
769 | 772 | |
| 773 | if ( ! empty( $type ) ) { |
| 774 | $query .= ' AND post_type = %s'; |
| 775 | $args[] = $post_type; |
| 776 | } |
| 777 | |
770 | 778 | if ( ! empty( $args ) ) { |
771 | 779 | return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); |
772 | 780 | } |
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 { |
851 | 851 | $this->assertNotFalse( add_meta( $p ) ); |
852 | 852 | $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); |
853 | 853 | } |
| 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 | } |
854 | 884 | } |