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 for adding link in featured image [ Latest Posts block ] #24548

Merged
merged 5 commits into from
Aug 18, 2020
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/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
"featuredImageSizeHeight": {
"type": "number",
"default": null
},
"addLinkToFeaturedImage": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
89 changes: 60 additions & 29 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class LatestPostsEdit extends Component {
featuredImageSizeSlug,
featuredImageSizeWidth,
featuredImageSizeHeight,
addLinkToFeaturedImage,
} = attributes;
const categorySuggestions = categoriesList.reduce(
( accumulator, category ) => ( {
Expand Down Expand Up @@ -264,6 +265,15 @@ class LatestPostsEdit extends Component {
isCollapsed={ false }
/>
</BaseControl>
<ToggleControl
label={ __( 'Add link to featured image' ) }
checked={ addLinkToFeaturedImage }
onChange={ ( value ) =>
setAttributes( {
addLinkToFeaturedImage: value,
} )
}
/>
</>
) }
</PanelBody>
Expand Down Expand Up @@ -390,12 +400,28 @@ class LatestPostsEdit extends Component {
excerptElement.innerText ||
'';

const imageSourceUrl = post.featuredImageSourceUrl;

const {
featuredImageInfo: {
url: imageSourceUrl,
alt: featuredImageAlt,
} = {},
} = post;
const imageClasses = classnames( {
'wp-block-latest-posts__featured-image': true,
[ `align${ featuredImageAlign }` ]: !! featuredImageAlign,
} );
const renderFeaturedImage =
displayFeaturedImage && imageSourceUrl;
const featuredImage = renderFeaturedImage && (
<img
src={ imageSourceUrl }
alt={ featuredImageAlt }
style={ {
maxWidth: featuredImageSizeWidth,
maxHeight: featuredImageSizeHeight,
} }
/>
);

const needsReadMore =
excerptLength <
Expand Down Expand Up @@ -424,17 +450,18 @@ class LatestPostsEdit extends Component {

return (
<li key={ i }>
{ displayFeaturedImage && (
{ renderFeaturedImage && (
<div className={ imageClasses }>
{ imageSourceUrl && (
<img
src={ imageSourceUrl }
alt=""
style={ {
maxWidth: featuredImageSizeWidth,
maxHeight: featuredImageSizeHeight,
} }
/>
{ addLinkToFeaturedImage ? (
<a
href={ post.link }
target="_blank"
draganescu marked this conversation as resolved.
Show resolved Hide resolved
rel="noreferrer noopener"
draganescu marked this conversation as resolved.
Show resolved Hide resolved
>
{ featuredImage }
</a>
) : (
featuredImage
) }
</div>
) }
Expand Down Expand Up @@ -542,24 +569,28 @@ export default withSelect( ( select, props ) => {
latestPosts: ! Array.isArray( posts )
? posts
: posts.map( ( post ) => {
if ( post.featured_media ) {
const image = getMedia( post.featured_media );
let url = get(
image,
[
'media_details',
'sizes',
featuredImageSizeSlug,
'source_url',
],
null
);
if ( ! url ) {
url = get( image, 'source_url', null );
}
return { ...post, featuredImageSourceUrl: url };
if ( ! post.featured_media ) return post;

const image = getMedia( post.featured_media );
let url = get(
image,
[
'media_details',
'sizes',
featuredImageSizeSlug,
'source_url',
],
null
);
if ( ! url ) {
url = get( image, 'source_url', null );
}
return post;
const featuredImageInfo = {
url,
// eslint-disable-next-line camelcase
alt: image?.alt_text,
};
return { ...post, featuredImageInfo };
} ),
};
} )( LatestPostsEdit );
25 changes: 17 additions & 8 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function render_block_core_latest_posts( $attributes ) {
$list_items_markup = '';

foreach ( $recent_posts as $post ) {
$post_link = esc_url( get_permalink( $post ) );

$list_items_markup .= '<li>';

Expand All @@ -75,16 +76,24 @@ function render_block_core_latest_posts( $attributes ) {
$image_classes .= ' align' . $attributes['featuredImageAlign'];
}

$featured_image = get_the_post_thumbnail(
$post,
$attributes['featuredImageSizeSlug'],
array(
'style' => $image_style,
)
);
if ( $attributes['addLinkToFeaturedImage'] ) {
$featured_image = sprintf(
'<a href="%1$s">%2$s</a>',
$post_link,
$featured_image
);
}
$list_items_markup .= sprintf(
'<div class="%1$s">%2$s</div>',
$image_classes,
get_the_post_thumbnail(
$post,
$attributes['featuredImageSizeSlug'],
array(
'style' => $image_style,
)
)
$featured_image
);
}

Expand All @@ -94,7 +103,7 @@ function render_block_core_latest_posts( $attributes ) {
}
$list_items_markup .= sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( get_permalink( $post ) ),
$post_link,
$title
);

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/latest-posts/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
}

.wp-block-latest-posts__featured-image {
a {
display: inline-block;
}
img {
height: auto;
width: auto;
Expand Down
47 changes: 24 additions & 23 deletions packages/e2e-tests/fixtures/blocks/core__latest-posts.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
[
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": false,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": false,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
"addLinkToFeaturedImage": false
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
[
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": true,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": true,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
]