From fe2195e5016a8504f9b214a6911841b87a1fe29f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 11 Feb 2025 20:21:39 +0100 Subject: [PATCH] Core Data: add type for term entity (#69151) Follow-up to #67668, makes it easy to use term types with `getEntityRecords` and the like --- packages/core-data/src/dynamic-entities.ts | 1 + packages/core-data/src/entity-types/index.ts | 3 ++ packages/core-data/src/entity-types/term.ts | 57 ++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 packages/core-data/src/entity-types/term.ts diff --git a/packages/core-data/src/dynamic-entities.ts b/packages/core-data/src/dynamic-entities.ts index 51b579cb0cfcf..7db8865fded64 100644 --- a/packages/core-data/src/dynamic-entities.ts +++ b/packages/core-data/src/dynamic-entities.ts @@ -18,6 +18,7 @@ export type WPEntityTypes< C extends ET.Context = 'edit' > = { Site: ET.Settings< C >; Status: ET.PostStatusObject< C >; Taxonomy: ET.Taxonomy< C >; + Term: ET.Term< C >; Theme: ET.Theme< C >; UnstableBase: ET.UnstableBase< C >; User: ET.User< C >; diff --git a/packages/core-data/src/entity-types/index.ts b/packages/core-data/src/entity-types/index.ts index 68087a74005b2..893de59cde548 100644 --- a/packages/core-data/src/entity-types/index.ts +++ b/packages/core-data/src/entity-types/index.ts @@ -17,6 +17,7 @@ import type { PostRevision } from './post-revision'; import type { Settings } from './settings'; import type { Sidebar } from './sidebar'; import type { Taxonomy } from './taxonomy'; +import type { Term } from './term'; import type { Theme } from './theme'; import type { User } from './user'; import type { Type } from './type'; @@ -46,6 +47,7 @@ export type { Taxonomy, TemplatePartArea, TemplateType, + Term, Theme, Type, Updatable, @@ -105,6 +107,7 @@ export interface PerPackageEntityRecords< C extends Context > { | Settings< C > | Sidebar< C > | Taxonomy< C > + | Term< C > | Theme< C > | User< C > | Type< C > diff --git a/packages/core-data/src/entity-types/term.ts b/packages/core-data/src/entity-types/term.ts new file mode 100644 index 0000000000000..f007832d7e755 --- /dev/null +++ b/packages/core-data/src/entity-types/term.ts @@ -0,0 +1,57 @@ +/** + * Internal dependencies + */ +import type { Context, ContextualField, OmitNevers } from './helpers'; + +import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records'; + +declare module './base-entity-records' { + export namespace BaseEntityRecords { + export interface Term< C extends Context > { + /** + * Unique identifier for the term. + */ + id: number; + /** + * Number of published posts for the term. + */ + count: ContextualField< number, 'view' | 'edit', C >; + /** + * HTML description of the term. + */ + description: ContextualField< string, 'view' | 'edit', C >; + /** + * URL of the term. + */ + link: string; + /** + * HTML title for the term. + */ + name: string; + /** + * An alphanumeric identifier for the term unique to its type. + */ + slug: string; + /** + * Type attribution for the term. + */ + taxonomy: string; + /** + * The parent term ID. Only present for hierarchical taxonomies. + */ + parent?: number; + /** + * Meta fields. + */ + meta: ContextualField< + Record< string, string >, + 'view' | 'edit', + C + >; + } + } +} + +export type Term< C extends Context = 'edit' > = OmitNevers< + _BaseEntityRecords.Term< C > +>;