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 ) |
703 | 703 | } |
704 | 704 | |
705 | 705 | /** |
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. |
707 | 707 | * |
708 | 708 | * @since 2.0.0 |
| 709 | * @since ?.?.? Added the `$type` argument to support a post type filtering. |
709 | 710 | * |
710 | 711 | * @global wpdb $wpdb WordPress database abstraction object. |
711 | 712 | * |
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. |
715 | 717 | * @return int Post ID if post exists, 0 otherwise. |
716 | 718 | */ |
717 | | function post_exists( $title, $content = '', $date = '' ) { |
| 719 | function post_exists( $title, $content = '', $date = '', $type = '' ) { |
718 | 720 | global $wpdb; |
719 | 721 | |
720 | 722 | $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); |
721 | 723 | $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); |
722 | 724 | $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' ) ); |
723 | 726 | |
724 | 727 | $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; |
725 | 728 | $args = array(); |
… |
… |
function post_exists( $title, $content = '', $date = '' ) { |
739 | 742 | $args[] = $post_content; |
740 | 743 | } |
741 | 744 | |
| 745 | if ( ! empty( $type ) ) { |
| 746 | $query .= ' AND post_type = %s'; |
| 747 | $args[] = $post_type; |
| 748 | } |
| 749 | |
742 | 750 | if ( ! empty( $args ) ) { |
743 | 751 | return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); |
744 | 752 | } |
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 { |
748 | 748 | $this->assertSame( $p, post_exists( $title, $content, $date ) ); |
749 | 749 | } |
750 | 750 | |
| 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 | } |
751 | 782 | } |