| @@ -36,12 +36,9 @@ | ||
| 36 | 36 | public function google_auth_url() { |
| 37 | 37 | // Get redirect_to parameter from request if provided |
| 38 | 38 | $redirect_to = $this->get_param( 'redirect-to', '' ); |
| 39 | 39 | |
| 40 | - // Use client-provided current_url instead of HTTP_REFERER for reliability | |
| 41 | - $current_url = $this->get_param( 'current_url', '' ); | |
| 42 | - | |
| 43 | - $url = $this->http()->google_auth_url( $redirect_to, $current_url ); | |
| 40 | + $url = $this->http()->google_auth_url( $redirect_to ); | |
| 44 | 41 | return [ |
| 45 | 42 | 'status' => 'success', |
| 46 | 43 | 'url' => $url |
| 47 | 44 | ]; |
| @@ -121,30 +118,8 @@ | ||
| 121 | 118 | if ( empty( $response['user']['api_key'] ) ) { |
| 122 | 119 | return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); |
| 123 | 120 | } |
| 124 | 121 | |
| 125 | - $options = $this->utils( 'options' ); | |
| 126 | - $options->use_current_user( true ); | |
| 127 | - | |
| 128 | - try { | |
| 129 | - return $this->store_connection( $response, $global_signin, $_ip, $_site_url ); | |
| 130 | - } finally { | |
| 131 | - $options->use_current_user( false ); | |
| 132 | - } | |
| 133 | - } | |
| 134 | - | |
| 135 | - /** | |
| 136 | - * Persist an authenticated connection against the acting user. | |
| 137 | - * | |
| 138 | - * @param array $response Cloud response, already validated. | |
| 139 | - * @param bool $global_signin Whether the user asked to sign in globally. | |
| 140 | - * @param string $_ip Request IP, echoed back into the profile. | |
| 141 | - * @param string $_site_url Site URL, echoed back into the profile. | |
| 142 | - * | |
| 143 | - * @return array | |
| 144 | - */ | |
| 145 | - private function store_connection( $response, $global_signin, $_ip, $_site_url ) { | |
| 146 | - | |
| 147 | 122 | if ( $global_signin && ! Login::is_globally_signed() ) { |
| 148 | 123 | Options::set_global_login(); |
| 149 | 124 | } |
| 150 | 125 | |
| @@ -196,28 +171,13 @@ | ||
| 196 | 171 | return $response; |
| 197 | 172 | } |
| 198 | 173 | |
| 199 | 174 | public function logout() { |
| 200 | - // Read the key off the acting user's own record. Options::get() falls back to | |
| 201 | - // the global-login administrator when no target is given, so $this->api_key | |
| 202 | - // resolves to the administrator's key for any linked user — disconnecting the | |
| 203 | - // administrator's account on the cloud as well as locally. | |
| 204 | - $api_key = $this->utils( 'options' )->get( 'api_key', '', get_current_user_id() ); | |
| 205 | - | |
| 206 | - if ( empty( $api_key ) ) { | |
| 207 | - return $this->error( | |
| 208 | - 'logout_error', | |
| 209 | - __( 'You are not connected to Templately.', 'templately' ), | |
| 210 | - 'logout', | |
| 211 | - 403 | |
| 212 | - ); | |
| 213 | - } | |
| 214 | - | |
| 215 | 175 | $response = $this->http()->mutation( |
| 216 | 176 | 'disconnect', |
| 217 | 177 | 'status, message, data', |
| 218 | 178 | [ |
| 219 | - 'api_key' => $api_key, | |
| 179 | + 'api_key' => $this->api_key, | |
| 220 | 180 | "site_url" => home_url( '/' ) |
| 221 | 181 | ] |
| 222 | 182 | )->post(); |
| 223 | 183 | |
| @@ -244,33 +204,22 @@ | ||
| 244 | 204 | return $response; |
| 245 | 205 | } |
| 246 | 206 | |
| 247 | 207 | public function delete(){ |
| 248 | - $options = $this->utils( 'options' ); | |
| 208 | + $this->utils( 'options' ) | |
| 209 | + ->remove( 'user' ) | |
| 210 | + ->remove( 'favourites' ) | |
| 211 | + ->remove( 'reviews' ) | |
| 212 | + ->remove( 'cloud_activity' ) | |
| 213 | + ->remove( 'api_key' ) | |
| 214 | + ->remove( 'global_login' ) | |
| 215 | + ->remove( 'total_download_counts' ) | |
| 216 | + ->remove( 'templates_in_clouds' ); | |
| 249 | 217 | |
| 250 | - // Pin the removals to the acting user. Without the pin, Options::user_id() | |
| 251 | - // resolves a linked user to the global-login administrator and the delete | |
| 252 | - // path wipes the administrator's connection instead of the caller's. | |
| 253 | - $options->use_current_user( true ); | |
| 218 | + if ( $this->utils( 'options' )->who_am_i() === 'global' ) { | |
| 219 | + $this->utils( 'options' )->remove_global_login(); | |
| 220 | + } | |
| 254 | 221 | |
| 255 | - try { | |
| 256 | - $options | |
| 257 | - ->remove( 'user' ) | |
| 258 | - ->remove( 'favourites' ) | |
| 259 | - ->remove( 'reviews' ) | |
| 260 | - ->remove( 'cloud_activity' ) | |
| 261 | - ->remove( 'api_key' ) | |
| 262 | - ->remove( 'global_login' ) | |
| 263 | - ->remove( 'total_download_counts' ) | |
| 264 | - ->remove( 'templates_in_clouds' ); | |
| 265 | - | |
| 266 | - if ( $options->who_am_i() === 'global' ) { | |
| 267 | - $options->remove_global_login(); | |
| 268 | - } | |
| 269 | - } finally { | |
| 270 | - $options->use_current_user( false ); | |
| 271 | - } | |
| 272 | - | |
| 273 | 222 | $global_user_id = $this->utils( 'options' )->is_global(); |
| 274 | 223 | $global_user = null; |
| 275 | 224 | |
| 276 | 225 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| @@ -276,12 +225,8 @@ | ||
| 276 | 225 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| 277 | 226 | $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id ); |
| 278 | 227 | |
| 279 | 228 | if ( ! empty( $global_user ) ) { |
| 280 | - if ( is_array( $global_user ) ) { | |
| 281 | - unset( $global_user['api_key'] ); | |
| 282 | - } | |
| 283 | - | |
| 284 | 229 | $global_user['meta'] = $this->user_meta(); |
| 285 | 230 | } |
| 286 | 231 | } |
| 287 | 232 | |
| @@ -295,13 +240,8 @@ | ||
| 295 | 240 | |
| 296 | 241 | $_user = ( new static )->utils( 'options' )->get( 'user', null ); |
| 297 | 242 | |
| 298 | 243 | if ( ! is_null( $_user ) ) { |
| 299 | - // Profiles stored before 3.7.1 may still carry the cloud API key. | |
| 300 | - if ( is_array( $_user ) ) { | |
| 301 | - unset( $_user['api_key'] ); | |
| 302 | - } | |
| 303 | - | |
| 304 | 244 | $_user['meta'] = self::get_instance()->user_meta(); |
| 305 | 245 | } |
| 306 | 246 | |
| 307 | 247 | if ( empty( $_user ) ) { |
| @@ -334,5 +274,5 @@ | ||
| 334 | 274 | |
| 335 | 275 | public static function signed_as_global(): bool { |
| 336 | 276 | return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() ); |
| 337 | 277 | } |
| 338 | -} | |
| 278 | +} | |