Skip to content

Commit 4316614

Browse files
oandregalclaude
andcommitted
Entity Fields: Preload the fields of the post types the Site Editor lists.
The Site Editor reads the fields of the post types it lists from the `/wp/v2/entity-fields` route before it can render them. The fields of the edited post type, if any, and of the post types of the screen the Site Editor opens on, from its `p` query arg, are now added to the preloaded paths. Synced from WordPress/gutenberg#84700. See #66229. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1c86d8c commit 4316614

3 files changed

Lines changed: 267 additions & 0 deletions

File tree

‎src/wp-includes/default-filters.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,5 +858,6 @@
858858

859859
// Entity Fields API.
860860
add_action( 'admin_footer', '_wp_add_entity_field_modules_to_editor_script', 10, 0 );
861+
add_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_fields', 10, 2 );
861862

862863
unset( $filter, $action, $post_type );

‎src/wp-includes/entity-fields.php‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,106 @@ function wp_get_entity_field_collection_fields( $directory ) {
252252
function wp_register_entity_field_collection( $directory ) {
253253
return WP_Entity_Fields_Registry::get_instance()->register_collection( $directory );
254254
}
255+
256+
/**
257+
* Returns the paths that serve the fields of the given post types, skipping
258+
* the post types that are not registered.
259+
*
260+
* A path must match the request of the `getFieldsConfig` core data resolver,
261+
* see packages/core-data/src/resolvers.js in Gutenberg.
262+
*
263+
* @since 7.2.0
264+
* @access private
265+
*
266+
* @param string[] $post_types The post types.
267+
* @return string[] One path of the `/wp/v2/entity-fields` route per post type,
268+
* in the order the post types are given, without duplicates.
269+
*/
270+
function _wp_get_post_type_fields_preload_paths( $post_types ) {
271+
$paths = array();
272+
foreach ( array_unique( $post_types ) as $post_type ) {
273+
if ( ! post_type_exists( $post_type ) ) {
274+
continue;
275+
}
276+
$paths[] = add_query_arg(
277+
array(
278+
'kind' => 'postType',
279+
'name' => $post_type,
280+
),
281+
'/wp/v2/entity-fields'
282+
);
283+
}
284+
285+
return $paths;
286+
}
287+
288+
/**
289+
* Returns the post types whose fields the screen of the site editor being
290+
* loaded lists, from its `p` query arg.
291+
*
292+
* The screens are the routes in
293+
* packages/edit-site/src/components/site-editor-routes in Gutenberg: the
294+
* lists and the editors of pages, templates, patterns, and template parts.
295+
* The patterns screen lists both patterns and template parts.
296+
*
297+
* @since 7.2.0
298+
* @access private
299+
*
300+
* @return string[] The post types, empty for any other screen.
301+
*/
302+
function _wp_get_site_editor_screen_post_types() {
303+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload.
304+
$path = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : '';
305+
$segments = explode( '/', trim( $path, '/' ) );
306+
307+
switch ( $segments[0] ) {
308+
case 'page':
309+
return array( 'page' );
310+
case 'template':
311+
case 'wp_template':
312+
return array( 'wp_template' );
313+
case 'pattern':
314+
return array( 'wp_block', 'wp_template_part' );
315+
case 'wp_block':
316+
return array( 'wp_block' );
317+
case 'wp_template_part':
318+
return array( 'wp_template_part' );
319+
}
320+
return array();
321+
}
322+
323+
/**
324+
* Preloads the fields of the post types the site editor lists.
325+
*
326+
* The site editor reads them from the `/wp/v2/entity-fields` route before it
327+
* can list the post fields, see `useFields` in
328+
* packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg: those of
329+
* the edited post, if any, and of the post types of the screen it opens on.
330+
*
331+
* The post editor is left untouched: it only reads the fields in the
332+
* DataForm-based inspector, an experiment of the Gutenberg plugin which
333+
* preloads them on its own.
334+
*
335+
* @since 7.2.0
336+
* @access private
337+
*
338+
* @param array $paths REST API paths to preload.
339+
* @param WP_Block_Editor_Context $context Block editor context.
340+
* @return array Filtered preload paths.
341+
*/
342+
function _wp_preload_entity_fields( $paths, $context ) {
343+
if ( 'core/edit-site' !== $context->name ) {
344+
return $paths;
345+
}
346+
347+
$post_types = array();
348+
if ( isset( $context->post ) ) {
349+
$post_types[] = $context->post->post_type;
350+
}
351+
$post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() );
352+
353+
return array_merge(
354+
$paths,
355+
_wp_get_post_type_fields_preload_paths( $post_types )
356+
);
357+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
require_once __DIR__ . '/base.php';
4+
5+
/**
6+
* Tests for the preloading of the entity fields.
7+
*
8+
* @package WordPress
9+
* @subpackage Entity Fields API
10+
* @since 7.2.0
11+
*
12+
* @group entity-fields
13+
*
14+
* @covers ::_wp_preload_entity_fields
15+
* @covers ::_wp_get_post_type_fields_preload_paths
16+
* @covers ::_wp_get_site_editor_screen_post_types
17+
*/
18+
class Tests_Entity_Fields_WpPreloadEntityFields extends WP_Entity_Fields_UnitTestCase {
19+
20+
public function tear_down() {
21+
unset( $_GET['p'] );
22+
parent::tear_down();
23+
}
24+
25+
/**
26+
* The preloading is hooked to the preload paths of the block editor.
27+
*
28+
* @ticket 66229
29+
*/
30+
public function test_the_fields_are_preloaded_on_the_block_editor_preload_paths() {
31+
$this->assertSame( 10, has_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_fields' ) );
32+
}
33+
34+
/**
35+
* The post editor does not preload the fields, whatever its `p` query arg.
36+
*
37+
* @ticket 66229
38+
*/
39+
public function test_the_post_editor_does_not_preload_the_fields() {
40+
$_GET['p'] = '/page';
41+
$post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) );
42+
$context = new WP_Block_Editor_Context(
43+
array(
44+
'name' => 'core/edit-post',
45+
'post' => $post,
46+
)
47+
);
48+
49+
$this->assertSame( array(), _wp_preload_entity_fields( array(), $context ) );
50+
}
51+
52+
/**
53+
* The preloaded path serves the fields of the post type.
54+
*
55+
* @ticket 66229
56+
*/
57+
public function test_the_preloaded_fields_path_serves_the_fields() {
58+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
59+
$_GET['p'] = '/page';
60+
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
61+
$paths = _wp_preload_entity_fields( array(), $context );
62+
63+
$preloaded = array_reduce( $paths, 'rest_preload_api_request', array() );
64+
65+
$this->assertSame( 'page', $preloaded[ $paths[0] ]['body']['name'] );
66+
$this->assertContains( 'author', wp_list_pluck( $preloaded[ $paths[0] ]['body']['fields'], 'id' ) );
67+
}
68+
69+
/**
70+
* The site editor preloads the fields of the post types its screen lists,
71+
* from the `p` query arg.
72+
*
73+
* @ticket 66229
74+
*
75+
* @dataProvider data_site_editor_screens
76+
*
77+
* @param string $path The `p` query arg.
78+
* @param string[] $post_types The post types whose fields are preloaded.
79+
*/
80+
public function test_the_site_editor_preloads_the_fields_of_its_screen( $path, $post_types ) {
81+
$_GET['p'] = $path;
82+
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
83+
84+
$paths = _wp_preload_entity_fields( array(), $context );
85+
unset( $_GET['p'] );
86+
87+
$expected = array();
88+
foreach ( $post_types as $post_type ) {
89+
$expected[] = '/wp/v2/entity-fields?kind=postType&name=' . $post_type;
90+
}
91+
$this->assertSame( $expected, $paths );
92+
}
93+
94+
/**
95+
* Data provider for test_the_site_editor_preloads_the_fields_of_its_screen().
96+
*
97+
* @return array[]
98+
*/
99+
public function data_site_editor_screens() {
100+
return array(
101+
'pages' => array( '/page', array( 'page' ) ),
102+
'page' => array( '/page/12', array( 'page' ) ),
103+
'templates' => array( '/template', array( 'wp_template' ) ),
104+
'template' => array( '/wp_template/emptytheme//index', array( 'wp_template' ) ),
105+
'patterns' => array( '/pattern', array( 'wp_block', 'wp_template_part' ) ),
106+
'pattern' => array( '/wp_block/12', array( 'wp_block' ) ),
107+
'template part' => array( '/wp_template_part/emptytheme//header', array( 'wp_template_part' ) ),
108+
'root' => array( '/', array() ),
109+
'styles' => array( '/styles', array() ),
110+
'navigation' => array( '/navigation', array() ),
111+
);
112+
}
113+
114+
/**
115+
* The site editor preloads the fields of the edited post, once, next to
116+
* those of its screen.
117+
*
118+
* @ticket 66229
119+
*/
120+
public function test_the_site_editor_preloads_the_fields_of_the_edited_post() {
121+
$post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) );
122+
$_GET['p'] = '/page';
123+
$context = new WP_Block_Editor_Context(
124+
array(
125+
'name' => 'core/edit-site',
126+
'post' => $post,
127+
)
128+
);
129+
130+
$paths = _wp_preload_entity_fields( array(), $context );
131+
unset( $_GET['p'] );
132+
133+
$this->assertSame( array( '/wp/v2/entity-fields?kind=postType&name=page' ), $paths );
134+
}
135+
136+
/**
137+
* Post types that are not registered are skipped.
138+
*
139+
* @ticket 66229
140+
*/
141+
public function test_unregistered_post_types_are_not_preloaded() {
142+
$this->assertSame(
143+
array( '/wp/v2/entity-fields?kind=postType&name=page' ),
144+
_wp_get_post_type_fields_preload_paths( array( 'page', 'not_a_post_type', 'page' ) )
145+
);
146+
}
147+
148+
/**
149+
* Other editors, a post editor context without a post, and a site editor
150+
* screen that lists no post fields are left untouched.
151+
*
152+
* @ticket 66229
153+
*/
154+
public function test_other_editors_do_not_preload_the_fields() {
155+
$site_editor = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
156+
$no_post = new WP_Block_Editor_Context( array( 'name' => 'core/edit-post' ) );
157+
$widgets = new WP_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) );
158+
159+
$this->assertSame( array(), _wp_preload_entity_fields( array(), $site_editor ) );
160+
$this->assertSame( array(), _wp_preload_entity_fields( array(), $no_post ) );
161+
$this->assertSame( array(), _wp_preload_entity_fields( array(), $widgets ) );
162+
}
163+
}

0 commit comments

Comments
 (0)