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