Skip to content

Commit

Permalink
Add option for adding link in featured image [ Latest Posts block ] (#…
Browse files Browse the repository at this point in the history
…24548)

* add option for adding link in featured image

* rename variable

* fix php lint errors

* change spaces to tabs

* add alt text in featured image (editor)
  • Loading branch information
ntsekouras committed Aug 18, 2020
1 parent 10e25f6 commit ba2d60a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 83 deletions.
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"
rel="noreferrer noopener"
>
{ 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
},
"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,
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
]

0 comments on commit ba2d60a

Please sign in to comment.