Skip to content

Commit

Permalink
DataViews Extensibility: Allow unregistering the view post revisions …
Browse files Browse the repository at this point in the history
…action (#64464)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent 7502c6a commit ea0bcf1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
6 changes: 6 additions & 0 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ interface ActionBase< Item > {
* Whether the action can be used as a bulk action.
*/
supportsBulk?: boolean;

/**
* The context in which the action is visible.
* This is only a "meta" information for now.
*/
context?: 'list' | 'single';
}

export interface RenderModalProps< Item > {
Expand Down
40 changes: 1 addition & 39 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
* WordPress dependencies
*/
import { external } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { useMemo, useEffect } from '@wordpress/element';

/**
Expand All @@ -31,40 +30,6 @@ const viewPostAction = {
},
};

const postRevisionsAction = {
id: 'view-post-revisions',
context: 'list',
label( items ) {
const revisionsCount =
items[ 0 ]._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0;
return sprintf(
/* translators: %s: number of revisions */
__( 'View revisions (%s)' ),
revisionsCount
);
},
isEligible: ( post ) => {
if ( post.status === 'trash' ) {
return false;
}
const lastRevisionId =
post?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null;
const revisionsCount =
post?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0;
return lastRevisionId && revisionsCount > 1;
},
callback( posts, { onActionPerformed } ) {
const post = posts[ 0 ];
const href = addQueryArgs( 'revision.php', {
revision: post?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id,
} );
document.location.href = href;
if ( onActionPerformed ) {
onActionPerformed( posts );
}
},
};

export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions, postTypeObject } = useSelect(
( select ) => {
Expand All @@ -84,15 +49,13 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
}, [ registerPostTypeActions, postType ] );

const isLoaded = !! postTypeObject;
const supportsRevisions = !! postTypeObject?.supports?.revisions;
return useMemo( () => {
if ( ! isLoaded ) {
return [];
}

let actions = [
postTypeObject?.viewable && viewPostAction,
supportsRevisions && postRevisionsAction,
...defaultActions,
].filter( Boolean );
// Filter actions based on provided context. If not provided
Expand Down Expand Up @@ -161,7 +124,6 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
postTypeObject?.viewable,
onActionPerformed,
isLoaded,
supportsRevisions,
context,
] );
}
47 changes: 47 additions & 0 deletions packages/editor/src/dataviews/actions/view-post-revisions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';
import { __, sprintf } from '@wordpress/i18n';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import type { Post } from '../types';

const viewPostRevisions: Action< Post > = {
id: 'view-post-revisions',
context: 'list',
label( items ) {
const revisionsCount =
items[ 0 ]._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0;
return sprintf(
/* translators: %s: number of revisions */
__( 'View revisions (%s)' ),
revisionsCount
);
},
isEligible( post ) {
if ( post.status === 'trash' ) {
return false;
}
const lastRevisionId =
post?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id ?? null;
const revisionsCount =
post?._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0;
return !! lastRevisionId && revisionsCount > 1;
},
callback( posts, { onActionPerformed } ) {
const post = posts[ 0 ];
const href = addQueryArgs( 'revision.php', {
revision: post?._links?.[ 'predecessor-version' ]?.[ 0 ]?.id,
} );
document.location.href = href;
if ( onActionPerformed ) {
onActionPerformed( posts );
}
},
};

export default viewPostRevisions;
4 changes: 4 additions & 0 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { PostType } from '../types';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import duplicatePost from '../actions/duplicate-post';
import viewPostRevisions from '../actions/view-post-revisions';

export function registerEntityAction< Item >(
kind: string,
Expand Down Expand Up @@ -88,6 +89,9 @@ export const registerPostTypeActions =
.getCurrentTheme();

const actions = [
!! postTypeConfig?.supports?.revisions
? viewPostRevisions
: undefined,
// @ts-ignore
globalThis.IS_GUTENBERG_PLUGIN
? ! [ 'wp_template', 'wp_block', 'wp_template_part' ].includes(
Expand Down
9 changes: 8 additions & 1 deletion packages/editor/src/dataviews/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export interface CommonPost {
type: string;
id: string | number;
blocks?: Object[];
_links?: Links;
}

interface Links {
'predecessor-version'?: { href: string; id: number }[];
'version-history'?: { href: string; count: number }[];
[ key: string ]: { href: string }[] | undefined;
}

export interface BasePost extends CommonPost {
Expand All @@ -27,7 +34,6 @@ export interface BasePost extends CommonPost {
featured_media?: number;
menu_order?: number;
ping_status?: 'open' | 'closed';
_links?: Record< string, { href: string }[] >;
}

export interface Template extends CommonPost {
Expand Down Expand Up @@ -69,6 +75,7 @@ export interface PostType {
supports?: {
'page-attributes'?: boolean;
title?: boolean;
revisions?: boolean;
};
}

Expand Down

0 comments on commit ea0bcf1

Please sign in to comment.