| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Restrict User Access |
| 4 |
* @author Joachim Jensen <joachim@dev.institute> |
| 5 |
* @license GPLv3 |
| 6 |
* @copyright 2022 by Joachim Jensen |
| 7 |
*/ |
| 8 |
|
| 9 |
class RUA_User implements RUA_User_Interface |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var WP_User |
| 13 |
*/ |
| 14 |
private $wp_user; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var RUA_Collection<RUA_User_Level>|RUA_User_Level[]|null |
| 18 |
*/ |
| 19 |
private $level_memberships; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private static $caps_cache = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param WP_User $user |
| 28 |
*/ |
| 29 |
public function __construct(WP_User $user) |
| 30 |
{ |
| 31 |
$this->wp_user = $user; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @inheritDoc |
| 36 |
*/ |
| 37 |
public function get_id() |
| 38 |
{ |
| 39 |
return $this->wp_user->ID; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @inheritDoc |
| 44 |
*/ |
| 45 |
public function get_attribute($name, $default_value = null) |
| 46 |
{ |
| 47 |
if ($this->wp_user->has_prop($name)) { |
| 48 |
return $this->wp_user->get($name); |
| 49 |
} |
| 50 |
return $default_value; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @inheritDoc |
| 55 |
*/ |
| 56 |
public function has_global_access() |
| 57 |
{ |
| 58 |
$has_access = in_array('administrator', $this->wp_user->roles); |
| 59 |
return apply_filters('rua/user/global-access', $has_access, $this->wp_user); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @inheritDoc |
| 64 |
*/ |
| 65 |
public function level_memberships() |
| 66 |
{ |
| 67 |
if (is_null($this->level_memberships)) { |
| 68 |
$user_id = $this->get_id(); |
| 69 |
$level_ids = []; |
| 70 |
|
| 71 |
if ($user_id) { |
| 72 |
$level_ids = (array)get_user_meta($user_id, RUA_App::META_PREFIX . 'level', false); |
| 73 |
} |
| 74 |
|
| 75 |
$this->level_memberships = new RUA_Collection(); |
| 76 |
$level_ids = array_unique($level_ids); |
| 77 |
foreach ($level_ids as $level_id) { |
| 78 |
$level_id = (int)$level_id; |
| 79 |
try { |
| 80 |
$this->level_memberships->put($level_id, rua_get_user_level($level_id, $this)); |
| 81 |
} catch (Exception $e) { |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return $this->level_memberships; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @since 1.1 |
| 90 |
* @param bool $hierarchical - include inherited levels |
| 91 |
* @param bool $synced_roles - include levels synced with role |
| 92 |
* @param bool $include_expired |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function get_level_ids( |
| 96 |
$hierarchical = true, |
| 97 |
$synced_roles = true, |
| 98 |
$include_expired = false |
| 99 |
) { |
| 100 |
if (!$hierarchical || !$synced_roles || $include_expired) { |
| 101 |
_deprecated_argument(__FUNCTION__, '2.1'); |
| 102 |
} |
| 103 |
|
| 104 |
$level_ids = []; |
| 105 |
foreach ($this->level_memberships() as $membership) { |
| 106 |
if (!$include_expired && !$membership->is_active()) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$level_ids[] = $membership->get_level_id(); |
| 111 |
if ($hierarchical) { |
| 112 |
$level_ids = array_merge($level_ids, $membership->get_level_extend_ids()); |
| 113 |
} |
| 114 |
} |
| 115 |
return apply_filters('rua/user_levels', $level_ids, $this); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @inheritDoc |
| 120 |
*/ |
| 121 |
public function add_level($level_id) |
| 122 |
{ |
| 123 |
$user_id = $this->get_id(); |
| 124 |
if (!$this->has_level($level_id)) { |
| 125 |
$this->reset_caps_cache(); |
| 126 |
add_user_meta($user_id, RUA_App::META_PREFIX . 'level', $level_id, false); |
| 127 |
add_user_meta($user_id, RUA_App::META_PREFIX . 'level_' . $level_id, time(), true); |
| 128 |
add_user_meta($user_id, RUA_App::META_PREFIX . 'level_status_' . $level_id, 'active', true); |
| 129 |
|
| 130 |
$this->level_memberships()->put($level_id, rua_get_user_level($level_id, $this)); |
| 131 |
do_action('rua/user_level/added', $this, $level_id); |
| 132 |
return true; |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @inheritDoc |
| 139 |
*/ |
| 140 |
public function remove_level($level_id) |
| 141 |
{ |
| 142 |
$user_id = $this->get_id(); |
| 143 |
$this->reset_caps_cache(); |
| 144 |
$deleted = delete_user_meta($user_id, RUA_App::META_PREFIX . 'level', $level_id); |
| 145 |
|
| 146 |
delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_' . $level_id); |
| 147 |
delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_status_' . $level_id); |
| 148 |
delete_user_meta($user_id, RUA_App::META_PREFIX . 'level_expiry_' . $level_id); |
| 149 |
|
| 150 |
if ($deleted) { |
| 151 |
$this->level_memberships()->remove($level_id); |
| 152 |
do_action('rua/user_level/removed', $this, $level_id); |
| 153 |
} |
| 154 |
return $deleted; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @inheritDoc |
| 159 |
*/ |
| 160 |
public function has_level($level_id) |
| 161 |
{ |
| 162 |
$memberships = $this->level_memberships(); |
| 163 |
return $memberships->has($level_id) && $memberships->get($level_id)->is_active(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @inheritDoc |
| 168 |
*/ |
| 169 |
public function get_level_start($level_id) |
| 170 |
{ |
| 171 |
_deprecated_function(__FUNCTION__, '2.1', 'level_memberships()->get($level_id)->get_start()'); |
| 172 |
if (!$this->has_level($level_id)) { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
return $this->level_memberships()->get($level_id)->get_start(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @inheritDoc |
| 180 |
*/ |
| 181 |
public function get_level_expiry($level_id) |
| 182 |
{ |
| 183 |
_deprecated_function(__FUNCTION__, '2.1', 'level_memberships()->get($level_id)->get_expiry()'); |
| 184 |
if (!$this->has_level($level_id)) { |
| 185 |
return 0; |
| 186 |
} |
| 187 |
return $this->level_memberships()->get($level_id)->get_expiry(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @inheritDoc |
| 192 |
*/ |
| 193 |
public function is_level_expired($level_id) |
| 194 |
{ |
| 195 |
_deprecated_function(__FUNCTION__, '2.1', '!level_memberships()->get($level_id)->is_active()'); |
| 196 |
if (!$this->has_level($level_id)) { |
| 197 |
return true; |
| 198 |
} |
| 199 |
return !$this->level_memberships()->get($level_id)->is_active(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* @inheritDoc |
| 204 |
*/ |
| 205 |
public function get_caps($current_caps = []) |
| 206 |
{ |
| 207 |
if (!isset(self::$caps_cache[$this->get_id()])) { |
| 208 |
self::$caps_cache[$this->get_id()] = $current_caps; |
| 209 |
|
| 210 |
if (!$this->has_global_access()) { |
| 211 |
$levels = $this->get_level_ids(); |
| 212 |
if ($levels) { |
| 213 |
self::$caps_cache[$this->get_id()] = array_merge( |
| 214 |
self::$caps_cache[$this->get_id()], |
| 215 |
//Make sure higher levels have priority |
| 216 |
//Side-effect: synced levels < normal levels |
| 217 |
RUA_App::instance()->level_manager->get_levels_caps(array_reverse($levels)) |
| 218 |
); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
return self::$caps_cache[$this->get_id()]; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* @since 1.1 |
| 227 |
*/ |
| 228 |
private function reset_caps_cache() |
| 229 |
{ |
| 230 |
unset(self::$caps_cache[$this->get_id()]); |
| 231 |
} |
| 232 |
} |
| 233 |
|