Make WordPress Core

Changeset 54489


Ignore:
Timestamp:
10/11/2022 04:30:40 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

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

    r54368 r54489  
    22092209    global $wpdb;
    22102210
     2211    /*
     2212     * Normalize `user_ID` to `user_id`, but pass the old key
     2213     * to the `preprocess_comment` filter for backward compatibility.
     2214     */
    22112215    if ( isset( $commentdata['user_ID'] ) ) {
    22122216        $commentdata['user_ID'] = (int) $commentdata['user_ID'];
    22132217        $commentdata['user_id'] = $commentdata['user_ID'];
     2218    } elseif ( isset( $commentdata['user_id'] ) ) {
     2219        $commentdata['user_id'] = (int) $commentdata['user_id'];
     2220        $commentdata['user_ID'] = $commentdata['user_id'];
    22142221    }
    22152222
     
    22362243    $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
    22372244
     2245    // Normalize `user_ID` to `user_id` again, after the filter.
    22382246    if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
    22392247        $commentdata['user_ID'] = (int) $commentdata['user_ID'];
     
    22412249    } elseif ( isset( $commentdata['user_id'] ) ) {
    22422250        $commentdata['user_id'] = (int) $commentdata['user_id'];
     2251        $commentdata['user_ID'] = $commentdata['user_id'];
    22432252    }
    22442253
     
    35883597    $commentdata = array(
    35893598        'comment_post_ID' => $comment_post_id,
    3590         'user_ID'         => $user_id,
    35913599    );
    35923600
     
    35973605        'comment_content',
    35983606        'comment_type',
    3599         'comment_parent'
     3607        'comment_parent',
     3608        'user_id'
    36003609    );
    36013610
  • trunk/tests/phpunit/tests/comment-submission.php

    r54368 r54489  
    846846     */
    847847    public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
    848 
    849848        $user = get_userdata( self::$author_id );
    850849        wp_set_current_user( $user->ID );
  • trunk/tests/phpunit/tests/comment.php

    r54090 r54489  
    99    protected static $notify_message = '';
    1010
     11    protected $preprocess_comment_data = array();
     12
    1113    public function set_up() {
    1214        parent::set_up();
     
    455457
    456458        $this->assertSame( strlen( $comment->comment_content ), 65535 );
     459    }
     460
     461    /**
     462     * @ticket 56244
     463     */
     464    public function test_wp_new_comment_sends_all_expected_parameters_to_preprocess_comment_filter() {
     465        $user = get_userdata( self::$user_id );
     466        wp_set_current_user( $user->ID );
     467
     468        $data = array(
     469            'comment_post_ID'      => self::$post_id,
     470            'comment_author'       => $user->display_name,
     471            'comment_author_email' => $user->user_email,
     472            'comment_author_url'   => $user->user_url,
     473            'comment_content'      => 'Comment',
     474            'comment_type'         => '',
     475            'comment_parent'       => 0,
     476            'user_id'              => $user->ID,
     477        );
     478
     479        add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
     480
     481        $comment = wp_new_comment( $data );
     482
     483        $this->assertNotWPError( $comment );
     484        $this->assertSameSetsWithIndex(
     485            array(
     486                'comment_post_ID'      => self::$post_id,
     487                'comment_author'       => $user->display_name,
     488                'comment_author_email' => $user->user_email,
     489                'comment_author_url'   => $user->user_url,
     490                'comment_content'      => $data['comment_content'],
     491                'comment_type'         => '',
     492                'comment_parent'       => 0,
     493                'user_ID'              => $user->ID,
     494                'user_id'              => $user->ID,
     495                'comment_author_IP'    => '127.0.0.1',
     496                'comment_agent'        => '',
     497            ),
     498            $this->preprocess_comment_data
     499        );
     500
     501    }
     502
     503    public function filter_preprocess_comment( $commentdata ) {
     504        $this->preprocess_comment_data = $commentdata;
     505        return $commentdata;
    457506    }
    458507
Note: See TracChangeset for help on using the changeset viewer.