| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
|
| 7 |
class Profile extends API { |
| 8 |
|
| 9 |
public function register_routes() { |
| 10 |
$this->get( 'profile/sync', [ $this, 'sync' ] ); |
| 11 |
$this->get( 'profile/verified', [ $this, 'verified' ] ); |
| 12 |
$this->get( 'profile/download-history', [ $this, 'get_download_history' ] ); |
| 13 |
$this->get( 'profile/my-favourites', [ $this, 'get_my_favourites' ] ); |
| 14 |
} |
| 15 |
|
| 16 |
public function sync() { |
| 17 |
$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 } }'; |
| 18 |
|
| 19 |
$funcArgs = [ |
| 20 |
'api_key' => $this->api_key, |
| 21 |
'site_url' => home_url( '/' ), |
| 22 |
'ip' => Helper::get_ip() |
| 23 |
]; |
| 24 |
|
| 25 |
$response = $this->http()->mutation( |
| 26 |
'connectWithApiKey', |
| 27 |
$query, |
| 28 |
$funcArgs |
| 29 |
)->post(); |
| 30 |
|
| 31 |
if( is_wp_error( $response ) ) { |
| 32 |
return $response; |
| 33 |
} |
| 34 |
|
| 35 |
$meta = [ |
| 36 |
'is_globally_signed' => Login::is_globally_signed(), |
| 37 |
'signed_as_global' => Login::signed_as_global(), |
| 38 |
]; |
| 39 |
|
| 40 |
if( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) { |
| 41 |
$_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] ); |
| 42 |
$this->utils('options')->set( 'cloud_activity', $_cloud_activity ); |
| 43 |
$meta['cloud_activity'] = $_cloud_activity; |
| 44 |
unset( $response['user']['my_cloud']['last_pushed'] ); |
| 45 |
} |
| 46 |
|
| 47 |
if( ! empty( $response['user']['favourites'] ) ) { |
| 48 |
$_favourites = $this->utils('helper')->normalizeFavourites( $response['user']['favourites'] ); |
| 49 |
$this->utils('options')->set('favourites', $_favourites); |
| 50 |
|
| 51 |
unset( $response['user']['favourites'] ); |
| 52 |
$meta['favourites'] = $_favourites; |
| 53 |
} |
| 54 |
|
| 55 |
$this->utils('options')->set('user', $response['user']); |
| 56 |
$response['user']['meta'] = Login::get_instance()->user_meta( $meta ); |
| 57 |
|
| 58 |
return $this->success( $response ); |
| 59 |
} |
| 60 |
|
| 61 |
public function verified(){ |
| 62 |
$funcArgs = [ |
| 63 |
'api_key' => $this->api_key |
| 64 |
]; |
| 65 |
|
| 66 |
$response = $this->http()->query( |
| 67 |
'isVerifiedUser', |
| 68 |
'', |
| 69 |
$funcArgs |
| 70 |
)->post(); |
| 71 |
|
| 72 |
if( $response ) { |
| 73 |
$user = $this->utils('options')->get('user'); |
| 74 |
|
| 75 |
$user['is_verified'] = true; |
| 76 |
$this->utils('options')->set('user', $user); |
| 77 |
} |
| 78 |
|
| 79 |
return $response; |
| 80 |
} |
| 81 |
|
| 82 |
public function get_download_history() { |
| 83 |
$page = $this->get_param( 'page', 1, 'intval' ); |
| 84 |
$per_page = $this->get_param( 'per_page', 10, 'intval' ); |
| 85 |
|
| 86 |
$response = $this->http()->mutation( |
| 87 |
'myDownloadHistory', |
| 88 |
'data{ name, thumbnail, downloaded_at, slug, type }, current_page, total_page', |
| 89 |
[ |
| 90 |
'api_key' => $this->api_key, |
| 91 |
'per_page' => $per_page, |
| 92 |
"page" => $page |
| 93 |
] |
| 94 |
)->post(); |
| 95 |
|
| 96 |
if ( is_wp_error( $response ) ) { |
| 97 |
return $this->error( |
| 98 |
'invalid_download_history_response', |
| 99 |
__( $response->get_error_message(), 'templately' ), |
| 100 |
'profile/download-history', |
| 101 |
$response->get_error_code() |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return $response; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_my_favourites() { |
| 109 |
$type = $this->get_param( 'itemType', 'all' ); |
| 110 |
$plan = $this->get_param( 'plan', 'all' ); |
| 111 |
$plan_type = $this->get_plan( $plan ); |
| 112 |
$page = $this->get_param( 'page', 1, 'intval' ); |
| 113 |
$per_page = $this->get_param( 'per_page', 20, 'intval' ); |
| 114 |
|
| 115 |
$funcArgs = [ |
| 116 |
'api_key' => $this->api_key, |
| 117 |
"page" => $page, |
| 118 |
"per_page" => $per_page, |
| 119 |
"plan_type" => $plan_type |
| 120 |
]; |
| 121 |
|
| 122 |
if( $type != 'all' ) { |
| 123 |
$funcArgs['type'] = $type; |
| 124 |
} |
| 125 |
|
| 126 |
$response = $this->http()->mutation( |
| 127 |
'myFavouriteItem', |
| 128 |
'total_page, current_page, data { id, name, rating, type, slug, favourite_count, thumbnail, thumbnail2, thumbnail3, price, author{ display_name, name, joined } }', |
| 129 |
$funcArgs |
| 130 |
)->post(); |
| 131 |
|
| 132 |
if ( is_wp_error( $response ) ) { |
| 133 |
return $this->error( 'invalid_my_favourites_response', __( $response->get_error_message(), 'templately' ), 'profile/my-favourites', $response->get_error_code() ); |
| 134 |
} |
| 135 |
|
| 136 |
return $response; |
| 137 |
} |
| 138 |
} |