Make WordPress Core

Changeset 39287


Ignore:
Timestamp:
11/18/2016 04:21:27 PM (9 years ago)
Author:
rachelbaker
Message:

REST API: On comment create, fallback to the user_agent header value.

If a user-agent is not explicitly provided in the author_user_agent parameter, fallback to the user_agent value in the request header.

Props dd32, jnylen0, rachelbaker.
Fixes #38817.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39278 r39287  
    10421042        }
    10431043
    1044         if ( isset( $request['author_user_agent'] ) ) {
     1044        if ( ! empty( $request['author_user_agent'] ) ) {
    10451045            $prepared_comment['comment_agent'] = $request['author_user_agent'];
     1046        } elseif ( $request->get_header( 'user_agent' ) ) {
     1047            $prepared_comment['comment_agent'] = $request->get_header( 'user_agent' );
    10461048        }
    10471049
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39196 r39287  
    12451245        $this->assertEquals( '139.130.4.5', $data['author_ip'] );
    12461246        $this->assertEquals( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent'] );
     1247    }
     1248
     1249    public function test_create_comment_user_agent_header() {
     1250        wp_set_current_user( self::$admin_id );
     1251
     1252        $params = array(
     1253            'post'         => self::$post_id,
     1254            'author_name'  => 'Homer Jay Simpson',
     1255            'author_email' => 'chunkylover53@aol.com',
     1256            'author_url'   => 'http://compuglobalhypermeganet.com',
     1257            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
     1258        );
     1259
     1260        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1261        $request->add_header( 'content-type', 'application/json' );
     1262        $request->add_header( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)' );
     1263        $request->set_body( wp_json_encode( $params ) );
     1264
     1265        $response = $this->server->dispatch( $request );
     1266        $this->assertEquals( 201, $response->get_status() );
     1267
     1268        $data = $response->get_data();
     1269
     1270        $new_comment = get_comment( $data['id'] );
     1271        $this->assertEquals( 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)', $new_comment->comment_agent );
    12471272    }
    12481273
Note: See TracChangeset for help on using the changeset viewer.