| @@ -17,12 +17,8 @@ | ||
| 17 | 17 | if ( '/templately/v1/pricing' === $_route ) { |
| 18 | 18 | return true; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - if ( '/templately/v1/google-auth-url' === $_route ) { | |
| 22 | - return true; | |
| 23 | - } | |
| 24 | - | |
| 25 | 21 | return parent::permission_check( $request ); |
| 26 | 22 | } |
| 27 | 23 | |
| 28 | 24 | public function register_routes() { |
| @@ -29,25 +25,10 @@ | ||
| 29 | 25 | $this->post( 'login', [$this, 'login'] ); |
| 30 | 26 | $this->post( 'logout', [$this, 'logout'] ); |
| 31 | 27 | $this->get( 'is-signed', [$this, 'is_signed'] ); |
| 32 | 28 | $this->get( 'pricing', [$this, 'pricing'] ); |
| 33 | - $this->get( 'google-auth-url', [$this, 'google_auth_url'] ); | |
| 34 | 29 | } |
| 35 | 30 | |
| 36 | - public function google_auth_url() { | |
| 37 | - // Get redirect_to parameter from request if provided | |
| 38 | - $redirect_to = $this->get_param( 'redirect-to', '' ); | |
| 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 ); | |
| 44 | - return [ | |
| 45 | - 'status' => 'success', | |
| 46 | - 'url' => $url | |
| 47 | - ]; | |
| 48 | - } | |
| 49 | - | |
| 50 | 31 | public function pricing(){ |
| 51 | 32 | $data = get_transient( "templately_subscriptions" ); |
| 52 | 33 | |
| 53 | 34 | if( is_array( $data ) && ! empty( $data ) ) { |
| @@ -53,12 +34,12 @@ | ||
| 53 | 34 | if( is_array( $data ) && ! empty( $data ) ) { |
| 54 | 35 | return $data; |
| 55 | 36 | } |
| 56 | 37 | |
| 57 | - $query = 'id, price, name, discounted_price, type, sites, coupon'; | |
| 38 | + $query = 'id, price, name, discounted_price, type, sites'; | |
| 58 | 39 | $response = $this->http()->query( |
| 59 | 40 | 'subscriptionPlans', |
| 60 | - $query | |
| 41 | + $query, | |
| 61 | 42 | )->post(); |
| 62 | 43 | |
| 63 | 44 | set_transient( "templately_subscriptions", $response, WEEK_IN_SECONDS ); |
| 64 | 45 | |
| @@ -79,10 +60,8 @@ | ||
| 79 | 60 | 'ip' => $_ip, |
| 80 | 61 | 'site_url' => $_site_url |
| 81 | 62 | ]; |
| 82 | 63 | |
| 83 | - $postArgs = []; | |
| 84 | - | |
| 85 | 64 | if ( $viaAPI ) { |
| 86 | 65 | $api_key = $this->get_param( 'api_key' ); |
| 87 | 66 | $funcArgs['api_key'] = $api_key; |
| 88 | 67 | |
| @@ -105,46 +84,20 @@ | ||
| 105 | 84 | if ( ! empty( $errors ) ) { |
| 106 | 85 | return $this->error( 'login_error', $errors, 'login', 400 ); |
| 107 | 86 | } |
| 108 | 87 | |
| 109 | - $query = 'status, message, user{ id, name, first_name, last_name, display_name, email, profile_photo, joined, is_verified, is_company_user, api_key, plan, plan_expire_at, my_cloud{ limit, usages, last_pushed }, favourites{ id, type }, show_notice, reviews{ type, type_id, rating }, subscription { id, name, sites } }'; | |
| 88 | + $query = 'status, message, user{ id, name, first_name, last_name, display_name, email, profile_photo, joined, is_verified, api_key, plan, plan_expire_at, my_cloud{ limit, usages, last_pushed }, favourites{ id, type }, show_notice }'; | |
| 110 | 89 | |
| 111 | 90 | $response = $this->http()->mutation( |
| 112 | 91 | $viaAPI ? 'connectWithApiKey' : 'connect', |
| 113 | 92 | $query, |
| 114 | 93 | $funcArgs |
| 115 | - )->post($postArgs); | |
| 94 | + )->post(); | |
| 116 | 95 | |
| 117 | 96 | if ( is_wp_error( $response ) ) { |
| 118 | 97 | return $response; |
| 119 | 98 | } |
| 120 | 99 | |
| 121 | - if ( empty( $response['user']['api_key'] ) ) { | |
| 122 | - return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); | |
| 123 | - } | |
| 124 | - | |
| 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 | 100 | if ( $global_signin && ! Login::is_globally_signed() ) { |
| 148 | 101 | Options::set_global_login(); |
| 149 | 102 | } |
| 150 | 103 | |
| @@ -172,25 +125,8 @@ | ||
| 172 | 125 | unset( $response['user']['favourites'] ); |
| 173 | 126 | $meta['favourites'] = $_favourites; |
| 174 | 127 | } |
| 175 | 128 | |
| 176 | - if ( ! empty( $response['user']['reviews'] ) ) { | |
| 177 | - $_reviews = $this->utils( 'helper' )->normalizeReviews( $response['user']['reviews'] ); | |
| 178 | - $this->utils( 'options' )->set( 'reviews', $_reviews ); | |
| 179 | - | |
| 180 | - unset( $response['user']['reviews'] ); | |
| 181 | - $meta['reviews'] = $_reviews; | |
| 182 | - } | |
| 183 | - | |
| 184 | - if(Helper::is_dev_api()){ | |
| 185 | - $response['user']['is_dev_api'] = true; | |
| 186 | - } | |
| 187 | - | |
| 188 | - if(! empty( $response['user'] ) && is_array($response['user'])){ | |
| 189 | - $response['user']['ip'] = $_ip; | |
| 190 | - $response['user']['site_url'] = base64_encode( $_site_url ); | |
| 191 | - } | |
| 192 | - | |
| 193 | 129 | $this->utils( 'options' )->set( 'user', $response['user'] ); |
| 194 | 130 | $response['user']['meta'] = $this->user_meta( $meta ); |
| 195 | 131 | |
| 196 | 132 | return $response; |
| @@ -196,28 +132,13 @@ | ||
| 196 | 132 | return $response; |
| 197 | 133 | } |
| 198 | 134 | |
| 199 | 135 | 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 | 136 | $response = $this->http()->mutation( |
| 216 | 137 | 'disconnect', |
| 217 | 138 | 'status, message, data', |
| 218 | 139 | [ |
| 219 | - 'api_key' => $api_key, | |
| 140 | + 'api_key' => $this->api_key, | |
| 220 | 141 | "site_url" => home_url( '/' ) |
| 221 | 142 | ] |
| 222 | 143 | )->post(); |
| 223 | 144 | |
| @@ -244,33 +165,20 @@ | ||
| 244 | 165 | return $response; |
| 245 | 166 | } |
| 246 | 167 | |
| 247 | 168 | public function delete(){ |
| 248 | - $options = $this->utils( 'options' ); | |
| 169 | + $this->utils( 'options' ) | |
| 170 | + ->remove( 'user' ) | |
| 171 | + ->remove( 'favourites' ) | |
| 172 | + ->remove( 'cloud_activity' ) | |
| 173 | + ->remove( 'api_key' ) | |
| 174 | + ->remove( 'global_login' ) | |
| 175 | + ->remove( 'templates_in_clouds' ); | |
| 249 | 176 | |
| 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 ); | |
| 177 | + if ( $this->utils( 'options' )->whoami() === 'global' ) { | |
| 178 | + $this->utils( 'options' )->remove_global_login(); | |
| 179 | + } | |
| 254 | 180 | |
| 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 | 181 | $global_user_id = $this->utils( 'options' )->is_global(); |
| 274 | 182 | $global_user = null; |
| 275 | 183 | |
| 276 | 184 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| @@ -276,12 +184,8 @@ | ||
| 276 | 184 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| 277 | 185 | $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id ); |
| 278 | 186 | |
| 279 | 187 | if ( ! empty( $global_user ) ) { |
| 280 | - if ( is_array( $global_user ) ) { | |
| 281 | - unset( $global_user['api_key'] ); | |
| 282 | - } | |
| 283 | - | |
| 284 | 188 | $global_user['meta'] = $this->user_meta(); |
| 285 | 189 | } |
| 286 | 190 | } |
| 287 | 191 | |
| @@ -287,9 +191,9 @@ | ||
| 287 | 191 | |
| 288 | 192 | return $global_user; |
| 289 | 193 | } |
| 290 | 194 | |
| 291 | - public static function is_signed(): array { | |
| 195 | + public static function is_signed() { | |
| 292 | 196 | $_response = [ |
| 293 | 197 | 'status' => 'success' |
| 294 | 198 | ]; |
| 295 | 199 | |
| @@ -295,13 +199,8 @@ | ||
| 295 | 199 | |
| 296 | 200 | $_user = ( new static )->utils( 'options' )->get( 'user', null ); |
| 297 | 201 | |
| 298 | 202 | 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 | 203 | $_user['meta'] = self::get_instance()->user_meta(); |
| 305 | 204 | } |
| 306 | 205 | |
| 307 | 206 | if ( empty( $_user ) ) { |
| @@ -312,9 +211,9 @@ | ||
| 312 | 211 | |
| 313 | 212 | return $_response; |
| 314 | 213 | } |
| 315 | 214 | |
| 316 | - public function user_meta( $meta = [] ): array { | |
| 215 | + public function user_meta( $meta = [] ) { | |
| 317 | 216 | $_meta = [ |
| 318 | 217 | 'link_account' => self::utils( 'options' )->link_account(), |
| 319 | 218 | 'unlink_account' => self::utils( 'options' )->unlink_account(), |
| 320 | 219 | 'is_globally_signed' => Login::is_globally_signed(), |
| @@ -319,9 +218,8 @@ | ||
| 319 | 218 | 'unlink_account' => self::utils( 'options' )->unlink_account(), |
| 320 | 219 | 'is_globally_signed' => Login::is_globally_signed(), |
| 321 | 220 | 'signed_as_global' => Login::signed_as_global(), |
| 322 | 221 | 'starred' => self::utils( 'options' )->get( 'favourites' ), |
| 323 | - 'reviews' => self::utils( 'options' )->get( 'reviews' ), | |
| 324 | 222 | 'cloud_activity' => self::utils( 'options' )->get( 'cloud_activity' ), |
| 325 | 223 | 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get( 'api_key' ) ) |
| 326 | 224 | ]; |
| 327 | 225 | |
| @@ -327,12 +225,12 @@ | ||
| 327 | 225 | |
| 328 | 226 | return array_merge( $_meta, $meta ); |
| 329 | 227 | } |
| 330 | 228 | |
| 331 | - public static function is_globally_signed(): bool { | |
| 229 | + public static function is_globally_signed() { | |
| 332 | 230 | return rest_sanitize_boolean( ( new static )->utils( 'options' )->is_globally_signed() ); |
| 333 | 231 | } |
| 334 | 232 | |
| 335 | - public static function signed_as_global(): bool { | |
| 233 | + public static function signed_as_global() { | |
| 336 | 234 | return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() ); |
| 337 | 235 | } |
| 338 | -} | |
| 236 | +} | |