From ae9f0d072b81be6ae832713e42844c5b838dcd87 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 12 Dec 2023 14:26:28 +0100 Subject: [PATCH 1/9] Comments block: Try client-side form submission --- docs/reference-guides/core-blocks.md | 2 +- .../src/comment-reply-link/block.json | 2 +- .../src/comment-reply-link/index.php | 11 + .../src/comment-template/block.json | 2 +- .../src/comment-template/index.php | 11 +- .../block-library/src/comments/block.json | 9 +- .../edit/comments-inspector-controls.js | 58 +++++- packages/block-library/src/comments/index.php | 31 ++- .../src/post-comments-form/block.json | 5 +- .../src/post-comments-form/index.php | 166 ++++++++++++++- .../src/post-comments-form/style.scss | 21 ++ .../src/post-comments-form/view.js | 189 ++++++++++++++++++ .../fixtures/blocks/core__comments.json | 1 + .../fixtures/blocks/core__post-comments.json | 3 +- 14 files changed, 495 insertions(+), 16 deletions(-) create mode 100644 packages/block-library/src/post-comments-form/view.js diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index c68bb419467f3..35073a9bf487e 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -181,7 +181,7 @@ An advanced block that allows displaying post comments using different visual co - **Name:** core/comments - **Category:** theme - **Supports:** align (full, wide), color (background, gradients, heading, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** legacy, tagName +- **Attributes:** enhancedSubmission, legacy, tagName ## Comments Pagination diff --git a/packages/block-library/src/comment-reply-link/block.json b/packages/block-library/src/comment-reply-link/block.json index c10129412145c..e7eaea52f9ca3 100644 --- a/packages/block-library/src/comment-reply-link/block.json +++ b/packages/block-library/src/comment-reply-link/block.json @@ -7,7 +7,7 @@ "ancestor": [ "core/comment-template" ], "description": "Displays a link to reply to a comment.", "textdomain": "default", - "usesContext": [ "commentId" ], + "usesContext": [ "commentId", "enhancedSubmission" ], "attributes": { "textAlign": { "type": "string" diff --git a/packages/block-library/src/comment-reply-link/index.php b/packages/block-library/src/comment-reply-link/index.php index 3f7b6e48b617c..468ef1900c44c 100644 --- a/packages/block-library/src/comment-reply-link/index.php +++ b/packages/block-library/src/comment-reply-link/index.php @@ -61,6 +61,17 @@ function render_block_core_comment_reply_link( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); + $p = new WP_HTML_Tag_Processor( $comment_reply_link ); + if ( $p->next_tag( + array( + 'tag_name' => 'A', + 'class_name' => 'comment-reply-link', + ) + ) ) { + $p->set_attribute( 'data-wp-on--click', 'actions.core.comments.changeReplyTo' ); + } + $comment_reply_link = $p->get_updated_html(); + return sprintf( '
%2$s
', $wrapper_attributes, diff --git a/packages/block-library/src/comment-template/block.json b/packages/block-library/src/comment-template/block.json index 7b9bfc5e0340f..dcde272998da6 100644 --- a/packages/block-library/src/comment-template/block.json +++ b/packages/block-library/src/comment-template/block.json @@ -7,7 +7,7 @@ "parent": [ "core/comments" ], "description": "Contains the block elements used to display a comment, like the title, date, author, avatar and more.", "textdomain": "default", - "usesContext": [ "postId" ], + "usesContext": [ "postId", "enhancedSubmission" ], "supports": { "align": true, "html": false, diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index 915c5880a8f96..43c4d02b8caaf 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -20,6 +20,7 @@ function block_core_comment_template_render_comments( $comments, $block ) { global $comment_depth; $thread_comments = get_option( 'thread_comments' ); $thread_comments_depth = get_option( 'thread_comments_depth' ); + $enhanced_submission = isset( $block->context['enhancedSubmission'] ) && $block->context['enhancedSubmission']; if ( empty( $comment_depth ) ) { $comment_depth = 1; @@ -83,7 +84,15 @@ function block_core_comment_template_render_comments( $comments, $block ) { } } - $content .= sprintf( '
  • %3$s
  • ', $comment->comment_ID, $comment_classes, $block_content ); + $comment_directives = $enhanced_submission ? ' data-wp-key="comment-' . $comment->comment_ID . '" data-wp-slot=\'{"name":"comment-' . $comment->comment_ID . '","position":"after"}\'' : ''; + + $content .= sprintf( + '
  • %4$s
  • ', + $comment->comment_ID, + $comment_classes, + $comment_directives, + $block_content + ); } return $content; diff --git a/packages/block-library/src/comments/block.json b/packages/block-library/src/comments/block.json index b35ea3505c816..222b891d6bf9f 100644 --- a/packages/block-library/src/comments/block.json +++ b/packages/block-library/src/comments/block.json @@ -14,6 +14,10 @@ "legacy": { "type": "boolean", "default": false + }, + "enhancedSubmission": { + "type": "boolean", + "default": false } }, "supports": { @@ -48,5 +52,8 @@ } }, "editorStyle": "wp-block-comments-editor", - "usesContext": [ "postId", "postType" ] + "usesContext": [ "postId", "postType" ], + "providesContext": { + "enhancedSubmission": "enhancedSubmission" + } } diff --git a/packages/block-library/src/comments/edit/comments-inspector-controls.js b/packages/block-library/src/comments/edit/comments-inspector-controls.js index 1a33cb68ea38a..d50a3cca3ccca 100644 --- a/packages/block-library/src/comments/edit/comments-inspector-controls.js +++ b/packages/block-library/src/comments/edit/comments-inspector-controls.js @@ -1,12 +1,19 @@ /** * WordPress dependencies */ -import { SelectControl } from '@wordpress/components'; +import { + SelectControl, + PanelBody, + ToggleControl, + Notice, +} from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; +import { useEffect, useRef } from '@wordpress/element'; +import { speak } from '@wordpress/a11y'; export default function CommentsInspectorControls( { - attributes: { tagName }, + attributes: { tagName, enhancedSubmission }, setAttributes, } ) { const htmlElementMessages = { @@ -17,8 +24,21 @@ export default function CommentsInspectorControls( { "The