WordPress.org

Make WordPress Core

Changeset 38864


Ignore:
Timestamp:
10/21/2016 06:27:18 PM (4 years ago)
Author:
rachelbaker
Message:

REST API: Allow comments to be created setting the user_agent parameter.

As of WordPress 4.3 the wp_new_comment() function has been updated to allow the comment_agent value to be set when a comment is created. The comments API endpoint now allows the comment author's user agent to be set when creating a comment.
Also, the readonly property on the author_user_agent parameter in the schema was removed.

Props rabmalin for the initial patch.
Fixes #38425.

Location:
trunk
Files:
2 edited

Legend:

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

    r38832 r38864  
    420420        }
    421421
    422         $prepared_comment['comment_agent'] = '';
     422        if ( ! isset( $prepared_comment['comment_agent'] ) ) {
     423            $prepared_comment['comment_agent'] = '';
     424        }
     425
    423426        $prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true );
    424427
     
    889892        }
    890893
     894        if ( isset( $request['author_user_agent'] ) ) {
     895            $prepared_comment['comment_agent'] = $request['author_user_agent'];
     896        }
     897
    891898        if ( isset( $request['type'] ) ) {
    892899            // Comment type "comment" needs to be created as an empty string.
     
    976983                    'type'         => 'string',
    977984                    'context'      => array( 'edit' ),
    978                     'readonly'     => true,
     985                    'arg_options'  => array(
     986                        'sanitize_callback' => 'sanitize_text_field',
     987                    ),
    979988                ),
    980989                'content'          => array(
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r38832 r38864  
    10651065    }
    10661066
    1067     public function test_create_comment_with_status_and_IP() {
     1067    public function test_create_comment_with_status_IP_and_user_agent() {
    10681068        $post_id = $this->factory->post->create();
    10691069        wp_set_current_user( $this->admin_id );
     
    10751075            'author_ip'    => '139.130.4.5',
    10761076            'author_url'   => 'http://androidsdungeon.com',
     1077            'author_user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
    10771078            'content'      => 'Worst Comment Ever!',
    10781079            'status'       => 'approved',
     
    10891090        $this->assertEquals( 'approved', $data['status'] );
    10901091        $this->assertEquals( '139.130.4.5', $data['author_ip'] );
     1092        $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'] );
    10911093    }
    10921094
Note: See TracChangeset for help on using the changeset viewer.