From 9a15a4f4098c642587d06bd1209d00a62bd24d40 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 21 Nov 2023 13:30:17 +1100 Subject: [PATCH 1/5] Initial commit: add revisions support for templates --- packages/core-data/src/entities.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 9f736af1c83784..bae0e783cc618a 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -21,7 +21,12 @@ const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ]; // A hardcoded list of post types that support revisions. // @TODO: Ideally this should be fetched from the `/types` REST API's view context. -const POST_TYPES_WITH_REVISIONS_SUPPORT = [ 'post', 'page' ]; +const POST_TYPES_WITH_REVISIONS_SUPPORT = [ + 'post', + 'page', + 'wp_template', + 'wp_template_part', +]; export const rootEntitiesConfig = [ { From 2cfd3fbb28dbd06a68627acfdfe976544d04d8d7 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 21 Nov 2023 13:41:37 +1100 Subject: [PATCH 2/5] Renames constant to be clearer about what these post types apply to. Adds wp_block and wp_navigation --- packages/core-data/src/entities.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index bae0e783cc618a..d5eecf600936cc 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -20,10 +20,13 @@ export const DEFAULT_ENTITY_KEY = 'id'; const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ]; // A hardcoded list of post types that support revisions. +// Reflects post types in Core's src/wp-includes/post.php. // @TODO: Ideally this should be fetched from the `/types` REST API's view context. -const POST_TYPES_WITH_REVISIONS_SUPPORT = [ +const POST_TYPE_ENTITIES_WITH_REVISIONS_SUPPORT = [ 'post', 'page', + 'wp_block', + 'wp_navigation', 'wp_template', 'wp_template_part', ]; @@ -313,7 +316,7 @@ async function loadPostTypeEntities() { }, mergedEdits: { meta: true }, supports: { - revisions: POST_TYPES_WITH_REVISIONS_SUPPORT.includes( + revisions: POST_TYPE_ENTITIES_WITH_REVISIONS_SUPPORT.includes( postType?.slug ), }, From 0f11173b122c2a874052dbd5237a8c54bb7b763e Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 22 Nov 2023 15:24:24 +1100 Subject: [PATCH 3/5] Testing updating record.key in the action. --- packages/core-data/src/actions.js | 2 ++ packages/core-data/src/reducer.js | 8 +++++++- packages/core-data/src/resolvers.js | 12 ++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/actions.js b/packages/core-data/src/actions.js index 4c5622ac780589..d12b62420c3607 100644 --- a/packages/core-data/src/actions.js +++ b/packages/core-data/src/actions.js @@ -955,8 +955,10 @@ export function receiveRevisions( invalidateCache = false, meta ) { + const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( name ); return { type: 'RECEIVE_ITEM_REVISIONS', + key: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, items: Array.isArray( records ) ? records : [ records ], recordKey, meta, diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 34558fcfbb142e..6014d205970634 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -238,8 +238,8 @@ function entity( entityConfig ) { // Inject the entity config into the action. replaceAction( ( action ) => { return { - ...action, key: entityConfig.key || DEFAULT_ENTITY_KEY, + ...action, }; } ), ] )( @@ -377,6 +377,12 @@ function entity( entityConfig ) { } if ( action.type === 'REMOVE_ITEMS' ) { + /* + For templates; + itemIds: ['twentytwentyfour//wp-custom-template-nag'] + But the state key is with one slash: twentytwentyfour/wp-custom-template-nag + So deleteEntityRecord needs send extra data to removeItems? + */ return Object.fromEntries( Object.entries( state ).filter( ( [ id ] ) => diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 6e821183e3116b..d76091364a8068 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -744,7 +744,9 @@ export const getRevisions = ) { return; } - + const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( + name + ); if ( query._fields ) { // If requesting specific fields, items and query association to said // records are stored by ID reference. Thus, fields must always include @@ -755,7 +757,7 @@ export const getRevisions = ...new Set( [ ...( getNormalizedCommaSeparable( query._fields ) || [] ), - DEFAULT_ENTITY_KEY, + isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, ] ), ].join(), }; @@ -857,7 +859,9 @@ export const getRevision = ) { return; } - + const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( + name + ); if ( query !== undefined && query._fields ) { // If requesting specific fields, items and query association to said // records are stored by ID reference. Thus, fields must always include @@ -868,7 +872,7 @@ export const getRevision = ...new Set( [ ...( getNormalizedCommaSeparable( query._fields ) || [] ), - DEFAULT_ENTITY_KEY, + isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, ] ), ].join(), }; From c12f97b3c6294ac50b52ebdd9ca9989acc20e578 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 22 Nov 2023 20:18:03 +1100 Subject: [PATCH 4/5] Moving `revisionKey` to the entity config and use it in the action and resolvers --- docs/reference-guides/data/data-core.md | 6 +--- packages/core-data/README.md | 6 +--- packages/core-data/src/actions.js | 48 +++++++++++++------------ packages/core-data/src/entities.js | 1 + packages/core-data/src/reducer.js | 6 ---- packages/core-data/src/resolvers.js | 12 +++---- 6 files changed, 32 insertions(+), 47 deletions(-) diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index b2a75638ace9fe..b80703dcc67b18 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -741,7 +741,7 @@ _Returns_ ### receiveRevisions -Returns an action object used in signalling that revisions have been received. +Action triggered to receive revision items. _Parameters_ @@ -753,10 +753,6 @@ _Parameters_ - _invalidateCache_ `?boolean`: Should invalidate query caches. - _meta_ `?Object`: Meta information about pagination. -_Returns_ - -- `Object`: Action object. - ### receiveThemeSupports > **Deprecated** since WP 5.9, this is not useful anymore, use the selector direclty. diff --git a/packages/core-data/README.md b/packages/core-data/README.md index ebc467f7fede2d..6677a32df08dc9 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -250,7 +250,7 @@ _Returns_ ### receiveRevisions -Returns an action object used in signalling that revisions have been received. +Action triggered to receive revision items. _Parameters_ @@ -262,10 +262,6 @@ _Parameters_ - _invalidateCache_ `?boolean`: Should invalidate query caches. - _meta_ `?Object`: Meta information about pagination. -_Returns_ - -- `Object`: Action object. - ### receiveThemeSupports > **Deprecated** since WP 5.9, this is not useful anymore, use the selector direclty. diff --git a/packages/core-data/src/actions.js b/packages/core-data/src/actions.js index d12b62420c3607..ef466291012a47 100644 --- a/packages/core-data/src/actions.js +++ b/packages/core-data/src/actions.js @@ -935,7 +935,7 @@ export function receiveDefaultTemplateId( query, templateId ) { } /** - * Returns an action object used in signalling that revisions have been received. + * Action triggered to receive revision items. * * @param {string} kind Kind of the received entity record revisions. * @param {string} name Name of the received entity record revisions. @@ -944,27 +944,29 @@ export function receiveDefaultTemplateId( query, templateId ) { * @param {?Object} query Query Object. * @param {?boolean} invalidateCache Should invalidate query caches. * @param {?Object} meta Meta information about pagination. - * @return {Object} Action object. */ -export function receiveRevisions( - kind, - name, - recordKey, - records, - query, - invalidateCache = false, - meta -) { - const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( name ); - return { - type: 'RECEIVE_ITEM_REVISIONS', - key: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, - items: Array.isArray( records ) ? records : [ records ], - recordKey, - meta, - query, - kind, - name, - invalidateCache, + +export const receiveRevisions = + ( kind, name, recordKey, records, query, invalidateCache = false, meta ) => + async ( { dispatch } ) => { + const configs = await dispatch( getOrLoadEntitiesConfig( kind ) ); + const entityConfig = configs.find( + ( config ) => config.kind === kind && config.name === name + ); + const key = + entityConfig && entityConfig?.revisionKey + ? entityConfig.revisionKey + : DEFAULT_ENTITY_KEY; + + dispatch( { + type: 'RECEIVE_ITEM_REVISIONS', + key, + items: Array.isArray( records ) ? records : [ records ], + recordKey, + meta, + query, + kind, + name, + invalidateCache, + } ); }; -} diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index d5eecf600936cc..f016336260ab16 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -359,6 +359,7 @@ async function loadPostTypeEntities() { }/${ parentId }/revisions${ revisionId ? '/' + revisionId : '' }`, + revisionKey: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, }; } ); } diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 6014d205970634..a499b42f175438 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -377,12 +377,6 @@ function entity( entityConfig ) { } if ( action.type === 'REMOVE_ITEMS' ) { - /* - For templates; - itemIds: ['twentytwentyfour//wp-custom-template-nag'] - But the state key is with one slash: twentytwentyfour/wp-custom-template-nag - So deleteEntityRecord needs send extra data to removeItems? - */ return Object.fromEntries( Object.entries( state ).filter( ( [ id ] ) => diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index d76091364a8068..245d64d05d0649 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -744,9 +744,7 @@ export const getRevisions = ) { return; } - const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( - name - ); + if ( query._fields ) { // If requesting specific fields, items and query association to said // records are stored by ID reference. Thus, fields must always include @@ -757,7 +755,7 @@ export const getRevisions = ...new Set( [ ...( getNormalizedCommaSeparable( query._fields ) || [] ), - isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, + entityConfig.revisionKey || DEFAULT_ENTITY_KEY, ] ), ].join(), }; @@ -859,9 +857,7 @@ export const getRevision = ) { return; } - const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( - name - ); + if ( query !== undefined && query._fields ) { // If requesting specific fields, items and query association to said // records are stored by ID reference. Thus, fields must always include @@ -872,7 +868,7 @@ export const getRevision = ...new Set( [ ...( getNormalizedCommaSeparable( query._fields ) || [] ), - isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY, + entityConfig.revisionKey || DEFAULT_ENTITY_KEY, ] ), ].join(), }; From 20bf7801b5d8d787c3e27940669cbfd956807b2f Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 29 Nov 2023 09:29:59 +1100 Subject: [PATCH 5/5] Me: Pray, depart hence, thou knavish trifle! Thy utility be as absent as mirth in a grave, a jest without jesters. Begone, thou witless motley, for even a jest doth surpass thy meager worth! Empty line: *whimper* --- packages/core-data/src/actions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core-data/src/actions.js b/packages/core-data/src/actions.js index ef466291012a47..49776d0562984f 100644 --- a/packages/core-data/src/actions.js +++ b/packages/core-data/src/actions.js @@ -945,7 +945,6 @@ export function receiveDefaultTemplateId( query, templateId ) { * @param {?boolean} invalidateCache Should invalidate query caches. * @param {?Object} meta Meta information about pagination. */ - export const receiveRevisions = ( kind, name, recordKey, records, query, invalidateCache = false, meta ) => async ( { dispatch } ) => {