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

[ アイコン ]ツールバーからのリンク指定機能にnoreferrer、nofollow、link descriptionオプションを追加 #2418

Merged
merged 9 commits into from
Jan 30, 2025
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ e.g.
1. VK Blocks examples.

== Changelog ==
[ Add function ][ Slider ] Added noreferrer, nofollow, and link description options to the link feature.

[ Add function ][ Icon / Slider ] Added noreferrer, nofollow, and link description options to the link feature.

= 1.94.2 =
[ Bug fix / Specification Change ][ Visual Embed ] Strengthened validation for XSS protection and restricted the range of allowed URLs.
Expand Down
8 changes: 8 additions & 0 deletions src/blocks/icon/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
"iconTarget": {
"type": "Boolean",
"default": false
},
"relAttribute": {
"type": "string",
"default": ""
},
"linkDescription": {
"type": "string",
"default": ""
}
},
"description": "Display icons with Font Awesome.",
Expand Down
16 changes: 10 additions & 6 deletions src/blocks/icon/component.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import parse from 'html-react-parser';
import { isHexColor } from '@vkblocks/utils/is-hex-color';
Expand All @@ -16,6 +17,8 @@ export class VKBIcon extends Component {
const iconFontColor = this.props.lbFontColor;
const iconUrl = this.props.lbUrl;
const iconTarget = this.props.lbTarget;
const relAttribute = this.props.lbRelAttribute;
const linkDescription = this.props.lbLinkDescription;

// outer & align
let outerClass = 'vk_icon_frame';
Expand Down Expand Up @@ -145,16 +148,17 @@ export class VKBIcon extends Component {
let blockContentWrapper = '';
if (iconUrl !== null && iconUrl !== undefined && iconUrl !== '') {
blockContentWrapper = (
/*
target=_blankで指定すると、WordPressが自動でnoopener noreferrerを付与する。
ブロックでもrelを付与しないとブロックが壊れる。
*/
<a
href={iconUrl}
className="vk_icon_link"
target={iconTarget && '_blank'}
rel={iconTarget && 'noopener noreferrer'}
{...(iconTarget ? { target: '_blank' } : {})}
{...(relAttribute ? { rel: relAttribute } : {})}
>
<span className="screen-reader-text">
{linkDescription
? linkDescription
: __('Icon link', 'vk-blocks-pro')}
</span>
{blockContent}
</a>
);
Expand Down
171 changes: 171 additions & 0 deletions src/blocks/icon/deprecated/1.94.2/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { Component } from '@wordpress/element';
import parse from 'html-react-parser';
import { isHexColor } from '@vkblocks/utils/is-hex-color';

export class VKBIcon extends Component {
render() {
let fontAwesomeIcon = this.props.lbFontAwesomeIcon;
const iconSize = this.props.lbSize;
const iconSizeUnit = this.props.lbSizeUnit;
const iconMargin = this.props.lbMargin;
const iconMarginUnit = this.props.lbMarginUnit;
const iconRadius = this.props.lbRadius;
const iconAlign = this.props.lbAlign;
const iconType = this.props.lbType;
const iconColor = this.props.lbColor;
const iconFontColor = this.props.lbFontColor;
const iconUrl = this.props.lbUrl;
const iconTarget = this.props.lbTarget;

// outer & align
let outerClass = 'vk_icon_frame';
if (iconAlign === 'center') {
outerClass += ' text-center';
} else if (iconAlign === 'right') {
outerClass += ' text-right';
}

// color style
let borderClass = 'vk_icon_border';
let borderStyle = {};

if (iconType === '0') {
// Solid color
if (
iconColor !== 'undefined' &&
iconColor !== null &&
iconColor !== undefined
) {
// Solid color
borderClass += ` has-background`;

if (isHexColor(iconColor)) {
// custom color
borderStyle = {
backgroundColor: `${iconColor}`,
};
} else {
// palette color
borderClass += ` has-${iconColor}-background-color`;
}
}
} else {
if (
iconColor !== 'undefined' &&
iconColor !== null &&
iconColor !== undefined
) {
borderClass += ` has-text-color`;

if (isHexColor(iconColor)) {
// custom color
borderStyle = {
color: `${iconColor}`,
};
} else {
// palette color
borderClass += ` has-${iconColor}-color`;
}
}

if (iconType === '1') {
// Icon & Frame
outerClass += ' is-style-outline';
} else {
// icon only
outerClass += ' is-style-noline';
}
}

// margin
if (
!(
iconSize === 36 &&
iconSizeUnit === 'px' &&
iconMargin === 22 &&
iconMarginUnit === 'px'
)
) {
borderStyle.width =
'calc(' +
(iconSize + iconSizeUnit) +
' + ' +
(iconMargin * 2 + iconMarginUnit) +
')';
borderStyle.height = borderStyle.width;
}

// border radius
if (iconRadius !== 50) {
borderStyle.borderRadius = iconRadius + `%`;
}

// icon font
let faIconTag = '';
if (fontAwesomeIcon) {
fontAwesomeIcon = fontAwesomeIcon.replace(/ fas/g, 'fas');

// font size
let fontStyle = ``;
let fontClass = ` vk_icon_font `;
if (!(iconSize === 36 && iconSizeUnit === 'px')) {
fontStyle = ` font-size:${iconSize}${iconSizeUnit};`;
}

// icon color
if (
iconFontColor !== 'undefined' &&
iconFontColor !== null &&
iconFontColor !== undefined
) {
if (isHexColor(iconFontColor)) {
// custom color
fontStyle += ` color:${iconFontColor};`;
} else {
// palette color
fontClass += ` has-text-color has-${iconFontColor}-color `;
}
}

// add class and inline css
const faIconFragment = fontAwesomeIcon.split(' ');
faIconFragment[0] = faIconFragment[0] + ` style="${fontStyle};"`;
faIconFragment[1] = ' ' + faIconFragment[1] + fontClass;
faIconTag = faIconFragment.join('');
}

const blockContent = (
<>
<div className={borderClass} style={borderStyle}>
{parse(faIconTag)}
</div>
</>
);

let blockContentWrapper = '';
if (iconUrl !== null && iconUrl !== undefined && iconUrl !== '') {
blockContentWrapper = (
/*
target=_blankで指定すると、WordPressが自動でnoopener noreferrerを付与する。
ブロックでもrelを付与しないとブロックが壊れる。
*/
<a
href={iconUrl}
className="vk_icon_link"
target={iconTarget && '_blank'}
rel={iconTarget && 'noopener noreferrer'}
>
{blockContent}
</a>
);
} else {
blockContentWrapper = blockContent;
}

return (
<>
<div className={outerClass}>{blockContentWrapper}</div>
</>
);
}
}
46 changes: 46 additions & 0 deletions src/blocks/icon/deprecated/1.94.2/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { VKBIcon } from './component';
import { useBlockProps } from '@wordpress/block-editor';

export default function save({ attributes }) {
let {
faIcon,
iconSize,
iconSizeUnit,
iconMargin,
iconMarginUnit,
iconRadius,
iconAlign,
iconType,
iconColor,
iconFontColor,
iconUrl,
iconTarget,
} = attributes;

if (faIcon && !faIcon.match(/<i/)) {
faIcon = `<i class="${faIcon}"></i>`;
}

const blockProps = useBlockProps.save({
className: `vk_icon`,
});

return (
<div {...blockProps}>
<VKBIcon
lbFontAwesomeIcon={faIcon}
lbSize={iconSize}
lbSizeUnit={iconSizeUnit}
lbMargin={iconMargin}
lbMarginUnit={iconMarginUnit}
lbRadius={iconRadius}
lbAlign={iconAlign}
lbType={iconType}
lbColor={iconColor}
lbFontColor={iconFontColor}
lbUrl={iconUrl}
lbTarget={iconTarget}
/>
</div>
);
}
38 changes: 31 additions & 7 deletions src/blocks/icon/deprecated/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import save1_13_2 from './1.13.2/save';
import save1_16_1 from './1.16.1/save';
import save1_94_2 from './1.94.2/save';

const blockAttributes = {
faIcon: {
Expand Down Expand Up @@ -48,26 +49,49 @@ const blockAttributes = {
},
};

/* 次回対応おねがいします
const blockAttributes2 = {
...blockAttributes,
iconColor: {
type: 'string',
},
iconUrl: {
type: 'string',
},
}

const blockAttributes3 = {
...blockAttributes2,
iconFontColor: {
type: 'string',
},
}
*/

const blockAttributes2 = {
...blockAttributes,
iconColor: {
/* 次回対応おねがいします
const blockAttributes4 = {
...blockAttributes3,
relAttribute: {
type: 'string',
default: ''
},
iconUrl: {
linkDescription: {
type: 'string',
},
default: ''
}
}
*/

export const deprecated = [
{
attributes: blockAttributes3,
save: save1_94_2,
migrate: (attributes) => {
return {
...attributes,
relAttribute: '',
linkDescription: '',
};
},
},
{
attributes: blockAttributes2,
save: save1_16_1,
Expand Down
10 changes: 10 additions & 0 deletions src/blocks/icon/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default function IconEdit(props) {
iconColor,
iconFontColor,
iconUrl,
relAttribute,
linkDescription,
iconTarget,
} = attributes;

Expand Down Expand Up @@ -280,6 +282,14 @@ export default function IconEdit(props) {
setLinkTarget={(target) =>
setAttributes({ iconTarget: !!target })
}
relAttribute={relAttribute}
setRelAttribute={(rel) =>
setAttributes({ relAttribute: rel })
}
linkDescription={linkDescription}
setLinkDescription={(description) =>
setAttributes({ linkDescription: description })
}
/>
</ToolbarGroup>
</BlockControls>
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/icon/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function save({ attributes }) {
iconFontColor,
iconUrl,
iconTarget,
relAttribute,
linkDescription,
} = attributes;

if (faIcon && !faIcon.match(/<i/)) {
Expand All @@ -40,6 +42,8 @@ export default function save({ attributes }) {
lbFontColor={iconFontColor}
lbUrl={iconUrl}
lbTarget={iconTarget}
lbRelAttribute={relAttribute}
lbLinkDescription={linkDescription}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:vk-blocks/icon {"iconFontColor":"#ffff00"} -->
<div class="wp-block-vk-blocks-icon vk_icon"><div class="vk_icon_frame"><div class="vk_icon_border"><i style="color:#ffff00" class="fas vk_icon_font fa-user"></i></div></div></div>
<!-- /wp:vk-blocks/icon -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:vk-blocks/icon {"iconFontColor":"#ffff00","iconUrl":"http://localhost:8888/","iconTarget":true,"relAttribute":"noreferrer nofollow","linkDescription":"test"} -->
<div class="wp-block-vk-blocks-icon vk_icon"><div class="vk_icon_frame"><a href="http://localhost:8888/" class="vk_icon_link" target="_blank" rel="noreferrer nofollow"><span class="screen-reader-text">test</span><div class="vk_icon_border"><i style="color:#ffff00" class="fas vk_icon_font fa-user"></i></div></a></div></div>
<!-- /wp:vk-blocks/icon -->
Loading