From d1e953335bd92058319ca57ad801c2ee770c681e Mon Sep 17 00:00:00 2001 From: Adam Cassis Date: Wed, 17 Jul 2024 12:36:20 +0200 Subject: [PATCH] feat(users): bulk user network sync --- includes/class-users.php | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/includes/class-users.php b/includes/class-users.php index f5391d99..e50b9c17 100644 --- a/includes/class-users.php +++ b/includes/class-users.php @@ -13,6 +13,8 @@ * Class to handle the Users admin page */ class Users { + private const SYNC_BULK_ACTION = 'sync-across-network'; + private const SYNC_BULK_SENDBACK_PARAM = 'np_network_manual_sync_updated_user_count'; /** * Runs the initialization. @@ -21,6 +23,9 @@ public static function init() { add_filter( 'manage_users_columns', [ __CLASS__, 'manage_users_columns' ] ); add_filter( 'manage_users_custom_column', [ __CLASS__, 'manage_users_custom_column' ], 99, 3 ); // priority must be higher than Jetpack's jetpack_show_connection_status (10). add_filter( 'users_list_table_query_args', [ __CLASS__, 'users_list_table_query_args' ] ); + add_filter( 'bulk_actions-users', [ __CLASS__, 'add_users_bulk_actions' ] ); + add_filter( 'handle_bulk_actions-users', [ __CLASS__, 'users_bulk_actions_sendback' ], 10, 3 ); + add_action( 'admin_init', [ __CLASS__, 'handle_users_bulk_actions' ] ); } /** @@ -119,4 +124,71 @@ public static function users_list_table_query_args( $args ) { } return $args; } + + /** + * Add custom bulk actions to the Users table. + * + * @param array $actions An array of the available bulk actions. + */ + public static function add_users_bulk_actions( $actions ) { + if ( current_user_can( 'edit_users' ) ) { + $actions[ self::SYNC_BULK_ACTION ] = __( 'Sync across network', 'newspack-network' ); + } + + return $actions; + } + + /** + * Change sendback URL for the bulk action. + * + * @param string $sendback The redirect URL. + * @param string $action The action being taken. + * @param array $user_ids The items to take the action on. + */ + public static function users_bulk_actions_sendback( $sendback, $action, $user_ids ) { + if ( $action === self::SYNC_BULK_ACTION && ! empty( $user_ids ) ) { + $sendback = add_query_arg( self::SYNC_BULK_SENDBACK_PARAM, count( $user_ids ), $sendback ); + } + return $sendback; + } + + /** + * Handle custom bulk actions to the Users table. + */ + public static function handle_users_bulk_actions() { + // Handle the bulk-manual-sync request. + if ( isset( $_GET['action'], $_REQUEST['users'], $_REQUEST['_wpnonce'] ) && $_GET['action'] === self::SYNC_BULK_ACTION ) { + if ( ! wp_verify_nonce( sanitize_text_field( $_REQUEST['_wpnonce'] ), 'bulk-users' ) ) { + return; + } + $user_ids = array_map( 'intval', (array) $_REQUEST['users'] ); + foreach ( $user_ids as $user_id ) { + $user = get_user_by( 'ID', $user_id ); + if ( $user ) { + do_action( 'newspack_network_manual_sync_user', $user ); + } + } + } + + // Handle the admin notice. + if ( isset( $_GET[ self::SYNC_BULK_SENDBACK_PARAM ] ) && $_GET[ self::SYNC_BULK_SENDBACK_PARAM ] > 0 ) { + $count = intval( $_GET[ self::SYNC_BULK_SENDBACK_PARAM ] ); + $message = sprintf( + /* translators: %d is the users count. */ + _n( + 'Scheduled %d user to be synced across the network.', + 'Scheduled %d users to be synced across the network.', + $count, + 'newspack-network' + ), + $count + ); + add_action( + 'admin_notices', + function () use ( $message ) { + printf( '

%s

', esc_html( $message ) ); + } + ); + } + } }