Comments: Consistently normalize user_ID to user_id in wp_new_comment().

For backward compatibility, the `user_id` parameter of `wp_new_comment()` can be spelled as `user_ID`, and plugins utilizing the `preprocess_comment` filter or the `comment_post` action should be able to receive both variations.

Follow-up to [12267], [12300], [28915], [36038], [53729].

Props peterwilsoncc, SergeyBiryukov.
Fixes #56244.

git-svn-id: https://develop.svn.wordpress.org/trunk@54489 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-10-11 16:30:40 +00:00
parent 645f474fc0
commit c5e9de416b
3 changed files with 60 additions and 3 deletions

View File

@ -2208,9 +2208,16 @@ function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment
function wp_new_comment( $commentdata, $wp_error = false ) {
global $wpdb;
/*
* Normalize `user_ID` to `user_id`, but pass the old key
* to the `preprocess_comment` filter for backward compatibility.
*/
if ( isset( $commentdata['user_ID'] ) ) {
$commentdata['user_ID'] = (int) $commentdata['user_ID'];
$commentdata['user_id'] = $commentdata['user_ID'];
} elseif ( isset( $commentdata['user_id'] ) ) {
$commentdata['user_id'] = (int) $commentdata['user_id'];
$commentdata['user_ID'] = $commentdata['user_id'];
}
$prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
@ -2235,11 +2242,13 @@ function wp_new_comment( $commentdata, $wp_error = false ) {
$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
// Normalize `user_ID` to `user_id` again, after the filter.
if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
$commentdata['user_ID'] = (int) $commentdata['user_ID'];
$commentdata['user_id'] = $commentdata['user_ID'];
} elseif ( isset( $commentdata['user_id'] ) ) {
$commentdata['user_id'] = (int) $commentdata['user_id'];
$commentdata['user_ID'] = $commentdata['user_id'];
}
$commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0;
@ -3587,7 +3596,6 @@ function wp_handle_comment_submission( $comment_data ) {
$commentdata = array(
'comment_post_ID' => $comment_post_id,
'user_ID' => $user_id,
);
$commentdata += compact(
@ -3596,7 +3604,8 @@ function wp_handle_comment_submission( $comment_data ) {
'comment_author_url',
'comment_content',
'comment_type',
'comment_parent'
'comment_parent',
'user_id'
);
/**

View File

@ -845,7 +845,6 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
* @covers ::wp_handle_comment_submission
*/
public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
$user = get_userdata( self::$author_id );
wp_set_current_user( $user->ID );

View File

@ -8,6 +8,8 @@ class Tests_Comment extends WP_UnitTestCase {
protected static $post_id;
protected static $notify_message = '';
protected $preprocess_comment_data = array();
public function set_up() {
parent::set_up();
reset_phpmailer_instance();
@ -456,6 +458,53 @@ class Tests_Comment extends WP_UnitTestCase {
$this->assertSame( strlen( $comment->comment_content ), 65535 );
}
/**
* @ticket 56244
*/
public function test_wp_new_comment_sends_all_expected_parameters_to_preprocess_comment_filter() {
$user = get_userdata( self::$user_id );
wp_set_current_user( $user->ID );
$data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => $user->user_url,
'comment_content' => 'Comment',
'comment_type' => '',
'comment_parent' => 0,
'user_id' => $user->ID,
);
add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
$comment = wp_new_comment( $data );
$this->assertNotWPError( $comment );
$this->assertSameSetsWithIndex(
array(
'comment_post_ID' => self::$post_id,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => $user->user_url,
'comment_content' => $data['comment_content'],
'comment_type' => '',
'comment_parent' => 0,
'user_ID' => $user->ID,
'user_id' => $user->ID,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => '',
),
$this->preprocess_comment_data
);
}
public function filter_preprocess_comment( $commentdata ) {
$this->preprocess_comment_data = $commentdata;
return $commentdata;
}
/**
* @ticket 32566
*