Repository navigation
Site editor: Add item counts to post list status tabs #82247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
im3dabasia
wants to merge
45
commits into
WordPress:trunk
Choose a base branch
from
im3dabasia:feat/80683-add-tab-count
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
c9f6d16
feat: Add items count to post status tabs
im3dabasia 40d0959
feat: Add count in the server side and use it in tabs
im3dabasia fae9bb3
fix: Invalidate stale counts for pages
im3dabasia e618f45
Revert "fix: Invalidate stale counts for pages"
im3dabasia 0736cf4
feat: Move code logic from 7.1 to 7.2 for count schema and view-confi…
im3dabasia 462cf8b
feat: Introduce a new view-tab-count component to show count of tabs
im3dabasia a048eb8
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia fcb9518
fix: Remove tabs component from admin-ui
im3dabasia 8ec5588
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 41882f7
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia d09363c
Tests: Allow the item count in v2 status tab names
im3dabasia ed2060d
Post list: Refresh status tab counts after actions that change them
im3dabasia 7ef8a12
Tests: Cover the post list status tab counts
im3dabasia b249b39
Post list: Refresh status tab counts when returning to the list
im3dabasia 36b7b12
View config: Leave out the count of views narrowed by more than status
im3dabasia 7f46b48
Add backport changelog entry for wordpress-develop#14070
im3dabasia 0556bc9
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia dd40362
View config: Account for inherited filters when counting views
im3dabasia 08e8734
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 2a68439
View config: Count a view only when every layout agrees on its filters
im3dabasia 7a1b121
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 65b50ec
Actions: Report the items a partly failed restore or permanent delete…
im3dabasia 37245e0
View config: Tighten the view counts for config search, repeated stat…
im3dabasia 3db7456
Post list: Refresh status tab counts after any action
im3dabasia 6cdf7f3
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia c73380b
Post list: Mute the tab count only on idle, unselected tabs
im3dabasia 550a56a
Tests: Find the bulk actions in the table header
im3dabasia 813beac
Post list: Mute the count on the selected tab too
im3dabasia 33af598
View config: Target the view counts at WordPress 7.3
im3dabasia baf846d
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 6e8b4f2
Merge branch 'trunk' into feat/80683-add-tab-count
ciampo 5f67102
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia d846a6d
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 529d5c3
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 0770f4a
Tests: Find the bulk actions outside the table
im3dabasia 8479099
Site editor: Show the item counts in the v1 Pages sidebar
im3dabasia 032edc5
Site editor: Refresh the v1 Pages sidebar counts after changes
im3dabasia ea52bfe
Tests: Cover the Pages status counts in both site editors
im3dabasia 842a5c1
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 2225261
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 3c29eae
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia 85e0199
Merge branch 'trunk' into feat/80683-add-tab-count
im3dabasia f371304
Tests: Convert the Pages status counts spec to TypeScript
im3dabasia 6dcfa9e
View config: Count comma-separated and any statuses
im3dabasia 63d312b
View config: Parse statuses as the REST API does
im3dabasia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/14070 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/82247 |
75 changes: 75 additions & 0 deletions
75
lib/compat/wordpress-7.3/class-gutenberg-rest-view-config-controller-7-3.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
| /** | ||
| * REST API: Gutenberg_REST_View_Config_Controller_7_3 class | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Controller which provides a REST endpoint for retrieving the default | ||
| * view configuration for a given entity type. | ||
| * | ||
| * Extends the 7.2 controller to add the item count of each view in the `page` | ||
| * view list. | ||
| * | ||
| * @since 7.3.0 | ||
| * | ||
| * @see Gutenberg_REST_View_Config_Controller_7_2 | ||
| */ | ||
| class Gutenberg_REST_View_Config_Controller_7_3 extends Gutenberg_REST_View_Config_Controller_7_2 { | ||
|
|
||
| /** | ||
| * Returns the view configuration for the given entity type. | ||
| * | ||
| * Adds the item count of each view in the `page` view list. The counts are | ||
| * added here rather than through the view config filter, so that they are | ||
| * computed from the view list after every filter has changed it. | ||
| * | ||
| * @since 7.3.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function get_items( $request ) { | ||
| $response = parent::get_items( $request ); | ||
| if ( is_wp_error( $response ) || 'postType' !== $request->get_param( 'kind' ) || 'page' !== $request->get_param( 'name' ) ) { | ||
| return $response; | ||
| } | ||
|
|
||
| // Skip the counting when the request leaves out the view list, such as | ||
| // the editor's request for the form alone. | ||
| if ( ! rest_is_field_included( 'view_list', $this->get_fields_for_response( $request ) ) ) { | ||
| return $response; | ||
| } | ||
|
|
||
| $data = $response->get_data(); | ||
| if ( is_array( $data['view_list'] ) ) { | ||
| $data['view_list'] = _gutenberg_add_counts_to_view_list( 'page', $data['view_list'], $data['default_view'], $data['default_layouts'] ); | ||
| $response->set_data( $data ); | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the view configuration schema, conforming to JSON Schema. | ||
| * | ||
| * Adds the `count` of each entry in the view list, which get_items() | ||
| * provides. | ||
| * | ||
| * @since 7.3.0 | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| $schema = parent::get_item_schema(); | ||
|
|
||
| $schema['properties']['view_list']['items']['properties']['count'] = array( | ||
| 'description' => __( 'Number of items the view holds.', 'gutenberg' ), | ||
| 'type' => 'integer', | ||
| 'readonly' => true, | ||
| ); | ||
|
|
||
| return $schema; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
| /** | ||
| * WordPress 7.3 compatibility functions for the Gutenberg | ||
| * editor plugin changes related to REST API. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Registers the View Config REST API routes. | ||
| * | ||
| * Replaces the 7.2 registration so the route is served by the 7.3 controller. | ||
| */ | ||
| function gutenberg_register_view_config_controller_endpoints_7_3() { | ||
| $view_config_controller = new Gutenberg_REST_View_Config_Controller_7_3(); | ||
| $view_config_controller->register_routes(); | ||
| } | ||
| remove_action( 'rest_api_init', 'gutenberg_register_view_config_controller_endpoints_7_2', PHP_INT_MAX ); | ||
| add_action( 'rest_api_init', 'gutenberg_register_view_config_controller_endpoints_7_3', PHP_INT_MAX ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?php | ||
| /** | ||
| * View config API: item counts of the view list. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Adds the item count of each view to a post type's view list. | ||
| * | ||
| * Runs on the view list as the client receives it, after every view config | ||
| * filter, so a count always describes the view it sits on. Only a view whose | ||
| * total a status count answers gets one: a view without filters, which lists | ||
| * every status but trash, and a view whose only filter is on the status. A view | ||
| * narrowed by anything else, such as a date filter a plugin added, is left | ||
| * without a count rather than showing a total it does not hold. That includes | ||
| * filters a view inherits from the default view or from the defaults of any | ||
| * layout it can be shown in. | ||
| * | ||
| * `wp_count_posts()` answers every view from one cached query, and its | ||
| * `readable` permission argument keeps private posts the current user cannot | ||
| * read out of the totals. | ||
| * | ||
| * @param string $post_type The post type the view list belongs to. | ||
| * @param array $view_list The view list. | ||
| * @param array|object $default_view The default view the views inherit from. | ||
| * @param array|object $default_layouts The default view properties of each layout. | ||
| * @return array The view list, with a count on each view a status total answers. | ||
| */ | ||
| function _gutenberg_add_counts_to_view_list( $post_type, $view_list, $default_view = array(), $default_layouts = array() ) { | ||
| $counts = (array) wp_count_posts( $post_type, 'readable' ); | ||
|
|
||
| foreach ( $view_list as $index => $entry ) { | ||
| $statuses = _gutenberg_get_view_list_entry_statuses( $entry, $default_view, $default_layouts ); | ||
| if ( null === $statuses ) { | ||
| continue; | ||
| } | ||
|
|
||
| $count = 0; | ||
| foreach ( $statuses as $status ) { | ||
| $count += isset( $counts[ $status ] ) ? (int) $counts[ $status ] : 0; | ||
| } | ||
| $view_list[ $index ]['count'] = $count; | ||
| } | ||
|
|
||
| return $view_list; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the statuses a view list entry lists, when the status is all it | ||
| * filters by in every layout it can be shown in. | ||
| * | ||
| * The user can switch a view to any layout on offer, and each layout's defaults | ||
| * can bring filters of their own. The server cannot see which layout the user | ||
| * picked, so a count has to hold in all of them: when the layouts disagree, the | ||
| * entry gets none. | ||
| * | ||
| * The entry and defaults come from view config filters, so their shape is not | ||
| * trusted. | ||
| * | ||
| * @param mixed $entry A view list entry. | ||
| * @param array|object $default_view The default view the entry inherits from. | ||
| * @param array|object $default_layouts The default view properties of each layout. | ||
| * @return string[]|null The statuses, or null when the view filters by anything | ||
| * else in some layout or its shape is not recognized. | ||
| */ | ||
| function _gutenberg_get_view_list_entry_statuses( $entry, $default_view = array(), $default_layouts = array() ) { | ||
| if ( ! is_array( $entry ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| $view = isset( $entry['view'] ) ? (array) $entry['view'] : array(); | ||
| $default_view = (array) $default_view; | ||
| $default_layouts = (array) $default_layouts; | ||
|
|
||
| // Without layouts on offer, no layout defaults apply. | ||
| $layouts = empty( $default_layouts ) ? array( array() ) : $default_layouts; | ||
|
|
||
| $statuses = null; | ||
| foreach ( $layouts as $layout_defaults ) { | ||
| $layout_statuses = _gutenberg_get_layered_view_statuses( | ||
| array( $default_view, (array) $layout_defaults, $view ) | ||
| ); | ||
| if ( null === $layout_statuses || ( null !== $statuses && $layout_statuses !== $statuses ) ) { | ||
| return null; | ||
| } | ||
| $statuses = $layout_statuses; | ||
| } | ||
|
|
||
| return $statuses; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the statuses a view lists, when the status is all it filters by. | ||
| * | ||
| * Resolves the view's filters out of its layers as the client does: a later | ||
| * layer that sets them replaces them outright rather than adding to them. A | ||
| * `search` in a layer is ignored, as the client drops it from the config and | ||
| * takes the search from the URL instead. | ||
| * | ||
| * @param array[] $layers The view's layers, lowest first. | ||
| * @return string[]|null The statuses, or null when the view filters by anything | ||
| * else or its shape is not recognized. | ||
| */ | ||
| function _gutenberg_get_layered_view_statuses( $layers ) { | ||
| $filters = array(); | ||
| foreach ( $layers as $layer ) { | ||
| if ( array_key_exists( 'filters', $layer ) ) { | ||
| $filters = $layer['filters']; | ||
| } | ||
| } | ||
|
|
||
| if ( ! is_array( $filters ) ) { | ||
| return null; | ||
| } | ||
| if ( empty( $filters ) ) { | ||
| // Every status but trash, matching what a view without filters queries. | ||
| return array( 'publish', 'future', 'draft', 'pending', 'private' ); | ||
| } | ||
| if ( 1 !== count( $filters ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| $filter = (array) reset( $filters ); | ||
| if ( | ||
| ! isset( $filter['field'], $filter['operator'], $filter['value'] ) || | ||
| 'status' !== $filter['field'] || | ||
| ! in_array( $filter['operator'], array( 'is', 'isAny' ), true ) | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| $values = is_array( $filter['value'] ) ? $filter['value'] : array( $filter['value'] ); | ||
| $statuses = array(); | ||
| foreach ( $values as $value ) { | ||
| if ( ! is_string( $value ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| // The REST API also accepts several statuses in one value, separated by | ||
| // commas or spaces, and parses them the same way. | ||
| foreach ( wp_parse_slug_list( $value ) as $status ) { | ||
| if ( 'any' === $status ) { | ||
| // As in WP_Query, `any` is every status not excluded from search. | ||
| $statuses = array_merge( $statuses, array_values( get_post_stati( array( 'exclude_from_search' => false ) ) ) ); | ||
| } else { | ||
| $statuses[] = $status; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A status listed twice is still queried once. | ||
| return array_values( array_unique( $statuses ) ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three global functions are only used by the View Config controller. Even with an underscore prefix, global functions effectively become public API, and we take on the responsibility of maintaining them. It’s also unclear how, or whether, they’ll be used elsewhere in the future. For now, how about moving them into the controller as protected methods? The same applies to the Core PR.