Skip to content

Commit 703897a

Browse files
sidharthpandita1t-hamanoCopons
authored
Latest Comments: Add option to display full comments (#72665)
* Changes made * test(latest-comments): add deprecated v1 migration fixtures for block validation Adds new test fixtures for the core/latest-comments block to verify the deprecated v1 migration path. Includes: - core__latest-comments__deprecated-v1.html - core__latest-comments__deprecated-v1.json - core__latest-comments__deprecated-v1.serialized.html These fixtures ensure correct migration from legacy 'displayExcerpt' to the new 'displayContent' attribute. * Extra spacing/indentation in block.json * removed unnecessary space/indentation * Fix code formatting * Fix code formatting * Update packages/block-library/src/latest-comments/index.php Co-authored-by: Aki Hamano <[email protected]> * Update packages/block-library/src/latest-comments/deprecated.js Co-authored-by: Aki Hamano <[email protected]> * Update packages/block-library/src/latest-comments/deprecated.js Co-authored-by: Aki Hamano <[email protected]> * Update packages/block-library/src/latest-comments/edit.js Co-authored-by: Aki Hamano <[email protected]> * Update packages/block-library/src/latest-comments/index.php Co-authored-by: Aki Hamano <[email protected]> * Update packages/block-library/src/latest-comments/edit.js Co-authored-by: Aki Hamano <[email protected]> * fixtures:regenerate * Changes made --------- Co-authored-by: sidharthpandita1 <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: Copons <[email protected]>
1 parent a3ef8e7 commit 703897a

File tree

11 files changed

+100
-17
lines changed

11 files changed

+100
-17
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ Display a list of your most recent comments. ([Source](https://github.com/WordPr
448448
- **Name:** core/latest-comments
449449
- **Category:** widgets
450450
- **Supports:** align, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
451-
- **Attributes:** commentsToShow, displayAvatar, displayDate, displayExcerpt
451+
- **Attributes:** commentsToShow, displayAvatar, displayContent, displayDate
452452

453453
## Latest Posts
454454

packages/block-library/src/latest-comments/block.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
"type": "boolean",
2323
"default": true
2424
},
25-
"displayExcerpt": {
26-
"type": "boolean",
27-
"default": true
25+
"displayContent": {
26+
"type": "string",
27+
"default": "excerpt",
28+
"enum": [ "none", "excerpt", "full" ]
2829
}
2930
},
3031
"supports": {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
5+
const v1 = {
6+
attributes: {
7+
commentsToShow: {
8+
type: 'number',
9+
default: 5,
10+
minimum: 1,
11+
maximum: 100,
12+
},
13+
displayAvatar: {
14+
type: 'boolean',
15+
default: true,
16+
},
17+
displayDate: {
18+
type: 'boolean',
19+
default: true,
20+
},
21+
displayExcerpt: {
22+
type: 'boolean',
23+
default: true,
24+
},
25+
},
26+
isEligible( attributes ) {
27+
return attributes?.displayExcerpt === false;
28+
},
29+
migrate( attributes ) {
30+
return {
31+
...attributes,
32+
displayContent: attributes.displayExcerpt ? 'excerpt' : 'none',
33+
};
34+
},
35+
};
36+
37+
export default [ v1 ];

packages/block-library/src/latest-comments/edit.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
55
import {
66
Disabled,
77
RangeControl,
8+
SelectControl,
89
ToggleControl,
910
__experimentalToolsPanel as ToolsPanel,
1011
__experimentalToolsPanelItem as ToolsPanelItem,
@@ -31,7 +32,7 @@ const MIN_COMMENTS = 1;
3132
const MAX_COMMENTS = 100;
3233

3334
export default function LatestComments( { attributes, setAttributes } ) {
34-
const { commentsToShow, displayAvatar, displayDate, displayExcerpt } =
35+
const { commentsToShow, displayAvatar, displayDate, displayContent } =
3536
attributes;
3637

3738
const serverSideAttributes = {
@@ -54,7 +55,7 @@ export default function LatestComments( { attributes, setAttributes } ) {
5455
commentsToShow: 5,
5556
displayAvatar: true,
5657
displayDate: true,
57-
displayExcerpt: true,
58+
displayContent: 'excerpt',
5859
} );
5960
} }
6061
dropdownMenuProps={ dropdownMenuProps }
@@ -98,20 +99,26 @@ export default function LatestComments( { attributes, setAttributes } ) {
9899
</ToolsPanelItem>
99100

100101
<ToolsPanelItem
101-
hasValue={ () => ! displayExcerpt }
102-
label={ __( 'Display excerpt' ) }
102+
hasValue={ () => displayContent !== 'excerpt' }
103+
label={ __( 'Display content' ) }
103104
onDeselect={ () =>
104-
setAttributes( { displayExcerpt: true } )
105+
setAttributes( { displayContent: 'excerpt' } )
105106
}
106107
isShownByDefault
107108
>
108-
<ToggleControl
109+
<SelectControl
109110
__nextHasNoMarginBottom
110-
label={ __( 'Display excerpt' ) }
111-
checked={ displayExcerpt }
112-
onChange={ () =>
111+
__next40pxDefaultSize
112+
label={ __( 'Display content' ) }
113+
value={ displayContent }
114+
options={ [
115+
{ label: __( 'No content' ), value: 'none' },
116+
{ label: __( 'Excerpt' ), value: 'excerpt' },
117+
{ label: __( 'Full content' ), value: 'full' },
118+
] }
119+
onChange={ ( value ) =>
113120
setAttributes( {
114-
displayExcerpt: ! displayExcerpt,
121+
displayContent: value,
115122
} )
116123
}
117124
/>

packages/block-library/src/latest-comments/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { comment as icon } from '@wordpress/icons';
99
import initBlock from '../utils/init-block';
1010
import metadata from './block.json';
1111
import edit from './edit';
12+
import deprecated from './deprecated';
1213

1314
const { name } = metadata;
1415

@@ -18,6 +19,7 @@ export const settings = {
1819
icon,
1920
example: {},
2021
edit,
22+
deprecated,
2123
};
2224

2325
export const init = () => initBlock( { name, metadata, settings } );

packages/block-library/src/latest-comments/index.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ function wp_latest_comments_draft_or_post_title( $post = 0 ) {
4343
* @return string Returns the post content with latest comments added.
4444
*/
4545
function render_block_core_latest_comments( $attributes ) {
46+
// Handle backward compatibility: check for old displayExcerpt attribute
47+
if ( isset( $attributes['displayExcerpt'] ) ) {
48+
$display_content = $attributes['displayExcerpt'] ? 'excerpt' : 'none';
49+
} else {
50+
$display_content = isset( $attributes['displayContent'] ) ? $attributes['displayContent'] : 'excerpt';
51+
}
52+
4653
$comments = get_comments(
4754
/** This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php */
4855
apply_filters(
@@ -112,7 +119,9 @@ function render_block_core_latest_comments( $attributes ) {
112119
);
113120
}
114121
$list_items_markup .= '</footer>';
115-
if ( $attributes['displayExcerpt'] ) {
122+
if ( 'full' === $display_content ) {
123+
$list_items_markup .= '<div class="wp-block-latest-comments__comment-excerpt">' . wpautop( get_comment_text( $comment ) ) . '</div>';
124+
} elseif ( 'excerpt' === $display_content ) {
116125
$list_items_markup .= '<div class="wp-block-latest-comments__comment-excerpt">' . wpautop( get_comment_excerpt( $comment ) ) . '</div>';
117126
}
118127
$list_items_markup .= '</article></li>';
@@ -126,7 +135,7 @@ function render_block_core_latest_comments( $attributes ) {
126135
if ( $attributes['displayDate'] ) {
127136
$classnames[] = 'has-dates';
128137
}
129-
if ( $attributes['displayExcerpt'] ) {
138+
if ( 'none' !== $display_content ) {
130139
$classnames[] = 'has-excerpts';
131140
}
132141
if ( empty( $comments ) ) {

test/integration/fixtures/blocks/core__latest-comments.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"commentsToShow": 5,
77
"displayAvatar": true,
88
"displayDate": true,
9-
"displayExcerpt": true
9+
"displayContent": "excerpt"
1010
},
1111
"innerBlocks": []
1212
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- wp:latest-comments {"displayExcerpt":false} /-->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"name": "core/latest-comments",
4+
"isValid": true,
5+
"attributes": {
6+
"commentsToShow": 5,
7+
"displayAvatar": true,
8+
"displayDate": true,
9+
"displayExcerpt": false,
10+
"displayContent": "none"
11+
},
12+
"innerBlocks": []
13+
}
14+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"blockName": "core/latest-comments",
4+
"attrs": {
5+
"displayExcerpt": false
6+
},
7+
"innerBlocks": [],
8+
"innerHTML": "",
9+
"innerContent": []
10+
}
11+
]

0 commit comments

Comments
 (0)