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