Make WordPress Core

Ticket #56244: 56244.2.diff

File 56244.2.diff, 4.5 KB (added by SergeyBiryukov, 4 years ago)
  • src/wp-includes/comment.php

     
    22082208function wp_new_comment( $commentdata, $wp_error = false ) {
    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
    22162223        $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
    … …  
    22352242
    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'] ) {
    2239                 $commentdata['user_ID'] = (int) $commentdata['user_ID'];
    2240                 $commentdata['user_id'] = $commentdata['user_ID'];
     2247                $commentdata['user_id'] = (int) $commentdata['user_ID'];
    22412248        } elseif ( isset( $commentdata['user_id'] ) ) {
    22422249                $commentdata['user_id'] = (int) $commentdata['user_id'];
    22432250        }
    … …  
    35873594
    35883595        $commentdata = array(
    35893596                'comment_post_ID' => $comment_post_id,
    3590                 'user_ID'         => $user_id,
    35913597        );
    35923598
    35933599        $commentdata += compact(
    … …  
    35963602                'comment_author_url',
    35973603                'comment_content',
    35983604                'comment_type',
    3599                 'comment_parent'
     3605                'comment_parent',
     3606                'user_id',
    36003607        );
    36013608
    36023609        /**
  • tests/phpunit/tests/comment-submission.php

     
    795795         * @ticket 34997
    796796         */
    797797        public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() {
    798 
    799798                $user = get_userdata( self::$author_id );
    800799                wp_set_current_user( $user->ID );
    801800
  • tests/phpunit/tests/comment.php

     
    88        protected static $post_id;
    99        protected static $notify_message = '';
    1010
     11        protected $preprocess_comment_data = array();
     12
    1113        public function set_up() {
    1214                parent::set_up();
    1315                reset_phpmailer_instance();
    … …  
    403405        }
    404406
    405407
    406         public function test_comment_field_lengths() {
     408        public function test_wp_new_comment_respects_comment_field_lengths() {
    407409                $data = array(
    408410                        'comment_post_ID'      => self::$post_id,
    409411                        'comment_author'       => 'Comment Author',
    … …  
    423425        }
    424426
    425427        /**
     428         * @ticket 56244
     429         */
     430        public function test_wp_new_comment_sends_all_expected_parameters_to_preprocess_comment_filter() {
     431                $user = get_userdata( self::$user_id );
     432                wp_set_current_user( $user->ID );
     433
     434                $data = array(
     435                        'comment_post_ID'      => self::$post_id,
     436                        'comment_author'       => $user->display_name,
     437                        'comment_author_email' => $user->user_email,
     438                        'comment_author_url'   => $user->user_url,
     439                        'comment_content'      => 'Comment',
     440                        'comment_type'         => '',
     441                        'comment_parent'       => 0,
     442                        'user_id'              => $user->ID,
     443                );
     444
     445                add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
     446
     447                $comment = wp_new_comment( $data );
     448
     449                remove_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) );
     450
     451                $this->assertNotWPError( $comment );
     452                $this->assertSameSetsWithIndex(
     453                        array(
     454                                'comment_post_ID'      => self::$post_id,
     455                                'comment_author'       => $user->display_name,
     456                                'comment_author_email' => $user->user_email,
     457                                'comment_author_url'   => $user->user_url,
     458                                'comment_content'      => $data['comment_content'],
     459                                'comment_type'         => '',
     460                                'comment_parent'       => 0,
     461                                'user_ID'              => $user->ID,
     462                                'user_id'              => $user->ID,
     463                                'comment_author_IP'    => '127.0.0.1',
     464                                'comment_agent'        => '',
     465                        ),
     466                        $this->preprocess_comment_data
     467                );
     468
     469        }
     470
     471        public function filter_preprocess_comment( $commentdata ) {
     472                $this->preprocess_comment_data = $commentdata;
     473                return $commentdata;
     474        }
     475
     476        /**
    426477         * @ticket 32566
    427478         */
    428479        public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() {