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

Create comments title with simple styling #40419

Merged
merged 15 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Create comments title with simple styling
  • Loading branch information
cbravobernal committed Apr 19, 2022
commit 6cec8db4c59a7a2c364f89185548f4b212e4f559
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ An advanced block that allows displaying post comments using different visual co
- **Supports:** align (full, wide), color (background, gradients, link, text), ~~html~~
- **Attributes:** tagName

## Comments Title

Displays a tittle with the comments count ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/comments-title))

- **Name:** core/comments-title
- **Category:** theme
- **Supports:** color (background, gradients, text), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign

## Cover

Add an image or video with a text overlay — great for headers. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/cover))
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function gutenberg_reregister_core_block_types() {
'comments-pagination-next.php' => 'core/comments-pagination-next',
'comments-pagination-numbers.php' => 'core/comments-pagination-numbers',
'comments-pagination-previous.php' => 'core/comments-pagination-previous',
'comments-title.php' => 'core/comments-title',
'file.php' => 'core/file',
'home-link.php' => 'core/home-link',
'image.php' => 'core/image',
Expand Down
36 changes: 36 additions & 0 deletions packages/block-library/src/comments-title/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comments-title",
"title": "Comments Title",
"category": "theme",
"ancestor": [ "core/comments-query-loop" ],
"description": "Displays a tittle with the comments count",
"textdomain": "default",
"usesContext": [ "commentId" ],
"attributes": {
"textAlign": {
"type": "string"
}
},
"supports": {
"html": false,
"color": {
"gradients": true,
"text": true,
"background": true,
"__experimentalDefaultControls": {
"text": true
}
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true
}
}
}
40 changes: 40 additions & 0 deletions packages/block-library/src/comments-title/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

export default function Edit( { attributes: { textAlign }, setAttributes } ) {
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const blockControls = (
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
}
/>
</BlockControls>
);

return (
<>
{ blockControls }
<div { ...blockProps }>{ __( 'Comments Title' ) }</div>
</>
);
}
18 changes: 18 additions & 0 deletions packages/block-library/src/comments-title/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { title as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

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

export const settings = {
icon,
edit,
};
54 changes: 54 additions & 0 deletions packages/block-library/src/comments-title/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Server-side rendering of the `core/comments-title` block.
*
* @package WordPress
*/

/**
* Renders the `core/comments-title` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Return the post comments title.
*/
function render_block_core_comments_title( $attributes, $content, $block ) {
$comments_title = '';
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
if ( 1 === (int) get_comments_number() ) {
$comments_title = sprintf(
/* translators: %s: The post title. */
__( 'One Response to "%s"' ),
get_the_title()
);
} else {
$comments_title = sprintf(
/* translators: 1: The number of comments, 2: The post title. */
_n( '%1$s Response to "%2$s"', '%1$s Responses to "%2$s"', get_comments_number() ),
number_format_i18n( get_comments_number() ),
get_the_title()
);
}
return sprintf(
'<h3 id="comments" %1$s>%2$s</h3>',
ockham marked this conversation as resolved.
Show resolved Hide resolved
$wrapper_attributes,
$comments_title
);
}

/**
* Registers the `core/comments-title` block on the server.
*/
function register_block_core_comments_title() {
register_block_type_from_metadata(
__DIR__ . '/comments-title',
array(
'render_callback' => 'render_block_core_comments_title',
)
);
}

add_action( 'init', 'register_block_core_comments_title' );
3 changes: 3 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as commentsQueryLoop from './comments-query-loop';
import * as commentsPagination from './comments-pagination';
import * as commentsPaginationNext from './comments-pagination-next';
import * as commentsPaginationNumbers from './comments-pagination-numbers';
import * as commentsTitle from './comments-title';
import * as cover from './cover';
import * as embed from './embed';
import * as file from './file';
Expand Down Expand Up @@ -212,11 +213,13 @@ export const __experimentalGetCoreBlocks = () => [
commentEditLink,
commentReplyLink,
commentTemplate,
commentsTitle,
commentsQueryLoop,
commentsPagination,
commentsPaginationNext,
commentsPaginationNumbers,
commentsPaginationPrevious,

postComments,
homeLink,
logInOut,
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/blocks/core__comments-title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:comments-title {"style":{"typography":{"textTransform":"uppercase","fontStyle":"italic","fontWeight":"900","lineHeight":"3"}},"textColor":"vivid-purple","fontSize":"x-large","fontFamily":"source-serif-pro"} /-->
20 changes: 20 additions & 0 deletions test/integration/fixtures/blocks/core__comments-title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"name": "core/comments-title",
"isValid": true,
"attributes": {
"textColor": "vivid-purple",
"fontFamily": "source-serif-pro",
"fontSize": "x-large",
"style": {
"typography": {
"textTransform": "uppercase",
"fontStyle": "italic",
"fontWeight": "900",
"lineHeight": "3"
}
}
},
"innerBlocks": []
}
]
21 changes: 21 additions & 0 deletions test/integration/fixtures/blocks/core__comments-title.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"blockName": "core/comments-title",
"attrs": {
"style": {
"typography": {
"textTransform": "uppercase",
"fontStyle": "italic",
"fontWeight": "900",
"lineHeight": "3"
}
},
"textColor": "vivid-purple",
"fontSize": "x-large",
"fontFamily": "source-serif-pro"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:comments-title {"textColor":"vivid-purple","fontFamily":"source-serif-pro","fontSize":"x-large","style":{"typography":{"textTransform":"uppercase","fontStyle":"italic","fontWeight":"900","lineHeight":"3"}}} /-->