| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\ProfileCollections\REST; |
| 4 |
|
| 5 |
use Templately\API\API; |
| 6 |
|
| 7 |
class Profile extends API { |
| 8 |
|
| 9 |
public function register_routes() { |
| 10 |
$this->get( 'profile/download-history', [ $this, 'get_download_history' ] ); |
| 11 |
$this->get( 'profile/my-favourites', [ $this, 'get_my_favourites' ] ); |
| 12 |
$this->get( 'profile/purchased-items', [ $this, 'get_my_purchased_items' ] ); |
| 13 |
} |
| 14 |
|
| 15 |
public function get_download_history() { |
| 16 |
$page = $this->get_param( 'page', 1, 'intval' ); |
| 17 |
$per_page = $this->get_param( 'per_page', 10, 'intval' ); |
| 18 |
|
| 19 |
$response = $this->http()->mutation( 'myDownloadHistory', 'data{ name, thumbnail, downloaded_at, slug, type }, current_page, total_page, total', [ |
| 20 |
'api_key' => $this->api_key, |
| 21 |
'per_page' => $per_page, |
| 22 |
"page" => $page |
| 23 |
] )->post(); |
| 24 |
|
| 25 |
if ( is_wp_error( $response ) ) { |
| 26 |
return $this->error( 'invalid_download_history_response', __( $response->get_error_message(), 'templately' ), 'profile/download-history' ); |
| 27 |
} |
| 28 |
|
| 29 |
if ( isset( $response['total'] ) ) { |
| 30 |
$this->utils( 'options' )->set( 'total_download_counts', $response['total'] ); |
| 31 |
} |
| 32 |
|
| 33 |
return $response; |
| 34 |
} |
| 35 |
|
| 36 |
public function get_my_favourites() { |
| 37 |
$type = $this->get_param( 'itemType', 'all' ); |
| 38 |
$plan = $this->get_param( 'plan', 'all' ); |
| 39 |
$plan_type = $this->get_plan( $plan ); |
| 40 |
$page = $this->get_param( 'page', 1, 'intval' ); |
| 41 |
$per_page = $this->get_param( 'per_page', 20, 'intval' ); |
| 42 |
|
| 43 |
$funcArgs = [ |
| 44 |
'api_key' => $this->api_key, |
| 45 |
"page" => $page, |
| 46 |
"per_page" => $per_page, |
| 47 |
"plan_type" => $plan_type |
| 48 |
]; |
| 49 |
|
| 50 |
if ( $type != 'all' ) { |
| 51 |
$funcArgs['type'] = $type; |
| 52 |
} |
| 53 |
|
| 54 |
$response = $this->http()->mutation( 'myFavouriteItem', 'total_page, current_page, data { id, name, rating, type, slug, favourite_count, thumbnail, thumbnail2, thumbnail3, price, author{ display_name, name, joined }, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link } }', $funcArgs )->post(); |
| 55 |
|
| 56 |
if ( is_wp_error( $response ) ) { |
| 57 |
return $this->error( 'invalid_my_favourites_response', __( $response->get_error_message(), 'templately' ), 'profile/my-favourites' ); |
| 58 |
} |
| 59 |
|
| 60 |
return $response; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* The buyer's purchased items. |
| 65 |
* |
| 66 |
* `platform` is OMITTED unless a caller explicitly asks for one. The cloud's |
| 67 |
* `myItems` resolver applies the platform filter only `when(! empty($args['platform']))`, |
| 68 |
* so sending nothing means "every platform" — which is what both callers want and |
| 69 |
* neither ever asked for: the paged My Items table sends no platform, and the |
| 70 |
* owned-item INDEX (`all=true`, the thing that decides whether a pack shows |
| 71 |
* "Purchase This Pack" or "Build Full Website") sends none either. |
| 72 |
* |
| 73 |
* This defaulted to `'elementor'`, so the index only ever contained Elementor |
| 74 |
* purchases. A Gutenberg pack the user had just bought stayed unowned forever — the |
| 75 |
* detail page kept offering to sell it back to them, through reloads and re-syncs, |
| 76 |
* because the answer it cached had the purchase filtered out server-side. |
| 77 |
*/ |
| 78 |
public function get_my_purchased_items() { |
| 79 |
$page = $this->get_param( 'page', 1, 'intval' ); |
| 80 |
$per_page = $this->get_param( 'per_page', 10, 'intval' ); |
| 81 |
$_all = $this->get_param( 'all', false ); |
| 82 |
$platform = $this->get_param( 'platform', '' ); |
| 83 |
|
| 84 |
$funcArgs = [ |
| 85 |
'api_key' => $this->api_key, |
| 86 |
'page' => $page, |
| 87 |
'per_page' => $per_page |
| 88 |
]; |
| 89 |
|
| 90 |
if ( ! empty( $platform ) ) { |
| 91 |
$funcArgs['platform'] = $platform; |
| 92 |
} |
| 93 |
|
| 94 |
$query = 'total_page, current_page, data { id, item_id, name, slug, type, platform, thumbnail, purchased_at }'; |
| 95 |
|
| 96 |
if ( $_all ) { |
| 97 |
$query = 'total_page, current_page, data { id, item_id, type, platform }'; |
| 98 |
$funcArgs['per_page'] = -1; |
| 99 |
} |
| 100 |
|
| 101 |
$response = $this->http()->query( 'myItems', $query, $funcArgs )->post(); |
| 102 |
|
| 103 |
if ( $response ) { |
| 104 |
// $user = $this->utils('options')->get('user'); |
| 105 |
|
| 106 |
// $user['is_verified'] = true; |
| 107 |
// $this->utils('options')->set('user', $user); |
| 108 |
} |
| 109 |
|
| 110 |
return $response; |
| 111 |
} |
| 112 |
} |