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

Post Comments Form: Add heading level control #43687

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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 @@ -501,7 +501,7 @@ Display a post's comments form. ([Source](https://github.com/WordPress/gutenberg
- **Name:** core/post-comments-form
- **Category:** theme
- **Supports:** color (background, gradients, link, text), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign
- **Attributes:** headingLevel, textAlign

## Post Comments Link

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"description": "Display a post's comments form.",
"textdomain": "default",
"attributes": {
"headingLevel": {
"type": "number",
"default": 3
},
"textAlign": {
"type": "string"
}
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 @@ -16,13 +16,14 @@ import {
* Internal dependencies
*/
import CommentsForm from './form';
import HeadingLevelDropdown from '../heading/heading-level-dropdown';

export default function PostCommentsFormEdit( {
attributes,
context,
setAttributes,
} ) {
const { textAlign } = attributes;
const { textAlign, headingLevel } = attributes;
const { postId, postType } = context;

const blockProps = useBlockProps( {
Expand All @@ -40,9 +41,19 @@ export default function PostCommentsFormEdit( {
setAttributes( { textAlign: nextAlign } );
} }
/>
<HeadingLevelDropdown
selectedLevel={ headingLevel }
onChange={ ( newHeadingLevel ) =>
setAttributes( { headingLevel: newHeadingLevel } )
}
/>
</BlockControls>
<div { ...blockProps }>
<CommentsForm postId={ postId } postType={ postType } />
<CommentsForm
headingLevel={ headingLevel }
postId={ postId }
postType={ postType }
/>
</div>
</>
);
Expand Down
12 changes: 8 additions & 4 deletions packages/block-library/src/post-comments-form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import { useDisabled, useInstanceId } from '@wordpress/compose';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

const CommentsFormPlaceholder = () => {
const CommentsFormPlaceholder = ( { headingLevel } ) => {
const HeadingTagName = 'h' + headingLevel;

const disabledFormRef = useDisabled();
const instanceId = useInstanceId( CommentsFormPlaceholder );

return (
<div className="comment-respond">
<h3 className="comment-reply-title">{ __( 'Leave a Reply' ) }</h3>
<HeadingTagName className="comment-reply-title">
{ __( 'Leave a Reply' ) }
</HeadingTagName>
<form noValidate className="comment-form" ref={ disabledFormRef }>
<p>
<label htmlFor={ `comment-${ instanceId }` }>
Expand Down Expand Up @@ -53,7 +57,7 @@ const CommentsFormPlaceholder = () => {
);
};

const CommentsForm = ( { postId, postType } ) => {
const CommentsForm = ( { headingLevel, postId, postType } ) => {
const [ commentStatus, setCommentStatus ] = useEntityProp(
'postType',
postType,
Expand Down Expand Up @@ -119,7 +123,7 @@ const CommentsForm = ( { postId, postType } ) => {
}
}

return <CommentsFormPlaceholder />;
return <CommentsFormPlaceholder headingLevel={ headingLevel } />;
};

export default CommentsForm;
12 changes: 11 additions & 1 deletion packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ function render_block_core_post_comments_form( $attributes, $content, $block ) {
$classes .= ' has-text-align-' . $attributes['textAlign'];
}

$heading = 'h3';
if ( isset( $attributes['headingLevel'] ) ) {
$heading = 'h' . $attributes['headingLevel'];
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

add_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );

ob_start();
comment_form( array(), $block->context['postId'] );
comment_form(
array(
'title_reply_before' => '<' . $heading . ' id="reply-title" class="comment-reply-title">',
'title_reply_after' => "</$heading>",
)
);
$form = ob_get_clean();

remove_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' );
Expand Down
4 changes: 3 additions & 1 deletion test/integration/fixtures/blocks/core__comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
{
"name": "core/post-comments-form",
"isValid": true,
"attributes": {},
"attributes": {
"headingLevel": 3
},
"innerBlocks": []
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
{
"name": "core/post-comments-form",
"isValid": true,
"attributes": {},
"attributes": {
"headingLevel": 3
},
"innerBlocks": []
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
"name": "core/post-comments-form",
"isValid": true,
"attributes": {},
"attributes": {
"headingLevel": 3
},
"innerBlocks": []
}
]