Skip to content

Commit

Permalink
Editor: Ensure Copy button in sidebar copies whole permalink, *with* …
Browse files Browse the repository at this point in the history
…URL protocol (WordPress#61876)

* PostURL: Copy whole permalink, *with* protocol

`usePostURLLabel` is meant to be used in labels and intentionally strips
the protocol of a URL. As a result, the copy button in the `PostURL`
component wrote something like "example.org/foobar" to the clipboard,
instead of "https://example.org/foobar".

As a fix, just grab the permalink directly.

* Docs: Fix grammar

* Fold permalink selection into existing useSelect call

* Filter through safeDecodeURIComponent -- you never know
  • Loading branch information
mcsf authored and patil-vipul committed Jun 17, 2024
1 parent 3da59e6 commit e4ffe5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ _Returns_

### getPermalinkParts

Returns the permalink for a post, split into it's three parts: the prefix, the postName, and the suffix.
Returns the permalink for a post, split into its three parts: the prefix, the postName, and the suffix.

_Parameters_

Expand Down
53 changes: 29 additions & 24 deletions packages/editor/src/components/post-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { useCopyToClipboard } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { usePostURLLabel } from './label';
import { store as editorStore } from '../../store';

/**
Expand All @@ -37,33 +36,39 @@ import { store as editorStore } from '../../store';
* @return {Component} The rendered PostURL component.
*/
export default function PostURL( { onClose } ) {
const { isEditable, postSlug, postLink, permalinkPrefix, permalinkSuffix } =
useSelect( ( select ) => {
const post = select( editorStore ).getCurrentPost();
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );
const permalinkParts = select( editorStore ).getPermalinkParts();
const hasPublishAction =
post?._links?.[ 'wp:action-publish' ] ?? false;
const {
isEditable,
postSlug,
postLink,
permalinkPrefix,
permalinkSuffix,
permalink,
} = useSelect( ( select ) => {
const post = select( editorStore ).getCurrentPost();
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );
const permalinkParts = select( editorStore ).getPermalinkParts();
const hasPublishAction = post?._links?.[ 'wp:action-publish' ] ?? false;

return {
isEditable:
select( editorStore ).isPermalinkEditable() &&
hasPublishAction,
postSlug: safeDecodeURIComponent(
select( editorStore ).getEditedPostSlug()
),
viewPostLabel: postType?.labels.view_item,
postLink: post.link,
permalinkPrefix: permalinkParts?.prefix,
permalinkSuffix: permalinkParts?.suffix,
};
}, [] );
return {
isEditable:
select( editorStore ).isPermalinkEditable() && hasPublishAction,
postSlug: safeDecodeURIComponent(
select( editorStore ).getEditedPostSlug()
),
viewPostLabel: postType?.labels.view_item,
postLink: post.link,
permalinkPrefix: permalinkParts?.prefix,
permalinkSuffix: permalinkParts?.suffix,
permalink: safeDecodeURIComponent(
select( editorStore ).getPermalink()
),
};
}, [] );
const { editPost } = useDispatch( editorStore );
const { createNotice } = useDispatch( noticesStore );
const [ forceEmptyField, setForceEmptyField ] = useState( false );
const postUrlLabel = usePostURLLabel();
const copyButtonRef = useCopyToClipboard( postUrlLabel, () => {
const copyButtonRef = useCopyToClipboard( permalink, () => {
createNotice( 'info', __( 'Copied URL to clipboard.' ), {
isDismissible: true,
type: 'snackbar',
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ export function getEditedPostSlug( state ) {
}

/**
* Returns the permalink for a post, split into it's three parts: the prefix,
* Returns the permalink for a post, split into its three parts: the prefix,
* the postName, and the suffix.
*
* @param {Object} state Editor state.
Expand Down

0 comments on commit e4ffe5b

Please sign in to comment.