Make WordPress Core

Changeset 49303


Ignore:
Timestamp:
10/24/2020 10:44:38 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API, XML-RPC: Synchronise empty comment content checks.

The REST API and XML-RPC now uses the same detection methodology for empty comment content as wp_handle_comment_submission(). Specifically, comments now have their content trimmed and '0' is allowed.

Props jaswrks, rmccue, dd32, rachelbaker, Cawa-93, aduth, TimothyBlynJacobs.
Fixes #43177.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r49271 r49303  
    38773877                }
    38783878
    3879                 if ( empty( $content_struct['content'] ) ) {
    3880                         return new IXR_Error( 403, __( 'Comment is required.' ) );
    3881                 }
    3882 
    38833879                $comment = array(
    38843880                        'comment_post_ID' => $post_id,
    3885                         'comment_content' => $content_struct['content'],
     3881                        'comment_content' => trim( $content_struct['content'] ),
    38863882                );
    38873883
    … …  
    39233919
    39243920                $comment['comment_parent'] = isset( $content_struct['comment_parent'] ) ? absint( $content_struct['comment_parent'] ) : 0;
     3921
     3922                /** This filter is documented in wp-includes/comment.php */
     3923                $allow_empty = apply_filters( 'allow_empty_comment', false, $comment );
     3924
     3925                if ( ! $allow_empty && '' === $comment['comment_content'] ) {
     3926                        return new IXR_Error( 403, __( 'Comment is required.' ) );
     3927                }
    39253928
    39263929                /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r49299 r49303  
    588588                $prepared_comment['comment_type'] = 'comment';
    589589
    590                 /*
    591                  * Do not allow a comment to be created with missing or empty
    592                  * comment_content. See wp_handle_comment_submission().
    593                  */
    594                 if ( empty( $prepared_comment['comment_content'] ) ) {
     590                if ( ! isset( $prepared_comment['comment_content'] ) ) {
     591                        $prepared_comment['comment_content'] = '';
     592                }
     593
     594                if ( ! $this->check_is_comment_content_allowed( $prepared_comment ) ) {
    595595                        return new WP_Error(
    596596                                'rest_comment_content_invalid',
    … …  
    12811281                 */
    12821282                if ( isset( $request['content'] ) && is_string( $request['content'] ) ) {
    1283                         $prepared_comment['comment_content'] = $request['content'];
     1283                        $prepared_comment['comment_content'] = trim( $request['content'] );
    12841284                } elseif ( isset( $request['content']['raw'] ) && is_string( $request['content']['raw'] ) ) {
    1285                         $prepared_comment['comment_content'] = $request['content']['raw'];
     1285                        $prepared_comment['comment_content'] = trim( $request['content']['raw'] );
    12861286                }
    12871287
    … …  
    18671867                return $email;
    18681868        }
     1869
     1870        /**
     1871         * If empty comments are not allowed, checks if the provided comment content is not empty.
     1872         *
     1873         * @since 5.6.0
     1874         *
     1875         * @param array $prepared_comment The prepared comment data.
     1876         * @return bool True if the content is allowed, false otherwise.
     1877         */
     1878        protected function check_is_comment_content_allowed( $prepared_comment ) {
     1879                $check = wp_parse_args(
     1880                        $prepared_comment,
     1881                        array(
     1882                                'comment_post_ID'      => 0,
     1883                                'comment_parent'       => 0,
     1884                                'user_ID'              => 0,
     1885                                'comment_author'       => null,
     1886                                'comment_author_email' => null,
     1887                                'comment_author_url'   => null,
     1888                        )
     1889                );
     1890
     1891                /** This filter is documented in wp-includes/comment.php */
     1892                $allow_empty = apply_filters( 'allow_empty_comment', false, $check );
     1893
     1894                if ( $allow_empty ) {
     1895                        return true;
     1896                }
     1897
     1898                /*
     1899                 * Do not allow a comment to be created with missing or empty
     1900                 * comment_content. See wp_handle_comment_submission().
     1901                 */
     1902                return '' !== $check['comment_content'];
     1903        }
    18691904}
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r49299 r49303  
    13991399        }
    14001400
     1401        /**
     1402         * @ticket 43177
     1403         */
     1404        public function test_create_item_invalid_only_spaces_content() {
     1405                wp_set_current_user( self::$admin_id );
     1406
     1407                $params = array(
     1408                        'post'         => self::$post_id,
     1409                        'author_name'  => 'Reverend Lovejoy',
     1410                        'author_email' => 'lovejoy@example.com',
     1411                        'author_url'   => 'http://timothylovejoy.jr',
     1412                        'content'      => '   ',
     1413                );
     1414
     1415                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1416                $request->add_header( 'content-type', 'application/json' );
     1417                $request->set_body( wp_json_encode( $params ) );
     1418
     1419                $response = rest_get_server()->dispatch( $request );
     1420                $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
     1421        }
     1422
     1423        /**
     1424         * @ticket 43177
     1425         */
     1426        public function test_create_item_allows_0_as_content() {
     1427                wp_set_current_user( self::$admin_id );
     1428
     1429                $params = array(
     1430                        'post'         => self::$post_id,
     1431                        'author_name'  => 'Reverend Lovejoy',
     1432                        'author_email' => 'lovejoy@example.com',
     1433                        'author_url'   => 'http://timothylovejoy.jr',
     1434                        'content'      => '0',
     1435                );
     1436
     1437                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1438                $request->add_header( 'content-type', 'application/json' );
     1439                $request->set_body( wp_json_encode( $params ) );
     1440
     1441                $response = rest_get_server()->dispatch( $request );
     1442                $this->assertSame( 201, $response->get_status() );
     1443                $this->assertEquals( '0', $response->get_data()['content']['raw'] );
     1444        }
     1445
     1446        /**
     1447         * @ticket 43177
     1448         */
     1449        public function test_create_item_allow_empty_comment_filter() {
     1450                add_filter( 'allow_empty_comment', '__return_true' );
     1451
     1452                wp_set_current_user( self::$admin_id );
     1453
     1454                $params = array(
     1455                        'post'         => self::$post_id,
     1456                        'author_name'  => 'Reverend Lovejoy',
     1457                        'author_email' => 'lovejoy@example.com',
     1458                        'author_url'   => 'http://timothylovejoy.jr',
     1459                        'content'      => '',
     1460                );
     1461
     1462                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1463                $request->add_header( 'content-type', 'application/json' );
     1464                $request->set_body( wp_json_encode( $params ) );
     1465
     1466                $response = rest_get_server()->dispatch( $request );
     1467                $this->assertSame( 201, $response->get_status() );
     1468                $this->assertEquals( '', $response->get_data()['content']['raw'] );
     1469        }
     1470
    14011471        public function test_create_item_invalid_date() {
    14021472                wp_set_current_user( self::$admin_id );
  • trunk/tests/phpunit/tests/xmlrpc/wp/newComment.php

    r49271 r49303  
    5151        }
    5252
     53        /**
     54         * @ticket 43177
     55         */
     56        public function test_empty_content_multiple_spaces() {
     57                $result = $this->myxmlrpcserver->wp_newComment(
     58                        array(
     59                                1,
     60                                'administrator',
     61                                'administrator',
     62                                self::$post->ID,
     63                                array(
     64                                        'content' => '   ',
     65                                ),
     66                        )
     67                );
     68
     69                $this->assertIXRError( $result );
     70                $this->assertSame( 403, $result->code );
     71        }
     72
     73        /**
     74         * @ticket 43177
     75         */
     76        public function test_valid_comment_0_content() {
     77                $result = $this->myxmlrpcserver->wp_newComment(
     78                        array(
     79                                1,
     80                                'administrator',
     81                                'administrator',
     82                                self::$post->ID,
     83                                array(
     84                                        'content' => '0',
     85                                ),
     86                        )
     87                );
     88
     89                $this->assertNotIXRError( $result );
     90        }
     91
     92        /**
     93         * @ticket 43177
     94         */
     95        public function test_valid_comment_allow_empty_content() {
     96                add_filter( 'allow_empty_comment', '__return_true' );
     97                $result = $this->myxmlrpcserver->wp_newComment(
     98                        array(
     99                                1,
     100                                'administrator',
     101                                'administrator',
     102                                self::$post->ID,
     103                                array(
     104                                        'content' => '   ',
     105                                ),
     106                        )
     107                );
     108
     109                $this->assertNotIXRError( $result );
     110        }
     111
    53112        function test_new_comment_post_closed() {
    54113                $post = self::factory()->post->create_and_get(
Note: See TracChangeset for help on using the changeset viewer.