WordPress.org

Make WordPress Core

Changeset 34129


Ignore:
Timestamp:
09/14/2015 09:39:46 PM (5 years ago)
Author:
wonderboymusic
Message:

More comment functions can accept a full object instead of comment_ID to reduce cache/db lookups.

See ##33638.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/comment.php

    r34042 r34129  
    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;
  • trunk/src/wp-admin/includes/ajax-actions.php

    r34074 r34129  
    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;
     
    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) ) {
     
    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        }
  • trunk/src/wp-includes/comment-functions.php

    r34106 r34129  
    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 */
     
    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
     
    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
     
    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;
     
    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);
     
    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 */
     
    10521052     * @param int $comment_id The comment ID.
    10531053     */
    1054     do_action( 'trash_comment', $comment_id );
    1055 
    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() );
     1054    do_action( 'trash_comment', $comment->comment_ID );
     1055
     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        /**
     
    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    }
     
    10761076/**
    10771077 * Removes a comment from the Trash
     1078 *
     1079 * @since 2.9.0
     1080 *
     1081 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
     1082 * @return bool True on success, false on failure.
     1083 */
     1084function wp_untrash_comment($comment_id) {
     1085    $comment = get_comment( $comment_id );
     1086    if ( ! $comment ) {
     1087        return false;
     1088    }
     1089
     1090    /**
     1091     * Fires immediately before a comment is restored from the Trash.
     1092     *
     1093     * @since 2.9.0
     1094     *
     1095     * @param int $comment_id The comment ID.
     1096     */
     1097    do_action( 'untrash_comment', $comment->comment_ID );
     1098
     1099    $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
     1100    if ( empty($status) )
     1101        $status = '0';
     1102
     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' );
     1106        /**
     1107         * Fires immediately after a comment is restored from the Trash.
     1108         *
     1109         * @since 2.9.0
     1110         *
     1111         * @param int $comment_id The comment ID.
     1112         */
     1113        do_action( 'untrashed_comment', $comment->comment_ID );
     1114        return true;
     1115    }
     1116
     1117    return false;
     1118}
     1119
     1120/**
     1121 * Marks a comment as Spam
     1122 *
     1123 * @since 2.9.0
     1124 *
     1125 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
     1126 * @return bool True on success, false on failure.
     1127 */
     1128function wp_spam_comment( $comment_id ) {
     1129    $comment = get_comment( $comment_id );
     1130    if ( ! $comment ) {
     1131        return false;
     1132    }
     1133
     1134    /**
     1135     * Fires immediately before a comment is marked as Spam.
     1136     *
     1137     * @since 2.9.0
     1138     *
     1139     * @param int $comment_id The comment ID.
     1140     */
     1141    do_action( 'spam_comment', $comment->comment_ID );
     1142
     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() );
     1148        /**
     1149         * Fires immediately after a comment is marked as Spam.
     1150         *
     1151         * @since 2.9.0
     1152         *
     1153         * @param int $comment_id The comment ID.
     1154         */
     1155        do_action( 'spammed_comment', $comment->comment_ID );
     1156        return true;
     1157    }
     1158
     1159    return false;
     1160}
     1161
     1162/**
     1163 * Removes a comment from the Spam
    10781164 *
    10791165 * @since 2.9.0
     
    10821168 * @return bool True on success, false on failure.
    10831169 */
    1084 function wp_untrash_comment($comment_id) {
     1170function wp_unspam_comment($comment_id) {
    10851171    if ( ! (int)$comment_id )
    10861172        return false;
    10871173
    1088     /**
    1089      * Fires immediately before a comment is restored from the Trash.
     1174    $comment = get_comment( $comment_id );
     1175    if ( ! $comment ) {
     1176        return false;
     1177    }
     1178
     1179    /**
     1180     * Fires immediately before a comment is unmarked as Spam.
    10901181     *
    10911182     * @since 2.9.0
     
    10931184     * @param int $comment_id The comment ID.
    10941185     */
    1095     do_action( 'untrash_comment', $comment_id );
     1186    do_action( 'unspam_comment', $comment_id );
    10961187
    10971188    $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
     
    10991190        $status = '0';
    11001191
    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');
    1104         /**
    1105          * Fires immediately after a comment is restored from the Trash.
    1106          *
    1107          * @since 2.9.0
    1108          *
    1109          * @param int $comment_id The comment ID.
    1110          */
    1111         do_action( 'untrashed_comment', $comment_id );
    1112         return true;
    1113     }
    1114 
    1115     return false;
    1116 }
    1117 
    1118 /**
    1119  * Marks a comment as Spam
    1120  *
    1121  * @since 2.9.0
    1122  *
    1123  * @param int $comment_id Comment ID.
    1124  * @return bool True on success, false on failure.
    1125  */
    1126 function wp_spam_comment($comment_id) {
    1127     if ( !$comment = get_comment($comment_id) )
    1128         return false;
    1129 
    1130     /**
    1131      * Fires immediately before a comment is marked as Spam.
    1132      *
    1133      * @since 2.9.0
    1134      *
    1135      * @param int $comment_id The comment ID.
    1136      */
    1137     do_action( 'spam_comment', $comment_id );
    1138 
    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() );
    1144         /**
    1145          * Fires immediately after a comment is marked as Spam.
    1146          *
    1147          * @since 2.9.0
    1148          *
    1149          * @param int $comment_id The comment ID.
    1150          */
    1151         do_action( 'spammed_comment', $comment_id );
    1152         return true;
    1153     }
    1154 
    1155     return false;
    1156 }
    1157 
    1158 /**
    1159  * Removes a comment from the Spam
    1160  *
    1161  * @since 2.9.0
    1162  *
    1163  * @param int $comment_id Comment ID.
    1164  * @return bool True on success, false on failure.
    1165  */
    1166 function wp_unspam_comment($comment_id) {
    1167     if ( ! (int)$comment_id )
    1168         return false;
    1169 
    1170     /**
    1171      * Fires immediately before a comment is unmarked as Spam.
    1172      *
    1173      * @since 2.9.0
    1174      *
    1175      * @param int $comment_id The comment ID.
    1176      */
    1177     do_action( 'unspam_comment', $comment_id );
    1178 
    1179     $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
    1180     if ( empty($status) )
    1181         $status = '0';
    1182 
    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' );
     
    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 */
     
    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);
     
    17151724    }
    17161725
    1717     clean_comment_cache($comment_id);
    1718 
    1719     $comment = get_comment($comment_id);
     1726    clean_comment_cache( $comment_old->comment_ID );
     1727
     1728    $comment = get_comment( $comment_old->comment_ID );
    17201729
    17211730    /**
     
    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);
     
    23172326 */
    23182327function 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' );
  • trunk/src/wp-includes/functions.php

    r34104 r34129  
    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    }
  • trunk/src/wp-includes/pluggable.php

    r34125 r34129  
    13541354 * @since 1.0.0
    13551355 *
    1356  * @param int    $comment_id Comment ID
    1357  * @param string $deprecated Not used
     1356 * @param int|WP_Comment  $comment_id Comment ID or WP_Comment object.
     1357 * @param string          $deprecated Not used
    13581358 * @return bool True on completion. False if no email addresses were specified.
    13591359 */
     
    13871387     * @param int   $comment_id The comment ID.
    13881388     */
    1389     $emails = apply_filters( 'comment_notification_recipients', $emails, $comment_id );
     1389    $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID );
    13901390    $emails = array_filter( $emails );
    13911391
     
    14101410     * @param int  $comment_id The comment ID.
    14111411     */
    1412     $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment_id );
     1412    $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );
    14131413
    14141414    // The comment was left by the author
     
    14761476    $notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n";
    14771477
    1478     if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
    1479         if ( EMPTY_TRASH_DAYS )
    1480             $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
    1481         else
    1482             $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
    1483         $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
     1478    if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
     1479        if ( EMPTY_TRASH_DAYS ) {
     1480            $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c={$comment->comment_ID}") ) . "\r\n";
     1481        } else {
     1482            $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c={$comment->comment_ID}") ) . "\r\n";
     1483        }
     1484        $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c={$comment->comment_ID}") ) . "\r\n";
    14841485    }
    14851486
     
    15101511     * @param int    $comment_id     Comment ID.
    15111512     */
    1512     $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment_id );
     1513    $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
    15131514
    15141515    /**
     
    15201521     * @param int    $comment_id Comment ID.
    15211522     */
    1522     $subject = apply_filters( 'comment_notification_subject', $subject, $comment_id );
     1523    $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
    15231524
    15241525    /**
     
    15301531     * @param int    $comment_id      Comment ID.
    15311532     */
    1532     $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment_id );
     1533    $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );
    15331534
    15341535    foreach ( $emails as $email ) {
Note: See TracChangeset for help on using the changeset viewer.