Skip to content

Commit 23f8409

Browse files
tyxlantsekouras
andauthored
Content Types: Introduce view items actions (#78104)
Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
1 parent 84ecc0f commit 23f8409

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import type { Action } from '@wordpress/dataviews';
5+
import { __ } from '@wordpress/i18n';
6+
import { external } from '@wordpress/icons';
7+
import { addQueryArgs } from '@wordpress/url';
8+
9+
/**
10+
* Internal dependencies
11+
*/
12+
import type { PostTypeFormData } from '../types';
13+
14+
const viewPostsAction: Action< PostTypeFormData > = {
15+
id: 'view-posts',
16+
label: __( 'View posts' ),
17+
icon: external,
18+
isPrimary: true,
19+
// Drafts are not registered with WordPress, so `edit.php?post_type=…`
20+
// would 404. Only surface the link for active post types.
21+
isEligible: ( item ) => item.status === 'publish',
22+
callback: ( items, { onActionPerformed } ) => {
23+
const item = items[ 0 ];
24+
if ( ! item?.slug ) {
25+
return;
26+
}
27+
document.location.href = addQueryArgs( 'edit.php', {
28+
post_type: item.slug,
29+
} );
30+
onActionPerformed?.( items );
31+
},
32+
};
33+
34+
export default viewPostsAction;

‎packages/content-types/src/post-types/list.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import activateAction from './actions/activate';
1515
import deactivateAction from './actions/deactivate';
1616
import deletePostTypeAction from './actions/delete';
1717
import duplicatePostTypeAction from './actions/duplicate';
18+
import viewPostsAction from './actions/view-posts';
1819
import {
1920
hasArchiveField,
2021
hierarchicalField,
@@ -52,6 +53,7 @@ export function PostTypesList() {
5253
editAction,
5354
quickEditPostTypeAction,
5455
duplicatePostTypeAction,
56+
viewPostsAction,
5557
activateAction,
5658
deactivateAction,
5759
deletePostTypeAction,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import type { Action } from '@wordpress/dataviews';
5+
import { __ } from '@wordpress/i18n';
6+
import { external } from '@wordpress/icons';
7+
import { addQueryArgs } from '@wordpress/url';
8+
9+
/**
10+
* Internal dependencies
11+
*/
12+
import type { TaxonomyFormData } from '../types';
13+
14+
const viewTermsAction: Action< TaxonomyFormData > = {
15+
id: 'view-terms',
16+
label: __( 'View terms' ),
17+
icon: external,
18+
isPrimary: true,
19+
// Drafts aren't registered, and `edit-tags.php` requires a `post_type`
20+
// query arg to render the term list, so the taxonomy must also be
21+
// attached to at least one post type.
22+
isEligible: ( item ) =>
23+
item.status === 'publish' &&
24+
Array.isArray( item.config.object_type ) &&
25+
item.config.object_type.length > 0,
26+
callback: ( items, { onActionPerformed } ) => {
27+
const item = items[ 0 ];
28+
const postType = item?.config.object_type?.[ 0 ];
29+
if ( ! item?.slug || ! postType ) {
30+
return;
31+
}
32+
document.location.href = addQueryArgs( 'edit-tags.php', {
33+
taxonomy: item.slug,
34+
post_type: postType,
35+
} );
36+
onActionPerformed?.( items );
37+
},
38+
};
39+
40+
export default viewTermsAction;

‎packages/content-types/src/taxonomies/list.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import activateAction from './actions/activate';
1515
import deactivateAction from './actions/deactivate';
1616
import deleteTaxonomyAction from './actions/delete';
1717
import duplicateTaxonomyAction from './actions/duplicate';
18+
import viewTermsAction from './actions/view-terms';
1819
import {
1920
hierarchicalField,
2021
publicField,
@@ -50,6 +51,7 @@ export function TaxonomiesList() {
5051
editAction,
5152
quickEditTaxonomyAction,
5253
duplicateTaxonomyAction,
54+
viewTermsAction,
5355
activateAction,
5456
deactivateAction,
5557
deleteTaxonomyAction,

0 commit comments

Comments
 (0)