Skip to content

Commit 6e0e8a0

Browse files
committed
Add helper to get all tokens for a user
1 parent 81c5a80 commit 6e0e8a0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

inc/tokens/class-access-token.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ public static function get_by_id( $id ) {
6060
return new static( $user, $id, $value[0] );
6161
}
6262

63+
/**
64+
* Get all tokens for the specified user.
65+
*
66+
* @return static[] List of tokens.
67+
*/
68+
public static function get_for_user( WP_User $user ) {
69+
$meta = get_user_meta( $user->ID );
70+
$tokens = [];
71+
foreach ( $meta as $key => $values ) {
72+
if ( strpos( $key, static::META_PREFIX ) !== 0 ) {
73+
continue;
74+
}
75+
76+
$real_key = substr( $key, strlen( static::META_PREFIX ) );
77+
$value = maybe_unserialize( $values[0] );
78+
$tokens[] = new static( $user, $real_key, $value );
79+
}
80+
return $tokens;
81+
}
82+
6383
/**
6484
* Creates a new token for the given client and user.
6585
*

0 commit comments

Comments
 (0)