[wp-trac] [WordPress Trac] #36302: wp_update_comment needs a filter

WordPress Trac noreply at wordpress.org
Thu Apr 28 13:56:49 UTC 2016


#36302: wp_update_comment needs a filter
-------------------------+------------------------------
 Reporter:  frankiet     |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Comments     |     Version:  4.4.2
 Severity:  normal       |  Resolution:
 Keywords:  needs-patch  |     Focuses:
-------------------------+------------------------------

Comment (by frankiet):

 Thank you all for the replies
 What do you think of this version?

 New
 {{{
 /**
  * Filter a comment's data before it is sanitized and updated into the
 database.
  *
  * @since x.x.x
  *
  * @param array $commentarr data.
  */
 $commentarr = apply_filters( 'wp_update_comment_data', $commentarr);

 // If an error occurred
 if ( ! $commentarr || is_wp_error( $commentarr ) ) {
     $error_message = __('We apologize, but an error has occurred while
 processing your request');
     if( is_wp_error( $commentarr ) ){
         $error_message = $commentarr->get_error_message();
     }

      /**
      * Fires after an error occurs
      *
      * @since  x.x.x
      *
      * @param string $error_message
      * @param array $commentarr data.
      */
     do_action( 'wp_update_comment_data_error', $error_message,
 $commentarr);

     if ( defined('DOING_AJAX') ){
         die($error_message );
     }

     wp_die( $error_message, 429 );
 }
 }}}


 New {{{do_action( 'edit_comment',$comment_ID, $comment, $old_comment); }}}

 Full version:
 {{{#!php
 <?php
 /**
  * Updates an existing comment in the database.
  *
  * Filters the comment and makes sure certain fields are valid before
 updating.
  *
  * @since 2.0.0
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
  * @param array $commentarr Contains information on the comment.
  * @return int Comment was updated if value is 1, or was not updated if
 value is 0.
  */
 function wp_update_comment($commentarr) {
     global $wpdb;

     // First, get all of the original fields
     $comment = get_comment($commentarr['comment_ID'], ARRAY_A);

     $old_comment = $comment;

     if ( empty( $comment ) ) {
         return 0;
     }

     // Make sure that the comment post ID is valid (if specified).
     if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post(
 $commentarr['comment_post_ID'] ) ) {
         return 0;
     }

     /**
      * Filter a comment's data before it is sanitized and updated into the
 database.
      *
      * @since x.x.x
      *
      * @param array $commentarr data.
      */
     $commentarr = apply_filters( 'wp_update_comment_data', $commentarr);

     // If an error occurred
     if ( ! $commentarr || is_wp_error( $commentarr ) ) {
         $error_message = __('We apologize, but an error has occurred while
 processing your request');
         if( is_wp_error( $commentarr ) ){
             $error_message = $commentarr->get_error_message();
         }

          /**
          * Fires after an error occurs
          *
          * @since x.x.x
          *
          * @param string $error_message
          * @param array $commentarr data.
          */
         do_action( 'wp_update_comment_data_error', $error_message,
 $commentarr);

         if ( defined('DOING_AJAX') ){
             die($error_message );
         }

         wp_die( $error_message, 429 );
     }

     // Escape data pulled from DB.
     $comment = wp_slash($comment);

     $old_status = $comment['comment_approved'];

     // Merge old and new fields with new fields overwriting old ones.
     $commentarr = array_merge($comment, $commentarr);

     $commentarr = wp_filter_comment( $commentarr );

     // Now extract the merged array.
     $data = wp_unslash( $commentarr );

     /**
      * Filter the comment content before it is updated in the database.
      *
      * @since 1.5.0
      *
      * @param string $comment_content The comment data.
      */
     $data['comment_content'] = apply_filters( 'comment_save_pre',
 $data['comment_content'] );

     $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date']
 );

     if ( ! isset( $data['comment_approved'] ) ) {
         $data['comment_approved'] = 1;
     } elseif ( 'hold' == $data['comment_approved'] ) {
         $data['comment_approved'] = 0;
     } elseif ( 'approve' == $data['comment_approved'] ) {
         $data['comment_approved'] = 1;
     }

     $comment_ID = $data['comment_ID'];
     $comment_post_ID = $data['comment_post_ID'];
     $keys = array( 'comment_post_ID', 'comment_content', 'comment_author',
 'comment_author_email', 'comment_approved', 'comment_karma',
 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type',
 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' );
     $data = wp_array_slice_assoc( $data, $keys );
     $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' )
 );

     clean_comment_cache( $comment_ID );
     wp_update_comment_count( $comment_post_ID );

     /**
      * Fires immediately after a comment is updated in the database.
      *
      * The hook also fires immediately before comment status transition
 hooks are fired.
      *
      * @since 1.2.0
      * @since x.x.x The `$comment` and `$old_comment` parameters were
 added.
      * @param int $comment_ID The comment ID.
      * @param object $comment The comment.
      * @param array $old_comment Old comment.
      */
     $comment = get_comment($comment_ID);
     do_action( 'edit_comment',$comment_ID, $comment, $old_comment);
     wp_transition_comment_status($comment->comment_approved, $old_status,
 $comment);
     return $rval;
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36302#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list