diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index 9b9c4f3205..ae6bdd0cb6 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -3876,13 +3876,9 @@ class wp_xmlrpc_server extends IXR_Server { return new IXR_Error( 403, __( 'Sorry, comments are closed for this item.' ) ); } - if ( empty( $content_struct['content'] ) ) { - return new IXR_Error( 403, __( 'Comment is required.' ) ); - } - $comment = array( 'comment_post_ID' => $post_id, - 'comment_content' => $content_struct['content'], + 'comment_content' => trim( $content_struct['content'] ), ); if ( $logged_in ) { @@ -3923,6 +3919,13 @@ class wp_xmlrpc_server extends IXR_Server { $comment['comment_parent'] = isset( $content_struct['comment_parent'] ) ? absint( $content_struct['comment_parent'] ) : 0; + /** This filter is documented in wp-includes/comment.php */ + $allow_empty = apply_filters( 'allow_empty_comment', false, $comment ); + + if ( ! $allow_empty && '' === $comment['comment_content'] ) { + return new IXR_Error( 403, __( 'Comment is required.' ) ); + } + /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ do_action( 'xmlrpc_call', 'wp.newComment' ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 85c8f22e8b..8d85fa076f 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -587,11 +587,11 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $prepared_comment['comment_type'] = 'comment'; - /* - * Do not allow a comment to be created with missing or empty - * comment_content. See wp_handle_comment_submission(). - */ - if ( empty( $prepared_comment['comment_content'] ) ) { + if ( ! isset( $prepared_comment['comment_content'] ) ) { + $prepared_comment['comment_content'] = ''; + } + + if ( ! $this->check_is_comment_content_allowed( $prepared_comment ) ) { return new WP_Error( 'rest_comment_content_invalid', __( 'Invalid comment content.' ), @@ -1280,9 +1280,9 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { * the 'content.raw' properties of the Request object. */ if ( isset( $request['content'] ) && is_string( $request['content'] ) ) { - $prepared_comment['comment_content'] = $request['content']; + $prepared_comment['comment_content'] = trim( $request['content'] ); } elseif ( isset( $request['content']['raw'] ) && is_string( $request['content']['raw'] ) ) { - $prepared_comment['comment_content'] = $request['content']['raw']; + $prepared_comment['comment_content'] = trim( $request['content']['raw'] ); } if ( isset( $request['post'] ) ) { @@ -1866,4 +1866,39 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { return $email; } + + /** + * If empty comments are not allowed, checks if the provided comment content is not empty. + * + * @since 5.6.0 + * + * @param array $prepared_comment The prepared comment data. + * @return bool True if the content is allowed, false otherwise. + */ + protected function check_is_comment_content_allowed( $prepared_comment ) { + $check = wp_parse_args( + $prepared_comment, + array( + 'comment_post_ID' => 0, + 'comment_parent' => 0, + 'user_ID' => 0, + 'comment_author' => null, + 'comment_author_email' => null, + 'comment_author_url' => null, + ) + ); + + /** This filter is documented in wp-includes/comment.php */ + $allow_empty = apply_filters( 'allow_empty_comment', false, $check ); + + if ( $allow_empty ) { + return true; + } + + /* + * Do not allow a comment to be created with missing or empty + * comment_content. See wp_handle_comment_submission(). + */ + return '' !== $check['comment_content']; + } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 3c169efea4..89aad36884 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-beta1-49302'; +$wp_version = '5.6-beta1-49303'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.