Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core data revisions: extend support to other post types #56353

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this should continue to use the default entity key.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean DEFAULT_ENTITY_KEY?

The value is id, which doesn't work for template/template parts. To get the unique record id we have to refer to the wp_id property.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for highlighting where these are set in core!

// @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',
];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current thinking is to extend the view context's response with support information.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just double-checking what you mean here (sorry if I'm missing context — so to speak!): that we have a hard-coded list of types here because calling '/wp/v2/types?context=view' doesn't include a supports array in the response for each post type?

That makes sense to me, and from looking up wp-includes/post.php the list here corresponds with types that all opt-in to revisions under supports 👍


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,
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
};
} );
}
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,
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
};
} ),
] )(
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
Loading