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

DataViews Extensibility: Allow unregistering the duplicate pattern action #64373

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { store as coreStore } from '@wordpress/core-data';
import { __, sprintf, _x } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { parse } from '@wordpress/blocks';
import { DataForm } from '@wordpress/dataviews';
import {
Expand All @@ -31,10 +30,6 @@ import { unlock } from '../../lock-unlock';
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
import { getItemTitle } from '../../dataviews/actions/utils';

// Patterns.
const { CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

// TODO: this should be shared with other components (see post-fields in edit-site).
const fields = [
{
Expand Down Expand Up @@ -268,27 +263,6 @@ const useDuplicatePostAction = ( postType ) => {
);
};

export const duplicatePatternAction = {
id: 'duplicate-pattern',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type !== TEMPLATE_PART_POST_TYPE,
modalHeader: _x( 'Duplicate pattern', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const duplicatedProps = useDuplicatePatternProps( {
pattern: item,
onSuccess: () => closeModal(),
} );
return (
<CreatePatternModalContents
onClose={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
{ ...duplicatedProps }
/>
);
},
};

export const duplicateTemplatePartAction = {
id: 'duplicate-template-part',
label: _x( 'Duplicate', 'action label' ),
Expand Down Expand Up @@ -383,7 +357,6 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
isTemplateOrTemplatePart &&
userCanCreatePostType &&
duplicateTemplatePartAction,
isPattern && userCanCreatePostType && duplicatePatternAction,
...defaultActions,
].filter( Boolean );
// Filter actions based on provided context. If not provided
Expand Down
40 changes: 40 additions & 0 deletions packages/editor/src/dataviews/actions/duplicate-pattern.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { _x } from '@wordpress/i18n';
// @ts-ignore
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import type { Pattern } from '../types';

// Patterns.
const { CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

const duplicatePattern: Action< Pattern > = {
id: 'duplicate-pattern',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type !== 'wp_template_part',
modalHeader: _x( 'Duplicate pattern', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const duplicatedProps = useDuplicatePatternProps( {
pattern: item,
onSuccess: () => closeModal?.(),
} );
return (
<CreatePatternModalContents
onClose={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
{ ...duplicatedProps }
/>
);
},
};

export default duplicatePattern;
11 changes: 11 additions & 0 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { doAction } from '@wordpress/hooks';
* Internal dependencies
*/
import deletePost from '../actions/delete-post';
import duplicatePattern from '../actions/duplicate-pattern';
import exportPattern from '../actions/export-pattern';
import resetPost from '../actions/reset-post';
import trashPost from '../actions/trash-post';
Expand Down Expand Up @@ -74,7 +75,17 @@ export const registerPostTypeActions =
.resolveSelect( coreStore )
.getPostType( postType ) ) as PostType;

const canCreate = await registry
.resolveSelect( coreStore )
.canUser( 'create', {
kind: 'postType',
name: postType,
} );
Comment on lines +78 to +83
Copy link
Member

Choose a reason for hiding this comment

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

This probably needs to be available as part of an item's permissions object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure to be honest but I can be convinced :P

The permissions are specific to the current item. This is not, this is the ability to create an item.


const actions = [
canCreate && postTypeConfig.slug === 'wp_block'
? duplicatePattern
: undefined,
postTypeConfig.supports?.title ? renamePost : undefined,
postTypeConfig?.supports?.[ 'page-attributes' ]
? reorderPage
Expand Down
Loading