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

[Block Library - Latest Posts]: Prevent opening the links in editor #40593

Merged
merged 4 commits into from May 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/block-library/src/latest-posts/edit.js
Expand Up @@ -28,9 +28,11 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { pin, list, grid } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticeStore } from '@wordpress/notices';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -66,6 +68,7 @@ function getFeaturedImageDetails( post, size ) {
}

export default function LatestPostsEdit( { attributes, setAttributes } ) {
const instanceId = useInstanceId( LatestPostsEdit );
const {
postsToShow,
order,
Expand Down Expand Up @@ -148,6 +151,20 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
]
);

// If a user clicks to a link prevent redirection and show a warning.
const { createWarningNotice, removeNotice } = useDispatch( noticeStore );
let noticeId;
const showRedirectionPreventedNotice = ( event ) => {
event.preventDefault();
// Remove previous warning if any, to show one at a time per block.
removeNotice( noticeId );
noticeId = `block-library/core/latest-posts/redirection-prevented/${ instanceId }`;
createWarningNotice( __( 'Links are disabled in the editor.' ), {
id: noticeId,
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
type: 'snackbar',
} );
};

const imageSizeOptions = imageSizes
.filter( ( { slug } ) => slug !== 'full' )
.map( ( { name, slug } ) => ( {
Expand Down Expand Up @@ -466,7 +483,11 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
.join( ' ' ) }
{ /* translators: excerpt truncation character, default … */ }
{ __( ' … ' ) }
<a href={ post.link } rel="noopener noreferrer">
<a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other places, we don't use real links at all. Example:

href="#comment-reply-pseudo-link"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been discussed thoroughly to find the best accessible solution for this and we'll probably replace this pseudo-links with the approach of this PR.

href={ post.link }
rel="noopener noreferrer"
onClick={ showRedirectionPreventedNotice }
>
{ __( 'Read more' ) }
</a>
</>
Expand All @@ -483,6 +504,9 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
className="wp-block-latest-posts__post-title"
href={ post.link }
rel="noreferrer noopener"
onClick={
showRedirectionPreventedNotice
}
>
{ featuredImage }
</a>
Expand All @@ -501,6 +525,7 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
}
: undefined
}
onClick={ showRedirectionPreventedNotice }
>
{ ! titleTrimmed ? __( '(no title)' ) : null }
</a>
Expand Down