WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/24/2020 10:44:38 PM (12 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.