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

Post Featured Image: Add link target and rel attributes #42853

Merged
merged 1 commit into from
Aug 1, 2022
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ Display a post's featured image. ([Source](https://github.com/WordPress/gutenber
- **Name:** core/post-featured-image
- **Category:** theme
- **Supports:** align (center, full, left, right, wide), color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~
- **Attributes:** height, isLink, scale, sizeSlug, width
- **Attributes:** height, isLink, linkTarget, rel, scale, sizeSlug, width

## Post Navigation Link

Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
},
"sizeSlug": {
"type": "string"
},
"rel": {
"type": "string",
"attribute": "rel",
"default": ""
},
"linkTarget": {
"type": "string",
"default": "_self"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
24 changes: 23 additions & 1 deletion packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PanelBody,
Placeholder,
Button,
TextControl,
} from '@wordpress/components';
import {
InspectorControls,
Expand Down Expand Up @@ -53,7 +54,8 @@ function PostFeaturedImageDisplay( {
context: { postId, postType: postTypeSlug, queryId },
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { isLink, height, width, scale, sizeSlug } = attributes;
const { isLink, height, width, scale, sizeSlug, rel, linkTarget } =
attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postTypeSlug,
Expand Down Expand Up @@ -128,6 +130,26 @@ function PostFeaturedImageDisplay( {
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<>
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
<TextControl
label={ __( 'Link rel' ) }
value={ rel }
onChange={ ( newRel ) =>
setAttributes( { rel: newRel } )
}
/>
</>
) }
</PanelBody>
</InspectorControls>
</>
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$wrapper_attributes = get_block_wrapper_attributes();
if ( $is_link ) {
$featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
$link_target = $attributes['linkTarget'];
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
$featured_image = sprintf( '<a href="%1$s" target="%2$s" %3$s>%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $link_target ), $rel, $featured_image );
}

$has_width = ! empty( $attributes['width'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"isValid": true,
"attributes": {
"isLink": false,
"scale": "cover"
"scale": "cover",
"rel": "",
"linkTarget": "_self"
},
"innerBlocks": []
}
Expand Down