| @@ -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(); |
| @@ -40,9 +32,9 @@ | ||
| 40 | 32 | /** |
| 41 | 33 | * Get the current user ID. |
| 42 | 34 | * @return int |
| 43 | 35 | */ |
| 44 | - public function current_user_id(): int { | |
| 36 | + public function current_user_id(){ | |
| 45 | 37 | return $this->current_user; |
| 46 | 38 | } |
| 47 | 39 | |
| 48 | 40 | /** |
| @@ -48,120 +40,101 @@ | ||
| 48 | 40 | /** |
| 49 | 41 | * Can a user link another templately account in a setup?. |
| 50 | 42 | * @return boolean |
| 51 | 43 | */ |
| 52 | - public function link_account(): bool { | |
| 53 | - return $this->who_am_i() === 'link' && ! $this->has_api; | |
| 44 | + public function link_account() { | |
| 45 | + return $this->whoami() === 'link' && ! $this->has_api; | |
| 54 | 46 | } |
| 55 | 47 | |
| 56 | - public function unlink_account(): bool { | |
| 57 | - 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; | |
| 58 | 50 | } |
| 59 | - | |
| 60 | 51 | /** |
| 61 | 52 | * Get determined who am I. |
| 62 | 53 | * @return string |
| 63 | 54 | */ |
| 64 | - public function who_am_i(): string { | |
| 65 | - $_who_am_i = 'local'; | |
| 55 | + public function whoami(){ | |
| 56 | + $_whoami = 'local'; | |
| 66 | 57 | |
| 67 | 58 | if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) { |
| 68 | - $_who_am_i = 'global'; | |
| 59 | + $_whoami = 'global'; | |
| 69 | 60 | } |
| 70 | 61 | |
| 71 | 62 | if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) { |
| 72 | - $_who_am_i = 'link'; | |
| 63 | + $_whoami = 'link'; | |
| 73 | 64 | } |
| 74 | 65 | |
| 75 | 66 | if( $this->is_global() == 0 ) { |
| 76 | - $_who_am_i = 'local'; | |
| 67 | + $_whoami = 'local'; | |
| 77 | 68 | } |
| 78 | 69 | |
| 79 | - return $_who_am_i; | |
| 70 | + return $_whoami; | |
| 80 | 71 | } |
| 81 | - | |
| 82 | 72 | /** |
| 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 | - /** | |
| 95 | 73 | * Get user id determine dynamically |
| 96 | 74 | * @return integer |
| 97 | 75 | */ |
| 98 | - private function user_id(): int { | |
| 99 | - if ( $this->force_current_user ) { | |
| 100 | - return $this->current_user; | |
| 101 | - } | |
| 76 | + private function user_id(){ | |
| 77 | + $_whoami = $this->whoami(); | |
| 78 | + // Helper::log( '_whoami: ' . $_whoami ); | |
| 102 | 79 | |
| 103 | - $_who_am_i = $this->who_am_i(); | |
| 104 | - | |
| 105 | 80 | if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 106 | 81 | $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) ); |
| 107 | - if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) { | |
| 82 | + if( $_whoami === 'link' && array_pop( $parse_uri ) === 'login' ) { | |
| 108 | 83 | return $this->current_user; |
| 109 | 84 | } |
| 110 | 85 | } |
| 111 | 86 | |
| 112 | - if( $_who_am_i === 'link' && $this->has_api ) { | |
| 87 | + if( $_whoami === 'link' && $this->has_api ) { | |
| 113 | 88 | return $this->current_user; |
| 114 | 89 | } |
| 115 | 90 | |
| 116 | - return $_who_am_i === 'local' ? $this->current_user : $this->is_global(); | |
| 91 | + return $_whoami === 'local' ? $this->current_user : $this->is_global(); | |
| 117 | 92 | } |
| 118 | - | |
| 119 | 93 | /** |
| 120 | 94 | * Globally logged in and the User ID of globally logged-in user. |
| 121 | 95 | * @return integer |
| 122 | 96 | */ |
| 123 | - public function is_global(): int { | |
| 97 | + public function is_global(){ | |
| 124 | 98 | return intval( get_option('_templately_global_login', 0) ); |
| 125 | 99 | } |
| 126 | - | |
| 127 | 100 | /** |
| 128 | 101 | * Set global login flag |
| 129 | 102 | * @return boolean |
| 130 | 103 | */ |
| 131 | - public static function set_global_login(): bool { | |
| 104 | + public static function set_global_login() { | |
| 132 | 105 | return update_option('_templately_global_login', get_current_user_id(), 'no'); |
| 133 | 106 | } |
| 134 | - | |
| 135 | 107 | /** |
| 136 | 108 | * Remove global login flag |
| 137 | 109 | * @return boolean |
| 138 | 110 | */ |
| 139 | - public function remove_global_login(): bool { | |
| 111 | + public function remove_global_login() { | |
| 140 | 112 | return delete_option( '_templately_global_login' ); |
| 141 | 113 | } |
| 142 | 114 | |
| 143 | - public function is_globally_signed(): bool { | |
| 144 | - return $this->who_am_i() !== 'local'; | |
| 115 | + public function is_globally_signed() { | |
| 116 | + return $this->whoami() !== 'local'; | |
| 145 | 117 | } |
| 146 | - | |
| 147 | - public function signed_as_global(): bool { | |
| 118 | + public function signed_as_global() { | |
| 148 | 119 | return $this->current_user === $this->is_global(); |
| 149 | 120 | } |
| 150 | - | |
| 151 | 121 | /** |
| 152 | - * Set optional user meta or option data | |
| 122 | + * Set optional usermeta or option data | |
| 153 | 123 | * |
| 154 | 124 | * @param string $key |
| 155 | 125 | * @param mixed $value |
| 156 | - * @param null $user_id | |
| 157 | 126 | * @return boolean |
| 158 | 127 | */ |
| 159 | - public function set( $key, $value, $user_id = null ): bool { | |
| 128 | + public function set( $key, $value, $user_id = null ){ | |
| 160 | 129 | $key = '_templately_' . $key; |
| 161 | 130 | |
| 162 | - $updated = $this->update_user_meta( $user_id, $key, $value ); | |
| 131 | + if( $user_id === null ) { | |
| 132 | + $user_id = $this->user_id(); | |
| 133 | + } | |
| 163 | 134 | |
| 135 | + $updated = update_user_meta( $user_id, $key, $value ); | |
| 136 | + | |
| 164 | 137 | if( $key === '_templately_api_key' && $updated ) { |
| 165 | 138 | $this->has_api = true; |
| 166 | 139 | } |
| 167 | 140 | |
| @@ -166,11 +139,10 @@ | ||
| 166 | 139 | } |
| 167 | 140 | |
| 168 | 141 | return $updated; |
| 169 | 142 | } |
| 170 | - | |
| 171 | 143 | /** |
| 172 | - * Get optional user meta or option data | |
| 144 | + * Get optional usermeta or option data | |
| 173 | 145 | * |
| 174 | 146 | * @param string $key |
| 175 | 147 | * @param mixed $default |
| 176 | 148 | * @return mixed |
| @@ -176,21 +148,20 @@ | ||
| 176 | 148 | * @return mixed |
| 177 | 149 | */ |
| 178 | 150 | public function get( $key, $default = false, $user_id = null ){ |
| 179 | 151 | $key = '_templately_' . $key; |
| 180 | - $_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 ); | |
| 181 | 153 | return ! empty( $_user_meta ) ? $_user_meta : $default; |
| 182 | 154 | } |
| 183 | - | |
| 184 | 155 | /** |
| 185 | - * Remove options data or user meta | |
| 156 | + * Remove options data or usermeta | |
| 186 | 157 | * |
| 187 | 158 | * @param string $key |
| 188 | 159 | * @return Options |
| 189 | 160 | */ |
| 190 | - public function remove( string $key ): Options { | |
| 161 | + public function remove( $key ){ | |
| 191 | 162 | $key = '_templately_' . $key; |
| 192 | - $updated = $this->delete_user_meta( $key ); | |
| 163 | + $updated = delete_user_meta( $this->user_id(), $key ); // delete user meta | |
| 193 | 164 | |
| 194 | 165 | if( $key === '_templately_api_key' && $updated ) { |
| 195 | 166 | $this->has_api = false; |
| 196 | 167 | } |
| @@ -197,63 +168,9 @@ | ||
| 197 | 168 | |
| 198 | 169 | return $this; |
| 199 | 170 | } |
| 200 | 171 | |
| 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 | - | |
| 239 | 172 | /** |
| 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 | - /** | |
| 256 | 173 | * Get option data |
| 257 | 174 | * |
| 258 | 175 | * @since 2.0.1 |
| 259 | 176 | * |
| @@ -276,8 +193,8 @@ | ||
| 276 | 193 | * @param string $autoload |
| 277 | 194 | * |
| 278 | 195 | * @return bool |
| 279 | 196 | */ |
| 280 | - public function update_option( $key, $value, $autoload = 'no' ): bool { | |
| 197 | + public function update_option( $key, $value, $autoload = 'no' ){ | |
| 281 | 198 | return update_option( $key, $value, $autoload ); |
| 282 | 199 | } |
| 283 | -} | |
| 200 | +} | |