Skip to content

Commit

Permalink
feat(users): bulk user network sync
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Jul 18, 2024
1 parent f994478 commit d1e9533
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions includes/class-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' ] );
}

/**
Expand Down Expand Up @@ -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( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html( $message ) );
}
);
}
}
}

0 comments on commit d1e9533

Please sign in to comment.