Make WordPress Core


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

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

See ##33638.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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' );
Note: See TracChangeset for help on using the changeset viewer.