Privacy: Introduce wp_count_user_requests() to publicly expose user request counts by type - #12716
Privacy: Introduce wp_count_user_requests() to publicly expose user request counts by type#12716SainathPoojary wants to merge 2 commits into
Conversation
…equest counts by type
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
Introduces a new public API, wp_count_user_requests(), to expose counts of privacy user requests by request type and grouped by post status, and refactors the privacy requests list table to use this centralized function.
Changes:
- Added
wp_count_user_requests( $type )inwp-includes/user.phpto return per-status counts for privacy request types. - Refactored
WP_Privacy_Requests_Table::get_request_counts()to call the new function instead of duplicating SQL + cache logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/wp-includes/user.php | Adds the new public counting function with caching and a filter. |
| src/wp-admin/includes/class-wp-privacy-requests-table.php | Removes duplicated counting logic and delegates counts to the new API. |
Comments suppressed due to low confidence (1)
src/wp-includes/user.php:4894
- When switching
wp_count_user_requests()to use salted cache reads, the write should also store the salted structure; otherwise every request is a cache miss (and if a different implementation stores non-array values under the same key, reads will always miss).
wp_cache_set( $cache_key, $counts, 'counts' );
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $cache_key = 'user-request-' . $type; | ||
| $counts = wp_cache_get( $cache_key, 'counts' ); | ||
|
|
| function wp_count_user_requests( $type = '' ) { | ||
| global $wpdb; | ||
|
|
||
| if ( ! in_array( $type, _wp_privacy_action_request_types(), true ) ) { | ||
| return new stdClass(); | ||
| } |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (2)
src/wp-includes/user.php:4904
- The filter doc uses
objectfor$counts, but this function returns astdClass(matching other count APIs). UsingstdClasshere improves consistency and accuracy of the inline docs.
* @param object $counts Number of requests for each status.
* @param string $type Request type.
*/
src/wp-includes/user.php:4864
wp_count_user_requests()is a new public API and currently has no PHPUnit coverage. There are existing privacy request tests (e.g.tests/phpunit/tests/privacy/wpCreateUserRequest.phpandtests/phpunit/tests/admin/wpPrivacyRequestsTable.php), so it would be good to add tests that (a) create requests for both core types with different statuses and (b) assert the returned counts per status and that invalid types return an empty object.
function wp_count_user_requests( $type = '' ) {
global $wpdb;
if ( ! in_array( $type, _wp_privacy_action_request_types(), true ) ) {
return new stdClass();
}
| /** | ||
| * Counts the number of user requests for a given request type, grouped by status. | ||
| * | ||
| * @since 6.8.0 | ||
| * | ||
| * @global wpdb $wpdb WordPress database abstraction object. |
| if ( false !== $counts ) { | ||
| /** This filter is documented in wp-includes/user.php */ | ||
| return apply_filters( 'wp_count_user_requests', $counts, $type ); | ||
| } |
| * @param string $type Request type. Accepted values are `'export_personal_data'` | ||
| * and `'remove_personal_data'`. | ||
| * @return object Number of requests for each status, or an empty object for an invalid type. | ||
| */ |
| * @return object Number of posts for each status. | ||
| */ | ||
| protected function get_request_counts() { | ||
| global $wpdb; | ||
|
|
||
| $cache_key = $this->post_type . '-' . $this->request_type; | ||
| $counts = wp_cache_get( $cache_key, 'counts' ); | ||
|
|
||
| if ( false !== $counts ) { | ||
| return $counts; | ||
| } | ||
|
|
||
| $results = (array) $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| "SELECT post_status, COUNT( * ) AS num_posts | ||
| FROM {$wpdb->posts} | ||
| WHERE post_type = %s | ||
| AND post_name = %s | ||
| GROUP BY post_status", | ||
| $this->post_type, | ||
| $this->request_type | ||
| ), | ||
| ARRAY_A | ||
| ); | ||
|
|
||
| $counts = array_fill_keys( get_post_stati(), 0 ); | ||
|
|
||
| foreach ( $results as $row ) { | ||
| $counts[ $row['post_status'] ] = $row['num_posts']; | ||
| } | ||
|
|
||
| $counts = (object) $counts; | ||
| wp_cache_set( $cache_key, $counts, 'counts' ); | ||
|
|
||
| return $counts; | ||
| return wp_count_user_requests( $this->request_type ); | ||
| } |
Trac ticket: #44034
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.