-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
base: trunk
Are you sure you want to change the base?
Changes from 20 commits
2bf37e7
635b3ba
e0dc011
ff09cfc
764734b
eb853d5
ff26fdf
b68bcfc
a0056b8
4bc0128
a4d3033
400e32b
bcc7803
744a225
6e2128a
ba71390
0f62c79
7f6ab7e
ed7fce1
4aa22aa
21b6653
9d20007
8ba3370
35e73ff
945ccb7
8876b70
bba1771
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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']; | ||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I acknowledge it doesn't make sense to use However, if you want this to work through any interface or flow that updates the comments, you might want to use the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this related to the REST API? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to consider this prior feedback: Perhaps it can be reflected in the experiment title? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ), | ||
|
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'] ); | ||
} | ||
} |
There was a problem hiding this comment.
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 newcomment_type
that you're introducing. Unless you want to intentionally hide those comments from the interface.There was a problem hiding this comment.
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?