Make WordPress Core

Opened 5 hours ago

Last modified 5 hours ago

#66362 new enhancement

REST API: Add item counts to the page view list in the view config endpoint

Reported by: im3dabasia1 Owned by:
Priority: normal Milestone: 7.3
Component: REST API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Adds a count to each status view in the page view list of the view config endpoint, so the Pages screen in the site editor can show item counts on its status tabs, like the classic Pages screen does.

Views narrowed by more than their status get no count.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/82247
Core PR: ​https://github.com/WordPress/wordpress-develop/pull/14070

Change History (2)

#1 @ravipatel
5 hours ago

Test Report

Description

This report validates that the indicated patch works as expected.

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/14070 (head b0167ed6ce)

Environment

  • WordPress: 7.2-beta1 trunk + PR #14070
  • PHP: 8.3.35
  • Server: wordpress-develop Docker environment
  • Database: MySQL (wordpress-develop Docker)
  • OS: Windows 11

Actual Results

  1. ✅ phpunit --group view-config: OK (98 tests, 238 assertions), single site and multisite. All 9 new count tests pass.
  2. ✅ Counts match the classic Pages screen. I created 2 published, 1 draft, 1 pending, 1 scheduled and 2 private pages (by different authors), plus 1 trashed page, and compared the page view list counts with wp_count_posts( 'page', 'readable' ):
    • Administrator and Editor: all 7, published 2, scheduled 1, drafts 1, pending 1, private 2, trash 1.
    • A role with edit_pages but no read_private_pages: all 6, private 1 (only their own). Other users' private pages are not counted.
  3. ✅ "All" excludes trash, matching the classic screen.
  4. ✅ The endpoint still requires edit_pages, so counts are not exposed to Subscribers or logged-out users.

Notes

  • The new @since tags (5 of them, in the controller and in view-config.php) say 7.2.0, but this ticket is milestoned for 7.3. They should probably be 7.3.0.
  • The PR description does not link this ticket yet, so the "Trac Ticket Missing" bot check on the PR still fails. Adding Trac ticket: https://core.trac.wordpress.org/ticket/66362 should fix it.
  • Not tested: the Gutenberg UI side (​https://github.com/WordPress/gutenberg/pull/82247). This report covers only the REST response.

Supplemental Artifacts

None.

This ticket was mentioned in ​PR #14070 on ​WordPress/wordpress-develop by ​@im3dabasia1.


5 hours ago
#2

  • Keywords has-unit-tests added

Backports the PHP changes from ​https://github.com/WordPress/gutenberg/pull/82247. The JS ships through the package sync.

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

What

GET /wp/v2/view-config?kind=postType&name=page now adds a count to each view list entry whose total a status count can answer:

  • A view without filters counts every status but trash.
  • A view whose only filter is on status (is / isAny) counts those statuses.
  • A view narrowed by anything else (another filter, or a search) gets no count, so it never shows a total that doesn't describe it.

The counts are added in WP_REST_View_Config_Controller::get_items() after every get_entity_view_config_* filter has run, so they describe the final view list, including views added or changed by plugins. One wp_count_posts( 'page', 'readable' ) call supplies every count, which keeps private pages the current user can't read out of the totals. The item schema now includes view_list[].count.

Why

Users can't tell how many drafts or pending items exist without opening each tab, while classic wp-admin shows these counts. This adds the server side of the status tab counts in the site editor's post list.

Files

  • src/wp-includes/view-config.php: adds _wp_add_counts_to_view_list() and _wp_get_view_list_entry_statuses().
  • src/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php: adds the counts in get_items() and count to the item schema.
  • tests/phpunit/tests/rest-api/rest-view-config-controller.php: three count tests, and test_get_items_matches_underlying_config now expects the counts.

Testing Instructions

  1. Have a few pages in different statuses (for example 2 drafts, 1 published, 1 in the trash).
  2. Open the site editor and run this in the browser console: await wp.apiFetch( { path: '/wp/v2/view-config?kind=postType&name=page' } )
  3. Confirm each entry in view_list has a count matching its status, and that all counts every page except trashed ones.
  4. Run the tests: npm run test:php -- --group view-config

Use of AI Tools

Ported with Claude Code (Claude Opus 5.5). I reviewed every change and ran the tests locally.

Note: See TracTickets for help on using tickets.