| @@ -12,17 +12,25 @@ | ||
| 12 | 12 | /** |
| 13 | 13 | * Current User Id |
| 14 | 14 | * @var integer |
| 15 | 15 | */ |
| 16 | - private $current_user = 0; | |
| 16 | + private $current_user; | |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * User has any api? |
| 20 | 20 | * @var boolean |
| 21 | 21 | */ |
| 22 | - private $has_api = false; | |
| 22 | + private $has_api; | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | + * Pin every derived read/write to the acting user, bypassing the | |
| 26 | + * global-login fallback in user_id(). | |
| 27 | + * | |
| 28 | + * @var bool | |
| 29 | + */ | |
| 30 | + private $force_current_user = false; | |
| 31 | + | |
| 32 | + /** | |
| 25 | 33 | * Automatically invoked and set up the properties. |
| 26 | 34 | */ |
| 27 | 35 | public function __construct(){ |
| 28 | 36 | $this->current_user = get_current_user_id(); |
| @@ -32,9 +40,9 @@ | ||
| 32 | 40 | /** |
| 33 | 41 | * Get the current user ID. |
| 34 | 42 | * @return int |
| 35 | 43 | */ |
| 36 | - public function current_user_id(){ | |
| 44 | + public function current_user_id(): int { | |
| 37 | 45 | return $this->current_user; |
| 38 | 46 | } |
| 39 | 47 | |
| 40 | 48 | /** |
| @@ -40,101 +48,120 @@ | ||
| 40 | 48 | /** |
| 41 | 49 | * Can a user link another templately account in a setup?. |
| 42 | 50 | * @return boolean |
| 43 | 51 | */ |
| 44 | - public function link_account() { | |
| 45 | - return $this->whoami() === 'link' && ! $this->has_api; | |
| 52 | + public function link_account(): bool { | |
| 53 | + return $this->who_am_i() === 'link' && ! $this->has_api; | |
| 46 | 54 | } |
| 47 | 55 | |
| 48 | - public function unlink_account() { | |
| 49 | - return ( $this->whoami() === 'link' || $this->whoami() === 'local' ) && $this->has_api; | |
| 56 | + public function unlink_account(): bool { | |
| 57 | + return ( $this->who_am_i() === 'link' || $this->who_am_i() === 'local' ) && $this->has_api; | |
| 50 | 58 | } |
| 59 | + | |
| 51 | 60 | /** |
| 52 | 61 | * Get determined who am I. |
| 53 | 62 | * @return string |
| 54 | 63 | */ |
| 55 | - public function whoami(){ | |
| 56 | - $_whoami = 'local'; | |
| 64 | + public function who_am_i(): string { | |
| 65 | + $_who_am_i = 'local'; | |
| 57 | 66 | |
| 58 | 67 | if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) { |
| 59 | - $_whoami = 'global'; | |
| 68 | + $_who_am_i = 'global'; | |
| 60 | 69 | } |
| 61 | 70 | |
| 62 | 71 | if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) { |
| 63 | - $_whoami = 'link'; | |
| 72 | + $_who_am_i = 'link'; | |
| 64 | 73 | } |
| 65 | 74 | |
| 66 | 75 | if( $this->is_global() == 0 ) { |
| 67 | - $_whoami = 'local'; | |
| 76 | + $_who_am_i = 'local'; | |
| 68 | 77 | } |
| 69 | 78 | |
| 70 | - return $_whoami; | |
| 79 | + return $_who_am_i; | |
| 71 | 80 | } |
| 81 | + | |
| 72 | 82 | /** |
| 83 | + * Pin the acting user as the target for every derived read/write. | |
| 84 | + * | |
| 85 | + * @param bool $force Whether to force the current user. | |
| 86 | + * @return Options | |
| 87 | + */ | |
| 88 | + public function use_current_user( bool $force = true ): Options { | |
| 89 | + $this->force_current_user = $force; | |
| 90 | + | |
| 91 | + return $this; | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 73 | 95 | * Get user id determine dynamically |
| 74 | 96 | * @return integer |
| 75 | 97 | */ |
| 76 | - private function user_id(){ | |
| 77 | - $_whoami = $this->whoami(); | |
| 78 | - // Helper::log( '_whoami: ' . $_whoami ); | |
| 98 | + private function user_id(): int { | |
| 99 | + if ( $this->force_current_user ) { | |
| 100 | + return $this->current_user; | |
| 101 | + } | |
| 79 | 102 | |
| 103 | + $_who_am_i = $this->who_am_i(); | |
| 104 | + | |
| 80 | 105 | if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 81 | 106 | $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) ); |
| 82 | - if( $_whoami === 'link' && array_pop( $parse_uri ) === 'login' ) { | |
| 107 | + if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) { | |
| 83 | 108 | return $this->current_user; |
| 84 | 109 | } |
| 85 | 110 | } |
| 86 | 111 | |
| 87 | - if( $_whoami === 'link' && $this->has_api ) { | |
| 112 | + if( $_who_am_i === 'link' && $this->has_api ) { | |
| 88 | 113 | return $this->current_user; |
| 89 | 114 | } |
| 90 | 115 | |
| 91 | - return $_whoami === 'local' ? $this->current_user : $this->is_global(); | |
| 116 | + return $_who_am_i === 'local' ? $this->current_user : $this->is_global(); | |
| 92 | 117 | } |
| 118 | + | |
| 93 | 119 | /** |
| 94 | 120 | * Globally logged in and the User ID of globally logged-in user. |
| 95 | 121 | * @return integer |
| 96 | 122 | */ |
| 97 | - public function is_global(){ | |
| 123 | + public function is_global(): int { | |
| 98 | 124 | return intval( get_option('_templately_global_login', 0) ); |
| 99 | 125 | } |
| 126 | + | |
| 100 | 127 | /** |
| 101 | 128 | * Set global login flag |
| 102 | 129 | * @return boolean |
| 103 | 130 | */ |
| 104 | - public static function set_global_login() { | |
| 131 | + public static function set_global_login(): bool { | |
| 105 | 132 | return update_option('_templately_global_login', get_current_user_id(), 'no'); |
| 106 | 133 | } |
| 134 | + | |
| 107 | 135 | /** |
| 108 | 136 | * Remove global login flag |
| 109 | 137 | * @return boolean |
| 110 | 138 | */ |
| 111 | - public function remove_global_login() { | |
| 139 | + public function remove_global_login(): bool { | |
| 112 | 140 | return delete_option( '_templately_global_login' ); |
| 113 | 141 | } |
| 114 | 142 | |
| 115 | - public function is_globally_signed() { | |
| 116 | - return $this->whoami() !== 'local'; | |
| 143 | + public function is_globally_signed(): bool { | |
| 144 | + return $this->who_am_i() !== 'local'; | |
| 117 | 145 | } |
| 118 | - public function signed_as_global() { | |
| 146 | + | |
| 147 | + public function signed_as_global(): bool { | |
| 119 | 148 | return $this->current_user === $this->is_global(); |
| 120 | 149 | } |
| 150 | + | |
| 121 | 151 | /** |
| 122 | - * Set optional usermeta or option data | |
| 152 | + * Set optional user meta or option data | |
| 123 | 153 | * |
| 124 | 154 | * @param string $key |
| 125 | 155 | * @param mixed $value |
| 156 | + * @param null $user_id | |
| 126 | 157 | * @return boolean |
| 127 | 158 | */ |
| 128 | - public function set( $key, $value, $user_id = null ){ | |
| 159 | + public function set( $key, $value, $user_id = null ): bool { | |
| 129 | 160 | $key = '_templately_' . $key; |
| 130 | 161 | |
| 131 | - if( $user_id === null ) { | |
| 132 | - $user_id = $this->user_id(); | |
| 133 | - } | |
| 162 | + $updated = $this->update_user_meta( $user_id, $key, $value ); | |
| 134 | 163 | |
| 135 | - $updated = update_user_meta( $user_id, $key, $value ); | |
| 136 | - | |
| 137 | 164 | if( $key === '_templately_api_key' && $updated ) { |
| 138 | 165 | $this->has_api = true; |
| 139 | 166 | } |
| 140 | 167 | |
| @@ -139,10 +166,11 @@ | ||
| 139 | 166 | } |
| 140 | 167 | |
| 141 | 168 | return $updated; |
| 142 | 169 | } |
| 170 | + | |
| 143 | 171 | /** |
| 144 | - * Get optional usermeta or option data | |
| 172 | + * Get optional user meta or option data | |
| 145 | 173 | * |
| 146 | 174 | * @param string $key |
| 147 | 175 | * @param mixed $default |
| 148 | 176 | * @return mixed |
| @@ -148,20 +176,21 @@ | ||
| 148 | 176 | * @return mixed |
| 149 | 177 | */ |
| 150 | 178 | public function get( $key, $default = false, $user_id = null ){ |
| 151 | 179 | $key = '_templately_' . $key; |
| 152 | - $_user_meta = get_user_meta( is_null( $user_id ) ? $this->user_id() : $user_id, $key, true ); | |
| 180 | + $_user_meta = $this->get_user_meta( $user_id, $key, true ); | |
| 153 | 181 | return ! empty( $_user_meta ) ? $_user_meta : $default; |
| 154 | 182 | } |
| 183 | + | |
| 155 | 184 | /** |
| 156 | - * Remove options data or usermeta | |
| 185 | + * Remove options data or user meta | |
| 157 | 186 | * |
| 158 | 187 | * @param string $key |
| 159 | 188 | * @return Options |
| 160 | 189 | */ |
| 161 | - public function remove( $key ){ | |
| 190 | + public function remove( string $key ): Options { | |
| 162 | 191 | $key = '_templately_' . $key; |
| 163 | - $updated = delete_user_meta( $this->user_id(), $key ); // delete user meta | |
| 192 | + $updated = $this->delete_user_meta( $key ); | |
| 164 | 193 | |
| 165 | 194 | if( $key === '_templately_api_key' && $updated ) { |
| 166 | 195 | $this->has_api = false; |
| 167 | 196 | } |
| @@ -168,9 +197,63 @@ | ||
| 168 | 197 | |
| 169 | 198 | return $this; |
| 170 | 199 | } |
| 171 | 200 | |
| 201 | + public function get_user_meta( $user_id, $key = '', $single = false ) { | |
| 202 | + $user_id = is_null( $user_id ) ? $this->user_id() : $user_id; | |
| 203 | + | |
| 204 | + if( ! is_multisite() ) { | |
| 205 | + return get_user_meta( $user_id, $key, $single); | |
| 206 | + } | |
| 207 | + | |
| 208 | + return get_user_option( $key, $user_id ); | |
| 209 | + } | |
| 210 | + | |
| 211 | + public function update_user_meta($user_id, $meta_key, $meta_value) { | |
| 212 | + if ( is_null( $user_id ) ) { | |
| 213 | + if ( ! $this->can_write() ) { | |
| 214 | + return false; | |
| 215 | + } | |
| 216 | + | |
| 217 | + $user_id = $this->user_id(); | |
| 218 | + } | |
| 219 | + | |
| 220 | + if( ! is_multisite() ) { | |
| 221 | + return update_user_meta( $user_id, $meta_key, $meta_value ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + return update_user_option( $user_id, $meta_key, $meta_value, $this->_is_global() ); | |
| 225 | + } | |
| 226 | + | |
| 227 | + public function delete_user_meta( $meta_key ): bool { | |
| 228 | + if ( ! $this->can_write() ) { | |
| 229 | + return false; | |
| 230 | + } | |
| 231 | + | |
| 232 | + if( ! is_multisite() ) { | |
| 233 | + return delete_user_meta( $this->user_id(), $meta_key ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + return delete_user_option( $this->user_id(), $meta_key, $this->_is_global() ); | |
| 237 | + } | |
| 238 | + | |
| 172 | 239 | /** |
| 240 | + * Whether the current request may write to a derived user target. | |
| 241 | + * | |
| 242 | + * Reads retain the global-login fallback for unauthenticated cloud callbacks, | |
| 243 | + * but an anonymous request must never write through it to the administrator. | |
| 244 | + * | |
| 245 | + * @return bool | |
| 246 | + */ | |
| 247 | + private function can_write(): bool { | |
| 248 | + return $this->current_user > 0; | |
| 249 | + } | |
| 250 | + | |
| 251 | + private function _is_global() { | |
| 252 | + return apply_filters( 'templately_multisite_is_global', false ); | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 173 | 256 | * Get option data |
| 174 | 257 | * |
| 175 | 258 | * @since 2.0.1 |
| 176 | 259 | * |
| @@ -193,8 +276,8 @@ | ||
| 193 | 276 | * @param string $autoload |
| 194 | 277 | * |
| 195 | 278 | * @return bool |
| 196 | 279 | */ |
| 197 | - public function update_option( $key, $value, $autoload = 'no' ){ | |
| 280 | + public function update_option( $key, $value, $autoload = 'no' ): bool { | |
| 198 | 281 | return update_option( $key, $value, $autoload ); |
| 199 | 282 | } |
| 200 | -} | |
| 283 | +} | |