Skip to content

Commit

Permalink
Add rel attribute for Social Icons (#45469)
Browse files Browse the repository at this point in the history
Closes #38630
  • Loading branch information
George Hotelling authored Nov 2, 2022
1 parent 80f919d commit 559d498
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ Display an icon linking to a social media profile or site. ([Source](https://git
- **Name:** core/social-link
- **Category:** widgets
- **Supports:** ~~html~~, ~~reusable~~
- **Attributes:** label, service, url
- **Attributes:** label, rel, service, url

## Social Icons

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/social-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
},
"label": {
"type": "string"
},
"rel": {
"type": "string"
}
},
"usesContext": [
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const SocialLinkEdit = ( {
isSelected,
setAttributes,
} ) => {
const { url, service, label } = attributes;
const { url, service, label, rel } = attributes;
const { showLabels, iconColorValue, iconBackgroundColorValue } = context;
const [ showURLPopover, setPopover ] = useState( false );
const classes = classNames( 'wp-social-link', 'wp-social-link-' + service, {
Expand Down Expand Up @@ -113,6 +113,13 @@ const SocialLinkEdit = ( {
</PanelRow>
</PanelBody>
</InspectorControls>
<InspectorControls __experimentalGroup="advanced">
<TextControl
label={ __( 'Link rel' ) }
value={ rel || '' }
onChange={ ( value ) => setAttributes( { rel: value } ) }
/>
</InspectorControls>
<li { ...blockProps }>
<Button
className="wp-block-social-link-anchor"
Expand Down
18 changes: 11 additions & 7 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function render_block_core_social_link( $attributes, $content, $block ) {
$service = ( isset( $attributes['service'] ) ) ? $attributes['service'] : 'Icon';
$url = ( isset( $attributes['url'] ) ) ? $attributes['url'] : false;
$label = ( isset( $attributes['label'] ) ) ? $attributes['label'] : block_core_social_link_get_name( $service );
$rel = ( isset( $attributes['rel'] ) ) ? $attributes['rel'] : '';
$show_labels = array_key_exists( 'showLabels', $block->context ) ? $block->context['showLabels'] : false;

// Don't render a link if there is no URL set.
Expand All @@ -43,11 +44,6 @@ function render_block_core_social_link( $attributes, $content, $block ) {
$url = 'https://' . $url;
}

$rel_target_attributes = '';
if ( $open_in_new_tab ) {
$rel_target_attributes = 'rel="noopener nofollow" target="_blank"';
}

$icon = block_core_social_link_get_icon( $service );
$wrapper_attributes = get_block_wrapper_attributes(
array(
Expand All @@ -57,13 +53,21 @@ function render_block_core_social_link( $attributes, $content, $block ) {
);

$link = '<li ' . $wrapper_attributes . '>';
$link .= '<a href="' . esc_url( $url ) . '" ' . $rel_target_attributes . ' class="wp-block-social-link-anchor">';
$link .= '<a href="' . esc_url( $url ) . '" class="wp-block-social-link-anchor">';
$link .= $icon;
$link .= '<span class="wp-block-social-link-label' . ( $show_labels ? '' : ' screen-reader-text' ) . '">';
$link .= esc_html( $label );
$link .= '</span></a></li>';

return $link;
$w = new WP_HTML_Tag_Processor( $link );
$w->next_tag( 'a' );
if ( $open_in_new_tab ) {
$w->set_attribute( 'rel', esc_attr( $rel ) . ' noopener nofollow' );
$w->set_attribute( 'target', '_blank' );
} elseif ( '' !== $rel ) {
$w->set_attribute( 'rel', esc_attr( $rel ) );
}
return $w;
}

/**
Expand Down

0 comments on commit 559d498

Please sign in to comment.