Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/rest api for block commenting #65181

Open
wants to merge 27 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2bf37e7
Add rest api filter for block comments
MD-sunilprajapati Sep 9, 2024
635b3ba
Merge branch 'poojabhimani12:add/rest-api-for-block-commenting' into …
MD-sunilprajapati Sep 10, 2024
e0dc011
Add experimental setting option to enable block comments
MD-sunilprajapati Sep 10, 2024
ff09cfc
Merge pull request #48 from MD-sunilprajapati/add/rest-api-for-block-…
poojabhimani12 Sep 10, 2024
764734b
Merge branch 'WordPress:trunk' into add/rest-api-for-block-commenting
poojabhimani12 Sep 10, 2024
eb853d5
update get avatar url rest-api filter
rishishah-multidots Sep 10, 2024
ff26fdf
Merge pull request #53 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 10, 2024
b68bcfc
update comment type dropdown filter to support block_comment type
rishishah-multidots Sep 10, 2024
a0056b8
update comment type dropdown filter to support block_comment type
rishishah-multidots Sep 10, 2024
4bc0128
update function document
rishishah-multidots Sep 10, 2024
a4d3033
Merge pull request #54 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 10, 2024
400e32b
added unit test cases for filter functions
rishishah-multidots Sep 11, 2024
bcc7803
Merge pull request #55 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 11, 2024
744a225
remove spacing from unit testing file
rishishah-multidots Sep 11, 2024
6e2128a
Merge pull request #56 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 11, 2024
ba71390
remove spacing from unit testing file
rishishah-multidots Sep 11, 2024
0f62c79
Merge pull request #57 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 11, 2024
7f6ab7e
resolve phpcs error in unit test file
rishishah-multidots Sep 11, 2024
ed7fce1
resolve phpcs error in unit test file
rishishah-multidots Sep 11, 2024
4aa22aa
Merge pull request #58 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 11, 2024
21b6653
remove experimental flag from this PR
rishishah-multidots Sep 24, 2024
9d20007
Merge branch 'add/rest-api-for-block-commenting' of github.com:rishis…
rishishah-multidots Sep 24, 2024
8ba3370
Merge pull request #62 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Sep 24, 2024
35e73ff
remove filters from rest API PR
rishishah-multidots Oct 3, 2024
945ccb7
Merge pull request #63 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Oct 3, 2024
8876b70
resolve conflict
rishishah-multidots Oct 3, 2024
bba1771
Merge pull request #66 from rishishah-multidots/add/rest-api-for-bloc…
poojabhimani12 Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions lib/compat/wordpress-6.7/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,62 @@ function gutenberg_override_default_rest_server() {
return 'Gutenberg_REST_Server';
}
add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 );

