Skip to content

Commit

Permalink
Core data revisions: extend support to other post types (#56353)
Browse files Browse the repository at this point in the history
* Initial commit: add revisions support for templates

* Renames constant to be clearer about what these post types apply to.
Adds wp_block and wp_navigation

* Testing updating record.key in the action.

* Moving `revisionKey` to the entity config and use it in the action and resolvers

* 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*
  • Loading branch information
ramonjd authored and derekblank committed Dec 7, 2023
1 parent 4cec0c8 commit c4df1d7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
6 changes: 1 addition & 5 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand All @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand All @@ -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.
Expand Down
45 changes: 24 additions & 21 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -944,25 +944,28 @@ 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
) {
return {
type: 'RECEIVE_ITEM_REVISIONS',
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,
} );
};
}
13 changes: 11 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ 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 = [ 'post', 'page' ];
const POST_TYPE_ENTITIES_WITH_REVISIONS_SUPPORT = [
'post',
'page',
'wp_block',
'wp_navigation',
'wp_template',
'wp_template_part',
];

export const rootEntitiesConfig = [
{
Expand Down Expand Up @@ -308,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
),
},
Expand Down Expand Up @@ -351,6 +359,7 @@ async function loadPostTypeEntities() {
}/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
revisionKey: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY,
};
} );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
} ),
] )(
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export const getRevisions =
...new Set( [
...( getNormalizedCommaSeparable( query._fields ) ||
[] ),
DEFAULT_ENTITY_KEY,
entityConfig.revisionKey || DEFAULT_ENTITY_KEY,
] ),
].join(),
};
Expand Down Expand Up @@ -868,7 +868,7 @@ export const getRevision =
...new Set( [
...( getNormalizedCommaSeparable( query._fields ) ||
[] ),
DEFAULT_ENTITY_KEY,
entityConfig.revisionKey || DEFAULT_ENTITY_KEY,
] ),
].join(),
};
Expand Down

0 comments on commit c4df1d7

Please sign in to comment.