diff --git a/lib/compat.php b/lib/compat.php index 84956857c262df..8c32d81ddefee2 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -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. diff --git a/lib/full-site-editing/edit-site-page.php b/lib/full-site-editing/edit-site-page.php index 8d9f09878cce8b..d8df5d1301bfe4 100644 --- a/lib/full-site-editing/edit-site-page.php +++ b/lib/full-site-editing/edit-site-page.php @@ -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', diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index af3dc37d66ce32..b315e20cd38c8d 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -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' ), }, @@ -52,6 +54,7 @@ export const defaultEntities = [ kind: 'root', key: 'slug', baseURL: '/wp/v2/taxonomies', + baseURLParams: { context: 'edit' }, plural: 'taxonomies', label: __( 'Taxonomy' ), }, @@ -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' ), @@ -75,6 +79,7 @@ export const defaultEntities = [ name: 'widgetType', kind: 'root', baseURL: '/wp/v2/widget-types', + baseURLParams: { context: 'edit' }, plural: 'widgetTypes', label: __( 'Widget types' ), }, @@ -83,12 +88,14 @@ 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' ), }, @@ -96,6 +103,7 @@ export const defaultEntities = [ name: 'menu', kind: 'root', baseURL: '/__experimental/menus', + baseURLParams: { context: 'edit' }, plural: 'menus', label: __( 'Menu' ), }, @@ -103,6 +111,7 @@ export const defaultEntities = [ name: 'menuItem', kind: 'root', baseURL: '/__experimental/menu-items', + baseURLParams: { context: 'edit' }, plural: 'menuItems', label: __( 'Menu Item' ), }, @@ -110,6 +119,7 @@ export const defaultEntities = [ name: 'menuLocation', kind: 'root', baseURL: '/__experimental/menu-locations', + baseURLParams: { context: 'edit' }, plural: 'menuLocations', label: __( 'Menu Location' ), key: 'name', @@ -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: { @@ -194,6 +205,7 @@ function* loadTaxonomyEntities() { return { kind: 'taxonomy', baseURL: '/wp/v2/' + taxonomy.rest_base, + baseURLParams: { context: 'edit' }, name, label: taxonomy.labels.singular_name, }; diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index e02fb7a8cddeb5..d362e074ca71c3 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -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 ) { diff --git a/packages/core-data/src/test/resolvers.js b/packages/core-data/src/test/resolvers.js index 098a399a6f5bf6..e22958c387f83a 100644 --- a/packages/core-data/src/test/resolvers.js +++ b/packages/core-data/src/test/resolvers.js @@ -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 @@ -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', () => {