| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\data; |
| 4 |
|
| 5 |
/** |
| 6 |
* Static function that wraps the WordPress current user functions. |
| 7 |
*/ |
| 8 |
class User { |
| 9 |
/** |
| 10 |
* Return the first role of the current user. If unauthenticated, return 'visitor'. |
| 11 |
*/ |
| 12 |
public static function get_role() { |
| 13 |
global $current_user; |
| 14 |
|
| 15 |
if ( is_user_logged_in() ) { |
| 16 |
$user_roles = $current_user->roles; |
| 17 |
$user_role = array_shift( $user_roles ); |
| 18 |
} else { |
| 19 |
$user_role = 'visitor'; |
| 20 |
} |
| 21 |
|
| 22 |
return $user_role; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Return true if the current user has the `manage_options` capability. |
| 27 |
*/ |
| 28 |
public static function is_admin() { |
| 29 |
return current_user_can( 'manage_options' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Set metadata for current user |
| 34 |
* |
| 35 |
* @param String $key metadata key to store data in. |
| 36 |
* @param String $data metadata value to store. |
| 37 |
*/ |
| 38 |
public static function set_metadata( $key, $data ) { |
| 39 |
update_user_meta( get_current_user_id(), $key, $data ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Fetch metadata stored for this user by key |
| 44 |
* |
| 45 |
* @param String $key metadata to retrieve by the key. |
| 46 |
*/ |
| 47 |
public static function get_metadata( $key ) { |
| 48 |
return get_user_meta( get_current_user_id(), $key, true ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Delete metadata associated with this user |
| 53 |
* |
| 54 |
* @param String $key key to delete metadata for. |
| 55 |
*/ |
| 56 |
public static function delete_metadata( $key ) { |
| 57 |
delete_user_meta( get_current_user_id(), $key ); |
| 58 |
} |
| 59 |
} |
| 60 |
|