/**
* Updates the comment type in the REST API for WordPress version 6.7.
*
* This function is used as a filter callback for the 'rest_pre_insert_comment' hook.
* It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request,
* and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment.
*
* @param array $prepared_comment The prepared comment data.
* @param WP_REST_Request $request The REST API request object.
* @return array The updated prepared comment data.
*/
if ( ! function_exists( 'update_comment_type_in_rest_api_6_7' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function update_comment_type_in_rest_api_6_7( $prepared_comment, $request ) {
if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) {
$prepared_comment['comment_type'] = $request['comment_type'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to extend the admin_comment_types_dropdown filter to allow users to filter in the admin interface by the new comment_type that you're introducing. Unless you want to intentionally hide those comments from the interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always allow there properties to be set and avoid hardcoding the comment type?

$prepared_comment['comment_approved'] = $request['comment_approved'];
}

return $prepared_comment;
}
add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_7', 10, 2 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reconsidering the previous approach of updating the comment during a REST request 🙌

Using rest_pre_insert_comment works OK if you always want to do it only through the REST API.

I acknowledge it doesn't make sense to use rest_preprocess_comment because the comment_type and comment_approved fields will be overridden automatically after that.

However, if you want this to work through any interface or flow that updates the comments, you might want to use the wp_update_comment_data filter instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a comment is submitted, whether it's a custom comment type or the default, comments from registered users will automatically be marked as "approved." For comments that need moderation, set their status to "on hold" initially. After review, you can update the status from "on hold" to "approved" as needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying that part @poojabhimani12.

I don't understand why we're doing it only when inserting a comment via the REST API. Wouldn't it be a better idea to do it consistently when a comment is inserted/updated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Comment REST API currently only allows inserting default comment types. If you try to use a custom comment type, it returns an incorrect comment type error.

To resolve this, a filter must be applied to include custom comment types in the REST API. However, no filter is needed when using the comment insert function, as it already supports the insertion of custom comment types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense; thanks a lot for elaborating 🙌

}

/**
* Updates the comment type for avatars in the WordPress REST API.
*
* This function adds the 'block_comment' type to the list of comment types
* for which avatars should be retrieved in the WordPress REST API.
*
* @param array $comment_type The array of comment types.
* @return array The updated array of comment types.
*/
if ( ! function_exists( 'update_get_avatar_comment_type' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function update_get_avatar_comment_type( $comment_type ) {
$comment_type[] = 'block_comment';
return $comment_type;
}
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type', 10, 1 );
}

/**
* Updates the comment type filter dropdown options.
*
* This function is only defined if the 'gutenberg-block-comment' experiment is enabled and the 'update_comment_type_filter_dropdown' function does not already exist.
* It returns an array of comment type options for the comment type filter dropdown in the admin area.
*
* @return array An associative array of comment type options.
* The keys are the comment type slugs and the values are the translated names of the comment types.
*/
if ( ! function_exists( 'update_comment_type_filter_dropdown' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function update_comment_type_filter_dropdown() {
return array(
'comment' => __( 'Comments' ),
'block_comment' => __( 'Block Comments' ),
);
}
add_filter( 'admin_comment_types_dropdown', 'update_comment_type_filter_dropdown' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to add this comment type to the comment management list? Comments are managed in the editor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have incorporated the comment type into the backend comment page filter based on the feedback received: #65181 (comment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to the REST API?

}
3 changes: 3 additions & 0 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function gutenberg_enable_experiments() {
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-page-client-side-navigation' ) ) {
wp_add_inline_script( 'wp-block-library', 'window.__experimentalFullPageClientSideNavigation = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-comment', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockComment = true', 'before' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this flag in this PR?

}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' );
}
Expand Down
12 changes: 12 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ function gutenberg_initialize_experiments_settings() {
)
);

add_settings_field(
'gutenberg-block-comment',
__( 'Block Comments', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable multi-user commenting on blocks in post editor', 'gutenberg' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider this prior feedback:

#60622 (comment)

Perhaps it can be reflected in the experiment title?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’ve made this update based on the feedback provided here: #60622 (comment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be adding this setting here, and hardcode the comment type. We should generalise it to work either with all comment types, or add an (experimental) option/setting to the comment type registration and use that setting (instead of hardcoding anything).

'id' => 'gutenberg-block-comment',
)
);

add_settings_field(
'gutenberg-media-processing',
__( 'Client-side media processing', 'gutenberg' ),
Expand Down
1 change: 1 addition & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function fail_if_died( $message ) {
'gutenberg-form-blocks' => 1,
'gutenberg-block-experiments' => 1,
'gutenberg-media-processing' => 1,
'gutenberg-block-comment' => 1,
),
);

Expand Down
61 changes: 61 additions & 0 deletions phpunit/class-block-comment-filter-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* This class contains unit tests for the block comment filter functions.
*
* @package Gutenberg
*/
class Tests_blockCommentFilter extends WP_UnitTestCase {

/**
* Tests that `update_comment_type_in_rest_api_6_7` updates comment type correctly.
*/
public function test_update_comment_type_in_rest_api_6_7() {
// Mock request and prepared comment
$request = new WP_REST_Request( WP_REST_Server::READABLE );
$request->set_param( 'comment_type', 'block_comment' );
$request->set_param( 'comment_approved', '1' );

$prepared_comment = array(
'comment_type' => '',
'comment_approved' => '0',
);

// Call the function
$updated_comment = update_comment_type_in_rest_api_6_7( $prepared_comment, $request );

// Assertions
$this->assertEquals( 'block_comment', $updated_comment['comment_type'] );
$this->assertEquals( '1', $updated_comment['comment_approved'] );
}

/**
* Tests that `update_comment_type_filter_dropdown` returns the correct options.
*/
public function test_update_get_avatar_comment_type() {

// Mock comment types
$comment_types = array( 'comment', 'pingback' );

// Call the function
$updated_comment_types = update_get_avatar_comment_type( $comment_types );

// Assertions
$this->assertContains( 'block_comment', $updated_comment_types );
}

/**
* Tests that `update_comment_type_filter_dropdown` returns the correct options.
*/
public function test_update_comment_type_filter_dropdown() {

// Call the function
$dropdown_options = update_comment_type_filter_dropdown();

// Assertions
$this->assertArrayHasKey( 'comment', $dropdown_options );
$this->assertArrayHasKey( 'block_comment', $dropdown_options );
$this->assertEquals( 'Comments', $dropdown_options['comment'] );
$this->assertEquals( 'Block Comments', $dropdown_options['block_comment'] );
}
}
Loading