Skip to content
Closed
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
27 changes: 12 additions & 15 deletions src/wp-includes/entity-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,13 @@ function _wp_get_site_editor_screen_post_types() {
}

/**
* Preloads the fields of the post types the site editor lists.
* Preloads the fields of the post types the editors list.
*
* The site editor reads them from the `/wp/v2/entity-fields` route before it
* can list the post fields, see `useFields` in
* packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg: those of
* the edited post, if any, and of the post types of the screen it opens on.
*
* The post editor is left untouched: it only reads the fields in the
* DataForm-based inspector, an experiment of the Gutenberg plugin which
* preloads them on its own.
* The editors read them from the `/wp/v2/entity-fields` route, see `useFields`
* in packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg. The
* post editor reads those of the edited post in the post summary of its
* inspector. The site editor reads those of the edited post, if any, and of
* the post types of the screen it opens on.
*
* @since 7.2.0
* @access private
Expand All @@ -342,15 +339,15 @@ function _wp_get_site_editor_screen_post_types() {
* @return array Filtered preload paths.
*/
function _wp_preload_entity_fields( $paths, $context ) {
if ( 'core/edit-site' !== $context->name ) {
return $paths;
}

$post_types = array();
if ( isset( $context->post ) ) {
if ( 'core/edit-post' === $context->name && isset( $context->post ) ) {
$post_types[] = $context->post->post_type;
} elseif ( 'core/edit-site' === $context->name ) {
if ( isset( $context->post ) ) {
$post_types[] = $context->post->post_type;
}
$post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() );
}
$post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() );

return array_merge(
$paths,
Expand Down
11 changes: 6 additions & 5 deletions tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public function test_the_fields_are_preloaded_on_the_block_editor_preload_paths(
}

/**
* The post editor does not preload the fields, whatever its `p` query arg.
* The post editor preloads the fields of the edited post type, which the
* post summary of its inspector reads, whatever its `p` query arg.
*
* @ticket 66229
* @ticket 66339
*/
public function test_the_post_editor_does_not_preload_the_fields() {
$_GET['p'] = '/page';
public function test_the_post_editor_preloads_the_fields_of_the_post_type() {
$_GET['p'] = '/wp_template';
$post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) );
$context = new WP_Block_Editor_Context(
array(
Expand All @@ -46,7 +47,7 @@ public function test_the_post_editor_does_not_preload_the_fields() {
)
);

$this->assertSame( array(), _wp_preload_entity_fields( array(), $context ) );
$this->assertSame( array( '/wp/v2/entity-fields?kind=postType&name=page' ), _wp_preload_entity_fields( array(), $context ) );
}

/**
Expand Down
Loading