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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ Displays the next or previous post link that is adjacent to the current post.

- **Name:** [core/post-navigation-link](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-theme/core-block-post-navigation-link/)
- **Category:** [theme](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-theme/)
- **Supports:** anchor, color (background, link, text), interactivity (clientNavigation), shadow, typography (fontSize, lineHeight, textAlign), ~~html~~, ~~reusable~~
- **Supports:** anchor, color (background, link, text), interactivity (clientNavigation), shadow, spacing (margin, padding), typography (fontSize, lineHeight, textAlign), ~~html~~, ~~reusable~~
- **Attributes:** arrow, label, linkLabel, showTitle, taxonomy, type

## Post Template
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Enhancements

- Site Tagline: Add Fit text support ([#83034](https://github.com/WordPress/gutenberg/pull/83034)).
- Post Navigation Link: Add border and spacing support. The block renders an empty wrapper when there is no adjacent post, so both supports skip serialization and the styles are applied only when a link renders ([#83122](https://github.com/WordPress/gutenberg/pull/83122)).
- Post Navigation Link: Add shadow support, withheld from the empty wrapper the same way ([#83058](https://github.com/WordPress/gutenberg/pull/83058)).

### Bug Fixes

Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/post-navigation-link/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ _Defined via the [`supports`](https://developer.wordpress.org/block-editor/refer
- [`shadow`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#shadow): `true`
- [`interactivity`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#interactivity):
- `clientNavigation`: `true`
- [`spacing`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#spacing):
- `margin`: `true`
- `padding`: `true`

## Context

Expand All @@ -50,7 +53,9 @@ _Defined via the [`usesContext` and `providesContext`](https://developer.wordpre

_Defined via the [`selectors`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-selectors/) property in block.json._

- **border**: `.wp-block-post-navigation-link:not(:empty)`
- **shadow**: `.wp-block-post-navigation-link:not(:empty)`
- **spacing**: `.wp-block-post-navigation-link:not(:empty)`

## Block Markup

Expand Down
26 changes: 25 additions & 1 deletion packages/block-library/src/post-navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,34 @@
},
"interactivity": {
"clientNavigation": true
},
"spacing": {
"__experimentalSkipSerialization": true,
"margin": true,
"padding": true,
"__experimentalDefaultControls": {
"margin": false,
"padding": false
}
},
"__experimentalBorder": {
"__experimentalSkipSerialization": true,
"color": true,
"radius": true,
"style": true,
"width": true,
"__experimentalDefaultControls": {
"color": false,
"radius": false,
"style": false,
"width": false
}
}
},
"selectors": {
"shadow": ".wp-block-post-navigation-link:not(:empty)"
"border": ".wp-block-post-navigation-link:not(:empty)",
"shadow": ".wp-block-post-navigation-link:not(:empty)",
"spacing": ".wp-block-post-navigation-link:not(:empty)"
},
"style": "wp-block-post-navigation-link"
}
20 changes: 19 additions & 1 deletion packages/block-library/src/post-navigation-link/edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
InspectorControls,
RichText,
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
} from '@wordpress/block-editor';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -43,8 +45,24 @@ export default function PostNavigationLinkEdit( {
}

const ariaLabel = isNext ? __( 'Next post' ) : __( 'Previous post' );

/*
* Border, shadow and spacing serialization is skipped for this block so the
* styles are not applied to the empty wrapper the front end renders when
* there is no adjacent post. The editor always renders the link, so they
* always apply here.
*/
const borderProps = useBorderProps( attributes );
const shadowProps = getShadowClassesAndStyles( attributes );
const blockProps = useBlockProps( { style: shadowProps.style } );
const spacingProps = getSpacingClassesAndStyles( attributes );
const blockProps = useBlockProps( {
className: borderProps.className,
style: {
...borderProps.style,
...shadowProps.style,
...spacingProps.style,
},
} );
const taxonomies = useSelect(
( select ) => {
const { getTaxonomies } = select( coreStore );
Expand Down
91 changes: 83 additions & 8 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,29 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
$content = $get_link_function( $format, $link );
}

// The `{next,previous}_post_link` filters can return null, which renders as no link.
$content = (string) $content;

/*
* Shadow serialization is skipped for this block so the shadow can be
* withheld from the empty wrapper rendered when there is no adjacent post.
* The wrapper itself is kept for backward compatibility.
* Border, shadow and spacing serialization is skipped for this block so
* those styles can be withheld from the empty wrapper rendered when there is
* no adjacent post. The wrapper itself is kept for backward compatibility.
*/
$styles = '';
if ( '' !== $content && ! empty( $attributes['style']['shadow'] ) ) {
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );
$styles = $shadow_styles['css'] ?? '';
$support_styles = '' === $content
? array(
'class' => '',
'style' => '',
)
: block_core_post_navigation_link_get_support_styles( $attributes );

if ( '' !== $support_styles['class'] ) {
$classes .= " {$support_styles['class']}";
}

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $classes,
'style' => $styles,
'style' => $support_styles['style'],
)
);

Expand All @@ -137,6 +145,73 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
);
}

/**
* Generates the border, shadow and spacing class names and inline styles for
* the `core/post-navigation-link` block.
*
* Mirrors the default border, shadow and spacing block support serialization,
* which the block opts out of so the styles are only applied when a link is
* rendered.
*
* @since 7.2.0
*
* @param array $attributes The block attributes.
* @return array {
* Border, shadow and spacing class names and inline styles.
*
* @type string $class Class names generated by the style engine, or an empty string.
* @type string $style Inline styles generated by the style engine, or an empty string.
* }
*/
function block_core_post_navigation_link_get_support_styles( $attributes ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a blocker, and nothing to do here, just noting for later or whenever we look back to this: this is very
similar to get_block_core_post_featured_image_border_attributes and get_block_core_avatar_border_attributes, probably others, but is slightly better as it has the is_numeric check.

JS has a shared helper (getBorderClassesAndStyles), maybe one day PHP could too.

Seems like wp_apply_border_support() puts the skip-serialization check at the top and returns early. Maybe that's why there's no shared helper 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Quite possibly. This definitely seems like a candidate for refactoring though.

$block_styles = $attributes['style'] ?? array();
$border = $block_styles['border'] ?? array();
$border_styles = array();

// Border radius and width accept unitless numbers from the original implementation.
if ( isset( $border['radius'] ) ) {
$border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
}

if ( isset( $border['width'] ) ) {
$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
}

if ( isset( $border['style'] ) ) {
$border_styles['style'] = $border['style'];
}

// A preset border color is stored in its own attribute rather than under `style`.
$border_styles['color'] = isset( $attributes['borderColor'] )
? "var:preset|color|{$attributes['borderColor']}"
: ( $border['color'] ?? null );

// Individual border sides e.g. top, left etc.
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
$border_styles[ $side ] = array(
'width' => $border[ $side ]['width'] ?? null,
'color' => $border[ $side ]['color'] ?? null,
'style' => $border[ $side ]['style'] ?? null,
);
}

$styles = wp_style_engine_get_styles(
array(
'border' => $border_styles,
'shadow' => $block_styles['shadow'] ?? null,
'spacing' => array(
'margin' => $block_styles['spacing']['margin'] ?? null,
'padding' => $block_styles['spacing']['padding'] ?? null,
),
)
);

return array(
'class' => $styles['classnames'] ?? '',
'style' => $styles['css'] ?? '',
);
}

/**
* Registers the `core/post-navigation-link` block on the server.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/post-navigation-link/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.wp-block-post-navigation-link {
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;

.wp-block-post-navigation-link__arrow-previous {
display: inline-block;
Expand Down
Loading
Loading