| @@ -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,22 @@ | ||
| 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 | + $url = $this->http()->google_auth_url( $redirect_to ); | |
| 41 | + return [ | |
| 42 | + 'status' => 'success', | |
| 43 | + 'url' => $url | |
| 44 | + ]; | |
| 45 | + } | |
| 46 | + | |
| 31 | 47 | public function pricing(){ |
| 32 | 48 | $data = get_transient( "templately_subscriptions" ); |
| 33 | 49 | |
| 34 | 50 | if( is_array( $data ) && ! empty( $data ) ) { |
| @@ -34,9 +50,9 @@ | ||
| 34 | 50 | if( is_array( $data ) && ! empty( $data ) ) { |
| 35 | 51 | return $data; |
| 36 | 52 | } |
| 37 | 53 | |
| 38 | - $query = 'id, price, name, discounted_price, type, sites'; | |
| 54 | + $query = 'id, price, name, discounted_price, type, sites, coupon'; | |
| 39 | 55 | $response = $this->http()->query( |
| 40 | 56 | 'subscriptionPlans', |
| 41 | 57 | $query |
| 42 | 58 | )->post(); |
| @@ -60,8 +76,10 @@ | ||
| 60 | 76 | 'ip' => $_ip, |
| 61 | 77 | 'site_url' => $_site_url |
| 62 | 78 | ]; |
| 63 | 79 | |
| 80 | + $postArgs = []; | |
| 81 | + | |
| 64 | 82 | if ( $viaAPI ) { |
| 65 | 83 | $api_key = $this->get_param( 'api_key' ); |
| 66 | 84 | $funcArgs['api_key'] = $api_key; |
| 67 | 85 | |
| @@ -84,20 +102,24 @@ | ||
| 84 | 102 | if ( ! empty( $errors ) ) { |
| 85 | 103 | return $this->error( 'login_error', $errors, 'login', 400 ); |
| 86 | 104 | } |
| 87 | 105 | |
| 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 } }'; | |
| 106 | + $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 | 107 | |
| 90 | 108 | $response = $this->http()->mutation( |
| 91 | 109 | $viaAPI ? 'connectWithApiKey' : 'connect', |
| 92 | 110 | $query, |
| 93 | 111 | $funcArgs |
| 94 | - )->post(); | |
| 112 | + )->post($postArgs); | |
| 95 | 113 | |
| 96 | 114 | if ( is_wp_error( $response ) ) { |
| 97 | 115 | return $response; |
| 98 | 116 | } |
| 99 | 117 | |
| 118 | + if ( empty( $response['user']['api_key'] ) ) { | |
| 119 | + return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); | |
| 120 | + } | |
| 121 | + | |
| 100 | 122 | if ( $global_signin && ! Login::is_globally_signed() ) { |
| 101 | 123 | Options::set_global_login(); |
| 102 | 124 | } |
| 103 | 125 | |
| @@ -132,8 +154,17 @@ | ||
| 132 | 154 | |
| 133 | 155 | unset( $response['user']['reviews'] ); |
| 134 | 156 | $meta['reviews'] = $_reviews; |
| 135 | 157 | } |
| 158 | + | |
| 159 | + if(Helper::is_dev_api()){ | |
| 160 | + $response['user']['is_dev_api'] = true; | |
| 161 | + } | |
| 162 | + | |
| 163 | + if(! empty( $response['user'] ) && is_array($response['user'])){ | |
| 164 | + $response['user']['ip'] = $_ip; | |
| 165 | + $response['user']['site_url'] = base64_encode( $_site_url ); | |
| 166 | + } | |
| 136 | 167 | |
| 137 | 168 | $this->utils( 'options' )->set( 'user', $response['user'] ); |
| 138 | 169 | $response['user']['meta'] = $this->user_meta( $meta ); |
| 139 | 170 | |