Changeset 51027
- Timestamp:
- 05/26/2021 02:16:01 AM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/post.php
r50809 r51027 764 764 * @since 2.0.0 765 765 * @since 5.2.0 Added the `$type` parameter. 766 * @since 5.8.0 Added the `$status` parameter. 766 767 * 767 768 * @global wpdb $wpdb WordPress database abstraction object. … … 771 772 * @param string $date Optional post date. 772 773 * @param string $type Optional post type. 774 * @param string $status Optional post status. 773 775 * @return int Post ID if post exists, 0 otherwise. 774 776 */ 775 function post_exists( $title, $content = '', $date = '', $type = '' ) {777 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) { 776 778 global $wpdb; 777 779 … … 780 782 $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); 781 783 $post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) ); 784 $post_status = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) ); 782 785 783 786 $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; … … 802 805 $query .= ' AND post_type = %s'; 803 806 $args[] = $post_type; 807 } 808 809 if ( ! empty( $status ) ) { 810 $query .= ' AND post_status = %s'; 811 $args[] = $post_status; 804 812 } 805 813 -
trunk/tests/phpunit/tests/admin/includesPost.php
r50527 r51027 904 904 $this->assertSame( 0, post_exists( $title, null, null, 'post' ) ); 905 905 } 906 907 /** 908 * Test the status support in post_exists() 909 * 910 * @ticket 34012 911 */ 912 public function test_post_exists_should_support_post_status() { 913 $title = 'Foo Bar'; 914 $post_type = 'post'; 915 $post_status = 'publish'; 916 $post_id = self::factory()->post->create( 917 array( 918 'post_title' => $title, 919 'post_type' => $post_type, 920 'post_status' => $post_status, 921 ) 922 ); 923 $this->assertSame( $post_id, post_exists( $title, null, null, null, $post_status ) ); 924 } 925 926 927 /** 928 * Test the type and status query in post_exists() 929 * 930 * @ticket 34012 931 */ 932 public function test_post_exists_should_support_post_type_status_combined() { 933 $title = 'Foo Bar'; 934 $post_type = 'post'; 935 $post_status = 'publish'; 936 $post_id = self::factory()->post->create( 937 array( 938 'post_title' => $title, 939 'post_type' => $post_type, 940 'post_status' => $post_status, 941 ) 942 ); 943 $this->assertSame( $post_id, post_exists( $title, null, null, $post_type, $post_status ) ); 944 } 945 946 /** 947 * Test that post_exists() doesn't find an existing draft post when looking for publish 948 * 949 * @ticket 34012 950 */ 951 public function test_post_exists_should_only_match_correct_post_status() { 952 $title = 'Foo Bar'; 953 $post_type = 'post'; 954 $post_status = 'draft'; 955 $post_id = self::factory()->post->create( 956 array( 957 'post_title' => $title, 958 'post_type' => $post_type, 959 'post_status' => $post_status, 960 ) 961 ); 962 $this->assertSame( 0, post_exists( $title, null, null, null, 'publish' ) ); 963 } 964 965 /** 966 * Test the status support in post_exists() 967 * 968 * @ticket 34012 969 */ 970 public function test_post_exists_should_not_match_invalid_post_type_and_status_combined() { 971 $title = 'Foo Bar'; 972 $post_type = 'post'; 973 $post_status = 'publish'; 974 $post_id = self::factory()->post->create( 975 array( 976 'post_title' => $title, 977 'post_type' => $post_type, 978 'post_status' => $post_status, 979 ) 980 ); 981 982 $this->assertSame( 0, post_exists( $title, null, null, $post_type, 'draft' ) ); 983 $this->assertSame( 0, post_exists( $title, null, null, 'wp_tests', $post_status ) ); 984 } 906 985 }
Note: See TracChangeset
for help on using the changeset viewer.