Skip to content
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
3 changes: 1 addition & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,7 @@ Display a post's comments count. ([Source](https://github.com/WordPress/gutenber

- **Name:** core/post-comments-count
- **Category:** theme
- **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign
- **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight, textAlign), ~~html~~

## Comments Form

Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/post-comments-count/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
"category": "theme",
"description": "Display a post's comments count.",
"textdomain": "default",
"attributes": {
"textAlign": {
"type": "string"
}
},
"usesContext": [ "postId" ],
"example": {
"viewportWidth": 350
Expand All @@ -32,6 +27,7 @@
"typography": {
"fontSize": true,
"lineHeight": true,
"textAlign": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
Expand Down
61 changes: 61 additions & 0 deletions packages/block-library/src/post-comments-count/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Internal dependencies
*/
import migrateTextAlign from '../utils/migrate-text-align';

const v1 = {
attributes: {
textAlign: {
type: 'string',
},
},
supports: {
anchor: true,
html: false,
spacing: {
margin: true,
padding: true,
},
color: {
gradients: true,
__experimentalDefaultControls: {
background: true,
text: true,
},
},
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontFamily: true,
__experimentalFontWeight: true,
__experimentalFontStyle: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalLetterSpacing: true,
__experimentalDefaultControls: {
fontSize: true,
},
},
interactivity: {
clientNavigation: true,
},
__experimentalBorder: {
radius: true,
color: true,
width: true,
style: true,
},
},
migrate: migrateTextAlign,
isEligible( attributes ) {
return (
!! attributes.textAlign ||
!! attributes.className?.match(
/\bhas-text-align-(left|center|right)\b/
)
);
},
save: () => null,
};

export default [ v1 ];
44 changes: 10 additions & 34 deletions packages/block-library/src/post-comments-count/edit.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useBlockProps } from '@wordpress/block-editor';
import { useState, useEffect } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

export default function PostCommentsCountEdit( {
attributes,
context,
setAttributes,
} ) {
const { textAlign } = attributes;
export default function PostCommentsCountEdit( { context } ) {
const { postId } = context;
const [ commentsCount, setCommentsCount ] = useState();
const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const blockProps = useBlockProps();

useEffect( () => {
if ( ! postId ) {
return;
}

const currentPostId = postId;

apiFetch( {
path: addQueryArgs( '/wp/v2/comments', {
post: postId,
Expand All @@ -48,6 +33,7 @@ export default function PostCommentsCountEdit( {
}, [ postId ] );

const hasPostAndComments = postId && commentsCount !== undefined;

const blockStyles = {
...blockProps.style,
textDecoration: hasPostAndComments
Expand All @@ -56,18 +42,8 @@ export default function PostCommentsCountEdit( {
};

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<div { ...blockProps } style={ blockStyles }>
{ hasPostAndComments ? commentsCount : '0' }
</div>
</>
<div { ...blockProps } style={ blockStyles }>
{ hasPostAndComments ? commentsCount : '0' }
</div>
);
}
2 changes: 2 additions & 0 deletions packages/block-library/src/post-comments-count/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import transforms from './transforms';
import deprecated from './deprecated';

const { name } = metadata;
export { metadata, name };
Expand All @@ -18,6 +19,7 @@ export const settings = {
icon,
edit,
transforms,
deprecated,
};

export const init = () => initBlock( { name, metadata, settings } );
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const transforms = {
blocks: [ 'core/post-comments-count' ],
transform: ( { textAlign } ) => {
return createBlock( 'core/post-comments-count', {
textAlign,
style: { typography: { textAlign } },
} );
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:post-comments-count {"textAlign":"left"} /-->

<!-- wp:post-comments-count {"textAlign":"center"} /-->

<!-- wp:post-comments-count {"textAlign":"right"} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"name": "core/post-comments-count",
"isValid": true,
"attributes": {
"style": {
"typography": {
"textAlign": "left"
}
}
},
"innerBlocks": []
},
{
"name": "core/post-comments-count",
"isValid": true,
"attributes": {
"style": {
"typography": {
"textAlign": "center"
}
}
},
"innerBlocks": []
},
{
"name": "core/post-comments-count",
"isValid": true,
"attributes": {
"style": {
"typography": {
"textAlign": "right"
}
}
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"blockName": "core/post-comments-count",
"attrs": {
"textAlign": "left"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/post-comments-count",
"attrs": {
"textAlign": "center"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/post-comments-count",
"attrs": {
"textAlign": "right"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:post-comments-count {"style":{"typography":{"textAlign":"left"}}} /-->

<!-- wp:post-comments-count {"style":{"typography":{"textAlign":"center"}}} /-->

<!-- wp:post-comments-count {"style":{"typography":{"textAlign":"right"}}} /-->
Loading