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

[Block: Post Navigation Link] Add an option for displaying the label inside the link #34952

Merged
merged 4 commits into from
Oct 4, 2021
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
4 changes: 4 additions & 0 deletions packages/block-library/src/post-navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"showTitle": {
"type": "boolean",
"default": false
},
"linkLabel": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
31 changes: 29 additions & 2 deletions packages/block-library/src/post-navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ import {
import { __ } from '@wordpress/i18n';

export default function PostNavigationLinkEdit( {
attributes: { type, label, showTitle, textAlign },
attributes: { type, label, showTitle, textAlign, linkLabel },
setAttributes,
} ) {
const isNext = type === 'next';
const placeholder = isNext ? __( 'Next' ) : __( 'Previous' );
let placeholder = isNext ? __( 'Next' ) : __( 'Previous' );

if ( showTitle ) {
/* translators: Label before for next and previous post. There is a space after the colon. */
placeholder = isNext ? __( 'Next: ' ) : __( 'Previous: ' );
}

const ariaLabel = isNext ? __( 'Next post' ) : __( 'Previous post' );
const blockProps = useBlockProps( {
className: classnames( {
Expand All @@ -44,6 +50,19 @@ export default function PostNavigationLinkEdit( {
} )
}
/>
{ showTitle && (
<ToggleControl
label={ __(
'Include the label as part of the link'
) }
checked={ !! linkLabel }
onChange={ () =>
setAttributes( {
linkLabel: ! linkLabel,
} )
}
/>
) }
</PanelBody>
</InspectorControls>
<BlockControls>
Expand All @@ -65,6 +84,14 @@ export default function PostNavigationLinkEdit( {
setAttributes( { label: newLabel } )
}
/>
{ showTitle && (
<a
href="#post-navigation-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ __( 'An example title' ) }
</a>
) }
</div>
</>
);
Expand Down
31 changes: 28 additions & 3 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
$format = '%link';
$link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
$label = '';

// If a custom label is provided, make this a link.
// `$label` is used to prepend the provided label, if we want to show the page title as well.
if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
Expand All @@ -42,11 +43,35 @@ function render_block_core_post_navigation_link( $attributes, $content ) {

// If we want to also show the page title, make the page title a link and prepend the label.
if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
if ( $label ) {
$format = "$label %link";
/*
* If the label link option is not enabled but there is a custom label,
* display the custom label as text before the linked title.
*/
if ( ! $attributes['linkLabel'] ) {
if ( $label ) {
$format = '<span class="post-navigation-link__label">' . $label . '</span> %link';
}
$link = '%title';
} elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {
// If the label link option is enabled and there is a custom label, display it before the title.
if ( $label ) {
$link = '<span class="post-navigation-link__label">' . $label . '</span> <span class="post-navigation-link__title">%title</title>';
} else {
/*
* If the label link option is enabled and there is no custom label,
* add a colon between the label and the post title.
*/
$label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );
$link = sprintf(
/* translators: 1: label. 2: post title */
__( '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>' ),
Copy link
Member

Choose a reason for hiding this comment

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

👋 @carolinan @MaggieCabrera I was doing some translation work and run into this string. I'm not sure if I'm missing something but I don't see anything here that needs to be translated. Can we remove _() from here?

Copy link
Member

Choose a reason for hiding this comment

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

I've create #35930

$label,
'%title'
);
}
}
$link = '%title';
}

// The dynamic portion of the function name, `$navigation_type`,
// refers to the type of adjacency, 'next' or 'previous'.
$get_link_function = "get_{$navigation_type}_post_link";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"isValid": true,
"attributes": {
"type": "next",
"showTitle": false
"showTitle": false,
"linkLabel": false
},
"innerBlocks": [],
"originalContent": ""
Expand Down