-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Inline comment experimental flag #60622
Changes from all commits
10c9ed0
271895a
f2b7114
5e0890c
031e8d5
6b0c3b3
d2d6215
cbee86f
b36c2ab
b7b1ba4
6c0862d
77ee009
a8ea60e
ec302ff
f002607
fdc44a3
bb2c0bd
e6ac0b4
9447911
bf84cc4
57ed39a
b9668fd
6628689
1d81922
87fe4f9
139135d
20f2bd3
2df0bdb
515a592
08fd837
251ae5f
e747430
b7a5bd7
16d2186
877f090
1ce0191
1a677d8
d67d2b1
e0cb8b5
9004004
d23a0a6
cf605d5
f115c3b
f748ad9
59e96c4
18e5081
c9f4964
1ed6d54
84733c5
2d5d162
e68cdcc
8042e22
ceb188c
575361c
8c45982
a2345e8
2004c04
8ac8328
868c095
ab73559
d5df8cc
533c5d4
c0a5dcd
56e475b
9de8c69
4104869
b8b0292
6996e5f
0321430
3bf3c4c
c66fbcd
751d727
9a42d64
68d5e1c
1f5a2c2
6477102
66832a7
5c70d90
cc28c6a
951d8b4
b0ad9d5
a5e12e1
cf59158
6cfdb29
465b438
c9d50d8
fd6f327
15c85e0
c7be827
bd1a7ce
19dfab8
48ee928
8fa3f85
5f0ae80
69d1878
d6e01f3
668671b
ee6fe75
de2cd8f
8e6da19
90ba459
e2fd75a
aad19d0
6f03c48
2b057cc
59caf1c
efa41ac
4ee210d
38bea58
609cad6
8a54986
2fd78b0
f236a42
b3c879d
bc308e9
11cf1f1
22c189a
33cd398
211944c
a4abbec
5c99dcc
50e8101
ce7f7bc
b0c5960
f768b4a
92870c4
37fa922
4d6841a
71f7a39
6f73a0b
edab788
6e528a1
b129367
8d17f22
fd4c045
4d3eeb1
f06340a
6b8027a
06c8526
e1d4396
c78f981
da72b79
d7bca6e
68d203e
b296d2d
bddceeb
c5094a0
a23241a
9548565
06e840f
3883a11
334daf2
b028876
cd63f6a
9b12661
f287eff
16ebd4c
7aa5508
e55109f
f66d825
7ff9a79
d01c693
2caf2d1
6bda66d
64b1b31
31ea3b5
bdf982f
c5a53a2
c0aee07
1a251ba
452181d
898a924
11d6693
0326444
bf20345
c419ce4
667163e
25364d8
5a53245
f4dd385
83a5955
ec1a8b3
2a4a427
ffc7f51
0d36a58
ed6e2e6
9a6556f
ebc28ca
02bd950
f5be6ac
973f559
e3c49b7
ab1395c
f9f24d9
63f915f
fc43c1e
68c5272
3d14991
6b32fcf
9db107b
c92de93
97a6d6e
0136fae
13f53d8
4d0ca6e
c5b76e8
02aabd9
65e1eb4
e3bc354
c8ff644
8cd46c3
45f9553
27ab929
b9017a4
e3cb45b
c4d4b1d
345c227
7a6c085
aa2d47b
7ce91e9
4cd5835
00076ff
7bf2c78
514853c
4b59a86
1cfd88e
45f6ac9
42a4eb2
e42e647
8744c83
127228b
5781530
ca54345
2e212c1
4b8986b
b42cd97
30ddf61
e446297
1955469
22893f8
b89ce85
3f9d745
90b038b
b7c0c46
48c3351
7c00570
047e07f
a342e27
ae50d7a
2ca51c3
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7488 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/60622 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7498 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/60622 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* 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_8' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { | ||
function update_comment_type_in_rest_api_6_8( $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_8', 10, 2 ); | ||
} | ||
|
||
/** | ||
* 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' ); | ||
} | ||
|
||
/** | ||
* Excludes block comments from the admin comments query. | ||
* | ||
* This function modifies the comments query to exclude comments of type 'block_comment' | ||
* when the query is for comments in the WordPress admin. | ||
* | ||
* @param WP_Comment_Query $query The current comments query. | ||
* | ||
* @return void | ||
*/ | ||
if ( ! function_exists( 'exclude_block_comments_from_admin' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { | ||
function exclude_block_comments_from_admin( $query ) { | ||
// Only modify the query if it's for comments | ||
if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { | ||
$query->set( 'type', '' ); | ||
|
||
add_filter( | ||
'comments_clauses', | ||
function ( $clauses ) { | ||
global $wpdb; | ||
// Exclude comments of type 'block_comment' | ||
$clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; | ||
return $clauses; | ||
} | ||
); | ||
} | ||
} | ||
add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/** | ||
* A custom REST server for Gutenberg. | ||
* | ||
* @package gutenberg | ||
* @since 6.8.0 | ||
*/ | ||
|
||
// Create a new class that extends WP_REST_Comments_Controller | ||
class Gutenberg_REST_Comment_Controller_6_8 extends WP_REST_Comments_Controller { | ||
|
||
public function create_item_permissions_check( $request ) { | ||
if ( ! is_user_logged_in() ) { | ||
if ( get_option( 'comment_registration' ) ) { | ||
return new WP_Error( | ||
'rest_comment_login_required', | ||
__( 'Sorry, you must be logged in to comment.' ), | ||
array( 'status' => 401 ) | ||
); | ||
} | ||
|
||
/** | ||
* Filters whether comments can be created via the REST API without authentication. | ||
* | ||
* Enables creating comments for anonymous users. | ||
* | ||
* @since 4.7.0 | ||
* | ||
* @param bool $allow_anonymous Whether to allow anonymous comments to | ||
* be created. Default `false`. | ||
* @param WP_REST_Request $request Request used to generate the | ||
* response. | ||
*/ | ||
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request ); | ||
|
||
if ( ! $allow_anonymous ) { | ||
return new WP_Error( | ||
'rest_comment_login_required', | ||
__( 'Sorry, you must be logged in to comment.' ), | ||
array( 'status' => 401 ) | ||
); | ||
} | ||
} | ||
|
||
// Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. | ||
if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { | ||
return new WP_Error( | ||
'rest_comment_invalid_author', | ||
/* translators: %s: Request parameter. */ | ||
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
|
||
if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) { | ||
if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) { | ||
return new WP_Error( | ||
'rest_comment_invalid_author_ip', | ||
/* translators: %s: Request parameter. */ | ||
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
} | ||
|
||
if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { | ||
return new WP_Error( | ||
'rest_comment_invalid_status', | ||
/* translators: %s: Request parameter. */ | ||
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'status' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
|
||
if ( empty( $request['post'] ) ) { | ||
return new WP_Error( | ||
'rest_comment_invalid_post_id', | ||
__( 'Sorry, you are not allowed to create this comment without a post.' ), | ||
array( 'status' => 403 ) | ||
); | ||
} | ||
|
||
$post = get_post( (int) $request['post'] ); | ||
|
||
if ( ! $post ) { | ||
return new WP_Error( | ||
'rest_comment_invalid_post_id', | ||
__( 'Sorry, you are not allowed to create this comment without a post.' ), | ||
array( 'status' => 403 ) | ||
); | ||
} | ||
|
||
if ( 'draft' === $post->post_status && 'comment' === $request['comment_type'] ) { | ||
return new WP_Error( | ||
'rest_comment_draft_post', | ||
__( 'Sorry, you are not allowed to create a comment on this post.' ), | ||
array( 'status' => 403 ) | ||
); | ||
} | ||
|
||
if ( 'trash' === $post->post_status ) { | ||
return new WP_Error( | ||
'rest_comment_trash_post', | ||
__( 'Sorry, you are not allowed to create a comment on this post.' ), | ||
array( 'status' => 403 ) | ||
); | ||
} | ||
|
||
if ( ! $this->check_read_post_permission( $post, $request ) ) { | ||
return new WP_Error( | ||
'rest_cannot_read_post', | ||
__( 'Sorry, you are not allowed to read the post for this comment.' ), | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
|
||
if ( ! comments_open( $post->ID ) && 'comment' === $request['comment_type'] ) { | ||
return new WP_Error( | ||
'rest_comment_closed', | ||
__( 'Sorry, comments are closed for this item.' ), | ||
array( 'status' => 403 ) | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
add_action( | ||
'rest_api_init', | ||
function () { | ||
$controller = new Gutenberg_REST_Comment_Controller_6_8(); | ||
$controller->register_routes(); | ||
} | ||
); |
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', '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. This should say something about being internal or for collaborators. 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'm happy to defer to Matías if he has suggestions. Perhaps: "Enable collaborative commenting on blocks."? |
||
'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,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
|
||
const { Fill: __unstableCommentIconFill, Slot } = createSlotFill( | ||
'__unstableCommentIconFill' | ||
); | ||
|
||
__unstableCommentIconFill.Slot = Slot; | ||
|
||
export default __unstableCommentIconFill; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
|
||
const { Fill: __unstableCommentIconToolbarFill, Slot } = createSlotFill( | ||
'__unstableCommentIconToolbarFill' | ||
); | ||
|
||
__unstableCommentIconToolbarFill.Slot = Slot; | ||
|
||
export default __unstableCommentIconToolbarFill; |
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.
Looking at https://github.com/WordPress/wordpress-develop/blob/0d9c56edfb94eba08eeb7a2a43d5614542a097ea/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L525, it seems you simply removed the condition. Could we add a filter here instead so it's something we can ship in core? We can't just remove the condition entirely, otherwise normal comments can be posted on draft posts. There should be a new filter that we can then use to bypass it?
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.
Same for the
comments_open
check.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.
An alternative is to check if the comment type is
comment
and otherwise skip all these checks. I think that could work too. 🤔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.
I'm saying this because, currently, someone who installs the Gutenberg plugin and enables this experiment will suddenly find that you can comment through the REST API on any post that the "comments closed" setting on.
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.
Done