Skip to content

Commit 1ed4a31

Browse files
poojabhimani12MD-sunilprajapatipoojabhimani12rishishah-multidotsingeniumed
authored andcommitted
Add Inline comment experimental flag (WordPress#60622)
Co-authored-by: MD-sunilprajapati <spmultidots@git.wordpress.org> Co-authored-by: poojabhimani12 <poojabhimani@git.wordpress.org> Co-authored-by: rishishah-multidots <rishishah@git.wordpress.org> Co-authored-by: ingeniumed <ingeniumed@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: ciampo <mciampini@git.wordpress.org> Co-authored-by: mtias <matveb@git.wordpress.org> Co-authored-by: jasmussen <joen@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: annezazu <annezazu@git.wordpress.org> Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
1 parent 39e004c commit 1ed4a31

File tree

22 files changed

+1276
-1
lines changed

22 files changed

+1276
-1
lines changed

backport-changelog/6.8/7488.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/7488
2+
3+
* https://github.com/WordPress/gutenberg/pull/60622

backport-changelog/6.8/7498.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/7498
2+
3+
* https://github.com/WordPress/gutenberg/pull/60622
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Updates the comment type in the REST API for WordPress version 6.7.
4+
*
5+
* This function is used as a filter callback for the 'rest_pre_insert_comment' hook.
6+
* It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request,
7+
* and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment.
8+
*
9+
* @param array $prepared_comment The prepared comment data.
10+
* @param WP_REST_Request $request The REST API request object.
11+
* @return array The updated prepared comment data.
12+
*/
13+
if ( ! function_exists( 'update_comment_type_in_rest_api_6_8' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
14+
function update_comment_type_in_rest_api_6_8( $prepared_comment, $request ) {
15+
if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) {
16+
$prepared_comment['comment_type'] = $request['comment_type'];
17+
$prepared_comment['comment_approved'] = $request['comment_approved'];
18+
}
19+
20+
return $prepared_comment;
21+
}
22+
add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_8', 10, 2 );
23+
}
24+
25+
/**
26+
* Updates the comment type for avatars in the WordPress REST API.
27+
*
28+
* This function adds the 'block_comment' type to the list of comment types
29+
* for which avatars should be retrieved in the WordPress REST API.
30+
*
31+
* @param array $comment_type The array of comment types.
32+
* @return array The updated array of comment types.
33+
*/
34+
if ( ! function_exists( 'update_get_avatar_comment_type' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
35+
function update_get_avatar_comment_type( $comment_type ) {
36+
$comment_type[] = 'block_comment';
37+
return $comment_type;
38+
}
39+
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
40+
}
41+
42+
/**
43+
* Excludes block comments from the admin comments query.
44+
*
45+
* This function modifies the comments query to exclude comments of type 'block_comment'
46+
* when the query is for comments in the WordPress admin.
47+
*
48+
* @param WP_Comment_Query $query The current comments query.
49+
*
50+
* @return void
51+
*/
52+
if ( ! function_exists( 'exclude_block_comments_from_admin' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
53+
function exclude_block_comments_from_admin( $query ) {
54+
// Only modify the query if it's for comments
55+
if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) {
56+
$query->set( 'type', '' );
57+
58+
add_filter(
59+
'comments_clauses',
60+
function ( $clauses ) {
61+
global $wpdb;
62+
// Exclude comments of type 'block_comment'
63+
$clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'";
64+
return $clauses;
65+
}
66+
);
67+
}
68+
}
69+
add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' );
70+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* A custom REST server for Gutenberg.
4+
*
5+
* @package gutenberg
6+
* @since 6.8.0
7+
*/
8+
9+
// Create a new class that extends WP_REST_Comments_Controller
10+
class Gutenberg_REST_Comment_Controller_6_8 extends WP_REST_Comments_Controller {
11+
12+
public function create_item_permissions_check( $request ) {
13+
if ( ! is_user_logged_in() ) {
14+
if ( get_option( 'comment_registration' ) ) {
15+
return new WP_Error(
16+
'rest_comment_login_required',
17+
__( 'Sorry, you must be logged in to comment.' ),
18+
array( 'status' => 401 )
19+
);
20+
}
21+
22+
/**
23+
* Filters whether comments can be created via the REST API without authentication.
24+
*
25+
* Enables creating comments for anonymous users.
26+
*
27+
* @since 4.7.0
28+
*
29+
* @param bool $allow_anonymous Whether to allow anonymous comments to
30+
* be created. Default `false`.
31+
* @param WP_REST_Request $request Request used to generate the
32+
* response.
33+
*/
34+
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request );
35+
36+
if ( ! $allow_anonymous ) {
37+
return new WP_Error(
38+
'rest_comment_login_required',
39+
__( 'Sorry, you must be logged in to comment.' ),
40+
array( 'status' => 401 )
41+
);
42+
}
43+
}
44+
45+
// Limit who can set comment `author`, `author_ip` or `status` to anything other than the default.
46+
if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
47+
return new WP_Error(
48+
'rest_comment_invalid_author',
49+
/* translators: %s: Request parameter. */
50+
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ),
51+
array( 'status' => rest_authorization_required_code() )
52+
);
53+
}
54+
55+
if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) {
56+
if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) {
57+
return new WP_Error(
58+
'rest_comment_invalid_author_ip',
59+
/* translators: %s: Request parameter. */
60+
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ),
61+
array( 'status' => rest_authorization_required_code() )
62+
);
63+
}
64+
}
65+
66+
if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
67+
return new WP_Error(
68+
'rest_comment_invalid_status',
69+
/* translators: %s: Request parameter. */
70+
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'status' ),
71+
array( 'status' => rest_authorization_required_code() )
72+
);
73+
}
74+
75+
if ( empty( $request['post'] ) ) {
76+
return new WP_Error(
77+
'rest_comment_invalid_post_id',
78+
__( 'Sorry, you are not allowed to create this comment without a post.' ),
79+
array( 'status' => 403 )
80+
);
81+
}
82+
83+
$post = get_post( (int) $request['post'] );
84+
85+
if ( ! $post ) {
86+
return new WP_Error(
87+
'rest_comment_invalid_post_id',
88+
__( 'Sorry, you are not allowed to create this comment without a post.' ),
89+
array( 'status' => 403 )
90+
);
91+
}
92+
93+
if ( 'draft' === $post->post_status && 'comment' === $request['comment_type'] ) {
94+
return new WP_Error(
95+
'rest_comment_draft_post',
96+
__( 'Sorry, you are not allowed to create a comment on this post.' ),
97+
array( 'status' => 403 )
98+
);
99+
}
100+
101+
if ( 'trash' === $post->post_status ) {
102+
return new WP_Error(
103+
'rest_comment_trash_post',
104+
__( 'Sorry, you are not allowed to create a comment on this post.' ),
105+
array( 'status' => 403 )
106+
);
107+
}
108+
109+
if ( ! $this->check_read_post_permission( $post, $request ) ) {
110+
return new WP_Error(
111+
'rest_cannot_read_post',
112+
__( 'Sorry, you are not allowed to read the post for this comment.' ),
113+
array( 'status' => rest_authorization_required_code() )
114+
);
115+
}
116+
117+
if ( ! comments_open( $post->ID ) && 'comment' === $request['comment_type'] ) {
118+
return new WP_Error(
119+
'rest_comment_closed',
120+
__( 'Sorry, comments are closed for this item.' ),
121+
array( 'status' => 403 )
122+
);
123+
}
124+
125+
return true;
126+
}
127+
}
128+
129+
add_action(
130+
'rest_api_init',
131+
function () {
132+
$controller = new Gutenberg_REST_Comment_Controller_6_8();
133+
$controller->register_routes();
134+
}
135+
);

