Pending Privacy Requests Badge in the Admin Menu
Shows a count bubble next to Tools and the Export and Erase Personal Data menu items when confirmed privacy requests are waiting for you to process them.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
global $menu, $submenu, $wpdb;
if ( ! current_user_can( 'export_others_personal_data' ) && ! current_user_can( 'erase_others_personal_data' ) ) {
return;
}
$counts = get_transient( 'wpide_privacy_request_counts' );
if ( false === $counts ) {
$counts = array( 'export_personal_data' => 0, 'remove_personal_data' => 0 );
$rows = $wpdb->get_results( "SELECT post_name, COUNT(*) AS total FROM {$wpdb->posts} WHERE post_type = 'user_request' AND post_status = 'request-confirmed' GROUP BY post_name" );
foreach ( $rows as $row ) {
$counts[ $row->post_name ] = (int) $row->total;
}
set_transient( 'wpide_privacy_request_counts', $counts, 10 * MINUTE_IN_SECONDS );
}
$bubble = function ( $count ) {
return $count ? ' <span class="awaiting-mod"><span class="pending-count">' . (int) $count . '</span></span>' : '';
};
$total = array_sum( $counts );
if ( ! $total || empty( $submenu['tools.php'] ) ) {
return;
}
foreach ( $submenu['tools.php'] as &$item ) {
if ( 'export-personal-data.php' === $item[2] ) {
$item[0] .= $bubble( $counts['export_personal_data'] );
} elseif ( 'erase-personal-data.php' === $item[2] ) {
$item[0] .= $bubble( $counts['remove_personal_data'] );
}
}
unset( $item );
foreach ( (array) $menu as $key => $item ) {
if ( isset( $item[2] ) && 'tools.php' === $item[2] ) {
$menu[ $key ][0] .= $bubble( $total );
}
}
}, 99 );
add_action( 'user_request_action_confirmed', function () {
delete_transient( 'wpide_privacy_request_counts' );
} );
add_action( 'transition_post_status', function ( $new, $old, $post ) {
if ( 'user_request' === $post->post_type ) {
delete_transient( 'wpide_privacy_request_counts' );
}
}, 10, 3 );
Notes
Only confirmed requests are counted, since those are the ones that need action from you.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.