From 38d1b54bb84eece64ff1e322e7cb3f5636d2d6cf Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Fri, 2 Sep 2022 23:03:52 +0530 Subject: [PATCH 01/20] Feat: Add block attributes related to the overlay --- .../src/post-featured-image/block.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/block-library/src/post-featured-image/block.json b/packages/block-library/src/post-featured-image/block.json index dd5956a4f9290..2c65f7017e4b3 100644 --- a/packages/block-library/src/post-featured-image/block.json +++ b/packages/block-library/src/post-featured-image/block.json @@ -32,6 +32,19 @@ "linkTarget": { "type": "string", "default": "_self" + }, + "overlayColor": { + "type": "string" + }, + "dimRatio": { + "type": "number", + "default": 100 + }, + "gradient": { + "type": "string" + }, + "customGradient": { + "type": "string" } }, "usesContext": [ "postId", "postType", "queryId" ], From 6dc42cfab50251d0102e895939c5d39270435005 Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Fri, 2 Sep 2022 23:07:34 +0530 Subject: [PATCH 02/20] Feat: Add overlay settings and markup for editor --- .../src/post-featured-image/edit.js | 112 +++++++++++++++++- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index e95c8c2253dc4..b15a8aa45d7a7 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -15,6 +15,8 @@ import { Placeholder, Button, TextControl, + RangeControl, + __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { InspectorControls, @@ -24,6 +26,9 @@ import { useBlockProps, store as blockEditorStore, __experimentalUseBorderProps as useBorderProps, + __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, + __experimentalUseGradient, + __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, } from '@wordpress/block-editor'; import { __, sprintf } from '@wordpress/i18n'; import { upload } from '@wordpress/icons'; @@ -49,8 +54,17 @@ function PostFeaturedImageDisplay( { context: { postId, postType: postTypeSlug, queryId }, } ) { const isDescendentOfQueryLoop = Number.isFinite( queryId ); - const { isLink, height, width, scale, sizeSlug, rel, linkTarget } = - attributes; + const { + isLink, + height, + width, + scale, + sizeSlug, + rel, + linkTarget, + dimRatio, + overlayColor, + } = attributes; const [ featuredImage, setFeaturedImage ] = useEntityProp( 'postType', postTypeSlug, @@ -58,6 +72,16 @@ function PostFeaturedImageDisplay( { postId ); + const { gradientClass, gradientValue, setGradient } = + __experimentalUseGradient(); + const colorGradientSettings = useMultipleOriginColorsAndGradients(); + + const dimRatioToClass = ( ratio ) => { + return ratio === undefined + ? null + : 'has-background-dim-' + 10 * Math.round( ratio / 10 ); + }; + const { media, postType } = useSelect( ( select ) => { const { getMedia, getPostType } = select( coreStore ); @@ -91,6 +115,10 @@ function PostFeaturedImageDisplay( { style: { width, height }, } ); const borderProps = useBorderProps( attributes ); + const overlayStyles = { + backgroundColor: overlayColor, + ...borderProps.style, + }; const placeholder = ( content ) => { return ( @@ -163,6 +191,65 @@ function PostFeaturedImageDisplay( { ) } + + + setAttributes( { + overlayColor: updatedOverlayColor, + } ), + onGradientChange: setGradient, + isShownByDefault: true, + resetAllFilter: () => ( { + overlayColor: undefined, + customOverlayColor: undefined, + gradient: undefined, + customGradient: undefined, + } ), + }, + ] } + panelId={ clientId } + { ...colorGradientSettings } + /> + { + // If there's a media background the dimRatio will be + // defaulted to 50 whereas it will be 100 for colors. + return dimRatio === undefined + ? false + : dimRatio !== ( mediaUrl ? 50 : 100 ); + } } + label={ __( 'Overlay opacity' ) } + onDeselect={ () => + setAttributes( { dimRatio: mediaUrl ? 50 : 100 } ) + } + resetAllFilter={ () => ( { + dimRatio: mediaUrl ? 50 : 100, + } ) } + isShownByDefault + panelId={ clientId } + > + + setAttributes( { + dimRatio: newDimRatio, + } ) + } + min={ 0 } + max={ 100 } + step={ 10 } + required + /> + + ); let image; @@ -247,7 +334,26 @@ function PostFeaturedImageDisplay( { ) } -
{ image }
+
+
); } From 360194c9e44caea985e5be8e8117304a6363ed7a Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Fri, 2 Sep 2022 23:10:53 +0530 Subject: [PATCH 03/20] Feat: Add overlay markup for the front-end --- .../src/post-featured-image/index.php | 68 +++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 5e7ed6f70332d..03d8a3e803406 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -19,10 +19,11 @@ 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 = get_block_core_post_featured_image_border_attributes( $attributes ); + $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 ); + $background_element_markup = get_block_core_post_featured_image_background_element_markup( $attributes ); if ( $is_link ) { $attr['alt'] = $post_title; @@ -42,7 +43,7 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) $has_width = ! empty( $attributes['width'] ); $has_height = ! empty( $attributes['height'] ); if ( ! $has_height && ! $has_width ) { - return "
$featured_image
"; + return "
{$background_element_markup}{$featured_image}
"; } if ( $has_width ) { @@ -57,7 +58,62 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) $featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image ); } - return "
$featured_image
"; + return "
{$background_element_markup}{$featured_image}
"; +} + +/** + * Generate markup for the HTML element that will be used for the overlay. + * + * @param array $attributes Block attributes. + * + * @return string HTML markup in string format. + */ +function get_block_core_post_featured_image_background_element_markup( $attributes ) { + $has_dim_background = isset( $attributes['dimRatio'] ) && $attributes['dimRatio']; + $has_gradient = ( isset( $attributes['gradient'] ) && $attributes['gradient'] ); + $has_custom_gradient = ( isset( $attributes['customGradient'] ) && $attributes['customGradient'] ); + $class_names = array( + 'wp-block-post-featured-image__background', + ); + $styles_properties = array(); + + if ( ! $has_dim_background && ! $has_gradient && ! $has_custom_gradient ) { + return ''; + } + + if ( $has_dim_background ) { + $class_names[] = 'has-background-dim'; + $class_names[] = "has-background-dim-{$attributes['dimRatio']}"; + } + + if ( $has_gradient || $has_custom_gradient ) { + $class_names[] = 'has-background-gradient'; + } + + if ( $has_gradient ) { + $class_names[] = "has-{$attributes['gradient']}-gradient-background"; + } + + if ( ! empty( $attributes['style']['border']['radius'] ) ) { + $styles_properties['border-radius'] = $attributes['style']['border']['radius']; + } + + if ( ! empty( $attributes['style']['border']['width'] ) ) { + $styles_properties['border-width'] = $attributes['style']['border']['width']; + } + + $styles = ''; + + foreach ( $styles_properties as $style_attribute => $style_attribute_value ) { + $styles .= "{$style_attribute}: $style_attribute_value; "; + } + + return sprintf( + '', + esc_attr( implode( ' ', $class_names ) ), + esc_attr( trim( $styles ) ) + ); + } /** From 1bbb77b97b076df8a80d0ffc466f2f1504958748 Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Fri, 2 Sep 2022 23:11:45 +0530 Subject: [PATCH 04/20] Feat: Add styles for the overlay --- .../src/post-featured-image/style.scss | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/block-library/src/post-featured-image/style.scss b/packages/block-library/src/post-featured-image/style.scss index 7af2f5331c82c..ebaed9186cbb3 100644 --- a/packages/block-library/src/post-featured-image/style.scss +++ b/packages/block-library/src/post-featured-image/style.scss @@ -16,4 +16,26 @@ &.alignfull img { width: 100%; } + + .wp-block-post-featured-image__background { + &.has-background-dim { + position: absolute; + inset: 0; + background-color: $black; + + @at-root .wp-block-post-featured-image { + position: relative; + } + } + + &.has-background-gradient { + background-color: transparent; + } + + @for $i from 0 through 10 { + &.has-background-dim-#{ $i * 10 } { + opacity: $i * 0.1; + } + } + } } From 9725c48b661430dc943d438b232f7b98029b78cb Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Fri, 2 Sep 2022 23:21:17 +0530 Subject: [PATCH 05/20] Feat: Add custom gradient on front-end --- packages/block-library/src/post-featured-image/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 03d8a3e803406..9c2cf488a51d3 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -102,6 +102,10 @@ function get_block_core_post_featured_image_background_element_markup( $attribut $styles_properties['border-width'] = $attributes['style']['border']['width']; } + if ( $has_custom_gradient ) { + $styles_properties['background-image'] = $attributes['customGradient']; + } + $styles = ''; foreach ( $styles_properties as $style_attribute => $style_attribute_value ) { From d45e19154322eeabfd000fefca30526f00378ee2 Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Sat, 3 Sep 2022 00:51:42 +0530 Subject: [PATCH 06/20] Refact: Use overlay instead of background for variable names --- .../src/post-featured-image/edit.js | 2 +- .../src/post-featured-image/index.php | 18 +++++++++--------- .../src/post-featured-image/style.scss | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index b15a8aa45d7a7..ae665f0e217aa 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -338,7 +338,7 @@ function PostFeaturedImageDisplay( {