| @@ -6,276 +6,172 @@ | ||
| 6 | 6 | use Templately\Utils\Helper; |
| 7 | 7 | use Templately\Utils\Options; |
| 8 | 8 | |
| 9 | 9 | class Login extends API { |
| 10 | - public function permission_check( WP_REST_Request $request ) { | |
| 10 | + public function permission_check( WP_REST_Request $request ) { | |
| 11 | 11 | $this->request = $request; |
| 12 | - $_route = $request->get_route(); | |
| 13 | - if ( '/templately/v1/login' === $_route ) { | |
| 14 | - return true; | |
| 15 | - } | |
| 12 | + return true; | |
| 13 | + } | |
| 16 | 14 | |
| 17 | - if ( '/templately/v1/pricing' === $_route ) { | |
| 18 | - return true; | |
| 19 | - } | |
| 15 | + public function register_routes() { | |
| 16 | + $this->post( 'login', [ $this, 'login' ] ); | |
| 17 | + $this->post( 'logout', [ $this, 'logout' ] ); | |
| 18 | + $this->get( 'is-signed', [ $this, 'is_signed' ] ); | |
| 19 | + } | |
| 20 | 20 | |
| 21 | - if ( '/templately/v1/google-auth-url' === $_route ) { | |
| 22 | - return true; | |
| 23 | - } | |
| 21 | + public function login(){ | |
| 22 | + $errors = []; | |
| 23 | + $_ip = Helper::get_ip(); | |
| 24 | + $_site_url = home_url( '/' ); | |
| 24 | 25 | |
| 25 | - return parent::permission_check( $request ); | |
| 26 | - } | |
| 26 | + $global_signin = (bool) $this->get_param( 'global_signin', false ); | |
| 27 | + $viaAPI = (bool) $this->get_param( 'viaAPI', false ); | |
| 28 | + $email = $this->get_param( 'email', '', 'sanitize_email' ); | |
| 29 | + $password = $this->get_param( 'password' ); | |
| 27 | 30 | |
| 28 | - public function register_routes() { | |
| 29 | - $this->post( 'login', [$this, 'login'] ); | |
| 30 | - $this->post( 'logout', [$this, 'logout'] ); | |
| 31 | - $this->get( 'is-signed', [$this, 'is_signed'] ); | |
| 32 | - $this->get( 'pricing', [$this, 'pricing'] ); | |
| 33 | - $this->get( 'google-auth-url', [$this, 'google_auth_url'] ); | |
| 34 | - } | |
| 31 | + $funcArgs = [ | |
| 32 | + 'ip' => $_ip, | |
| 33 | + 'site_url' => $_site_url, | |
| 34 | + ]; | |
| 35 | 35 | |
| 36 | - public function google_auth_url() { | |
| 37 | - // Get redirect_to parameter from request if provided | |
| 38 | - $redirect_to = $this->get_param( 'redirect-to', '' ); | |
| 36 | + if( $viaAPI ) { | |
| 37 | + $api_key = $this->get_param( 'api_key' ); | |
| 38 | + $funcArgs['api_key'] = $api_key; | |
| 39 | 39 | |
| 40 | - // Use client-provided current_url instead of HTTP_REFERER for reliability | |
| 41 | - $current_url = $this->get_param( 'current_url', '' ); | |
| 40 | + if ( empty( $api_key ) ) { | |
| 41 | + $errors['api_key'] = __( 'API Key field cannot be empty.', 'templately' ); | |
| 42 | + } | |
| 43 | + } else { | |
| 44 | + $funcArgs['email'] = $email; | |
| 45 | + $funcArgs['password'] = $password; | |
| 42 | 46 | |
| 43 | - $url = $this->http()->google_auth_url( $redirect_to, $current_url ); | |
| 44 | - return [ | |
| 45 | - 'status' => 'success', | |
| 46 | - 'url' => $url | |
| 47 | - ]; | |
| 48 | - } | |
| 47 | + if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { | |
| 48 | + $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' ); | |
| 49 | + } | |
| 49 | 50 | |
| 50 | - public function pricing(){ | |
| 51 | - $data = get_transient( "templately_subscriptions" ); | |
| 51 | + if ( empty( $password ) ) { | |
| 52 | + $errors['password'] = __( 'Password field cannot be empty.', 'templately' ); | |
| 53 | + } | |
| 54 | + } | |
| 52 | 55 | |
| 53 | - if( is_array( $data ) && ! empty( $data ) ) { | |
| 54 | - return $data; | |
| 56 | + if( ! empty( $errors ) ) { | |
| 57 | + return $this->error( 'login_error', $errors, 'login', 400 ); | |
| 55 | 58 | } |
| 56 | 59 | |
| 57 | - $query = 'id, price, name, discounted_price, type, sites, coupon'; | |
| 58 | - $response = $this->http()->query( | |
| 59 | - 'subscriptionPlans', | |
| 60 | - $query | |
| 60 | + $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 } }'; | |
| 61 | + | |
| 62 | + $response = $this->http()->mutation( | |
| 63 | + $viaAPI ? 'connectWithApiKey' : 'connect', | |
| 64 | + $query, | |
| 65 | + $funcArgs | |
| 61 | 66 | )->post(); |
| 62 | 67 | |
| 63 | - set_transient( "templately_subscriptions", $response, WEEK_IN_SECONDS ); | |
| 68 | + if ( $global_signin && ! Login::is_globally_signed() ) { | |
| 69 | + Options::set_global_login(); | |
| 70 | + } | |
| 64 | 71 | |
| 65 | - return $response; | |
| 66 | - } | |
| 72 | + if ( ! empty( $response['user']['api_key'] ) ) { | |
| 73 | + $this->utils('options')->set( 'api_key', $response['user']['api_key'] ); | |
| 74 | + unset( $response['user']['api_key'] ); | |
| 75 | + } | |
| 67 | 76 | |
| 68 | - public function login() { | |
| 69 | - $errors = []; | |
| 70 | - $_ip = Helper::get_ip(); | |
| 71 | - $_site_url = home_url( '/' ); | |
| 77 | + $meta = [ | |
| 78 | + 'is_globally_signed' => Login::is_globally_signed(), | |
| 79 | + 'signed_as_global' => Login::signed_as_global(), | |
| 80 | + ]; | |
| 72 | 81 | |
| 73 | - $global_signin = (bool) $this->get_param( 'global_signin', false ); | |
| 74 | - $viaAPI = (bool) $this->get_param( 'viaAPI', false ); | |
| 75 | - $email = $this->get_param( 'email', '', 'sanitize_email' ); | |
| 76 | - $password = $this->get_param( 'password' ); | |
| 82 | + if( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) { | |
| 83 | + $_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] ); | |
| 84 | + $this->utils('options')->set( 'cloud_activity', $_cloud_activity ); | |
| 85 | + $meta['cloud_activity'] = $_cloud_activity; | |
| 86 | + unset( $response['user']['my_cloud']['last_pushed'] ); | |
| 87 | + } | |
| 77 | 88 | |
| 78 | - $funcArgs = [ | |
| 79 | - 'ip' => $_ip, | |
| 80 | - 'site_url' => $_site_url | |
| 81 | - ]; | |
| 89 | + if( ! empty( $response['user']['favourites'] ) ) { | |
| 90 | + $_favourites = $this->utils('helper')->normalizeFavourites( $response['user']['favourites'] ); | |
| 91 | + $this->utils('options')->set('favourites', $_favourites); | |
| 82 | 92 | |
| 83 | - $postArgs = []; | |
| 93 | + unset( $response['user']['favourites'] ); | |
| 94 | + $meta['favourites'] = $_favourites; | |
| 95 | + } | |
| 84 | 96 | |
| 85 | - if ( $viaAPI ) { | |
| 86 | - $api_key = $this->get_param( 'api_key' ); | |
| 87 | - $funcArgs['api_key'] = $api_key; | |
| 97 | + $this->utils('options')->set('user', $response['user']); | |
| 98 | + $response['user']['meta'] = $this->user_meta( $meta ); | |
| 88 | 99 | |
| 89 | - if ( empty( $api_key ) ) { | |
| 90 | - $errors['api_key'] = __( 'API Key field cannot be empty.', 'templately' ); | |
| 91 | - } | |
| 92 | - } else { | |
| 93 | - $funcArgs['email'] = $email; | |
| 94 | - $funcArgs['password'] = addcslashes( $password, '"' ); | |
| 100 | + return $response; | |
| 101 | + } | |
| 95 | 102 | |
| 96 | - if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { | |
| 97 | - $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' ); | |
| 98 | - } | |
| 103 | + public function logout(){ | |
| 104 | + // Remove All Metas | |
| 105 | + $this->utils('options') | |
| 106 | + ->remove('user') | |
| 107 | + ->remove('favourites') | |
| 108 | + ->remove('cloud_activity') | |
| 109 | + ->remove('api_key') | |
| 110 | + ->remove('global_login'); | |
| 99 | 111 | |
| 100 | - if ( empty( $password ) ) { | |
| 101 | - $errors['password'] = __( 'Password field cannot be empty.', 'templately' ); | |
| 102 | - } | |
| 103 | - } | |
| 112 | + if( $this->utils('options')->whoami() === 'global' ) { | |
| 113 | + $this->utils('options')->remove_global_login(); | |
| 114 | + } | |
| 104 | 115 | |
| 105 | - if ( ! empty( $errors ) ) { | |
| 106 | - return $this->error( 'login_error', $errors, 'login', 400 ); | |
| 107 | - } | |
| 116 | + $global_user_id = $this->utils('options')->is_global(); | |
| 117 | + if( $global_user_id !== $this->utils('options')->current_user_id() ) { | |
| 118 | + $global_user = $this->utils('options')->get( 'user', false, $global_user_id ); | |
| 108 | 119 | |
| 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 } }'; | |
| 120 | + if( ! empty( $global_user ) ) { | |
| 121 | + $global_user['meta'] = $this->user_meta(); | |
| 122 | + } | |
| 123 | + } | |
| 110 | 124 | |
| 111 | - $response = $this->http()->mutation( | |
| 112 | - $viaAPI ? 'connectWithApiKey' : 'connect', | |
| 113 | - $query, | |
| 114 | - $funcArgs | |
| 115 | - )->post($postArgs); | |
| 125 | + $response = [ | |
| 126 | + 'status' => 'success', | |
| 127 | + 'message' => __( 'Logged out.', 'templately' ), | |
| 128 | + ]; | |
| 116 | 129 | |
| 117 | - if ( is_wp_error( $response ) ) { | |
| 118 | - return $response; | |
| 119 | - } | |
| 130 | + if( ! empty( $global_user ) ) { | |
| 131 | + $response['global_user'] = $global_user; | |
| 132 | + } | |
| 120 | 133 | |
| 121 | - if ( empty( $response['user']['api_key'] ) ) { | |
| 122 | - return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 ); | |
| 123 | - } | |
| 134 | + return $response; | |
| 135 | + } | |
| 124 | 136 | |
| 125 | - if ( $global_signin && ! Login::is_globally_signed() ) { | |
| 126 | - Options::set_global_login(); | |
| 127 | - } | |
| 137 | + public static function is_signed(){ | |
| 138 | + $_response = [ | |
| 139 | + 'status' => 'success' | |
| 140 | + ]; | |
| 128 | 141 | |
| 129 | - if ( ! empty( $response['user']['api_key'] ) ) { | |
| 130 | - $this->utils( 'options' )->set( 'api_key', $response['user']['api_key'] ); | |
| 131 | - unset( $response['user']['api_key'] ); | |
| 132 | - } | |
| 142 | + $_user = (new static)->utils( 'options' )->get('user', null); | |
| 133 | 143 | |
| 134 | - $meta = [ | |
| 135 | - 'is_globally_signed' => Login::is_globally_signed(), | |
| 136 | - 'signed_as_global' => Login::signed_as_global() | |
| 137 | - ]; | |
| 144 | + if( ! is_null( $_user ) ) { | |
| 145 | + $_user['meta'] = self::get_instance()->user_meta(); | |
| 146 | + } | |
| 138 | 147 | |
| 139 | - if ( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) { | |
| 140 | - $_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] ); | |
| 141 | - $this->utils( 'options' )->set( 'cloud_activity', $_cloud_activity ); | |
| 142 | - $meta['cloud_activity'] = $_cloud_activity; | |
| 143 | - unset( $response['user']['my_cloud']['last_pushed'] ); | |
| 144 | - } | |
| 145 | - | |
| 146 | - if ( ! empty( $response['user']['favourites'] ) ) { | |
| 147 | - $_favourites = $this->utils( 'helper' )->normalizeFavourites( $response['user']['favourites'] ); | |
| 148 | - $this->utils( 'options' )->set( 'favourites', $_favourites ); | |
| 149 | - | |
| 150 | - unset( $response['user']['favourites'] ); | |
| 151 | - $meta['favourites'] = $_favourites; | |
| 152 | - } | |
| 153 | - | |
| 154 | - if ( ! empty( $response['user']['reviews'] ) ) { | |
| 155 | - $_reviews = $this->utils( 'helper' )->normalizeReviews( $response['user']['reviews'] ); | |
| 156 | - $this->utils( 'options' )->set( 'reviews', $_reviews ); | |
| 157 | - | |
| 158 | - unset( $response['user']['reviews'] ); | |
| 159 | - $meta['reviews'] = $_reviews; | |
| 148 | + if( empty( $_user ) ) { | |
| 149 | + $_response['status'] = 'error'; | |
| 160 | 150 | } |
| 161 | 151 | |
| 162 | - if(Helper::is_dev_api()){ | |
| 163 | - $response['user']['is_dev_api'] = true; | |
| 164 | - } | |
| 152 | + $_response['user'] = $_user; | |
| 165 | 153 | |
| 166 | - if(! empty( $response['user'] ) && is_array($response['user'])){ | |
| 167 | - $response['user']['ip'] = $_ip; | |
| 168 | - $response['user']['site_url'] = base64_encode( $_site_url ); | |
| 169 | - } | |
| 154 | + return $_response; | |
| 155 | + } | |
| 170 | 156 | |
| 171 | - $this->utils( 'options' )->set( 'user', $response['user'] ); | |
| 172 | - $response['user']['meta'] = $this->user_meta( $meta ); | |
| 157 | + public function user_meta( $meta = [] ){ | |
| 158 | + $_meta = [ | |
| 159 | + 'link_account' => self::utils( 'options' )->link_account(), | |
| 160 | + 'unlink_account' => self::utils( 'options' )->unlink_account(), | |
| 161 | + 'is_globally_signed' => Login::is_globally_signed(), | |
| 162 | + 'signed_as_global' => Login::signed_as_global(), | |
| 163 | + 'starred' => self::utils('options')->get('favourites'), | |
| 164 | + 'cloud_activity' => self::utils( 'options' )->get('cloud_activity'), | |
| 165 | + 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get('api_key' ) ), | |
| 166 | + ]; | |
| 173 | 167 | |
| 174 | - return $response; | |
| 175 | - } | |
| 168 | + return array_merge( $_meta, $meta ); | |
| 169 | + } | |
| 176 | 170 | |
| 177 | - public function logout() { | |
| 178 | - $response = $this->http()->mutation( | |
| 179 | - 'disconnect', | |
| 180 | - 'status, message, data', | |
| 181 | - [ | |
| 182 | - 'api_key' => $this->api_key, | |
| 183 | - "site_url" => home_url( '/' ) | |
| 184 | - ] | |
| 185 | - )->post(); | |
| 186 | - | |
| 187 | - if ( is_wp_error( $response ) ) { | |
| 188 | - return $response; | |
| 189 | - } | |
| 190 | - | |
| 191 | - if ( ! isset( $response['status'] ) || $response['status'] !== 'success' ) { | |
| 192 | - return $this->error( 'logout_error', $response['message'], 'logout', 404 ); | |
| 193 | - } | |
| 194 | - | |
| 195 | - // Remove All Metas | |
| 196 | - $global_user = $this->delete(); | |
| 197 | - | |
| 198 | - $response = [ | |
| 199 | - 'status' => 'success', | |
| 200 | - 'message' => __( 'Logged out.', 'templately' ) | |
| 201 | - ]; | |
| 202 | - | |
| 203 | - if ( ! empty( $global_user ) ) { | |
| 204 | - $response['global_user'] = $global_user; | |
| 205 | - } | |
| 206 | - | |
| 207 | - return $response; | |
| 208 | - } | |
| 209 | - | |
| 210 | - public function delete(){ | |
| 211 | - $this->utils( 'options' ) | |
| 212 | - ->remove( 'user' ) | |
| 213 | - ->remove( 'favourites' ) | |
| 214 | - ->remove( 'reviews' ) | |
| 215 | - ->remove( 'cloud_activity' ) | |
| 216 | - ->remove( 'api_key' ) | |
| 217 | - ->remove( 'global_login' ) | |
| 218 | - ->remove( 'total_download_counts' ) | |
| 219 | - ->remove( 'templates_in_clouds' ); | |
| 220 | - | |
| 221 | - if ( $this->utils( 'options' )->who_am_i() === 'global' ) { | |
| 222 | - $this->utils( 'options' )->remove_global_login(); | |
| 223 | - } | |
| 224 | - | |
| 225 | - $global_user_id = $this->utils( 'options' )->is_global(); | |
| 226 | - $global_user = null; | |
| 227 | - | |
| 228 | - if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) { | |
| 229 | - $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id ); | |
| 230 | - | |
| 231 | - if ( ! empty( $global_user ) ) { | |
| 232 | - $global_user['meta'] = $this->user_meta(); | |
| 233 | - } | |
| 234 | - } | |
| 235 | - | |
| 236 | - return $global_user; | |
| 171 | + public static function is_globally_signed(){ | |
| 172 | + return rest_sanitize_boolean( ( new static )->utils('options')->is_globally_signed() ); | |
| 237 | 173 | } |
| 238 | - | |
| 239 | - public static function is_signed(): array { | |
| 240 | - $_response = [ | |
| 241 | - 'status' => 'success' | |
| 242 | - ]; | |
| 243 | - | |
| 244 | - $_user = ( new static )->utils( 'options' )->get( 'user', null ); | |
| 245 | - | |
| 246 | - if ( ! is_null( $_user ) ) { | |
| 247 | - $_user['meta'] = self::get_instance()->user_meta(); | |
| 248 | - } | |
| 249 | - | |
| 250 | - if ( empty( $_user ) ) { | |
| 251 | - $_response['status'] = 'error'; | |
| 252 | - } | |
| 253 | - | |
| 254 | - $_response['user'] = $_user; | |
| 255 | - | |
| 256 | - return $_response; | |
| 257 | - } | |
| 258 | - | |
| 259 | - public function user_meta( $meta = [] ): array { | |
| 260 | - $_meta = [ | |
| 261 | - 'link_account' => self::utils( 'options' )->link_account(), | |
| 262 | - 'unlink_account' => self::utils( 'options' )->unlink_account(), | |
| 263 | - 'is_globally_signed' => Login::is_globally_signed(), | |
| 264 | - 'signed_as_global' => Login::signed_as_global(), | |
| 265 | - 'starred' => self::utils( 'options' )->get( 'favourites' ), | |
| 266 | - 'reviews' => self::utils( 'options' )->get( 'reviews' ), | |
| 267 | - 'cloud_activity' => self::utils( 'options' )->get( 'cloud_activity' ), | |
| 268 | - 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get( 'api_key' ) ) | |
| 269 | - ]; | |
| 270 | - | |
| 271 | - return array_merge( $_meta, $meta ); | |
| 272 | - } | |
| 273 | - | |
| 274 | - public static function is_globally_signed(): bool { | |
| 275 | - return rest_sanitize_boolean( ( new static )->utils( 'options' )->is_globally_signed() ); | |
| 276 | - } | |
| 277 | - | |
| 278 | - public static function signed_as_global(): bool { | |
| 279 | - return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() ); | |
| 280 | - } | |
| 174 | + public static function signed_as_global(){ | |
| 175 | + return rest_sanitize_boolean( ( new static )->utils('options')->signed_as_global() ); | |
| 176 | + } | |
| 281 | 177 | } |