web/wp-includes/comment.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
   130  * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
   130  * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
   131  * @return object|array|null Depends on $output value.
   131  * @return object|array|null Depends on $output value.
   132  */
   132  */
   133 function &get_comment(&$comment, $output = OBJECT) {
   133 function &get_comment(&$comment, $output = OBJECT) {
   134 	global $wpdb;
   134 	global $wpdb;
       
   135 	$null = null;
   135 
   136 
   136 	if ( empty($comment) ) {
   137 	if ( empty($comment) ) {
   137 		if ( isset($GLOBALS['comment']) )
   138 		if ( isset($GLOBALS['comment']) )
   138 			$_comment = & $GLOBALS['comment'];
   139 			$_comment = & $GLOBALS['comment'];
   139 		else
   140 		else
   144 	} else {
   145 	} else {
   145 		if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
   146 		if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
   146 			$_comment = & $GLOBALS['comment'];
   147 			$_comment = & $GLOBALS['comment'];
   147 		} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
   148 		} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
   148 			$_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
   149 			$_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
       
   150 			if ( ! $_comment )
       
   151 				return $null;
   149 			wp_cache_add($_comment->comment_ID, $_comment, 'comment');
   152 			wp_cache_add($_comment->comment_ID, $_comment, 'comment');
   150 		}
   153 		}
   151 	}
   154 	}
   152 
   155 
   153 	$_comment = apply_filters('get_comment', $_comment);
   156 	$_comment = apply_filters('get_comment', $_comment);
   206 		$approved = "comment_approved = '0'";
   209 		$approved = "comment_approved = '0'";
   207 	elseif ( 'approve' == $status )
   210 	elseif ( 'approve' == $status )
   208 		$approved = "comment_approved = '1'";
   211 		$approved = "comment_approved = '1'";
   209 	elseif ( 'spam' == $status )
   212 	elseif ( 'spam' == $status )
   210 		$approved = "comment_approved = 'spam'";
   213 		$approved = "comment_approved = 'spam'";
       
   214 	elseif ( 'trash' == $status )
       
   215 		$approved = "comment_approved = 'trash'";
   211 	else
   216 	else
   212 		$approved = "( comment_approved = '0' OR comment_approved = '1' )";
   217 		$approved = "( comment_approved = '0' OR comment_approved = '1' )";
   213 
   218 
   214 	$order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
   219 	$order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
   215 
   220 
   357 	}
   362 	}
   358 
   363 
   359 	return $comment_count;
   364 	return $comment_count;
   360 }
   365 }
   361 
   366 
       
   367 //
       
   368 // Comment meta functions
       
   369 //
       
   370 
       
   371 /**
       
   372  * Add meta data field to a comment.
       
   373  *
       
   374  * @since 2.9
       
   375  * @uses add_metadata
       
   376  * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
       
   377  *
       
   378  * @param int $comment_id Comment ID.
       
   379  * @param string $key Metadata name.
       
   380  * @param mixed $value Metadata value.
       
   381  * @param bool $unique Optional, default is false. Whether the same key should not be added.
       
   382  * @return bool False for failure. True for success.
       
   383  */
       
   384 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
       
   385 	return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
       
   386 }
       
   387 
       
   388 /**
       
   389  * Remove metadata matching criteria from a comment.
       
   390  *
       
   391  * You can match based on the key, or key and value. Removing based on key and
       
   392  * value, will keep from removing duplicate metadata with the same key. It also
       
   393  * allows removing all metadata matching key, if needed.
       
   394  *
       
   395  * @since 2.9
       
   396  * @uses delete_metadata
       
   397  * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
       
   398  *
       
   399  * @param int $comment_id comment ID
       
   400  * @param string $meta_key Metadata name.
       
   401  * @param mixed $meta_value Optional. Metadata value.
       
   402  * @return bool False for failure. True for success.
       
   403  */
       
   404 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
       
   405 	return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
       
   406 }
       
   407 
       
   408 /**
       
   409  * Retrieve comment meta field for a comment.
       
   410  *
       
   411  * @since 2.9
       
   412  * @uses get_metadata
       
   413  * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
       
   414  *
       
   415  * @param int $comment_id Comment ID.
       
   416  * @param string $key The meta key to retrieve.
       
   417  * @param bool $single Whether to return a single value.
       
   418  * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
       
   419  *  is true.
       
   420  */
       
   421 function get_comment_meta($comment_id, $key, $single = false) {
       
   422 	return get_metadata('comment', $comment_id, $key, $single);
       
   423 }
       
   424 
       
   425 /**
       
   426  * Update comment meta field based on comment ID.
       
   427  *
       
   428  * Use the $prev_value parameter to differentiate between meta fields with the
       
   429  * same key and comment ID.
       
   430  *
       
   431  * If the meta field for the comment does not exist, it will be added.
       
   432  *
       
   433  * @since 2.9
       
   434  * @uses update_metadata
       
   435  * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
       
   436  *
       
   437  * @param int $comment_id Comment ID.
       
   438  * @param string $key Metadata key.
       
   439  * @param mixed $value Metadata value.
       
   440  * @param mixed $prev_value Optional. Previous value to check before removing.
       
   441  * @return bool False on failure, true if success.
       
   442  */
       
   443 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
       
   444 	return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
       
   445 }
       
   446 
   362 /**
   447 /**
   363  * Sanitizes the cookies sent to the user already.
   448  * Sanitizes the cookies sent to the user already.
   364  *
   449  *
   365  * Will only do anything if the cookies have already been created for the user.
   450  * Will only do anything if the cookies have already been created for the user.
   366  * Mostly used after cookies had been sent to use elsewhere.
   451  * Mostly used after cookies had been sent to use elsewhere.
   404 	global $wpdb;
   489 	global $wpdb;
   405 	extract($commentdata, EXTR_SKIP);
   490 	extract($commentdata, EXTR_SKIP);
   406 
   491 
   407 	// Simple duplicate check
   492 	// Simple duplicate check
   408 	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
   493 	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
   409 	$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
   494 	$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' ";
   410 	if ( $comment_author_email )
   495 	if ( $comment_author_email )
   411 		$dupe .= "OR comment_author_email = '$comment_author_email' ";
   496 		$dupe .= "OR comment_author_email = '$comment_author_email' ";
   412 	$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
   497 	$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
   413 	if ( $wpdb->get_var($dupe) ) {
   498 	if ( $wpdb->get_var($dupe) ) {
   414 		if ( defined('DOING_AJAX') )
   499 		if ( defined('DOING_AJAX') )
   417 		wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
   502 		wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
   418 	}
   503 	}
   419 
   504 
   420 	do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
   505 	do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
   421 
   506 
   422 	if ( $user_id ) {
   507 	if ( isset($user_id) && $user_id) {
   423 		$userdata = get_userdata($user_id);
   508 		$userdata = get_userdata($user_id);
   424 		$user = new WP_User($user_id);
   509 		$user = new WP_User($user_id);
   425 		$post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
   510 		$post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
   426 	}
   511 	}
   427 
   512 
   461  */
   546  */
   462 function check_comment_flood_db( $ip, $email, $date ) {
   547 function check_comment_flood_db( $ip, $email, $date ) {
   463 	global $wpdb;
   548 	global $wpdb;
   464 	if ( current_user_can( 'manage_options' ) )
   549 	if ( current_user_can( 'manage_options' ) )
   465 		return; // don't throttle admins
   550 		return; // don't throttle admins
   466 	if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = %s OR comment_author_email = %s ORDER BY comment_date DESC LIMIT 1", $ip, $email) ) ) {
   551 	$hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
       
   552 	if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) {
   467 		$time_lastcomment = mysql2date('U', $lasttime, false);
   553 		$time_lastcomment = mysql2date('U', $lasttime, false);
   468 		$time_newcomment  = mysql2date('U', $date, false);
   554 		$time_newcomment  = mysql2date('U', $date, false);
   469 		$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
   555 		$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
   470 		if ( $flood_die ) {
   556 		if ( $flood_die ) {
   471 			do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
   557 			do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
   621  * @return bool True if comment contains blacklisted content, false if comment does not
   707  * @return bool True if comment contains blacklisted content, false if comment does not
   622  */
   708  */
   623 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
   709 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
   624 	do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
   710 	do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
   625 
   711 
   626 	if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
       
   627 		foreach ( (array) $chars[1] as $char ) {
       
   628 			// If it's an encoded char in the normal ASCII set, reject
       
   629 			if ( 38 == $char )
       
   630 				continue; // Unless it's &
       
   631 			if ( $char < 128 )
       
   632 				return true;
       
   633 		}
       
   634 	}
       
   635 
       
   636 	$mod_keys = trim( get_option('blacklist_keys') );
   712 	$mod_keys = trim( get_option('blacklist_keys') );
   637 	if ( '' == $mod_keys )
   713 	if ( '' == $mod_keys )
   638 		return false; // If moderation keys are empty
   714 		return false; // If moderation keys are empty
   639 	$words = explode("\n", $mod_keys );
   715 	$words = explode("\n", $mod_keys );
   640 
   716 
   691 
   767 
   692 	if ( false !== $count )
   768 	if ( false !== $count )
   693 		return $count;
   769 		return $count;
   694 
   770 
   695 	$where = '';
   771 	$where = '';
   696 	if( $post_id > 0 )
   772 	if ( $post_id > 0 )
   697 		$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
   773 		$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
   698 
   774 
   699 	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
   775 	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
   700 
   776 
   701 	$total = 0;
   777 	$total = 0;
   702 	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
   778 	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
   703 	$known_types = array_keys( $approved );
   779 	$known_types = array_keys( $approved );
   704 	foreach( (array) $count as $row_num => $row ) {
   780 	foreach( (array) $count as $row_num => $row ) {
   705 		$total += $row['num_comments'];
   781 		// Don't count post-trashed toward totals
       
   782 		if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
       
   783 			$total += $row['num_comments'];
   706 		if ( in_array( $row['comment_approved'], $known_types ) )
   784 		if ( in_array( $row['comment_approved'], $known_types ) )
   707 			$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
   785 			$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
   708 	}
   786 	}
   709 
   787 
   710 	$stats['total_comments'] = $total;
   788 	$stats['total_comments'] = $total;
   726  * post ID available.
   804  * post ID available.
   727  *
   805  *
   728  * @since 2.0.0
   806  * @since 2.0.0
   729  * @uses $wpdb
   807  * @uses $wpdb
   730  * @uses do_action() Calls 'delete_comment' hook on comment ID
   808  * @uses do_action() Calls 'delete_comment' hook on comment ID
       
   809  * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success
   731  * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
   810  * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
   732  * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
   811  * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
   733  *
   812  *
   734  * @param int $comment_id Comment ID
   813  * @param int $comment_id Comment ID
   735  * @return bool False if delete comment query failure, true on success.
   814  * @return bool False if delete comment query failure, true on success.
   736  */
   815  */
   737 function wp_delete_comment($comment_id) {
   816 function wp_delete_comment($comment_id) {
   738 	global $wpdb;
   817 	global $wpdb;
       
   818 	if (!$comment = get_comment($comment_id))
       
   819 		return false;
       
   820 
       
   821 	if (wp_get_comment_status($comment_id) != 'trash' && wp_get_comment_status($comment_id) != 'spam' && EMPTY_TRASH_DAYS > 0)
       
   822 		return wp_trash_comment($comment_id);
       
   823 
   739 	do_action('delete_comment', $comment_id);
   824 	do_action('delete_comment', $comment_id);
   740 
       
   741 	$comment = get_comment($comment_id);
       
   742 
       
   743 	if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
       
   744 		return false;
       
   745 
   825 
   746 	// Move children up a level.
   826 	// Move children up a level.
   747 	$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
   827 	$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
   748 	if ( !empty($children) ) {
   828 	if ( !empty($children) ) {
   749 		$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
   829 		$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
   750 		clean_comment_cache($children);
   830 		clean_comment_cache($children);
   751 	}
   831 	}
   752 
   832 
       
   833 	// Delete metadata
       
   834 	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d ", $comment_id ) );
       
   835 	if ( !empty($meta_ids) ) {
       
   836 		do_action( 'delete_commentmeta', $meta_ids );
       
   837 		$in_meta_ids = "'" . implode("', '", $meta_ids) . "'";
       
   838 		$wpdb->query( "DELETE FROM $wpdb->commentmeta WHERE meta_id IN ($in_meta_ids)" );
       
   839 		do_action( 'deleted_commentmeta', $meta_ids );
       
   840 	}
       
   841 
       
   842 	if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
       
   843 		return false;
       
   844 	do_action('deleted_comment', $comment_id);
       
   845 
   753 	$post_id = $comment->comment_post_ID;
   846 	$post_id = $comment->comment_post_ID;
   754 	if ( $post_id && $comment->comment_approved == 1 )
   847 	if ( $post_id && $comment->comment_approved == 1 )
   755 		wp_update_comment_count($post_id);
   848 		wp_update_comment_count($post_id);
   756 
   849 
   757 	clean_comment_cache($comment_id);
   850 	clean_comment_cache($comment_id);
   760 	wp_transition_comment_status('delete', $comment->comment_approved, $comment);
   853 	wp_transition_comment_status('delete', $comment->comment_approved, $comment);
   761 	return true;
   854 	return true;
   762 }
   855 }
   763 
   856 
   764 /**
   857 /**
       
   858  * Moves a comment to the Trash
       
   859  *
       
   860  * @since 2.9.0
       
   861  * @uses do_action() on 'trash_comment' before trashing
       
   862  * @uses do_action() on 'trashed_comment' after trashing
       
   863  *
       
   864  * @param int $comment_id Comment ID.
       
   865  * @return mixed False on failure
       
   866  */
       
   867 function wp_trash_comment($comment_id) {
       
   868 	if ( EMPTY_TRASH_DAYS == 0 )
       
   869 		return wp_delete_comment($comment_id);
       
   870 
       
   871 	if ( !$comment = get_comment($comment_id) )
       
   872 		return false;
       
   873 
       
   874 	do_action('trash_comment', $comment_id);
       
   875 
       
   876 	if ( wp_set_comment_status($comment_id, 'trash') ) {
       
   877 		add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
       
   878 		add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
       
   879 		do_action('trashed_comment', $comment_id);
       
   880 		return true;
       
   881 	}
       
   882 
       
   883 	return false;
       
   884 }
       
   885 
       
   886 /**
       
   887  * Removes a comment from the Trash
       
   888  *
       
   889  * @since 2.9.0
       
   890  * @uses do_action() on 'untrash_comment' before untrashing
       
   891  * @uses do_action() on 'untrashed_comment' after untrashing
       
   892  *
       
   893  * @param int $comment_id Comment ID.
       
   894  * @return mixed False on failure
       
   895  */
       
   896 function wp_untrash_comment($comment_id) {
       
   897 	if ( ! (int)$comment_id )
       
   898 		return false;
       
   899 
       
   900 	do_action('untrash_comment', $comment_id);
       
   901 
       
   902 	$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
       
   903 	if ( empty($status) )
       
   904 		$status = '0';
       
   905 
       
   906 	if ( wp_set_comment_status($comment_id, $status) ) {
       
   907 		delete_comment_meta($comment_id, '_wp_trash_meta_time');
       
   908 		delete_comment_meta($comment_id, '_wp_trash_meta_status');
       
   909 		do_action('untrashed_comment', $comment_id);
       
   910 		return true;
       
   911 	}
       
   912 
       
   913 	return false;
       
   914 }
       
   915 
       
   916 /**
       
   917  * Marks a comment as Spam
       
   918  *
       
   919  * @since 2.9.0
       
   920  * @uses do_action() on 'spam_comment' before spamming
       
   921  * @uses do_action() on 'spammed_comment' after spamming
       
   922  *
       
   923  * @param int $comment_id Comment ID.
       
   924  * @return mixed False on failure
       
   925  */
       
   926 function wp_spam_comment($comment_id) {
       
   927 	if ( !$comment = get_comment($comment_id) )
       
   928 		return false;
       
   929 
       
   930 	do_action('spam_comment', $comment_id);
       
   931 
       
   932 	if ( wp_set_comment_status($comment_id, 'spam') ) {
       
   933 		add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
       
   934 		do_action('spammed_comment', $comment_id);
       
   935 		return true;
       
   936 	}
       
   937 
       
   938 	return false;
       
   939 }
       
   940 
       
   941 /**
       
   942  * Removes a comment from the Spam
       
   943  *
       
   944  * @since 2.9.0
       
   945  * @uses do_action() on 'unspam_comment' before unspamming
       
   946  * @uses do_action() on 'unspammed_comment' after unspamming
       
   947  *
       
   948  * @param int $comment_id Comment ID.
       
   949  * @return mixed False on failure
       
   950  */
       
   951 function wp_unspam_comment($comment_id) {
       
   952 	if ( ! (int)$comment_id )
       
   953 		return false;
       
   954 
       
   955 	do_action('unspam_comment', $comment_id);
       
   956 
       
   957 	$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
       
   958 	if ( empty($status) )
       
   959 		$status = '0';
       
   960 
       
   961 	if ( wp_set_comment_status($comment_id, $status) ) {
       
   962 		delete_comment_meta($comment_id, '_wp_trash_meta_status');
       
   963 		do_action('unspammed_comment', $comment_id);
       
   964 		return true;
       
   965 	}
       
   966 
       
   967 	return false;
       
   968 }
       
   969 
       
   970 /**
   765  * The status of a comment by ID.
   971  * The status of a comment by ID.
   766  *
   972  *
   767  * @since 1.0.0
   973  * @since 1.0.0
   768  *
   974  *
   769  * @param int $comment_id Comment ID
   975  * @param int $comment_id Comment ID
   770  * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure.
   976  * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
   771  */
   977  */
   772 function wp_get_comment_status($comment_id) {
   978 function wp_get_comment_status($comment_id) {
   773 	$comment = get_comment($comment_id);
   979 	$comment = get_comment($comment_id);
   774 	if ( !$comment )
   980 	if ( !$comment )
   775 		return false;
   981 		return false;
   776 
   982 
   777 	$approved = $comment->comment_approved;
   983 	$approved = $comment->comment_approved;
   778 
   984 
   779 	if ( $approved == NULL )
   985 	if ( $approved == NULL )
   780 		return 'deleted';
   986 		return false;
   781 	elseif ( $approved == '1' )
   987 	elseif ( $approved == '1' )
   782 		return 'approved';
   988 		return 'approved';
   783 	elseif ( $approved == '0' )
   989 	elseif ( $approved == '0' )
   784 		return 'unapproved';
   990 		return 'unapproved';
   785 	elseif ( $approved == 'spam' )
   991 	elseif ( $approved == 'spam' )
   786 		return 'spam';
   992 		return 'spam';
       
   993 	elseif ( $approved == 'trash' )
       
   994 		return 'trash';
   787 	else
   995 	else
   788 		return false;
   996 		return false;
   789 }
   997 }
   790 
   998 
   791 /**
   999 /**
   923  *
  1131  *
   924  * @param array $commentdata Contains information on the comment.
  1132  * @param array $commentdata Contains information on the comment.
   925  * @return array Parsed comment information.
  1133  * @return array Parsed comment information.
   926  */
  1134  */
   927 function wp_filter_comment($commentdata) {
  1135 function wp_filter_comment($commentdata) {
   928 	$commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']);
  1136 	if ( isset($commentdata['user_ID']) )
       
  1137 		$commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
       
  1138 	elseif ( isset($commentdata['user_id']) )
       
  1139 		$commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_id']);
   929 	$commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
  1140 	$commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
   930 	$commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
  1141 	$commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
   931 	$commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
  1142 	$commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
   932 	$commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
  1143 	$commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
   933 	$commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
  1144 	$commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
   974  */
  1185  */
   975 function wp_new_comment( $commentdata ) {
  1186 function wp_new_comment( $commentdata ) {
   976 	$commentdata = apply_filters('preprocess_comment', $commentdata);
  1187 	$commentdata = apply_filters('preprocess_comment', $commentdata);
   977 
  1188 
   978 	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
  1189 	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
   979 	$commentdata['user_ID']         = (int) $commentdata['user_ID'];
  1190 	if ( isset($commentdata['user_ID']) )
   980 
  1191 		$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
   981 	$commentdata['comment_parent'] = absint($commentdata['comment_parent']);
  1192 	elseif ( isset($commentdata['user_id']) )
       
  1193 		$commentdata['user_id'] = (int) $commentdata['user_id'];
       
  1194 
       
  1195 	$commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
   982 	$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
  1196 	$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
   983 	$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
  1197 	$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
   984 
  1198 
   985 	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
  1199 	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
   986 	$commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT'];
  1200 	$commentdata['comment_agent']     = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
   987 
  1201 
   988 	$commentdata['comment_date']     = current_time('mysql');
  1202 	$commentdata['comment_date']     = current_time('mysql');
   989 	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
  1203 	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
   990 
  1204 
   991 	$commentdata = wp_filter_comment($commentdata);
  1205 	$commentdata = wp_filter_comment($commentdata);
  1000 		if ( '0' == $commentdata['comment_approved'] )
  1214 		if ( '0' == $commentdata['comment_approved'] )
  1001 			wp_notify_moderator($comment_ID);
  1215 			wp_notify_moderator($comment_ID);
  1002 
  1216 
  1003 		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
  1217 		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
  1004 
  1218 
  1005 		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
  1219 		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_id'] )
  1006 			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
  1220 			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
  1007 	}
  1221 	}
  1008 
  1222 
  1009 	return $comment_ID;
  1223 	return $comment_ID;
  1010 }
  1224 }
  1030 	global $wpdb;
  1244 	global $wpdb;
  1031 
  1245 
  1032 	$status = '0';
  1246 	$status = '0';
  1033 	switch ( $comment_status ) {
  1247 	switch ( $comment_status ) {
  1034 		case 'hold':
  1248 		case 'hold':
       
  1249 		case '0':
  1035 			$status = '0';
  1250 			$status = '0';
  1036 			break;
  1251 			break;
  1037 		case 'approve':
  1252 		case 'approve':
       
  1253 		case '1':
  1038 			$status = '1';
  1254 			$status = '1';
  1039 			if ( get_option('comments_notify') ) {
  1255 			if ( get_option('comments_notify') ) {
  1040 				$comment = get_comment($comment_id);
  1256 				$comment = get_comment($comment_id);
  1041 				wp_notify_postauthor($comment_id, $comment->comment_type);
  1257 				wp_notify_postauthor($comment_id, $comment->comment_type);
  1042 			}
  1258 			}
  1043 			break;
  1259 			break;
  1044 		case 'spam':
  1260 		case 'spam':
  1045 			$status = 'spam';
  1261 			$status = 'spam';
  1046 			break;
  1262 			break;
  1047 		case 'delete':
  1263 		case 'trash':
  1048 			return wp_delete_comment($comment_id);
  1264 			$status = 'trash';
  1049 			break;
  1265 			break;
  1050 		default:
  1266 		default:
  1051 			return false;
  1267 			return false;
  1052 	}
  1268 	}
       
  1269 
       
  1270 	$comment_old = wp_clone(get_comment($comment_id));
  1053 
  1271 
  1054 	if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
  1272 	if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
  1055 		if ( $wp_error )
  1273 		if ( $wp_error )
  1056 			return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
  1274 			return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
  1057 		else
  1275 		else
  1061 	clean_comment_cache($comment_id);
  1279 	clean_comment_cache($comment_id);
  1062 
  1280 
  1063 	$comment = get_comment($comment_id);
  1281 	$comment = get_comment($comment_id);
  1064 
  1282 
  1065 	do_action('wp_set_comment_status', $comment_id, $comment_status);
  1283 	do_action('wp_set_comment_status', $comment_id, $comment_status);
  1066 	wp_transition_comment_status($comment_status, $comment->comment_approved, $comment);
  1284 	wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
  1067 
  1285 
  1068 	wp_update_comment_count($comment->comment_post_ID);
  1286 	wp_update_comment_count($comment->comment_post_ID);
  1069 
  1287 
  1070 	return true;
  1288 	return true;
  1071 }
  1289 }
  1087 
  1305 
  1088 	// First, get all of the original fields
  1306 	// First, get all of the original fields
  1089 	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
  1307 	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
  1090 
  1308 
  1091 	// Escape data pulled from DB.
  1309 	// Escape data pulled from DB.
  1092 	$comment = $wpdb->escape($comment);
  1310 	$comment = esc_sql($comment);
  1093 
  1311 
  1094 	$old_status = $comment['comment_approved'];
  1312 	$old_status = $comment['comment_approved'];
  1095 
  1313 
  1096 	// Merge old and new fields with new fields overwriting old ones.
  1314 	// Merge old and new fields with new fields overwriting old ones.
  1097 	$commentarr = array_merge($comment, $commentarr);
  1315 	$commentarr = array_merge($comment, $commentarr);
  1307 function do_all_pings() {
  1525 function do_all_pings() {
  1308 	global $wpdb;
  1526 	global $wpdb;
  1309 
  1527 
  1310 	// Do pingbacks
  1528 	// Do pingbacks
  1311 	while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
  1529 	while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
  1312 		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
  1530 		$mid = $wpdb->get_var( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme' LIMIT 1");
       
  1531 		do_action( 'delete_postmeta', $mid );
       
  1532 		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mid ) );
       
  1533 		do_action( 'deleted_postmeta', $mid );
  1313 		pingback($ping->post_content, $ping->ID);
  1534 		pingback($ping->post_content, $ping->ID);
  1314 	}
  1535 	}
  1315 
  1536 
  1316 	// Do Enclosures
  1537 	// Do Enclosures
  1317 	while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
  1538 	while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
  1318 		$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
  1539 		$mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme'", $enclosure->ID) );
       
  1540 		do_action( 'delete_postmeta', $mid );
       
  1541 		$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id =  %d", $mid) );
       
  1542 		do_action( 'deleted_postmeta', $mid );
  1319 		do_enclose($enclosure->post_content, $enclosure->ID);
  1543 		do_enclose($enclosure->post_content, $enclosure->ID);
  1320 	}
  1544 	}
  1321 
  1545 
  1322 	// Do Trackbacks
  1546 	// Do Trackbacks
  1323 	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
  1547 	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
  1455 			$pagelinkedfrom = get_permalink($post_ID);
  1679 			$pagelinkedfrom = get_permalink($post_ID);
  1456 
  1680 
  1457 			// using a timeout of 3 seconds should be enough to cover slow servers
  1681 			// using a timeout of 3 seconds should be enough to cover slow servers
  1458 			$client = new IXR_Client($pingback_server_url);
  1682 			$client = new IXR_Client($pingback_server_url);
  1459 			$client->timeout = 3;
  1683 			$client->timeout = 3;
  1460 			$client->useragent .= ' -- WordPress/' . $wp_version;
  1684 			$client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom);
  1461 
       
  1462 			// when set to true, this outputs debug messages by itself
  1685 			// when set to true, this outputs debug messages by itself
  1463 			$client->debug = false;
  1686 			$client->debug = false;
  1464 
  1687 
  1465 			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
  1688 			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
  1466 				add_ping( $post_ID, $pagelinkedto );
  1689 				add_ping( $post_ID, $pagelinkedto );