Skip to content

Privacy: Introduce wp_count_user_requests() to publicly expose user request counts by type - #12716

Draft
SainathPoojary wants to merge 2 commits into
WordPress:trunkfrom
SainathPoojary:fix/44034
Draft

Privacy: Introduce wp_count_user_requests() to publicly expose user request counts by type#12716
SainathPoojary wants to merge 2 commits into
WordPress:trunkfrom
SainathPoojary:fix/44034

Conversation

@SainathPoojary

Copy link
Copy Markdown

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.

Copilot AI review requested due to automatic review settings July 27, 2026 14:35
@github-actions

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 ) in wp-includes/user.php to 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.

Comment thread src/wp-includes/user.php Outdated
Comment on lines +4866 to +4868
$cache_key = 'user-request-' . $type;
$counts = wp_cache_get( $cache_key, 'counts' );

Comment thread src/wp-includes/user.php
Comment on lines +4859 to +4864
function wp_count_user_requests( $type = '' ) {
global $wpdb;

if ( ! in_array( $type, _wp_privacy_action_request_types(), true ) ) {
return new stdClass();
}
Copilot AI review requested due to automatic review settings July 27, 2026 14:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 object for $counts, but this function returns a stdClass (matching other count APIs). Using stdClass here 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.php and tests/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();
	}

Comment thread src/wp-includes/user.php
Comment on lines +4848 to +4853
/**
* 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.
Comment thread src/wp-includes/user.php
Comment on lines +4870 to +4873
if ( false !== $counts ) {
/** This filter is documented in wp-includes/user.php */
return apply_filters( 'wp_count_user_requests', $counts, $type );
}
Comment thread src/wp-includes/user.php
Comment on lines +4855 to +4858
* @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.
*/
Comment on lines 105 to 109
* @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 );
}
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