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.

Built from https://develop.svn.wordpress.org/trunk@49303


git-svn-id: http://core.svn.wordpress.org/trunk@49065 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2020-10-24 22:46:09 +00:00
parent 36c4c943ba
commit 19c72d58e7
3 changed files with 51 additions and 13 deletions

View File

@ -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' );

View File

@ -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'];
}
}

View File

@ -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.