diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php
index 20a3fb1354..a9f488a791 100644
a
|
b
|
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) |
766 | 766 | * @param string $content Optional post content. |
767 | 767 | * @param string $date Optional post date. |
768 | 768 | * @param string $type Optional post type. |
| 769 | * @param string $status Optional post status. |
769 | 770 | * @return int Post ID if post exists, 0 otherwise. |
770 | 771 | */ |
771 | | function post_exists( $title, $content = '', $date = '', $type = '' ) { |
| 772 | function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) { |
772 | 773 | global $wpdb; |
773 | 774 | |
774 | 775 | $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); |
775 | 776 | $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); |
776 | 777 | $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); |
777 | 778 | $post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) ); |
| 779 | $post_status = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) ); |
778 | 780 | |
779 | 781 | $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; |
780 | 782 | $args = array(); |
… |
… |
function post_exists( $title, $content = '', $date = '', $type = '' ) { |
799 | 801 | $args[] = $post_type; |
800 | 802 | } |
801 | 803 | |
| 804 | if ( ! empty( $status ) ) { |
| 805 | $query .= ' AND post_status = %s'; |
| 806 | $args[] = $post_status; |
| 807 | } |
| 808 | |
802 | 809 | if ( ! empty( $args ) ) { |
803 | 810 | return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); |
804 | 811 | } |
diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php
index 366bfefcd7..27f036c280 100644
a
|
b
|
class Tests_Admin_Includes_Post extends WP_UnitTestCase { |
901 | 901 | ); |
902 | 902 | $this->assertSame( 0, post_exists( $title, null, null, 'post' ) ); |
903 | 903 | } |
| 904 | |
| 905 | /** |
| 906 | * Test the status support in post_exists() |
| 907 | * |
| 908 | * @ticket 34012 |
| 909 | */ |
| 910 | public function test_post_exists_should_support_post_status() { |
| 911 | $title = 'Foo Bar'; |
| 912 | $post_type = 'post'; |
| 913 | $post_status = 'publish'; |
| 914 | $post_id = self::factory()->post->create( |
| 915 | array( |
| 916 | 'post_title' => $title, |
| 917 | 'post_type' => $post_type, |
| 918 | 'post_status' => $post_status, |
| 919 | ) |
| 920 | ); |
| 921 | $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) ); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Test that post_exists() doesn't find an existing draft post when looking for publish |
| 926 | * |
| 927 | * @ticket 34012 |
| 928 | */ |
| 929 | public function test_post_exists_should_only_match_correct_post_status() { |
| 930 | $title = 'Foo Bar'; |
| 931 | $post_type = 'post'; |
| 932 | $post_status = 'draft'; |
| 933 | $post_id = self::factory()->post->create( |
| 934 | array( |
| 935 | 'post_title' => $title, |
| 936 | 'post_type' => $post_type, |
| 937 | 'post_status' => $post_status, |
| 938 | ) |
| 939 | ); |
| 940 | $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) ); |
| 941 | } |
904 | 942 | } |