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