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

Comments Title: Make non-editable #40817

Merged
merged 9 commits into from
May 6, 2022
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Displays a title with the number of comments ([Source](https://github.com/WordPr
- **Name:** core/comments-title
- **Category:** theme
- **Supports:** align, color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~anchor~~, ~~html~~
- **Attributes:** level, multipleCommentsLabel, showCommentsCount, showPostTitle, singleCommentLabel, textAlign
- **Attributes:** level, showCommentsCount, showPostTitle, textAlign

## Cover

Expand Down
6 changes: 0 additions & 6 deletions packages/block-library/src/comments-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
"textAlign": {
"type": "string"
},
"singleCommentLabel": {
"type": "string"
},
"multipleCommentsLabel": {
"type": "string"
},
"showPostTitle": {
"type": "boolean",
"default": true
Expand Down
34 changes: 34 additions & 0 deletions packages/block-library/src/comments-title/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Internal dependencies
*/
import metadata from './block.json';

const { attributes, supports } = metadata;

export default [
{
attributes: {
...attributes,
singleCommentLabel: {
type: 'string',
},
multipleCommentsLabel: {
type: 'string',
},
},
supports,
migrate: ( oldAttributes ) => {
/* eslint-disable no-unused-vars */
const {
singleCommentLabel,
multipleCommentsLabel,
...newAttributes
} = oldAttributes;
/* eslint-enable no-unused-vars */
return newAttributes;
},
isEligible: ( { multipleCommentsLabel, singleCommentLabel } ) =>
multipleCommentsLabel || singleCommentLabel,
save: () => null,
},
];
144 changes: 44 additions & 100 deletions packages/block-library/src/comments-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ import {
AlignmentControl,
BlockControls,
useBlockProps,
PlainText,
InspectorControls,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import { useEntityProp } from '@wordpress/core-data';
import {
PanelBody,
ToggleControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
Expand All @@ -31,20 +25,12 @@ import { addQueryArgs } from '@wordpress/url';
import HeadingLevelDropdown from '../heading/heading-level-dropdown';

export default function Edit( {
attributes: {
textAlign,
singleCommentLabel,
multipleCommentsLabel,
showPostTitle,
showCommentsCount,
level,
},
attributes: { textAlign, showPostTitle, showCommentsCount, level },
setAttributes,
context: { postType, postId },
} ) {
const TagName = 'h' + level;
const [ commentsCount, setCommentsCount ] = useState();
const [ editingMode, setEditingMode ] = useState( 'plural' );
const [ rawTitle ] = useEntityProp( 'postType', postType, 'title', postId );
const isSiteEditor = typeof postId === 'undefined';
const blockProps = useBlockProps( {
Expand Down Expand Up @@ -100,22 +86,6 @@ export default function Edit( {
const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
{ isSiteEditor && (
<ToggleGroupControl
label={ __( 'Editing mode' ) }
onChange={ setEditingMode }
value={ editingMode }
>
<ToggleGroupControlOption
label={ __( 'Singular' ) }
value="singular"
/>
<ToggleGroupControlOption
label={ __( 'Plural' ) }
value="plural"
/>
</ToggleGroupControl>
) }
<ToggleControl
label={ __( 'Show post title' ) }
checked={ showPostTitle }
Expand All @@ -136,78 +106,52 @@ export default function Edit( {

const postTitle = isSiteEditor ? __( '"Post Title"' ) : `"${ rawTitle }"`;

const singlePlaceholder = showPostTitle
? __( 'One response to ' )
: __( 'One response' );

const singlePlaceholderNoCount = showPostTitle
? __( 'Response to ' )
: __( 'Response' );

const multiplePlaceholder = showPostTitle
? __( 'responses to ' )
: __( 'responses' );

const multiplePlaceholderNoCount = showPostTitle
? __( 'Responses to ' )
: __( 'Responses' );
let placeholder;
if ( showCommentsCount && commentsCount !== undefined ) {
if ( showPostTitle ) {
if ( commentsCount === 1 ) {
/* translators: %s: Post title. */
placeholder = sprintf( __( 'One response to %s' ), postTitle );
Comment on lines +113 to +114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
placeholder = sprintf(
/* translators: 1: Number of comments, 2: Post title. */
_n(
'%1$s response to %2$s',
'%1$s responses to %2$s',
commentsCount
),
Comment on lines +117 to +122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commentsCount,
postTitle
);
}
} else if ( commentsCount === 1 ) {
placeholder = __( 'One response' );
Copy link
Contributor Author

@ockham ockham May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
placeholder = sprintf(
/* translators: %s: Number of comments. */
_n( '%s responses', '%s responses', commentsCount ),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String doesn't exist in Core yet. However, this is arguably needed to fix the issue at the core of this PR (use sprintf with placeholders).

commentsCount
);
}
} else if ( showPostTitle ) {
if ( commentsCount === 1 ) {
/* translators: %s: Post title. */
placeholder = sprintf( __( 'Response to %s' ), postTitle );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String doesn't exist in Core yet. However, this is arguably needed to fix the issue at the core of this PR (use sprintf with placeholders).

} else {
/* translators: %s: Post title. */
placeholder = sprintf( __( 'Responses to %s' ), postTitle );
ockham marked this conversation as resolved.
Show resolved Hide resolved
}
} else if ( commentsCount === 1 ) {
placeholder = __( 'Response' );
Copy link
Contributor Author

@ockham ockham May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
placeholder = __( 'Responses' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return (
<>
{ blockControls }
{ inspectorControls }
<TagName { ...blockProps }>
{ editingMode === 'singular' || commentsCount === 1 ? (
<>
<PlainText
__experimentalVersion={ 2 }
tagName="span"
aria-label={
showCommentsCount
? singlePlaceholder
: singlePlaceholderNoCount
}
placeholder={
showCommentsCount
? singlePlaceholder
: singlePlaceholderNoCount
}
value={ singleCommentLabel }
onChange={ ( newLabel ) =>
setAttributes( {
singleCommentLabel: newLabel,
} )
}
/>
{ showPostTitle ? postTitle : null }
</>
) : (
<>
{ showCommentsCount ? commentsCount : null }
<PlainText
__experimentalVersion={ 2 }
tagName="span"
aria-label={
showCommentsCount
? ` ${ multiplePlaceholder }`
: multiplePlaceholderNoCount
}
placeholder={
showCommentsCount
? ` ${ multiplePlaceholder }`
: multiplePlaceholderNoCount
}
value={ multipleCommentsLabel }
onChange={ ( newLabel ) =>
setAttributes( {
multipleCommentsLabel: newLabel,
} )
}
/>
{ showPostTitle ? postTitle : null }
</>
) }
</TagName>
<TagName { ...blockProps }>{ placeholder }</TagName>
</>
);
}
2 changes: 2 additions & 0 deletions packages/block-library/src/comments-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { commentTitle as icon } from '@wordpress/icons';
*/
import metadata from './block.json';
import edit from './edit';
import deprecated from './deprecated';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
edit,
deprecated,
};
61 changes: 39 additions & 22 deletions packages/block-library/src/comments-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function render_block_core_comments_title( $attributes ) {
$show_post_title = ! empty( $attributes['showPostTitle'] ) && $attributes['showPostTitle'];
$show_comments_count = ! empty( $attributes['showCommentsCount'] ) && $attributes['showCommentsCount'];
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
$post_title = $show_post_title ? sprintf( '"%1$s"', get_the_title() ) : null;
$comments_count = number_format_i18n( get_comments_number() );
$comments_count = get_comments_number();
$post_title = '&#8220;' . get_the_title() . '&#8221;';
$tag_name = 'h2';
if ( isset( $attributes['level'] ) ) {
$tag_name = 'h' . $attributes['level'];
Expand All @@ -33,28 +33,45 @@ function render_block_core_comments_title( $attributes ) {
return;
}

$single_default_comment_label = $show_post_title ? __( 'Response to' ) : __( 'Response' );
if ( $show_comments_count ) {
$single_default_comment_label = $show_post_title ? __( 'One response to' ) : __( 'One response' );
if ( $show_post_title ) {
if ( '1' === $comments_count ) {
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
/* translators: %s: Post title. */
$comments_title = sprintf( __( 'One response to %s' ), $post_title );
} else {
$comments_title = sprintf(
/* translators: 1: Number of comments, 2: Post title. */
_n(
'%1$s response to %2$s',
'%1$s responses to %2$s',
$comments_count
),
number_format_i18n( $comments_count ),
$post_title
);
}
} elseif ( '1' === $comments_count ) {
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
$comments_title = __( 'One response' );
} else {
$comments_title = sprintf(
/* translators: %s: Number of comments. */
_n( '%s responses', '%s responses', $comments_count ),
number_format_i18n( $comments_count )
);
}
} elseif ( $show_post_title ) {
if ( '1' === $comments_count ) {
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
/* translators: %s: Post title. */
$comments_title = sprintf( __( 'Response to %s' ), $post_title );
} else {
/* translators: %s: Post title. */
$comments_title = sprintf( __( 'Responses to %s' ), $post_title );
}
} elseif ( '1' === $comments_count ) {
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
$comments_title = __( 'Response' );
} else {
$comments_title = __( 'Responses' );
}
$single_comment_label = ! empty( $attributes['singleCommentLabel'] ) ? $attributes['singleCommentLabel'] : $single_default_comment_label;

$multiple_default_comment_label = $show_post_title ? __( 'Responses to' ) : __( 'Responses' );
if ( $show_comments_count ) {
$multiple_default_comment_label = $show_post_title ? __( 'responses to' ) : __( 'responses' );
}

$multiple_comment_label = ! empty( $attributes['multipleCommentsLabel'] ) ? $attributes['multipleCommentsLabel'] : $multiple_default_comment_label;

$comments_title = '%1$s %2$s %3$s';

$comments_title = sprintf(
$comments_title,
// If there is only one comment, only display the label.
'1' !== $comments_count && $show_comments_count ? $comments_count : null,
'1' === $comments_count ? $single_comment_label : $multiple_comment_label,
$post_title
);

return sprintf(
'<%1$s id="comments" %2$s>%3$s</%1$s>',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:comments-title {"textAlign":"center","singleCommentLabel":"One reply to ","multipleCommentsLabel":" replies to ","level":4} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"name": "core/comments-title",
"isValid": true,
"attributes": {
"textAlign": "center",
"showPostTitle": true,
"showCommentsCount": true,
"level": 4
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"blockName": "core/comments-title",
"attrs": {
"textAlign": "center",
"singleCommentLabel": "One reply to ",
"multipleCommentsLabel": " replies to ",
"level": 4
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:comments-title {"textAlign":"center","level":4} /-->