| @@ -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,41 +25,24 @@ | ||
| 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 | - $data = get_transient( "templately_subscriptions_v2" ); | |
| 32 | + $data = get_transient( "templately_subscriptions" ); | |
| 52 | 33 | |
| 53 | 34 | if( is_array( $data ) && ! empty( $data ) ) { |
| 54 | 35 | return $data; |
| 55 | 36 | } |
| 56 | 37 | |
| 57 | - // Field set drives the Subscription screen's plan comparison grid, so it | |
| 58 | - // carries the per-plan limits too, not just price/sites. | |
| 59 | - $query = 'id, price, name, slug, discounted_price, type, sites, coupon, my_cloud_items, pro_items, workspace, fsi_limit, ai_credit, description'; | |
| 38 | + $query = 'id, price, name, discounted_price, type, sites, coupon'; | |
| 60 | 39 | $response = $this->http()->query( |
| 61 | 40 | 'subscriptionPlans', |
| 62 | 41 | $query |
| 63 | 42 | )->post(); |
| 64 | 43 | |
| 65 | - set_transient( "templately_subscriptions_v2", $response, WEEK_IN_SECONDS ); | |
| 44 | + set_transient( "templately_subscriptions", $response, WEEK_IN_SECONDS ); | |
| 66 | 45 | |
| 67 | 46 | return $response; |
| 68 | 47 | } |
| 69 | 48 | |
| @@ -107,13 +86,9 @@ | ||
| 107 | 86 | if ( ! empty( $errors ) ) { |
| 108 | 87 | return $this->error( 'login_error', $errors, 'login', 400 ); |
| 109 | 88 | } |
| 110 | 89 | |
| 111 | - // `ends_at`, `plan_type` and `cancel_at_period_end` drive the Subscription | |
| 112 | - // screen's renewal line. They are asked for instead of leaning on | |
| 113 | - // `plan_expire_at`, which the API resolves with `empty($subscription->ends_at) | |
| 114 | - // ?? …` — a boolean that never falls through, so it never carries a date. | |
| 115 | - $query = 'status, message, user{ id, name, first_name, last_name, display_name, email, profile_photo, joined, is_verified, is_company_user, is_restricted_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, subscription_plan_id, ends_at, plan_type, cancel_at_period_end } }'; | |
| 90 | + $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, reviews{ type, type_id, rating } }'; | |
| 116 | 91 | |
| 117 | 92 | $response = $this->http()->mutation( |
| 118 | 93 | $viaAPI ? 'connectWithApiKey' : 'connect', |
| 119 | 94 | $query, |
| @@ -123,34 +98,8 @@ | ||
| 123 | 98 | if ( is_wp_error( $response ) ) { |
| 124 | 99 | return $response; |
| 125 | 100 | } |
| 126 | 101 | |
| 127 | - if ( empty( $response['user']['api_key'] ) ) { | |
| 128 | - return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); | |
| 129 | - } | |
| 130 | - | |
| 131 | - $options = $this->utils( 'options' ); | |
| 132 | - $options->use_current_user( true ); | |
| 133 | - | |
| 134 | - try { | |
| 135 | - return $this->store_connection( $response, $global_signin, $_ip, $_site_url ); | |
| 136 | - } finally { | |
| 137 | - $options->use_current_user( false ); | |
| 138 | - } | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | - * Persist an authenticated connection against the acting user. | |
| 143 | - * | |
| 144 | - * @param array $response Cloud response, already validated. | |
| 145 | - * @param bool $global_signin Whether the user asked to sign in globally. | |
| 146 | - * @param string $_ip Request IP, echoed back into the profile. | |
| 147 | - * @param string $_site_url Site URL, echoed back into the profile. | |
| 148 | - * | |
| 149 | - * @return array | |
| 150 | - */ | |
| 151 | - private function store_connection( $response, $global_signin, $_ip, $_site_url ) { | |
| 152 | - | |
| 153 | 102 | if ( $global_signin && ! Login::is_globally_signed() ) { |
| 154 | 103 | Options::set_global_login(); |
| 155 | 104 | } |
| 156 | 105 | |
| @@ -186,17 +135,10 @@ | ||
| 186 | 135 | unset( $response['user']['reviews'] ); |
| 187 | 136 | $meta['reviews'] = $_reviews; |
| 188 | 137 | } |
| 189 | 138 | |
| 190 | - if(Helper::is_dev_api()){ | |
| 191 | - $response['user']['is_dev_api'] = true; | |
| 192 | - } | |
| 139 | + // No longer need to manage is_live_api state - API selection is now handled by TEMPLATELY_DEV_API constant | |
| 193 | 140 | |
| 194 | - if(! empty( $response['user'] ) && is_array($response['user'])){ | |
| 195 | - $response['user']['ip'] = $_ip; | |
| 196 | - $response['user']['site_url'] = base64_encode( $_site_url ); | |
| 197 | - } | |
| 198 | - | |
| 199 | 141 | $this->utils( 'options' )->set( 'user', $response['user'] ); |
| 200 | 142 | $response['user']['meta'] = $this->user_meta( $meta ); |
| 201 | 143 | |
| 202 | 144 | return $response; |
| @@ -202,28 +144,13 @@ | ||
| 202 | 144 | return $response; |
| 203 | 145 | } |
| 204 | 146 | |
| 205 | 147 | public function logout() { |
| 206 | - // Read the key off the acting user's own record. Options::get() falls back to | |
| 207 | - // the global-login administrator when no target is given, so $this->api_key | |
| 208 | - // resolves to the administrator's key for any linked user — disconnecting the | |
| 209 | - // administrator's account on the cloud as well as locally. | |
| 210 | - $api_key = $this->utils( 'options' )->get( 'api_key', '', get_current_user_id() ); | |
| 211 | - | |
| 212 | - if ( empty( $api_key ) ) { | |
| 213 | - return $this->error( | |
| 214 | - 'logout_error', | |
| 215 | - __( 'You are not connected to Templately.', 'templately' ), | |
| 216 | - 'logout', | |
| 217 | - 403 | |
| 218 | - ); | |
| 219 | - } | |
| 220 | - | |
| 221 | 148 | $response = $this->http()->mutation( |
| 222 | 149 | 'disconnect', |
| 223 | 150 | 'status, message, data', |
| 224 | 151 | [ |
| 225 | - 'api_key' => $api_key, | |
| 152 | + 'api_key' => $this->api_key, | |
| 226 | 153 | "site_url" => home_url( '/' ) |
| 227 | 154 | ] |
| 228 | 155 | )->post(); |
| 229 | 156 | |
| @@ -250,33 +177,22 @@ | ||
| 250 | 177 | return $response; |
| 251 | 178 | } |
| 252 | 179 | |
| 253 | 180 | public function delete(){ |
| 254 | - $options = $this->utils( 'options' ); | |
| 181 | + $this->utils( 'options' ) | |
| 182 | + ->remove( 'user' ) | |
| 183 | + ->remove( 'favourites' ) | |
| 184 | + ->remove( 'reviews' ) | |
| 185 | + ->remove( 'cloud_activity' ) | |
| 186 | + ->remove( 'api_key' ) | |
| 187 | + ->remove( 'global_login' ) | |
| 188 | + ->remove( 'total_download_counts' ) | |
| 189 | + ->remove( 'templates_in_clouds' ); | |
| 255 | 190 | |
| 256 | - // Pin the removals to the acting user. Without the pin, Options::user_id() | |
| 257 | - // resolves a linked user to the global-login administrator and the delete | |
| 258 | - // path wipes the administrator's connection instead of the caller's. | |
| 259 | - $options->use_current_user( true ); | |
| 191 | + if ( $this->utils( 'options' )->who_am_i() === 'global' ) { | |
| 192 | + $this->utils( 'options' )->remove_global_login(); | |
| 193 | + } | |
| 260 | 194 | |
| 261 | - try { | |
| 262 | - $options | |
| 263 | - ->remove( 'user' ) | |
| 264 | - ->remove( 'favourites' ) | |
| 265 | - ->remove( 'reviews' ) | |
| 266 | - ->remove( 'cloud_activity' ) | |
| 267 | - ->remove( 'api_key' ) | |
| 268 | - ->remove( 'global_login' ) | |
| 269 | - ->remove( 'total_download_counts' ) | |
| 270 | - ->remove( 'templates_in_clouds' ); | |
| 271 | - | |
| 272 | - if ( $options->who_am_i() === 'global' ) { | |
| 273 | - $options->remove_global_login(); | |
| 274 | - } | |
| 275 | - } finally { | |
| 276 | - $options->use_current_user( false ); | |
| 277 | - } | |
| 278 | - | |
| 279 | 195 | $global_user_id = $this->utils( 'options' )->is_global(); |
| 280 | 196 | $global_user = null; |
| 281 | 197 | |
| 282 | 198 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| @@ -282,12 +198,8 @@ | ||
| 282 | 198 | if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { |
| 283 | 199 | $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id ); |
| 284 | 200 | |
| 285 | 201 | if ( ! empty( $global_user ) ) { |
| 286 | - if ( is_array( $global_user ) ) { | |
| 287 | - unset( $global_user['api_key'] ); | |
| 288 | - } | |
| 289 | - | |
| 290 | 202 | $global_user['meta'] = $this->user_meta(); |
| 291 | 203 | } |
| 292 | 204 | } |
| 293 | 205 | |
| @@ -301,13 +213,8 @@ | ||
| 301 | 213 | |
| 302 | 214 | $_user = ( new static )->utils( 'options' )->get( 'user', null ); |
| 303 | 215 | |
| 304 | 216 | if ( ! is_null( $_user ) ) { |
| 305 | - // Profiles stored before 3.7.1 may still carry the cloud API key. | |
| 306 | - if ( is_array( $_user ) ) { | |
| 307 | - unset( $_user['api_key'] ); | |
| 308 | - } | |
| 309 | - | |
| 310 | 217 | $_user['meta'] = self::get_instance()->user_meta(); |
| 311 | 218 | } |
| 312 | 219 | |
| 313 | 220 | if ( empty( $_user ) ) { |
| @@ -340,5 +247,5 @@ | ||
| 340 | 247 | |
| 341 | 248 | public static function signed_as_global(): bool { |
| 342 | 249 | return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() ); |
| 343 | 250 | } |
| 344 | -} | |
| 251 | +} | |