Ticket #34012: post.diff
File post.diff, 2.2 KB (added by , 9 years ago) |
---|
-
post.php
610 610 /** 611 611 * Determine if a post exists based on title, content, and date 612 612 * 613 * For best performance use the $date, $type, and $status together 614 * all of which are required to properly use a compound index. 615 * $date is left where it is positionally for legacy reasons 616 * 613 617 * @since 2.0.0 618 * @since 4.4.0 Added the $type, $status parameters 614 619 * 615 620 * @global wpdb $wpdb 616 621 * … … 617 622 * @param string $title Post title 618 623 * @param string $content Optional post content 619 624 * @param string $date Optional post date 625 * @param string $type Optional post type 626 * @param string $status Optional post status 620 627 * @return int Post ID if post exists, 0 otherwise. 621 628 */ 622 function post_exists( $title, $content = '', $date = '') {629 function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) { 623 630 global $wpdb; 624 631 625 $ post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ));626 $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );627 $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );628 629 $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";630 $args = array();631 632 if ( !empty ( $date ) ) {633 $query .= ' AND post_date = %s';634 $args[] = $post_date;632 $query = array(); 633 foreach( compact( 'title', 'content', 'date', 'type', 'status' ) as $key => $val ) { 634 if ( empty( $val ) ) { 635 continue; 636 } 637 $key = sprintf( 'post_%s', $key ); 638 $query[] = $wpdb->prepare( 639 $key . ' = %s', 640 wp_unslash( sanitize_post_field( $key, $val, 0, 'db' ) ) 641 ); 635 642 } 636 643 637 if ( !empty ( $title ) ) { 638 $query .= ' AND post_title = %s'; 639 $args[] = $post_title; 644 if ( empty( $query ) ) { 645 return 0; 640 646 } 641 647 642 if ( !empty ( $content ) ) { 643 $query .= 'AND post_content = %s'; 644 $args[] = $post_content; 645 } 646 647 if ( !empty ( $args ) ) 648 return (int) $wpdb->get_var( $wpdb->prepare($query, $args) ); 649 650 return 0; 648 return (int)$wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE 1=1 AND " . implode( ' AND ', $query ) . ' LIMIT 1' ); 651 649 } 652 650 653 651 /**