From 6da22023b3e32fda67457c2cb8758b1f55adcb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Fri, 18 Sep 2020 22:28:02 +0200 Subject: [PATCH] add target and rel setting in SocialLink Block --- .../block-library/src/social-link/block.json | 6 ++ .../block-library/src/social-link/edit.js | 94 +++++++++++++------ .../block-library/src/social-link/index.php | 4 +- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/social-link/block.json b/packages/block-library/src/social-link/block.json index b525d267a1171..9a9e9b4a155bd 100644 --- a/packages/block-library/src/social-link/block.json +++ b/packages/block-library/src/social-link/block.json @@ -13,6 +13,12 @@ }, "label": { "type": "string" + }, + "linkTarget": { + "type": "string" + }, + "rel": { + "type": "string" } }, "supports": { diff --git a/packages/block-library/src/social-link/edit.js b/packages/block-library/src/social-link/edit.js index 99183622dd0c9..f0afd1bb5790f 100644 --- a/packages/block-library/src/social-link/edit.js +++ b/packages/block-library/src/social-link/edit.js @@ -8,9 +8,8 @@ import classNames from 'classnames'; */ import { InspectorControls, - URLPopover, - URLInput, __experimentalBlock as Block, + __experimentalLinkControl as LinkControl, } from '@wordpress/block-editor'; import { Fragment, useState } from '@wordpress/element'; import { @@ -18,18 +17,21 @@ import { PanelBody, PanelRow, TextControl, + ToggleControl, + Popover, } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; -import { keyboardReturn } from '@wordpress/icons'; /** * Internal dependencies */ import { getIconBySite, getNameBySite } from './social-list'; +const NEW_TAB_REL = 'noreferrer noopener'; + const SocialLinkEdit = ( { attributes, setAttributes, isSelected } ) => { - const { url, service, label } = attributes; - const [ showURLPopover, setPopover ] = useState( false ); + const { url, service, label, rel, linkTarget } = attributes; + const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false ); const classes = classNames( 'wp-social-link', 'wp-social-link-' + service, { 'wp-social-link__is-incomplete': ! url, } ); @@ -37,6 +39,28 @@ const SocialLinkEdit = ( { attributes, setAttributes, isSelected } ) => { const IconComponent = getIconBySite( service ); const socialLinkName = getNameBySite( service ); + const onToggleOpenInNewTab = ( value ) => { + const newLinkTarget = value ? '_blank' : undefined; + + let updatedRel = rel; + if ( newLinkTarget && ! rel ) { + updatedRel = NEW_TAB_REL; + } else if ( ! newLinkTarget && rel === NEW_TAB_REL ) { + updatedRel = undefined; + } + + setAttributes( { + linkTarget: newLinkTarget, + rel: updatedRel, + } ); + }; + + const onSetLinkRel = ( value ) => { + setAttributes( { rel: value } ); + }; + + const opensInNewTab = linkTarget === '_blank'; + return ( @@ -61,36 +85,44 @@ const SocialLinkEdit = ( { attributes, setAttributes, isSelected } ) => { /> + + + + - diff --git a/packages/block-library/src/social-link/index.php b/packages/block-library/src/social-link/index.php index dc732a7a1f90e..0968765aef184 100644 --- a/packages/block-library/src/social-link/index.php +++ b/packages/block-library/src/social-link/index.php @@ -16,6 +16,8 @@ function render_block_core_social_link( $attributes ) { $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 ); + $target = ( isset( $attributes['linkTarget'] ) ) ? 'target="' . esc_attr( $attributes['linkTarget'] ) . '"' : ''; + $rel = ( isset( $attributes['rel'] ) ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : ''; $class_name = isset( $attributes['className'] ) ? ' ' . $attributes['className'] : false; // Don't render a link if there is no URL set. @@ -24,7 +26,7 @@ function render_block_core_social_link( $attributes ) { } $icon = block_core_social_link_get_icon( $service ); - return ''; + return ''; } /**