Skip to content

Commit 166ee86

Browse files
committed
More comment functions can accept a full object instead of comment_ID to reduce cache/db lookups.
See ##33638. git-svn-id: https://develop.svn.wordpress.org/trunk@34129 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ab7ca36 commit 166ee86

5 files changed

Lines changed: 83 additions & 70 deletions

File tree

src/wp-admin/comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@
278278
$redir = add_query_arg( array('unspammed' => '1'), $redir );
279279
break;
280280
case 'approvecomment' :
281-
wp_set_comment_status( $comment_id, 'approve' );
281+
wp_set_comment_status( $comment, 'approve' );
282282
$redir = add_query_arg( array( 'approved' => 1 ), $redir );
283283
break;
284284
case 'unapprovecomment' :
285-
wp_set_comment_status( $comment_id, 'hold' );
285+
wp_set_comment_status( $comment, 'hold' );
286286
$redir = add_query_arg( array( 'unapproved' => 1 ), $redir );
287287
break;
288288
}

src/wp-admin/includes/ajax-actions.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function wp_ajax_delete_comment() {
520520
wp_die( -1 );
521521

522522
check_ajax_referer( "delete-comment_$id" );
523-
$status = wp_get_comment_status( $comment->comment_ID );
523+
$status = wp_get_comment_status( $comment );
524524

525525
$delta = -1;
526526
if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
@@ -730,15 +730,16 @@ function wp_ajax_dim_comment() {
730730
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) )
731731
wp_die( -1 );
732732

733-
$current = wp_get_comment_status( $comment->comment_ID );
733+
$current = wp_get_comment_status( $comment );
734734
if ( isset( $_POST['new'] ) && $_POST['new'] == $current )
735735
wp_die( time() );
736736

737737
check_ajax_referer( "approve-comment_$id" );
738-
if ( in_array( $current, array( 'unapproved', 'spam' ) ) )
739-
$result = wp_set_comment_status( $comment->comment_ID, 'approve', true );
740-
else
741-
$result = wp_set_comment_status( $comment->comment_ID, 'hold', true );
738+
if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) {
739+
$result = wp_set_comment_status( $comment, 'approve', true );
740+
} else {
741+
$result = wp_set_comment_status( $comment, 'hold', true );
742+
}
742743

743744
if ( is_wp_error($result) ) {
744745
$x = new WP_Ajax_Response( array(
@@ -1015,7 +1016,7 @@ function wp_ajax_replyto_comment( $action ) {
10151016
wp_die( -1 );
10161017
}
10171018

1018-
if ( wp_set_comment_status( $parent->comment_ID, 'approve' ) )
1019+
if ( wp_set_comment_status( $parent, 'approve' ) )
10191020
$comment_auto_approved = true;
10201021
}
10211022
}

src/wp-includes/comment-functions.php

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -969,16 +969,16 @@ function wp_count_comments( $post_id = 0 ) {
969969
*
970970
* @global wpdb $wpdb WordPress database abstraction object.
971971
*
972-
* @param int $comment_id Comment ID
973-
* @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
972+
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
973+
* @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
974974
* @return bool True on success, false on failure.
975975
*/
976976
function wp_delete_comment($comment_id, $force_delete = false) {
977977
global $wpdb;
978978
if (!$comment = get_comment($comment_id))
979979
return false;
980980

981-
if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
981+
if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) )
982982
return wp_trash_comment($comment_id);
983983

984984
/**
@@ -988,21 +988,21 @@ function wp_delete_comment($comment_id, $force_delete = false) {
988988
*
989989
* @param int $comment_id The comment ID.
990990
*/
991-
do_action( 'delete_comment', $comment_id );
991+
do_action( 'delete_comment', $comment->comment_ID );
992992

993993
// Move children up a level.
994-
$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
994+
$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) );
995995
if ( !empty($children) ) {
996-
$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
996+
$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID));
997997
clean_comment_cache($children);
998998
}
999999

10001000
// Delete metadata
1001-
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1001+
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
10021002
foreach ( $meta_ids as $mid )
10031003
delete_metadata_by_mid( 'comment', $mid );
10041004

1005-
if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1005+
if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) )
10061006
return false;
10071007

10081008
/**
@@ -1012,16 +1012,16 @@ function wp_delete_comment($comment_id, $force_delete = false) {
10121012
*
10131013
* @param int $comment_id The comment ID.
10141014
*/
1015-
do_action( 'deleted_comment', $comment_id );
1015+
do_action( 'deleted_comment', $comment->comment_ID );
10161016

