From 5def941eebb7a92da1c447e12a8d876fbfc2b730 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 1 Aug 2022 19:49:41 +1000
Subject: [PATCH 1/5] Add border support to post featured image block
---
.../src/post-featured-image/block.json | 12 ++++
.../src/post-featured-image/edit.js | 10 ++-
.../src/post-featured-image/index.php | 61 +++++++++++++++++--
.../src/post-featured-image/style.scss | 1 +
4 files changed, 79 insertions(+), 5 deletions(-)
diff --git a/packages/block-library/src/post-featured-image/block.json b/packages/block-library/src/post-featured-image/block.json
index a0ff90808c0e81..a1c3f75aab8a2c 100644
--- a/packages/block-library/src/post-featured-image/block.json
+++ b/packages/block-library/src/post-featured-image/block.json
@@ -42,6 +42,18 @@
"text": false,
"background": false
},
+ "__experimentalBorder": {
+ "color": true,
+ "radius": true,
+ "width": true,
+ "__experimentalSelector": "img",
+ "__experimentalSkipSerialization": true,
+ "__experimentalDefaultControls": {
+ "color": true,
+ "radius": true,
+ "width": true
+ }
+ },
"html": false,
"spacing": {
"margin": true,
diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js
index 6ef4d0ee0a4088..4f965f667afda9 100644
--- a/packages/block-library/src/post-featured-image/edit.js
+++ b/packages/block-library/src/post-featured-image/edit.js
@@ -18,6 +18,7 @@ import {
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
+ __experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
@@ -95,6 +96,7 @@ function PostFeaturedImageDisplay( {
const blockProps = useBlockProps( {
style: { width, height },
} );
+ const borderProps = useBorderProps( attributes );
const onSelectImage = ( value ) => {
if ( value?.id ) {
@@ -165,6 +167,11 @@ function PostFeaturedImageDisplay( {
}
const label = __( 'Add a featured image' );
+ const imageStyles = {
+ ...borderProps.style,
+ height,
+ objectFit: height && scale,
+ };
if ( ! featuredImage ) {
image = (
@@ -196,6 +203,7 @@ function PostFeaturedImageDisplay( {
placeholder()
) : (
);
}
diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php
index a1206f29b61222..cd4f0b0a7f9ecd 100644
--- a/packages/block-library/src/post-featured-image/index.php
+++ b/packages/block-library/src/post-featured-image/index.php
@@ -19,10 +19,15 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$post_ID = $block->context['postId'];
- $is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
- $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
- $post_title = trim( strip_tags( get_the_title( $post_ID ) ) );
- $attr = $is_link ? array( 'alt' => $post_title ) : array();
+ $is_link = isset( $attributes['isLink'] ) && $attributes['isLink'];
+ $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
+ $post_title = trim( strip_tags( get_the_title( $post_ID ) ) );
+ $attr = get_block_core_post_featured_image_border_attributes( $attributes );
+
+ if ( $is_link ) {
+ $attr['alt'] = $post_title;
+ }
+
$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
if ( ! $featured_image ) {
return '';
@@ -55,6 +60,54 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
return "";
}
+/**
+ * Generates class names and styles to apply the border support styles for
+ * the Post Featured Image block.
+ *
+ * @param array $attributes The block attributes.
+ * @return array The border-related classnames and styles for the block.
+ */
+function get_block_core_post_featured_image_border_attributes( $attributes ) {
+ $border_styles = array();
+ $sides = array( 'top', 'right', 'bottom', 'left' );
+
+ // Border radius.
+ if ( isset( $attributes['style']['border']['radius'] ) ) {
+ $border_styles['radius'] = $attributes['style']['border']['radius'];
+ }
+
+ // Border style.
+ if ( isset( $attributes['style']['border']['style'] ) ) {
+ $border_styles['style'] = $attributes['style']['border']['style'];
+ }
+
+ // Border width.
+ if ( isset( $attributes['style']['border']['width'] ) ) {
+ $border_styles['width'] = $attributes['style']['border']['width'];
+ }
+
+ // Border color.
+ $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
+ $custom_color = _wp_array_get( $attributes, array( 'style', 'border', 'color' ), null );
+ $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
+
+ // Individual border styles e.g. top, left etc.
+ foreach ( $sides as $side ) {
+ $border = _wp_array_get( $attributes, array( 'style', 'border', $side ), null );
+ $border_styles[ $side ] = array(
+ 'color' => isset( $border['color'] ) ? $border['color'] : null,
+ 'style' => isset( $border['style'] ) ? $border['style'] : null,
+ 'width' => isset( $border['width'] ) ? $border['width'] : null,
+ );
+ }
+
+ $border_attributes = gutenberg_style_engine_get_styles( array( 'border' => $border_styles ) );
+ return array(
+ 'class' => $border_attributes['classnames'],
+ 'style' => $border_attributes['css'],
+ );
+}
+
/**
* Registers the `core/post-featured-image` block on the server.
*/
diff --git a/packages/block-library/src/post-featured-image/style.scss b/packages/block-library/src/post-featured-image/style.scss
index 85ca80b427e617..7af2f5331c82cd 100644
--- a/packages/block-library/src/post-featured-image/style.scss
+++ b/packages/block-library/src/post-featured-image/style.scss
@@ -9,6 +9,7 @@
width: 100%;
height: auto;
vertical-align: bottom;
+ box-sizing: border-box;
}
&.alignwide img,
From 621575b04ee25ded3535194f8bf6edb6a6692ac1 Mon Sep 17 00:00:00 2001
From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Date: Mon, 8 Aug 2022 15:01:14 +1000
Subject: [PATCH 2/5] Apply border to placeholder as well
---
.../src/post-featured-image/block.json | 2 +-
.../src/post-featured-image/edit.js | 46 ++++++++++++++-----
.../src/post-featured-image/editor.scss | 41 +++++++++++++++++
3 files changed, 76 insertions(+), 13 deletions(-)
diff --git a/packages/block-library/src/post-featured-image/block.json b/packages/block-library/src/post-featured-image/block.json
index a1c3f75aab8a2c..dd5956a4f9290a 100644
--- a/packages/block-library/src/post-featured-image/block.json
+++ b/packages/block-library/src/post-featured-image/block.json
@@ -46,7 +46,7 @@
"color": true,
"radius": true,
"width": true,
- "__experimentalSelector": "img",
+ "__experimentalSelector": "img, .block-editor-media-placeholder",
"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"color": true,
diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js
index 4f965f667afda9..e95c8c2253dc4b 100644
--- a/packages/block-library/src/post-featured-image/edit.js
+++ b/packages/block-library/src/post-featured-image/edit.js
@@ -1,3 +1,8 @@
+/**
+ * External dependencies
+ */
+import classnames from 'classnames';
+
/**
* WordPress dependencies
*/
@@ -31,17 +36,6 @@ import DimensionControls from './dimension-controls';
const ALLOWED_MEDIA_TYPES = [ 'image' ];
-const placeholder = ( content ) => {
- return (
-