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

Add option to use Post Featured Image with consistent height #27545

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"isLink": {
"type": "boolean",
"default": false
},
"useAsCover": {
"type": "boolean",
"default": false
},
"minHeight": {
"type": "number"
},
"minHeightUnit": {
"type": "string"
}
},
"usesContext": [
Expand Down
36 changes: 36 additions & 0 deletions packages/block-library/src/post-featured-image/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Platform } from '@wordpress/element';

const isWeb = Platform.OS === 'web';
export const CSS_UNITS = [
{
value: 'px',
label: isWeb ? 'px' : __( 'Pixels (px)' ),
default: '300',
},
{
value: 'em',
label: isWeb ? 'em' : __( 'Relative to parent font size (em)' ),
default: '20',
},
{
value: 'rem',
label: isWeb ? 'rem' : __( 'Relative to root font size (rem)' ),
default: '20',
},
{
value: 'vw',
label: isWeb ? 'vw' : __( 'Viewport width (vw)' ),
default: '20',
},
{
value: 'vh',
label: isWeb ? 'vh' : __( 'Viewport height (vh)' ),
default: '50',
},
];
export const ALLOWED_MEDIA_TYPES = [ 'image' ];
export const COVER_MIN_HEIGHT = 50;
66 changes: 36 additions & 30 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { Icon, withNotices } from '@wordpress/components';
import {
Icon,
ToggleControl,
PanelBody,
withNotices,
} from '@wordpress/components';
import {
InspectorControls,
BlockControls,
MediaPlaceholder,
MediaReplaceFlow,
BlockIcon,
useBlockProps,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { postFeaturedImage } from '@wordpress/icons';

const ALLOWED_MEDIA_TYPES = [ 'image' ];
/**
* Internal dependencies
*/
import FeaturedImageInspectorControls from './inspector-controls';
import { ALLOWED_MEDIA_TYPES } from './constants';

const placeholderChip = (
<div className="post-featured-image_placeholder">
<Icon icon={ postFeaturedImage } />
Expand All @@ -29,12 +32,13 @@ const placeholderChip = (
);

function PostFeaturedImageDisplay( {
attributes: { isLink },
attributes,
setAttributes,
context: { postId, postType },
noticeUI,
noticeOperations,
} ) {
const { useAsCover, minHeight, minHeightUnit } = attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
Expand Down Expand Up @@ -73,33 +77,35 @@ function PostFeaturedImageDisplay( {
} }
/>
);
} else {
// We have a Featured image so show a Placeholder if is loading.
image = ! media ? (
placeholderChip
) : (
} else if ( ! media ) {
// We have a Featured image so show a Placeholder while it's loading.
image = placeholderChip;
} else if ( ! useAsCover ) {
image = (
<img
src={ media.source_url }
alt={ media.alt_text || __( 'Featured image' ) }
/>
);
}

const featuredImageMinHeight = minHeightUnit
? `${ minHeight }${ minHeightUnit }`
: minHeight;
const blockProps = useBlockProps( {
className: classnames( { 'is-used-as-cover': useAsCover } ),
style: {
backgroundImage:
useAsCover && `url(' ${ media?.[ 'source_url' ] }' )`,
minHeight: useAsCover && featuredImageMinHeight,
},
} );
return (
<>
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType
) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
<FeaturedImageInspectorControls
attributes={ attributes }
postType={ postType }
setAttributes={ setAttributes }
/>
<BlockControls>
{ !! media && (
<MediaReplaceFlow
Expand All @@ -112,7 +118,7 @@ function PostFeaturedImageDisplay( {
/>
) }
</BlockControls>
<div { ...useBlockProps() }>{ image }</div>
<div { ...blockProps }>{ image }</div>
</>
);
}
Expand Down
30 changes: 25 additions & 5 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
$post_ID = $block->context['postId'];

$featured_image = get_the_post_thumbnail( $post_ID );
$post_ID = $block->context['postId'];
$featured_image = '';
$classnames = array();
$styles = array();
if ( isset( $attributes['useAsCover'] ) && $attributes['useAsCover'] ) {
$featured_image_url = get_the_post_thumbnail_url( $post_ID );
if ( $featured_image_url ) {
$classnames[] = 'is-used-as-cover';
if ( isset( $attributes['minHeight'] ) ) {
$min_height_unit = $attributes['minHeightUnit'] ? $attributes['minHeightUnit'] : 'px';
$styles[] = "min-height:{$attributes['minHeight']}{$min_height_unit}";
}
$styles[] = "background-image:url('{$featured_image_url}')";
}
$featured_image = '';
} else {
$featured_image = get_the_post_thumbnail( $post_ID );
}

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && has_post_thumbnail( $post_ID ) ) {
$featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
}

$wrapper_attributes = get_block_wrapper_attributes();
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => implode( ' ', $classnames ),
'style' => implode( ';', $styles ),
)
);

return '<p ' . $wrapper_attributes . '>' . $featured_image . '</p>';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* WordPress dependencies
*/
import { BaseControl, PanelBody, ToggleControl } from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import {
InspectorControls,
__experimentalUnitControl as UnitControl,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { CSS_UNITS, COVER_MIN_HEIGHT } from './constants';

const CoverHeightInput = ( {
onChange,
onUnitChange,
unit = 'px',
value = '',
} ) => {
const inputId = useInstanceId(
UnitControl,
'block-post-featured-image-height-input'
);
const handleOnChange = ( unprocessedValue ) => {
const inputValue = parseInt( unprocessedValue, 10 );
if ( Number.isNaN( inputValue ) ) return;
onChange( inputValue );
};
const min = unit === 'px' ? COVER_MIN_HEIGHT : 0;
return (
<BaseControl label={ __( 'Minimum height of cover' ) } id={ inputId }>
<UnitControl
id={ inputId }
min={ min }
onChange={ handleOnChange }
onUnitChange={ onUnitChange }
step="1"
style={ { maxWidth: 80 } }
unit={ unit }
units={ CSS_UNITS }
value={ value }
isResetValueOnUnitChange
/>
</BaseControl>
);
};

const FeaturedImageInspectorControls = ( {
attributes: { useAsCover, minHeight, minHeightUnit, isLink },
setAttributes,
postType,
} ) => {
return (
<InspectorControls>
<PanelBody title={ __( 'Image settings' ) }>
<ToggleControl
label={ __( 'Use as cover' ) }
onChange={ () =>
setAttributes( { useAsCover: ! useAsCover } )
}
checked={ !! useAsCover }
/>
{ useAsCover && (
<CoverHeightInput
value={ minHeight }
unit={ minHeightUnit }
onChange={ ( newMinHeight ) =>
setAttributes( { minHeight: newMinHeight } )
}
onUnitChange={ ( newUnit ) =>
setAttributes( {
minHeightUnit: newUnit,
} )
}
/>
) }
</PanelBody>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType
) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ !! isLink }
/>
</PanelBody>
</InspectorControls>
);
};

export default FeaturedImageInspectorControls;
13 changes: 13 additions & 0 deletions packages/block-library/src/post-featured-image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@
max-width: 100%;
height: auto;
}
&.is-used-as-cover {
position: relative;
min-height: 300px;
background-position: center center;
background-size: cover;
a {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasmussen is there a better way to achieve full height/width link with no content?

Copy link
Contributor

Choose a reason for hiding this comment

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

would it make more sense to just wrap the image in an a tag?

position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
}
}
}