10171017
$post_id = $comment->comment_post_ID;
10181018
if ( $post_id && $comment->comment_approved == 1 )
10191019
wp_update_comment_count($post_id);
10201020

1021-
clean_comment_cache($comment_id);
1021+
clean_comment_cache( $comment->comment_ID );
10221022

10231023
/** This action is documented in wp-includes/comment-functions.php */
1024-
do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1024+
do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );
10251025

10261026
wp_transition_comment_status('delete', $comment->comment_approved, $comment);
10271027
return true;
@@ -1034,7 +1034,7 @@ function wp_delete_comment($comment_id, $force_delete = false) {
10341034
*
10351035
* @since 2.9.0
10361036
*
1037-
* @param int $comment_id Comment ID.
1037+
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
10381038
* @return bool True on success, false on failure.
10391039
*/
10401040
function wp_trash_comment($comment_id) {
@@ -1051,13 +1051,13 @@ function wp_trash_comment($comment_id) {
10511051
*
10521052
* @param int $comment_id The comment ID.
10531053
*/
1054-
do_action( 'trash_comment', $comment_id );
1054+
do_action( 'trash_comment', $comment->comment_ID );
10551055

1056-
if ( wp_set_comment_status($comment_id, 'trash') ) {
1057-
delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
1058-
delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
1059-
add_comment_meta( $comment_id, '_wp_trash_meta_status', $comment->comment_approved );
1060-
add_comment_meta( $comment_id, '_wp_trash_meta_time', time() );
1056+
if ( wp_set_comment_status( $comment, 'trash' ) ) {
1057+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
1058+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
1059+
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
1060+
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
10611061

10621062
/**
10631063
* Fires immediately after a comment is sent to Trash.
@@ -1066,7 +1066,7 @@ function wp_trash_comment($comment_id) {
10661066
*
10671067
* @param int $comment_id The comment ID.
10681068
*/
1069-
do_action( 'trashed_comment', $comment_id );
1069+
do_action( 'trashed_comment', $comment->comment_ID );
10701070
return true;
10711071
}
10721072

@@ -1078,12 +1078,14 @@ function wp_trash_comment($comment_id) {
10781078
*
10791079
* @since 2.9.0
10801080
*
1081-
* @param int $comment_id Comment ID.
1081+
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
10821082
* @return bool True on success, false on failure.
10831083
*/
10841084
function wp_untrash_comment($comment_id) {
1085-
if ( ! (int)$comment_id )
1085+
$comment = get_comment( $comment_id );
1086+
if ( ! $comment ) {
10861087
return false;
1088+
}
10871089

10881090
/**
10891091
* Fires immediately before a comment is restored from the Trash.
@@ -1092,23 +1094,23 @@ function wp_untrash_comment($comment_id) {
10921094
*
10931095
* @param int $comment_id The comment ID.
10941096
*/
1095-
do_action( 'untrash_comment', $comment_id );
1097+
do_action( 'untrash_comment', $comment->comment_ID );
10961098

1097-
$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1099+
$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
10981100
if ( empty($status) )
10991101
$status = '0';
11001102

1101-
if ( wp_set_comment_status($comment_id, $status) ) {
1102-
delete_comment_meta($comment_id, '_wp_trash_meta_time');
1103-
delete_comment_meta($comment_id, '_wp_trash_meta_status');
1103+
if ( wp_set_comment_status( $comment, $status ) ) {
1104+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
1105+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
11041106
/**
11051107
* Fires immediately after a comment is restored from the Trash.
11061108
*
11071109
* @since 2.9.0
11081110
*
11091111
* @param int $comment_id The comment ID.
11101112
*/
1111-
do_action( 'untrashed_comment', $comment_id );
1113+
do_action( 'untrashed_comment', $comment->comment_ID );
11121114
return true;
11131115
}
11141116

@@ -1120,12 +1122,14 @@ function wp_untrash_comment($comment_id) {
11201122
*
11211123
* @since 2.9.0
11221124
*
1123-
* @param int $comment_id Comment ID.
1125+
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
11241126
* @return bool True on success, false on failure.
11251127
*/
1126-
function wp_spam_comment($comment_id) {
1127-
if ( !$comment = get_comment($comment_id) )
1128+
function wp_spam_comment( $comment_id ) {
1129+
$comment = get_comment( $comment_id );
1130+
if ( ! $comment ) {
11281131
return false;
1132+
}
11291133

11301134
/**
11311135
* Fires immediately before a comment is marked as Spam.
@@ -1134,21 +1138,21 @@ function wp_spam_comment($comment_id) {
11341138
*
11351139
* @param int $comment_id The comment ID.
11361140
*/
1137-
do_action( 'spam_comment', $comment_id );
1141+
do_action( 'spam_comment', $comment->comment_ID );
11381142

1139-
if ( wp_set_comment_status($comment_id, 'spam') ) {
1140-
delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
1141-
delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
1142-
add_comment_meta( $comment_id, '_wp_trash_meta_status', $comment->comment_approved );
1143-
add_comment_meta( $comment_id, '_wp_trash_meta_time', time() );
1143+
if ( wp_set_comment_status( $comment, 'spam' ) ) {
1144+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
1145+
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
1146+
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
1147+
add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
11441148
/**
11451149
* Fires immediately after a comment is marked as Spam.
11461150
*
11471151
* @since 2.9.0
11481152
*
11491153
* @param int $comment_id The comment ID.
11501154
*/
1151-
do_action( 'spammed_comment', $comment_id );
1155+
do_action( 'spammed_comment', $comment->comment_ID );
11521156
return true;
11531157
}
11541158

@@ -1167,6 +1171,11 @@ function wp_unspam_comment($comment_id) {
11671171
if ( ! (int)$comment_id )
11681172
return false;
11691173

1174+
$comment = get_comment( $comment_id );
1175+
if ( ! $comment ) {
1176+
return false;
1177+
}
1178+
11701179
/**
11711180
* Fires immediately before a comment is unmarked as Spam.
11721181
*
@@ -1180,7 +1189,7 @@ function wp_unspam_comment($comment_id) {
11801189
if ( empty($status) )
11811190
$status = '0';
11821191

1183-
if ( wp_set_comment_status($comment_id, $status) ) {
1192+
if ( wp_set_comment_status( $comment, $status ) ) {
11841193
delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
11851194
delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
11861195
/**
@@ -1675,9 +1684,9 @@ function wp_new_comment_notify_postauthor( $comment_ID ) {
16751684
*
16761685
* global wpdb $wpdb
16771686
*
1678-
* @param int $comment_id Comment ID.
1679-
* @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
1680-
* @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
1687+
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
1688+
* @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
1689+
* @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
16811690
* @return bool|WP_Error True on success, false or WP_Error on failure.
16821691
*/
16831692
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
@@ -1707,16 +1716,16 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
17071716

17081717
$comment_old = clone get_comment($comment_id);
17091718

1710-
if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
1719+
if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array( 'comment_ID' => $comment_old->comment_ID ) ) ) {
17111720
if ( $wp_error )
17121721
return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
17131722
else
17141723
return false;
17151724
}
17161725

1717-
clean_comment_cache($comment_id);
1726+
clean_comment_cache( $comment_old->comment_ID );
17181727

1719-
$comment = get_comment($comment_id);
1728+
$comment = get_comment( $comment_old->comment_ID );
17201729

17211730
/**
17221731
* Fires immediately before transitioning a comment's status from one to another
@@ -1728,7 +1737,7 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
17281737
* @param string|bool $comment_status Current comment status. Possible values include
17291738
* 'hold', 'approve', 'spam', 'trash', or false.
17301739
*/
1731-
do_action( 'wp_set_comment_status', $comment_id, $comment_status );
1740+
do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
17321741

17331742
wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
17341743

@@ -2316,8 +2325,10 @@ function xmlrpc_pingback_error( $ixr_error ) {
23162325
* @param int|array $ids Comment ID or array of comment IDs to remove from cache
23172326
*/
23182327
function clean_comment_cache($ids) {
2319-
foreach ( (array) $ids as $id )
2320-
wp_cache_delete($id, 'comment');
2328+
foreach ( (array) $ids as $id ) {
2329+
wp_cache_delete( $id, 'comment' );
2330+
wp_cache_delete( "comment-{$id}", 'counts' );
2331+
}
23212332

23222333
wp_cache_set( 'last_changed', microtime(), 'comment' );
23232334
}

src/wp-includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4265,7 +4265,7 @@ function wp_scheduled_delete() {
42654265
delete_comment_meta($comment_id, '_wp_trash_meta_time');
42664266
delete_comment_meta($comment_id, '_wp_trash_meta_status');
42674267
} else {
4268-
wp_delete_comment($comment_id);
4268+
wp_delete_comment( $del_comment );
42694269
}
42704270
}
42714271
}

0 commit comments

Comments
 (0)