Changeset 44959
- Timestamp:
- 03/21/2019 12:43:48 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/post.php
r44938 r44959 732 732 733 733 /** 734 * Determine if a post exists based on title, content, and date734 * 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 … … 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"; … … 766 769 $query .= ' AND post_content = %s'; 767 770 $args[] = $post_content; 771 } 772 773 if ( ! empty( $type ) ) { 774 $query .= ' AND post_type = %s'; 775 $args[] = $post_type; 768 776 } 769 777 -
trunk/tests/phpunit/tests/admin/includesPost.php
r44672 r44959 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( 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 } 854 888 }
Note: See TracChangeset
for help on using the changeset viewer.