admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
acfe-user-functions.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * acfe_get_roles |
| 9 | * |
| 10 | * Retrieve all available roles. |
| 11 | * Multisite super admin role will be included as 'super_admin'. |
| 12 | * 'guest' role will be included for not logged in users. |
| 13 | * |
| 14 | * @param array $filter_roles |
| 15 | * |
| 16 | * @return array |
| 17 | */ |
| 18 | function acfe_get_roles($filter_roles = array()){ |
| 19 | |
| 20 | // vars |
| 21 | global $wp_roles; |
| 22 | $roles = array(); |
| 23 | |
| 24 | // multisite: prepend super admin role |
| 25 | if(is_multisite()){ |
| 26 | $roles['super_admin'] = __('Super Admin'); |
| 27 | } |
| 28 | |
| 29 | // loop roles |
| 30 | foreach(acfe_as_array($wp_roles->roles) as $role => $settings){ |
| 31 | $roles[ $role ] = $settings['name']; |
| 32 | } |
| 33 | |
| 34 | // append guest role (not logged in) |
| 35 | $roles['guest'] = __('Guest', 'acfe'); |
| 36 | |
| 37 | // filter roles |
| 38 | $filter_roles = acfe_as_array($filter_roles); |
| 39 | if(!empty($filter_roles)){ |
| 40 | $roles = array_intersect_key($roles, array_flip($filter_roles)); // only keep roles in filter |
| 41 | } |
| 42 | |
| 43 | // return |
| 44 | return $roles; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * acfe_get_current_user_roles |
| 50 | * |
| 51 | * Retrieve currently logged user roles. |
| 52 | * Not logged users will return 'guest' role. |
| 53 | * Multisite super admin will return 'super_admin' role in addition to their regular roles. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | function acfe_get_current_user_roles(){ |
| 58 | |
| 59 | // get current user |
| 60 | $current_user = wp_get_current_user(); |
| 61 | |
| 62 | // not logged in |
| 63 | if(empty($current_user->ID)){ |
| 64 | return array('guest'); |
| 65 | } |
| 66 | |
| 67 | // get roles |
| 68 | $roles = acfe_get_user_roles($current_user); |
| 69 | |
| 70 | // filter |
| 71 | $roles = apply_filters('acfe/load_current_user_roles', $roles); |
| 72 | |
| 73 | // return |
| 74 | return $roles; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * acfe_get_user_roles |
| 81 | * |
| 82 | * @param $user_or_id |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | function acfe_get_user_roles($user_or_id){ |
| 87 | |
| 88 | // default |
| 89 | $roles = array(); |
| 90 | |
| 91 | // prepare user |
| 92 | $user = $user_or_id; |
| 93 | |
| 94 | // get user object |
| 95 | if(is_numeric($user)){ |
| 96 | $user = get_user_by('id', $user); |
| 97 | } |
| 98 | |
| 99 | // validate |
| 100 | if(!$user instanceof WP_User){ |
| 101 | return $roles; |
| 102 | } |
| 103 | |
| 104 | // get roles |
| 105 | $roles = acfe_as_array($user->roles); |
| 106 | |
| 107 | // multisite: append super admin role if user is super admin |
| 108 | if(is_multisite() && current_user_can('setup_network')){ |
| 109 | $roles[] = 'super_admin'; |
| 110 | } |
| 111 | |
| 112 | // filter |
| 113 | $roles = apply_filters('acfe/load_user_roles', $roles, $user); |
| 114 | |
| 115 | // return |
| 116 | return $roles; |
| 117 | |
| 118 | |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * acfe_has_current_user_role |
| 124 | * |
| 125 | * @param $filter_roles |
| 126 | * |
| 127 | * @return bool |
| 128 | */ |
| 129 | function acfe_has_current_user_role($filter_roles){ |
| 130 | |
| 131 | // get current user roles |
| 132 | $user_roles = acfe_get_current_user_roles(); |
| 133 | |
| 134 | // normalize roles as array |
| 135 | $filter_roles = acfe_as_array($filter_roles); |
| 136 | |
| 137 | // check if role is in user roles |
| 138 | foreach($filter_roles as $filter_role){ |
| 139 | if(in_array($filter_role, $user_roles, true)){ |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // not found |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * acfe_has_user_role |
| 151 | * |
| 152 | * @param $user_or_id |
| 153 | * @param $filter_roles |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | function acfe_has_user_role($user_or_id, $filter_roles){ |
| 158 | |
| 159 | // get user roles |
| 160 | $user_roles = acfe_get_user_roles($user_or_id); |
| 161 | |
| 162 | // normalize roles as array |
| 163 | $filter_roles = acfe_as_array($filter_roles); |
| 164 | |
| 165 | // check if role is in user roles |
| 166 | foreach($filter_roles as $filter_role){ |
| 167 | if(in_array($filter_role, $user_roles, true)){ |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // not found |
| 173 | return false; |
| 174 | |
| 175 | } |