Skip to content

Commit

Permalink
Related Posts: Add per-block header settings. (#30897)
Browse files Browse the repository at this point in the history
Adds a toggle for headline visibility and a text field for the actual headline text.
  • Loading branch information
a8ck3n authored May 25, 2023
1 parent 3be4a6e commit 7cd00df
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Related Posts: Add per-block header support.
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { PanelBody, RangeControl, ToggleControl, ToolbarGroup } from '@wordpress/components';
import {
PanelBody,
RangeControl,
TextControl,
ToggleControl,
ToolbarGroup,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export const MAX_POSTS_TO_SHOW = 6;

export function RelatedPostsInspectorControls( { attributes, setAttributes } ) {
const { displayAuthor, displayContext, displayDate, displayThumbnails, postsToShow } = attributes;
const {
displayAuthor,
displayContext,
displayDate,
displayHeadline,
displayThumbnails,
headline,
postsToShow,
} = attributes;

return (
<PanelBody title={ __( 'General settings', 'jetpack' ) }>
Expand Down Expand Up @@ -35,6 +49,18 @@ export function RelatedPostsInspectorControls( { attributes, setAttributes } ) {
min={ 1 }
max={ MAX_POSTS_TO_SHOW }
/>
<ToggleControl
label={ __( 'Display headline', 'jetpack' ) }
checked={ displayHeadline }
onChange={ value => setAttributes( { displayHeadline: value } ) }
/>
{ displayHeadline && (
<TextControl
value={ headline }
onChange={ value => setAttributes( { headline: value } ) }
label={ __( 'Headline', 'jetpack' ) }
/>
) }
</PanelBody>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ export class RelatedPostsEdit extends Component {
displayAuthor,
displayContext,
displayDate,
displayHeadline,
displayThumbnails,
headline,
postLayout,
postsToShow,
} = attributes;
Expand Down Expand Up @@ -218,6 +220,7 @@ export class RelatedPostsEdit extends Component {
</BlockControls>

<div className={ className } id={ `related-posts-${ instanceId }` }>
{ displayHeadline && <h3>{ headline }</h3> }
<div className={ previewClassName } data-layout={ postLayout }>
<RelatedPostsPreviewRows posts={ displayPosts } />
</div>
Expand Down
10 changes: 10 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/related-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export const settings = {
type: 'boolean',
default: true,
},
displayHeadline: {
type: 'boolean',
default: false,
},
displayThumbnails: {
type: 'boolean',
default: false,
Expand All @@ -52,6 +56,10 @@ export const settings = {
type: 'boolean',
default: false,
},
headline: {
type: 'string',
default: '',
},
postsToShow: {
type: 'number',
default: 3,
Expand Down Expand Up @@ -96,8 +104,10 @@ export const settings = {
postLayout: 'grid',
displayAuthor: false,
displayDate: true,
displayHeadline: false,
displayThumbnails: true,
displayContext: false,
headline: '',
postsToShow: 2,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
[
{
"clientId": "_clientId_0",
"name": "jetpack/related-posts",
"isValid": true,
"attributes": {
"postLayout": "list",
"displayDate": false,
"displayAuthor": false,
"displayThumbnails": true,
"displayContext": true,
"postsToShow": 4
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "jetpack/related-posts",
"isValid": true,
"attributes": {
"postLayout": "list",
"displayDate": false,
"displayAuthor": false,
"displayHeadline": false,
"displayThumbnails": true,
"displayContext": true,
"headline": "",
"postsToShow": 4
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ public function render_block( $attributes ) {
'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
'show_context' => isset( $attributes['displayContext'] ) && $attributes['displayContext'],
'layout' => isset( $attributes['postLayout'] ) && 'list' === $attributes['postLayout'] ? $attributes['postLayout'] : 'grid',
Expand Down Expand Up @@ -518,12 +519,22 @@ public function render_block( $attributes ) {
$wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
}

$headline_markup = '';
if ( $block_attributes['show_headline'] === true ) {
$headline = $block_attributes['headline'];
if ( strlen( trim( $headline ) ) !== 0 ) {
$headline_markup = sprintf(
'<h3 class="jp-relatedposts-headline">%1$s</h3>',
esc_html( $headline )
);
}
}
$display_markup = sprintf(
'<nav class="jp-relatedposts-i2%1$s"%2$s data-layout="%3$s">%4$s%5$s</nav>',
! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
esc_attr( $block_attributes['layout'] ),
$block_attributes['headline'],
$headline_markup,
$rows_markup
);

Expand Down

0 comments on commit 7cd00df

Please sign in to comment.