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

Add: Feature to modify the comment form title #57541

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ Display a post's comments form. ([Source](https://github.com/WordPress/gutenberg
- **Name:** core/post-comments-form
- **Category:** theme
- **Supports:** color (background, gradients, heading, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign
- **Attributes:** commentFormTitle, textAlign

## Comments Link

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"attributes": {
"textAlign": {
"type": "string"
},
"commentFormTitle": {
"type": "string"
}
},
"usesContext": [ "postId", "postType" ],
Expand Down
15 changes: 13 additions & 2 deletions packages/block-library/src/post-comments-form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ export default function PostCommentsFormEdit( {
context,
setAttributes,
} ) {
const { textAlign } = attributes;
const { textAlign, commentFormTitle } = attributes;
const { postId, postType } = context;

const instanceId = useInstanceId( PostCommentsFormEdit );
const instanceIdDesc = sprintf( 'comments-form-edit-%d-desc', instanceId );

const commentFormTitleActions = {
title: commentFormTitle,
setTitle: ( nextTitle ) => {
setAttributes( { commentFormTitle: nextTitle } );
},
};

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
Expand All @@ -49,7 +56,11 @@ export default function PostCommentsFormEdit( {
/>
</BlockControls>
<div { ...blockProps }>
<CommentsForm postId={ postId } postType={ postType } />
<CommentsForm
postId={ postId }
postType={ postType }
commentFormTitleActions={ commentFormTitleActions }
/>
<VisuallyHidden id={ instanceIdDesc }>
{ __( 'Comments form disabled in editor.' ) }
</VisuallyHidden>
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/post-comments-form/editor.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
.wp-block-post-comments-form * {
pointer-events: none;

.comment-reply-title {
pointer-events: visible;
cursor: text;

&.disabled {
pointer-events: none;
cursor: default;
}
}

&.block-editor-warning * {
pointer-events: auto;
}
Expand Down
47 changes: 43 additions & 4 deletions packages/block-library/src/post-comments-form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,53 @@ import {
Warning,
store as blockEditorStore,
__experimentalGetElementClassName,
RichText,
} from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

const CommentsFormPlaceholder = () => {
const CommentsFormPlaceholder = ( { commentFormTitleActions } ) => {
const instanceId = useInstanceId( CommentsFormPlaceholder );

let isCommentFormTitleActionsEmpty = false;

if ( ! commentFormTitleActions ) {
isCommentFormTitleActionsEmpty = true;

commentFormTitleActions = {
title: __( 'Leave a reply' ),
setTitle: null,
};
} else if (
null === commentFormTitleActions.title ||
undefined === commentFormTitleActions.title
) {
commentFormTitleActions.title = '';
} else if (
null === commentFormTitleActions.setTitle ||
undefined === commentFormTitleActions.setTitle
) {
commentFormTitleActions.setTitle = null;
}

return (
<div className="comment-respond">
<h3 className="comment-reply-title">{ __( 'Leave a Reply' ) }</h3>
<RichText
tagName="h3"
className={
'comment-reply-title' +
( isCommentFormTitleActionsEmpty ? ' disabled' : '' )
}
placeholder={ __( 'Leave a reply' ) }
value={ commentFormTitleActions.title }
onChange={ ( text ) => {
if ( commentFormTitleActions.setTitle !== null ) {
commentFormTitleActions.setTitle( text );
}
} }
/>
<form
noValidate
className="comment-form"
Expand Down Expand Up @@ -58,7 +93,7 @@ const CommentsFormPlaceholder = () => {
);
};

const CommentsForm = ( { postId, postType } ) => {
const CommentsForm = ( { postId, postType, commentFormTitleActions } ) => {
const [ commentStatus, setCommentStatus ] = useEntityProp(
'postType',
postType,
Expand Down Expand Up @@ -124,7 +159,11 @@ const CommentsForm = ( { postId, postType } ) => {
}
}

return <CommentsFormPlaceholder />;
return (
<CommentsFormPlaceholder
commentFormTitleActions={ commentFormTitleActions }
/>
);
};

export default CommentsForm;
6 changes: 5 additions & 1 deletion packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function render_block_core_post_comments_form( $attributes, $content, $block ) {
add_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );

ob_start();
comment_form( array(), $block->context['postId'] );
$args = array();
if ( ! empty( $attributes['commentFormTitle'] ) ) {
$args['title_reply'] = $attributes['commentFormTitle'];
}
comment_form( $args, $block->context['postId'] );
$form = ob_get_clean();

remove_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
Expand Down
Loading