lib/experimental/editor-settings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ function gutenberg_enable_experiments() {
2828
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-page-client-side-navigation' ) ) {
2929
wp_add_inline_script( 'wp-block-library', 'window.__experimentalFullPageClientSideNavigation = true', 'before' );
3030
}
31+
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-comment', $gutenberg_experiments ) ) {
32+
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockComment = true', 'before' );
33+
}
3134
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) {
3235
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' );
3336
}

lib/experiments-page.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ function gutenberg_initialize_experiments_settings() {
163163
)
164164
);
165165

166+
add_settings_field(
167+
'gutenberg-block-comment',
168+
__( 'Block Comments', 'gutenberg' ),
169+
'gutenberg_display_experiment_field',
170+
'gutenberg-experiments',
171+
'gutenberg_experiments_section',
172+
array(
173+
'label' => __( 'Enable multi-user commenting on blocks', 'gutenberg' ),
174+
'id' => 'gutenberg-block-comment',
175+
)
176+
);
177+
166178
add_settings_field(
167179
'gutenberg-media-processing',
168180
__( 'Client-side media processing', 'gutenberg' ),

lib/load.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ function gutenberg_is_experiment_enabled( $name ) {
4646
require __DIR__ . '/compat/wordpress-6.7/class-gutenberg-rest-server.php';
4747
require __DIR__ . '/compat/wordpress-6.7/rest-api.php';
4848

49+
// WordPress 6.8 compat.
50+
require __DIR__ . '/compat/wordpress-6.8/block-comments.php';
51+
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';
52+
4953
// Plugin specific code.
5054
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
5155
require_once __DIR__ . '/class-wp-rest-edit-site-export-controller-gutenberg.php';

packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { pipe, useCopyToClipboard } from '@wordpress/compose';
1919
* Internal dependencies
2020
*/
2121
import BlockActions from '../block-actions';
22+
import __unstableCommentIconFill from '../../components/collab/block-comment-icon-slot';
2223
import BlockHTMLConvertButton from './block-html-convert-button';
2324
import __unstableBlockSettingsMenuFirstItem from './block-settings-menu-first-item';
2425
import BlockSettingsMenuControls from '../block-settings-menu-controls';
@@ -294,6 +295,9 @@ export function BlockSettingsDropdown( {
294295
</MenuItem>
295296
</>
296297
) }
298+
<__unstableCommentIconFill.Slot
299+
fillProps={ { onClose } }
300+
/>
297301
</MenuGroup>
298302
{ canCopyStyles && ! isContentOnly && (
299303
<MenuGroup>

packages/block-editor/src/components/block-settings-menu/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components';
77
* Internal dependencies
88
*/
99
import BlockSettingsDropdown from './block-settings-dropdown';
10+
import __unstableCommentIconToolbarFill from '../../components/collab/block-comment-icon-toolbar-slot';
1011

1112
export function BlockSettingsMenu( { clientIds, ...props } ) {
1213
return (
1314
<ToolbarGroup>
15+
<__unstableCommentIconToolbarFill.Slot />
16+
1417
<ToolbarItem>
1518
{ ( toggleProps ) => (
1619
<BlockSettingsDropdown
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { createSlotFill } from '@wordpress/components';
5+
6+
const { Fill: __unstableCommentIconFill, Slot } = createSlotFill(
7+
'__unstableCommentIconFill'
8+
);
9+
10+
__unstableCommentIconFill.Slot = Slot;
11+
12+
export default __unstableCommentIconFill;

0 commit comments

Comments
 (0)