Skip to content

Commit

Permalink
Core Data: specify context=edit only when needed (#30482)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder authored Apr 23, 2021
1 parent 47f1147 commit 306566f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
17 changes: 0 additions & 17 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,6 @@ function gutenberg_inject_default_block_context( $args ) {

add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' );

/**
* Amends the paths to preload when initializing edit post.
*
* @see https://core.trac.wordpress.org/ticket/50606
*
* @since 8.4.0
*
* @param array $preload_paths Default path list that will be preloaded.
* @return array Modified path list to preload.
*/
function gutenberg_preload_edit_post( $preload_paths ) {
$additional_paths = array( '/?context=edit' );
return array_merge( $preload_paths, $additional_paths );
}

add_filter( 'block_editor_preload_paths', 'gutenberg_preload_edit_post' );

/**
* Override post type labels for Reusable Block custom post type.
* The labels are different from the ones in Core.
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function gutenberg_edit_site_init( $hook ) {
array(
'preload_paths' => array(
array( '/wp/v2/media', 'OPTIONS' ),
'/?context=edit',
'/',
'/wp/v2/types?context=edit',
'/wp/v2/taxonomies?context=edit',
'/wp/v2/pages?context=edit',
Expand Down
12 changes: 12 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export const defaultEntities = [
kind: 'root',
key: 'slug',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
{
name: 'media',
kind: 'root',
baseURL: '/wp/v2/media',
baseURLParams: { context: 'edit' },
plural: 'mediaItems',
label: __( 'Media' ),
},
Expand All @@ -52,6 +54,7 @@ export const defaultEntities = [
kind: 'root',
key: 'slug',
baseURL: '/wp/v2/taxonomies',
baseURLParams: { context: 'edit' },
plural: 'taxonomies',
label: __( 'Taxonomy' ),
},
Expand All @@ -67,6 +70,7 @@ export const defaultEntities = [
name: 'widget',
kind: 'root',
baseURL: '/wp/v2/widgets',
baseURLParams: { context: 'edit' },
plural: 'widgets',
transientEdits: { blocks: true },
label: __( 'Widgets' ),
Expand All @@ -75,6 +79,7 @@ export const defaultEntities = [
name: 'widgetType',
kind: 'root',
baseURL: '/wp/v2/widget-types',
baseURLParams: { context: 'edit' },
plural: 'widgetTypes',
label: __( 'Widget types' ),
},
Expand All @@ -83,33 +88,38 @@ export const defaultEntities = [
name: 'user',
kind: 'root',
baseURL: '/wp/v2/users',
baseURLParams: { context: 'edit' },
plural: 'users',
},
{
name: 'comment',
kind: 'root',
baseURL: '/wp/v2/comments',
baseURLParams: { context: 'edit' },
plural: 'comments',
label: __( 'Comment' ),
},
{
name: 'menu',
kind: 'root',
baseURL: '/__experimental/menus',
baseURLParams: { context: 'edit' },
plural: 'menus',
label: __( 'Menu' ),
},
{
name: 'menuItem',
kind: 'root',
baseURL: '/__experimental/menu-items',
baseURLParams: { context: 'edit' },
plural: 'menuItems',
label: __( 'Menu Item' ),
},
{
name: 'menuLocation',
kind: 'root',
baseURL: '/__experimental/menu-locations',
baseURLParams: { context: 'edit' },
plural: 'menuLocations',
label: __( 'Menu Location' ),
key: 'name',
Expand Down Expand Up @@ -165,6 +175,7 @@ function* loadPostTypeEntities() {
return {
kind: 'postType',
baseURL: '/wp/v2/' + postType.rest_base,
baseURLParams: { context: 'edit' },
name,
label: postType.labels.singular_name,
transientEdits: {
Expand Down Expand Up @@ -194,6 +205,7 @@ function* loadTaxonomyEntities() {
return {
kind: 'taxonomy',
baseURL: '/wp/v2/' + taxonomy.rest_base,
baseURLParams: { context: 'edit' },
name,
label: taxonomy.labels.singular_name,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export function* getEntityRecord( kind, name, key = '', query ) {

// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const path = addQueryArgs( entity.baseURL + '/' + key, {
...entity.baseURLParams,
...query,
context: 'edit',
} );

if ( query !== undefined ) {
Expand Down
56 changes: 52 additions & 4 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ jest.mock( '../locks/actions', () => ( {

describe( 'getEntityRecord', () => {
const POST_TYPE = { slug: 'post' };
const ENTITIES = [
{
name: 'postType',
kind: 'root',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
];

it( 'yields with requested post type', async () => {
const entities = [
{ name: 'postType', kind: 'root', baseURL: '/wp/v2/types' },
];
const fulfillment = getEntityRecord( 'root', 'postType', 'post' );
// Trigger generator
fulfillment.next();
// Provide entities and acquire lock
expect( fulfillment.next( entities ).value.type ).toEqual(
expect( fulfillment.next( ENTITIES ).value.type ).toEqual(
'MOCKED_ACQUIRE_LOCK'
);
// trigger apiFetch
Expand All @@ -64,6 +69,49 @@ describe( 'getEntityRecord', () => {
'MOCKED_RELEASE_LOCK'
);
} );

it( 'accepts a query that overrides default api path', async () => {
const query = { context: 'view', _envelope: '1' };
const queryObj = { include: [ 'post' ], ...query };

const fulfillment = getEntityRecord(
'root',
'postType',
'post',
query
);

// Trigger generator
fulfillment.next();

// Provide entities and acquire lock
expect( fulfillment.next( ENTITIES ).value.type ).toEqual(
'MOCKED_ACQUIRE_LOCK'
);

// Check resolution cache for an existing entity that fulfills the request with query
const {
value: { args: selectArgs },
} = fulfillment.next();
expect( selectArgs ).toEqual( [ 'root', 'postType', queryObj ] );

// Trigger apiFetch, test that the query is present in the url
const { value: apiFetchAction } = fulfillment.next();
expect( apiFetchAction.request ).toEqual( {
path: '/wp/v2/types/post?context=view&_envelope=1',
} );

// Receive response
const { value: received } = fulfillment.next( POST_TYPE );
expect( received ).toEqual(
receiveEntityRecords( 'root', 'postType', POST_TYPE, queryObj )
);

// Release lock
expect( fulfillment.next().value.type ).toEqual(
'MOCKED_RELEASE_LOCK'
);
} );
} );

describe( 'getEntityRecords', () => {
Expand Down

0 comments on commit 306566f

Please sign in to comment.