[wp-trac] [WordPress Trac] #36302: wp_update_comment needs a filter
WordPress Trac
noreply at wordpress.org
Tue Mar 22 19:21:27 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 | Keywords:
Focuses: |
-------------------------+-----------------------------
wp_update_comment needs a filter
Prospective Update: Here's what the filters might look like:
{{{#!php
<?php
/**
* To be able to add additional checks
* such as (empty comment_content, same content...etc)
*/
$commentdata = apply_filters( 'wp_update_comment', $commentdata );
if ( ! $commentdata || is_wp_error( $commentdata ) ) {
$message = apply_filters(
'wp_update_comment_error',
__('We apologize, but an error has occurred while
processing your request'));
do_action( 'wp_update_comment_error',$commentdata);
if ( defined('DOING_AJAX') ){
die($message );
}
wp_die( $message, 429 );
}
}}}
WP 4.4.2 wp_update_comment
{{{#!php
<?php
function wp_update_comment($commentarr) {
global $wpdb;
// First, get all of the original fields
$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
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;
}
/**
* To be able to add additional checks
* such as (empty comment_content, same content...etc)
*/
$commentdata = apply_filters( 'wp_update_comment', $commentdata );
if ( ! $commentdata || is_wp_error( $commentdata ) ) {
$message = apply_filters(
'wp_update_comment_error',
__('We apologize, but an error has occurred while
processing your request'));
do_action( 'wp_update_comment_error',$commentdata);
if ( defined('DOING_AJAX') ){
die($message );
}
wp_die( $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
*
* @param int $comment_ID The comment ID.
*/
do_action( 'edit_comment', $comment_ID );
$comment = get_comment($comment_ID);
wp_transition_comment_status($comment->comment_approved,
$old_status, $comment);
return $rval;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/36302>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list