Skip to content

View Config: Add version handling#12391

Closed
ntsekouras wants to merge 3 commits into
WordPress:trunkfrom
ntsekouras:backport-79809
Closed

View Config: Add version handling#12391
ntsekouras wants to merge 3 commits into
WordPress:trunkfrom
ntsekouras:backport-79809

Conversation

@ntsekouras

@ntsekouras ntsekouras commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Backport the GB PR: WordPress/gutenberg#79809

Trac ticket: https://core.trac.wordpress.org/ticket/65577

What?

Adds version handling to the server-side view configuration API (wp_get_entity_view_config()).

Why?

Right now consumers of the filters have to customize the config by walking the raw payload (find the right index, foreach, unset, etc.), and imperative array manipulation can never be migrated. A later shape change could break existing callbacks in the wild. So even though there is only version 1 today and no migrations exist, this code is the safeguard to keep filters working in a possible shape change.

How?

The approach here partially mirrors theme.json version handling, where documents declare a version and WP_Theme_JSON_Data->update_with() migrates older data forward. So instead of mutating a single payload directly, now callbacks receive a data container (WP_View_Config_Data) and update the config through the container's specific methods.

Any update made is marked against the version it was written, and the payload could be migrated in newer versions.

Where it differs from theme.json handling is the need for updating configs that are arrays (e.g. fields and view_list) and handling index based transforms cannot happen reliably in a filter. These props though have stable identities (field.id, view_list.slug), so each part of the config has its own update function and patches are keyed by that identity: the key names the member, the value carries only the changes. All the update functions follow the same rules — an associative array merges key by key, a numerically indexed array replaces wholesale, and null deletes what it names: a nested key, a whole member, or a whole top-level key (which resets it to its default).

update_properties()

  • update_properties( $patch, $version ) merges partial changes into default_view, default_layouts, and form except its fields.
  • a null patch value unsets a nested key, and null for a whole top-level key resets it to its default.
function example_filter_page_view_config( $data ) {
    // Unset a nested value with null: drop the grid layout option. Form
    // properties other than `fields` also merge here.
    $data->update_properties(
        array(
            'default_layouts' => array( 'grid' => null ),
            'form'            => array( 'layout' => array( 'type' => 'regular' ) ),
        ),
        1
    );

    return $data;
}
add_filter( 'get_entity_view_config_postType_page', 'example_filter_page_view_config' );

update_view_list_items()

  • update_view_list_items( $items, $version ) patches the view_list, keyed by slug.
  • a matching view merges in place, an unknown slug appends a new view to the end, and null removes the view.
function example_filter_page_view_config( $data ) {
    // Add a saved view, retitle the existing Drafts view — matched by slug,
    // only the given keys change — and remove the Trash view with null.
    $data->update_view_list_items(
        array(
            'my-drafts' => array(
                'title' => __( 'My drafts', 'example' ),
                'view'  => array(
                    'filters' => array(
                        array(
                            'field'    => 'status',
                            'operator' => 'isAny',
                            'value'    => 'draft',
                            'isLocked' => true,
                        ),
                    ),
                ),
            ),
            'drafts'    => array( 'title' => __( 'In progress', 'example' ) ),
            'trash'     => null,
        ),
        1
    );

    return $data;
}
add_filter( 'get_entity_view_config_postType_page', 'example_filter_page_view_config' );

update_form_fields()

  • update_form_fields( $fields, $version ) patches the form fields, keyed by id. A field is found wherever it lives — at the top level or nested inside a group's children — so a patch only needs the id.
  • a matching field merges in place, an unknown id appends a new field, and null removes the field.
  • the id lives in the patch key and the value only carries overrides, so a new field with no overrides is 'my_field' => array(). Inside a field patch, an associative children value merges into the group's children by id (unknown ids append to the group), while a numerically indexed one replaces them wholesale.
function example_filter_page_view_config( $data ) {
    // Change label position of the post-content-info field, remove the slug
    // and author fields with null, and append a field to the discussion group.
    $data->update_form_fields(
        array(
            'post-content-info' => array(
                'layout' => array( 'labelPosition' => 'side' ),
            ),
            'slug'              => null,
            'author'            => null,
            'discussion'        => array(
                'children' => array( 'my_field' => array() ),
            ),
        ),
        1
    );

    return $data;
}
add_filter( 'get_entity_view_config_postType_page', 'example_filter_page_view_config' );

set()

Replaces a whole top-level key. It shouldn't be the default choice — a callback using it stops inheriting core's future changes to that key — but it's useful for cases like a CPT that doesn't want the default post form at all.

function example_filter_book_view_config( $data ) {
    // The default post form doesn't fit books; define one from scratch.
    return $data->set(
        'form',
        array(
            'layout' => array( 'type' => 'panel' ),
            'fields' => array( 'isbn', 'publisher' ),
        ),
        1
    );
}
add_filter( 'get_entity_view_config_postType_book', 'example_filter_book_view_config' );

Testing Instructions

The editor changes consuming the versioned endpoint live in the Gutenberg packages, so testing here is against the API and REST endpoint directly.

  1. Start a WordPress development environment and add a callback combining the patches above, e.g. in a mu-plugin:
add_filter(
	'get_entity_view_config_postType_page',
	function ( $data ) {
		$data->update_view_list_items(
			array(
				'my-drafts' => array(
					'title' => 'My drafts',
					'view'  => array(
						'filters' => array(
							array(
								'field'    => 'status',
								'operator' => 'isAny',
								'value'    => 'draft',
								'isLocked' => true,
							),
						),
					),
				),
				'drafts'    => array( 'title' => 'In progress' ),
				'trash'     => null,
			),
			1
		);

		$data->update_form_fields(
			array(
				'slug'       => null,
				'author'     => null,
				'discussion' => array( 'children' => array( 'my_field' => array() ) ),
			),
			1
		);

		$data->update_properties(
			array( 'default_layouts' => array( 'grid' => null ) ),
			1
		);

		return $data;
	}
);
  1. Log in as an admin, open the editor for any post or page (or the Site Editor), and request the config from the browser console:
wp.apiFetch( { path: '/wp/v2/view-config?kind=postType&name=page' } ).then( console.log );
  1. Verify in the response:
    • version is 1.
    • view_list: the "My drafts" view is appended, the Drafts view is retitled "In progress" (its filters untouched), and the Trash view is gone.
    • form.fields: slug and author are gone, and the discussion group's children include my_field.
    • default_layouts no longer has a grid key.
  2. Remove the callback and verify the response returns to the defaults.
  3. Play around with different payloads.
  4. CI green.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8, Claude Fable 5
Used for: Porting the merged Gutenberg PR to core with direction, changes and review by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ntsekouras ntsekouras self-assigned this Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ntsekouras, mcsf.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ntsekouras ntsekouras marked this pull request as draft July 2, 2026 10:24
@ntsekouras ntsekouras marked this pull request as ready for review July 7, 2026 06:18
@ntsekouras ntsekouras changed the title View Config: Add versioning handling View Config: Add version handling Jul 7, 2026

@mcsf mcsf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks pretty good! I left a couple of recommendations.

Comment thread src/wp-includes/view-config.php
Comment thread src/wp-includes/view-config.php Outdated
Comment thread src/wp-includes/view-config.php Outdated
Comment thread tests/phpunit/tests/view-config.php Outdated
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 62668
GitHub commit: 51a575c

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants