| @@ -21,16 +21,8 @@ | ||
| 21 | 21 | */ |
| 22 | 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 | - /** | |
| 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 | 36 | public function current_user_id(): int { |
| 53 | - if ( $this->current_user <= 0 ) { | |
| 54 | - $this->current_user = get_current_user_id(); | |
| 55 | - } | |
| 56 | - | |
| 57 | 37 | return $this->current_user; |
| 58 | 38 | } |
| 59 | 39 | |
| 60 | 40 | /** |
| @@ -75,13 +55,13 @@ | ||
| 75 | 55 | */ |
| 76 | 56 | public function who_am_i(): string { |
| 77 | 57 | $_who_am_i = 'local'; |
| 78 | 58 | |
| 79 | - if( $this->is_global() > 0 && $this->is_global() === $this->current_user_id() ) { | |
| 59 | + if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) { | |
| 80 | 60 | $_who_am_i = 'global'; |
| 81 | 61 | } |
| 82 | 62 | |
| 83 | - if( $this->is_global() > 0 && $this->is_global() !== $this->current_user_id() ) { | |
| 63 | + if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) { | |
| 84 | 64 | $_who_am_i = 'link'; |
| 85 | 65 | } |
| 86 | 66 | |
| 87 | 67 | if( $this->is_global() == 0 ) { |
| @@ -91,42 +71,26 @@ | ||
| 91 | 71 | return $_who_am_i; |
| 92 | 72 | } |
| 93 | 73 | |
| 94 | 74 | /** |
| 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 | 75 | * Get user id determine dynamically |
| 108 | 76 | * @return integer |
| 109 | 77 | */ |
| 110 | 78 | private function user_id(): int { |
| 111 | - if ( $this->force_current_user ) { | |
| 112 | - return $this->current_user_id(); | |
| 113 | - } | |
| 114 | - | |
| 115 | 79 | $_who_am_i = $this->who_am_i(); |
| 116 | 80 | |
| 117 | 81 | if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 118 | 82 | $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) ); |
| 119 | 83 | if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) { |
| 120 | - return $this->current_user_id(); | |
| 84 | + return $this->current_user; | |
| 121 | 85 | } |
| 122 | 86 | } |
| 123 | 87 | |
| 124 | 88 | if( $_who_am_i === 'link' && $this->has_api ) { |
| 125 | - return $this->current_user_id(); | |
| 89 | + return $this->current_user; | |
| 126 | 90 | } |
| 127 | 91 | |
| 128 | - return $_who_am_i === 'local' ? $this->current_user_id() : $this->is_global(); | |
| 92 | + return $_who_am_i === 'local' ? $this->current_user : $this->is_global(); | |
| 129 | 93 | } |
| 130 | 94 | |
| 131 | 95 | /** |
| 132 | 96 | * Globally logged in and the User ID of globally logged-in user. |
| @@ -156,9 +120,9 @@ | ||
| 156 | 120 | return $this->who_am_i() !== 'local'; |
| 157 | 121 | } |
| 158 | 122 | |
| 159 | 123 | public function signed_as_global(): bool { |
| 160 | - return $this->current_user_id() === $this->is_global(); | |
| 124 | + return $this->current_user === $this->is_global(); | |
| 161 | 125 | } |
| 162 | 126 | |
| 163 | 127 | /** |
| 164 | 128 | * Set optional user meta or option data |
| @@ -220,16 +184,10 @@ | ||
| 220 | 184 | return get_user_option( $key, $user_id ); |
| 221 | 185 | } |
| 222 | 186 | |
| 223 | 187 | 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 | - } | |
| 188 | + $user_id = is_null( $user_id ) ? $this->user_id() : $user_id; | |
| 228 | 189 | |
| 229 | - $user_id = $this->user_id(); | |
| 230 | - } | |
| 231 | - | |
| 232 | 190 | if( ! is_multisite() ) { |
| 233 | 191 | return update_user_meta( $user_id, $meta_key, $meta_value ); |
| 234 | 192 | } |
| 235 | 193 | |
| @@ -236,12 +194,8 @@ | ||
| 236 | 194 | return update_user_option( $user_id, $meta_key, $meta_value, $this->_is_global() ); |
| 237 | 195 | } |
| 238 | 196 | |
| 239 | 197 | public function delete_user_meta( $meta_key ): bool { |
| 240 | - if ( ! $this->can_write() ) { | |
| 241 | - return false; | |
| 242 | - } | |
| 243 | - | |
| 244 | 198 | if( ! is_multisite() ) { |
| 245 | 199 | return delete_user_meta( $this->user_id(), $meta_key ); |
| 246 | 200 | } |
| 247 | 201 | |
| @@ -247,20 +201,8 @@ | ||
| 247 | 201 | |
| 248 | 202 | return delete_user_option( $this->user_id(), $meta_key, $this->_is_global() ); |
| 249 | 203 | } |
| 250 | 204 | |
| 251 | - /** | |
| 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 | 205 | private function _is_global() { |
| 264 | 206 | return apply_filters( 'templately_multisite_is_global', false ); |
| 265 | 207 | } |
| 266 | 208 | |
| @@ -291,5 +233,5 @@ | ||
| 291 | 233 | */ |
| 292 | 234 | public function update_option( $key, $value, $autoload = 'no' ): bool { |
| 293 | 235 | return update_option( $key, $value, $autoload ); |
| 294 | 236 | } |
| 295 | -} | |
| 237 | +} | |