| @@ -17,8 +17,12 @@ | ||
| 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 | + | |
| 21 | 25 | return parent::permission_check( $request ); |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | public function register_routes() { |
| @@ -25,10 +29,25 @@ | ||
| 25 | 29 | $this->post( 'login', [$this, 'login'] ); |
| 26 | 30 | $this->post( 'logout', [$this, 'logout'] ); |
| 27 | 31 | $this->get( 'is-signed', [$this, 'is_signed'] ); |
| 28 | 32 | $this->get( 'pricing', [$this, 'pricing'] ); |
| 33 | + $this->get( 'google-auth-url', [$this, 'google_auth_url'] ); | |
| 29 | 34 | } |
| 30 | 35 | |
| 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 | + | |
| 31 | 50 | public function pricing(){ |
| 32 | 51 | $data = get_transient( "templately_subscriptions" ); |
| 33 | 52 | |
| 34 | 53 | if( is_array( $data ) && ! empty( $data ) ) { |
| @@ -34,9 +53,9 @@ | ||
| 34 | 53 | if( is_array( $data ) && ! empty( $data ) ) { |
| 35 | 54 | return $data; |
| 36 | 55 | } |
| 37 | 56 | |
| 38 | - $query = 'id, price, name, discounted_price, type, sites'; | |
| 57 | + $query = 'id, price, name, discounted_price, type, sites, coupon'; | |
| 39 | 58 | $response = $this->http()->query( |
| 40 | 59 | 'subscriptionPlans', |
| 41 | 60 | $query |
| 42 | 61 | )->post(); |
| @@ -60,8 +79,10 @@ | ||
| 60 | 79 | 'ip' => $_ip, |
| 61 | 80 | 'site_url' => $_site_url |
| 62 | 81 | ]; |
| 63 | 82 | |
| 83 | + $postArgs = []; | |
| 84 | + | |
| 64 | 85 | if ( $viaAPI ) { |
| 65 | 86 | $api_key = $this->get_param( 'api_key' ); |
| 66 | 87 | $funcArgs['api_key'] = $api_key; |
| 67 | 88 | |
| @@ -84,20 +105,24 @@ | ||
| 84 | 105 | if ( ! empty( $errors ) ) { |
| 85 | 106 | return $this->error( 'login_error', $errors, 'login', 400 ); |
| 86 | 107 | } |
| 87 | 108 | |
| 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, reviews{ type, type_id, rating } }'; | |
| 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 } }'; | |
| 89 | 110 | |
| 90 | 111 | $response = $this->http()->mutation( |
| 91 | 112 | $viaAPI ? 'connectWithApiKey' : 'connect', |
| 92 | 113 | $query, |
| 93 | 114 | $funcArgs |
| 94 | - )->post(); | |
| 115 | + )->post($postArgs); | |
| 95 | 116 | |
| 96 | 117 | if ( is_wp_error( $response ) ) { |
| 97 | 118 | return $response; |
| 98 | 119 | } |
| 99 | 120 | |
| 121 | + if ( empty( $response['user']['api_key'] ) ) { | |
| 122 | + return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); | |
| 123 | + } | |
| 124 | + | |
| 100 | 125 | if ( $global_signin && ! Login::is_globally_signed() ) { |
| 101 | 126 | Options::set_global_login(); |
| 102 | 127 | } |
| 103 | 128 | |
| @@ -132,8 +157,17 @@ | ||
| 132 | 157 | |
| 133 | 158 | unset( $response['user']['reviews'] ); |
| 134 | 159 | $meta['reviews'] = $_reviews; |
| 135 | 160 | } |
| 161 | + | |
| 162 | + if(Helper::is_dev_api()){ | |
| 163 | + $response['user']['is_dev_api'] = true; | |
| 164 | + } | |
| 165 | + | |
| 166 | + if(! empty( $response['user'] ) && is_array($response['user'])){ | |
| 167 | + $response['user']['ip'] = $_ip; | |
| 168 | + $response['user']['site_url'] = base64_encode( $_site_url ); | |
| 169 | + } | |
| 136 | 170 | |
| 137 | 171 | $this->utils( 'options' )->set( 'user', $response['user'] ); |
| 138 | 172 | $response['user']['meta'] = $this->user_meta( $meta ); |
| 139 | 173 | |