diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 142f9c57f5..c208c3dae3 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -763,6 +763,7 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) * * @since 2.0.0 * @since 5.2.0 Added the `$type` parameter. + * @since 5.8.0 Added the `$status` parameter. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -770,15 +771,17 @@ function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) * @param string $content Optional post content. * @param string $date Optional post date. * @param string $type Optional post type. + * @param string $status Optional post status. * @return int Post ID if post exists, 0 otherwise. */ -function post_exists( $title, $content = '', $date = '', $type = '' ) { +function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) { global $wpdb; $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); $post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) ); + $post_status = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) ); $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; $args = array(); @@ -803,6 +806,11 @@ function post_exists( $title, $content = '', $date = '', $type = '' ) { $args[] = $post_type; } + if ( ! empty( $status ) ) { + $query .= ' AND post_status = %s'; + $args[] = $post_status; + } + if ( ! empty( $args ) ) { return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); } diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 915f1fbd04..6b65b4beec 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -903,4 +903,83 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { ); $this->assertSame( 0, post_exists( $title, null, null, 'post' ) ); } + + /** + * Test the status support in post_exists() + * + * @ticket 34012 + */ + public function test_post_exists_should_support_post_status() { + $title = 'Foo Bar'; + $post_type = 'post'; + $post_status = 'publish'; + $post_id = self::factory()->post->create( + array( + 'post_title' => $title, + 'post_type' => $post_type, + 'post_status' => $post_status, + ) + ); + $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) ); + } + + + /** + * Test the type and status query in post_exists() + * + * @ticket 34012 + */ + public function test_post_exists_should_support_post_type_status_combined() { + $title = 'Foo Bar'; + $post_type = 'post'; + $post_status = 'publish'; + $post_id = self::factory()->post->create( + array( + 'post_title' => $title, + 'post_type' => $post_type, + 'post_status' => $post_status, + ) + ); + $this->assertSame( $post_id, post_exists( $title, null, null, $post_type, $post_status ) ); + } + + /** + * Test that post_exists() doesn't find an existing draft post when looking for publish + * + * @ticket 34012 + */ + public function test_post_exists_should_only_match_correct_post_status() { + $title = 'Foo Bar'; + $post_type = 'post'; + $post_status = 'draft'; + $post_id = self::factory()->post->create( + array( + 'post_title' => $title, + 'post_type' => $post_type, + 'post_status' => $post_status, + ) + ); + $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) ); + } + + /** + * Test the status support in post_exists() + * + * @ticket 34012 + */ + public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() { + $title = 'Foo Bar'; + $post_type = 'post'; + $post_status = 'publish'; + $post_id = self::factory()->post->create( + array( + 'post_title' => $title, + 'post_type' => $post_type, + 'post_status' => $post_status, + ) + ); + + $this->assertSame( 0, post_exists( $title, null, null, $post_type, 'draft' ) ); + $this->assertSame( 0, post_exists( $title, null, null, 'wp_tests', $post_status ) ); + } }