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

Try/inline block commenting #44

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
64 changes: 36 additions & 28 deletions lib/compat/wordpress-6.7/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,44 +115,52 @@ function gutenberg_override_default_rest_server() {
}
add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 );


/**
* Adds user avatar URLs to the REST API response for WordPress version 6.7 and below.
*
* This function is hooked to the `rest_prepare_comment` filter and is responsible for updating the comment metadata based on the data provided in the REST API request.
*
* It performs the following tasks:
* - Updates the `block_comment` metadata for the comment based on the `meta` field in the request.
* - Updates the `comment_type` and `comment_approved` fields for the comment based on the corresponding fields in the request.
* - Retrieves the author's avatar URLs and adds them to the response data.
* - Retrieves the `block_comment` metadata for the comment and adds it to the response data.
* This function is used to add avatar URLs for the author of a post in the REST API response.
* It checks if the function 'add_user_avatar_urls_in_rest_response_6_7' does not already exist,
* and if not, defines the function.
*
* @param WP_REST_Response $response The response object.
* @param WP_Comment $comment The comment object.
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response The updated response object.
* @param object $response The REST API response object.
* @return object The modified REST API response object with added avatar URLs.
*/
if ( ! function_exists( 'update_comment_meta_from_rest_request_6_7' ) ) {
function update_comment_meta_from_rest_request_6_7( $response, $comment, $request ) {

if ( ! empty( $request['comment_type'] ) ) {
$comment_data = array(
'comment_ID' => $comment->comment_ID,
'comment_type' => $request['comment_type'],
'comment_approved' => isset( $request['comment_approved'] ) ? $request['comment_approved'] : 0,
);

wp_update_comment( $comment_data );
}
if ( ! function_exists( 'add_user_avatar_urls_in_rest_response_6_7' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function add_user_avatar_urls_in_rest_response_6_7( $response ) {

if ( $response->data['author'] ) {
$avatar_url = get_avatar_url( $response->data['author'] );
$response->data['author_avatar_urls'] = array(
'default' => $avatar_url,
'48' => add_query_arg( 's', 48, $avatar_url ),
'96' => add_query_arg( 's', 96, $avatar_url ),
'default' => get_avatar_url( $response->data['author'] ),
'48' => add_query_arg( 's', 48, get_avatar_url( $response->data['author'], array( 'size' => 48 ) ) ),
'96' => add_query_arg( 's', 96, get_avatar_url( $response->data['author'], array( 'size' => 96 ) ) ),
);
}

return $response;
}
}
add_filter( 'rest_prepare_comment', 'update_comment_meta_from_rest_request_6_7', 10, 3 );
add_filter( 'rest_prepare_comment', 'add_user_avatar_urls_in_rest_response_6_7', 10, 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 );
2 changes: 1 addition & 1 deletion lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function gutenberg_initialize_experiments_settings() {

add_settings_field(
'gutenberg-block-comment',
__( 'Comments', 'gutenberg' ),
__( 'Block Comments', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function CollabSidebar() {
post: postId,
content: sanitisedComment,
comment_type: 'block_comment',
comment_approved: 0,
};

if ( parentCommentId ) {
Expand Down
Loading