RSS Block: Add option to open links in new tab/window and control rel attributes#69641
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| "addNofollow": { | ||
| "type": "boolean", | ||
| "default": false | ||
| }, | ||
| "additionalRelAttributes": { | ||
| "type": "string", | ||
| "default": "" |
There was a problem hiding this comment.
I think there's no need to have two separate attributes for rel. Maybe we should just display single text control, using a single attribute (linkRel) and let people input nofollow or any additional field as needed.
|
|
||
| if ( ! empty( $attributes['openInNewTab'] ) ) { | ||
| $link_attributes .= ' target="_blank"'; | ||
| $rel_attributes[] = 'noopener noreferrer'; |
There was a problem hiding this comment.
As far as I know, these values are no longer required. See related discussion in #26914.
|
@Mamaduka Thank you for the feedback! I've updated the implementation to address your suggestions:
|
There was a problem hiding this comment.
Thank you, @dhruvikpatel18!
I just remembered that the Social Icon block also has a custom "Rel" field, let's match UI and implementation (minus HTML API) to it.
P.S. Rebasing should resolve failing e2e tests.
| if ( ! empty( $rel_attributes ) ) { | ||
| $link_attributes .= ' rel="' . esc_attr( implode( ' ', array_unique( $rel_attributes ) ) ) . '"'; | ||
| } |
There was a problem hiding this comment.
I think this is unnecessary; you're already assigning value above.
PS There're some WPCS violations in this file that also need to be fixed.
| "type": "boolean", | ||
| "default": false | ||
| }, | ||
| "linkRel": { |
There was a problem hiding this comment.
| "linkRel": { | |
| "rel": { |
I just remembered that the Social Icons block also has a custom rel handler; let's make it consistent.
gutenberg/packages/block-library/src/social-link/block.json
Lines 22 to 24 in bb9028d
| $rel_attributes = explode( ' ', $attributes['linkRel'] ); | ||
| $rel_attributes = array_filter( array_map( 'sanitize_html_class', $rel_attributes ) ); | ||
|
|
||
| if ( ! empty( $rel_attributes ) ) { | ||
| $link_attributes .= ' rel="' . esc_attr( implode( ' ', $rel_attributes ) ) . '"'; | ||
| } |
There was a problem hiding this comment.
Do we really need all this sanitization for escaped value? Could this be something like:
$link_attributes .= ' rel="' . esc_attr( trim( $attributes['rel'] ) ) . '"';| $processor = new WP_HTML_Tag_Processor( "<a href='{$link}'>{$title}</a>" ); | ||
| $processor->next_tag( 'a' ); | ||
|
|
||
| if ( $open_in_new_tab ) { | ||
| $processor->set_attribute( 'target', '_blank' ); | ||
| $processor->set_attribute( 'rel', trim( $rel . ' noopener nofollow' ) ); | ||
| } elseif ( '' !== $rel ) { | ||
| $processor->set_attribute( 'rel', $rel ); | ||
| } | ||
|
|
||
| $title = "<div class='wp-block-rss__item-title'>" . $processor->get_updated_html() . "</div>"; |
There was a problem hiding this comment.
@dhruvikpatel18, I'm sorry if my previous comment was unclear, but as I mentioned, we don't need to use HTML API here.
There was a problem hiding this comment.
@Mamaduka Sorry for misunderstanding your previous feedback, Removed HTML API usage.
| $link_attributes .= ' rel="' . esc_attr( trim( $rel . ' noopener nofollow' ) ) . '"'; | ||
| } elseif ( '' !== $rel ) { | ||
| $link_attributes .= ' rel="' . esc_attr( trim( $rel ) ) . '"'; |
There was a problem hiding this comment.
I think we can drop noopener. See: #26914 (comment).
Additionally, $rel is already trimmed above, and another trim call seems unnecessary.
There was a problem hiding this comment.
I also realized that this leaves nofollow hardcoded, and users won't be able to remove it, if the link opens in a new tab. Not 100% sure what the desired flow is here.
There was a problem hiding this comment.
You're right about the hardcoded nofollow, actually it was there from previous implementation which i referred from social-link block.
|
@dhruvikpatel18, IIRC you can fix a failing JS unit test by running |
| if ( $open_in_new_tab ) { | ||
| $link_attributes .= ' target="_blank"'; | ||
|
|
||
| if ( '' !== $rel ) { | ||
| $link_attributes .= ' rel="' . esc_attr( $rel ) . '"'; | ||
| } | ||
| } elseif ( '' !== $rel ) { | ||
| $link_attributes .= ' rel="' . esc_attr( $rel ) . '"'; | ||
| } |
There was a problem hiding this comment.
Suggestion: The conditions here can be simplified.
if ( $open_in_new_tab ) {
$link_attributes .= ' target="_blank"';
}
if ( '' !== $rel ) {
$link_attributes .= ' rel="' . esc_attr( $rel ) . '"';
}
Mamaduka
left a comment
There was a problem hiding this comment.
Thank you, @dhruvikpatel18!
The implementation looks good. There's one last nitpick, but generally, I think this is good to merge.
| if ( $open_in_new_tab ) { | ||
| $link_attributes .= ' target="_blank"'; | ||
| } | ||
|
|
||
| if ( '' !== $rel ) { | ||
| $link_attributes .= ' rel="' . esc_attr( $rel ) . '"'; | ||
| } |
There was a problem hiding this comment.
One last item. I think we can move this outside of the loop. There's no need to recreate $link_attributes on each iteration. It doesn't require any values from $item.
There was a problem hiding this comment.
Yes, there is no need to recreate $link_attributes, I've updated it.
… attributes (WordPress#69641) Co-authored-by: dhruvikpatel18 <dhruvik18@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: bhubbard <bhubbard@git.wordpress.org>
Closes #49285
What?
This PR adds new options to the RSS block allowing users to:
Why?
When linking to external websites through an RSS feed, users often want the ability to:
Testing Instructions
Screenshots or screencast
RSS.block.mov