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