post_exists() and comment_exists() fixes. Fixes post duplication during import. Props tott. fixes #8460

git-svn-id: https://develop.svn.wordpress.org/trunk@10722 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2009-03-05 22:16:29 +00:00
parent a3ff3cdbf2
commit a5740e48a1
2 changed files with 31 additions and 16 deletions

View File

@ -19,6 +19,9 @@
function comment_exists($comment_author, $comment_date) { function comment_exists($comment_author, $comment_date) {
global $wpdb; global $wpdb;
$comment_author = stripslashes($comment_author);
$comment_date = stripslashes($comment_date);
return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) ); WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
} }

View File

@ -397,30 +397,42 @@ function get_post_to_edit( $id ) {
} }
/** /**
* {@internal Missing Short Description}} * Determine if a post exists based on title, content, and date
* *
* @since unknown * @since unknown
* *
* @param unknown_type $title * @param string $title Post title
* @param unknown_type $content * @param string $content Optional post content
* @param unknown_type $post_date * @param string $date Optional post date
* @return unknown * @return int Post ID if post exists, 0 otherwise.
*/ */
function post_exists($title, $content = '', $post_date = '') { function post_exists($title, $content = '', $date = '') {
global $wpdb; global $wpdb;
$title = stripslashes($title); $post_title = stripslashes( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
$content = stripslashes($content); $post_content = stripslashes( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$post_date = stripslashes($post_date); $post_date = stripslashes( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
if (!empty ($post_date)) $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$post_date = $wpdb->prepare("AND post_date = %s", $post_date); $args = array();
if (!empty ($title)) if ( !empty ( $date ) ) {
return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s $post_date", $title) ); $query .= ' AND post_date = %s';
else $args[] = $post_date;
if (!empty ($content)) }
return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_content = %s $post_date", $content) );
if ( !empty ( $title ) ) {
$query .= ' AND post_title = %s';
$args[] = $post_title;
}
if ( !empty ( $content ) ) {
$query .= 'AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0; return 0;
} }