Ticket #34012: 34012.diff
File 34012.diff, 3.4 KB (added by , 9 years ago) |
---|
-
src/wp-admin/includes/post.php
657 657 /** 658 658 * Determine if a post exists based on title, content, and date 659 659 * 660 * For best performance use the $date, $type, and $status together 661 * all of which are required to properly use a compound index. 662 * $date is left where it is positionally for legacy reasons 663 * 660 664 * @since 2.0.0 665 * @since 4.4.0 Added the $type, $status parameters 661 666 * 662 667 * @global wpdb $wpdb 663 668 * … … 664 669 * @param string $title Post title 665 670 * @param string $content Optional post content 666 671 * @param string $date Optional post date 672 * @param string $type Optional post type 673 * @param string $status Optional post status 667 674 * @return int Post ID if post exists, 0 otherwise. 668 675 */ 669 function post_exists( $title, $content = '', $date = '') {676 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) { 670 677 global $wpdb; 671 678 672 $ post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ));673 $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );674 $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );675 676 $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";677 $args = array();678 679 if ( !empty ( $date ) ) {680 $query .= ' AND post_date = %s';681 $args[] = $post_date;679 $query = array(); 680 foreach( compact( 'title', 'content', 'date', 'type', 'status' ) as $key => $val ) { 681 if ( empty( $val ) ) { 682 continue; 683 } 684 $key = sprintf( 'post_%s', $key ); 685 $query[] = $wpdb->prepare( 686 $key . ' = %s', 687 wp_unslash( sanitize_post_field( $key, $val, 0, 'db' ) ) 688 ); 682 689 } 683 690 684 if ( !empty ( $title ) ) { 685 $query .= ' AND post_title = %s'; 686 $args[] = $post_title; 691 if ( empty( $query ) ) { 692 return 0; 687 693 } 688 694 689 if ( !empty ( $content ) ) { 690 $query .= 'AND post_content = %s'; 691 $args[] = $post_content; 692 } 693 694 if ( !empty ( $args ) ) 695 return (int) $wpdb->get_var( $wpdb->prepare($query, $args) ); 696 697 return 0; 695 return (int)$wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE 1=1 AND " . implode( ' AND ', $query ) . ' LIMIT 1' ); 698 696 } 699 697 700 698 /** -
tests/phpunit/tests/post/post-exists.php
1 <?php 2 3 /** 4 * @group post 5 */ 6 class Tests_Post_Exists extends WP_UnitTestCase { 7 function setUp() { 8 parent::setUp(); 9 $this->post1 = $this->factory->post->create( array( 'post_title' => 'Post One Title', 'post_content' => 'Post One Content' ) ); 10 $this->post2 = $this->factory->post->create( array( 'post_title' => 'Post Two Title', 'post_content' => 'Post Two Content' ) ); 11 } 12 13 function tearDown() { 14 wp_delete_post($this->post1); 15 wp_delete_post($this->post2); 16 parent::tearDown(); 17 } 18 19 function test_post_exists_title() { 20 $post = post_exists( 'Post One Title' ); 21 $this->assertEquals( $post, $this->post1 ); 22 } 23 24 function test_post_exists_bad_title() { 25 $post = post_exists( 'No existing Title' ); 26 $this->assertEquals( $post, 0 ); 27 } 28 29 function test_post_exists_content() { 30 $post = post_exists( 'Post One Title', 'Post One Content' ); 31 $this->assertEquals( $post, $this->post1 ); 32 } 33 34 } 35 No newline at end of file