| @@ -35,10 +35,28 @@ | ||
| 35 | 35 | 'per_page' => [ |
| 36 | 36 | 'default' => 40, |
| 37 | 37 | 'required' => false |
| 38 | 38 | ], |
| 39 | + 'sort_by' => [ | |
| 40 | + 'default' => 'latest', | |
| 41 | + 'required' => false, | |
| 42 | + 'validate_callback' => function( $param, $request, $key ){ | |
| 43 | + return empty( $param ) || in_array( $param, [ 'latest', 'rating', 'download' ], true ); | |
| 44 | + } | |
| 45 | + ], | |
| 39 | 46 | ] ); |
| 40 | 47 | |
| 48 | + // only number | |
| 49 | + $this->get( 'items/(?P<id>[0-9]+)', [ $this, 'get_item_by_id' ], [ | |
| 50 | + 'id' => [ | |
| 51 | + 'required' => true, | |
| 52 | + ], | |
| 53 | + 'type' => [ | |
| 54 | + 'default' => 'block' | |
| 55 | + ] | |
| 56 | + ] ); | |
| 57 | + | |
| 58 | + // with slug | |
| 41 | 59 | $this->get( 'items/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'get_item' ], [ |
| 42 | 60 | 'slug' => [ |
| 43 | 61 | 'required' => true, |
| 44 | 62 | ], |
| @@ -62,8 +80,10 @@ | ||
| 62 | 80 | $this->post( 'items/favourite', [ $this, 'set_favourite' ] ); |
| 63 | 81 | $this->post( 'items/rating', [ $this, 'set_rating' ] ); |
| 64 | 82 | $this->get( 'items-count', [ $this, 'get_counts' ] ); |
| 65 | 83 | $this->get( 'featured-items', [ $this, 'featured_items' ] ); |
| 84 | + $this->get( 'trending-items', [ $this, 'trending_items' ] ); | |
| 85 | + $this->get( 'related-items', [ $this, 'related_items' ]); | |
| 66 | 86 | } |
| 67 | 87 | |
| 68 | 88 | /** |
| 69 | 89 | * @param string $type |
| @@ -85,9 +105,10 @@ | ||
| 85 | 105 | $include_search = $this->get_param( 'include_search' ); |
| 86 | 106 | $page = $this->get_param( 'page', 1, 'intval' ); |
| 87 | 107 | $per_page = $this->get_param( 'per_page', 40, 'intval' ); |
| 88 | 108 | $template_type_id = $this->get_param( 'template_type_id', 0, 'intval' ); |
| 89 | - $category_id = $this->get_param( 'category_id', 0, 'intval' ); | |
| 109 | + $categories_raw = $this->get_param( 'categories' ); | |
| 110 | + $sort_by = $this->get_param( 'sort_by', 'latest' ); | |
| 90 | 111 | |
| 91 | 112 | $dependencies = $request->get_param( 'dependencies' ); |
| 92 | 113 | $tags = $request->get_param( 'tags' ); |
| 93 | 114 | |
| @@ -94,8 +115,9 @@ | ||
| 94 | 115 | $funcArgs = [ |
| 95 | 116 | 'page' => $page, |
| 96 | 117 | 'per_page' => $per_page, |
| 97 | 118 | 'platform' => $platform, |
| 119 | + 'sort_by' => $sort_by, | |
| 98 | 120 | ]; |
| 99 | 121 | |
| 100 | 122 | if ( $plan_type > 1 ) { |
| 101 | 123 | $funcArgs['plan_type'] = $plan_type; |
| @@ -100,10 +122,13 @@ | ||
| 100 | 122 | if ( $plan_type > 1 ) { |
| 101 | 123 | $funcArgs['plan_type'] = $plan_type; |
| 102 | 124 | } |
| 103 | 125 | |
| 104 | - if ( $category_id ) { | |
| 105 | - $funcArgs['category_id'] = $category_id; | |
| 126 | + if ( ! empty( $categories_raw ) ) { | |
| 127 | + $decoded = json_decode( $categories_raw, true ); | |
| 128 | + if ( is_array( $decoded ) && ! empty( $decoded ) ) { | |
| 129 | + $funcArgs['categories'] = wp_slash( json_encode( array_values( array_filter( array_map( 'intval', $decoded ) ) ) ) ); | |
| 130 | + } | |
| 106 | 131 | } |
| 107 | 132 | if ( $template_type_id > 0 ) { |
| 108 | 133 | $funcArgs['template_type_id'] = $template_type_id; |
| 109 | 134 | } |
| @@ -121,11 +146,11 @@ | ||
| 121 | 146 | if ( ! empty( $include_search ) ) { |
| 122 | 147 | $funcArgs['include_search'] = $include_search; |
| 123 | 148 | } |
| 124 | 149 | |
| 125 | - $query = 'total_page, current_page, data { id, fullsite_import, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, thumbnail, thumbnail2, thumbnail3 }'; | |
| 150 | + $query = 'total_page, current_page, data { id, fullsite_import, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, thumbnail, thumbnail2, thumbnail3, badges }'; | |
| 126 | 151 | if( $type !== 'packs' ) { |
| 127 | - $query = 'total_page, current_page, data { id, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, thumbnail }'; | |
| 152 | + $query = 'total_page, current_page, data { id, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, thumbnail, pack { id, has_settings }, badges }'; | |
| 128 | 153 | } |
| 129 | 154 | |
| 130 | 155 | if( $type === false ) { |
| 131 | 156 | return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); |
| @@ -130,13 +155,24 @@ | ||
| 130 | 155 | if( $type === false ) { |
| 131 | 156 | return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); |
| 132 | 157 | } |
| 133 | 158 | |
| 134 | - return $this->http()->query( | |
| 135 | - $type, | |
| 136 | - $query, | |
| 137 | - $funcArgs | |
| 138 | - )->post(); | |
| 159 | + $transient_key = "_templately_items_" . md5(json_encode([$type, $query, $funcArgs])); | |
| 160 | + $response = false; | |
| 161 | + | |
| 162 | + if ( defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) { | |
| 163 | + $response = Database::get_transient( $transient_key ); | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( empty( $response ) ) { | |
| 167 | + $response = $this->http()->query( $type, $query, $funcArgs )->post(); | |
| 168 | + | |
| 169 | + if ( ! empty( $response ) && ! is_wp_error( $response ) && defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) { | |
| 170 | + Database::set_transient( $transient_key, $response ); | |
| 171 | + } | |
| 172 | + } | |
| 173 | + | |
| 174 | + return $response; | |
| 139 | 175 | } |
| 140 | 176 | |
| 141 | 177 | public function get_item() { |
| 142 | 178 | $slug = $this->get_param( 'slug' ); |
| @@ -155,17 +191,37 @@ | ||
| 155 | 191 | if( $type === false ) { |
| 156 | 192 | return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); |
| 157 | 193 | } |
| 158 | 194 | |
| 159 | - $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, thumbnail, downloads, categories{ id, name, slug }, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, tags{ name, id }, categories{ name, id }, screenshots{ url }, banner, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail } }, live_url, template_type{ id, name, slug }'; | |
| 195 | + $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, downloads, categories{ id, name, slug }, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, tags{ name, id }, categories{ name, id }, screenshots{ url }, banner, published_at, updated_at, is_trending, badges, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail, badges }, has_settings }, live_url, template_type{ id, name, slug }'; | |
| 160 | 196 | $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }'; |
| 161 | 197 | |
| 162 | 198 | if ( $type == 'packs' ) { |
| 163 | - $params = 'data { id, fullsite_import, name, rating, type, slug, live_url, price, features, favourite_count, thumbnail, downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }'; | |
| 199 | + $params = 'data { id, fullsite_import, ai_compatible, has_settings, has_attachments, name, rating, type, slug, live_url, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, description, tags{ name, id }, banner, published_at, updated_at, is_trending, badges, template_type{ id, name, slug } downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }'; | |
| 164 | 200 | } |
| 165 | 201 | |
| 166 | - $response = $this->http()->query( $type, $params, [ 'slug' => $slug ] )->post(); | |
| 202 | + $args = [ | |
| 203 | + 'slug' => $slug, | |
| 204 | + ]; | |
| 205 | + if (!empty($this->api_key)) { | |
| 206 | + $args['api_key'] = $this->api_key; | |
| 207 | + } | |
| 167 | 208 | |
| 209 | + $transient_key = "_templately_items_" . md5(json_encode([$type, $params, $args])); | |
| 210 | + $response = false; | |
| 211 | + | |
| 212 | + if ( defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) { | |
| 213 | + $response = Database::get_transient( $transient_key ); | |
| 214 | + } | |
| 215 | + | |
| 216 | + if ( empty( $response ) ) { | |
| 217 | + $response = $this->http()->query( $type, $params, $args )->post(); | |
| 218 | + | |
| 219 | + if ( ! empty( $response ) && ! is_wp_error( $response ) && defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) { | |
| 220 | + Database::set_transient( $transient_key, $response ); | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 168 | 224 | if ( is_wp_error( $response ) ) { |
| 169 | 225 | return $response; |
| 170 | 226 | } |
| 171 | 227 | |
| @@ -180,8 +236,58 @@ | ||
| 180 | 236 | |
| 181 | 237 | return current( $response['data'] ); |
| 182 | 238 | } |
| 183 | 239 | |
| 240 | + public function get_item_by_id() { | |
| 241 | + $id = (int) $this->get_param( 'id' ); | |
| 242 | + $_type = $this->get_param( 'type', 'blocks' ); | |
| 243 | + $type = $this->get_query_types( $_type ); | |
| 244 | + | |
| 245 | + if ( empty( $id ) ) { | |
| 246 | + return $this->error( | |
| 247 | + 'invalid_item_slug', | |
| 248 | + __( 'Items id cannot be empty.', 'templately' ), | |
| 249 | + 'items/:id', | |
| 250 | + '400' | |
| 251 | + ); | |
| 252 | + } | |
| 253 | + | |
| 254 | + if( $type === false ) { | |
| 255 | + return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); | |
| 256 | + } | |
| 257 | + | |
| 258 | + $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, downloads, categories{ id, name, slug }, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, tags{ name, id }, categories{ name, id }, screenshots{ url }, banner, published_at, updated_at, is_trending, badges, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail, badges }, has_settings }, live_url, template_type{ id, name, slug }'; | |
| 259 | + $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }'; | |
| 260 | + | |
| 261 | + if ( $type == 'packs' ) { | |
| 262 | + $params = 'data { id, fullsite_import, ai_compatible, has_settings, has_attachments, name, rating, type, slug, live_url, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, description, tags{ name, id }, banner, published_at, updated_at, is_trending, badges, template_type{ id, name, slug } downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }'; | |
| 263 | + } | |
| 264 | + | |
| 265 | + $args = [ | |
| 266 | + 'id' => $id, | |
| 267 | + ]; | |
| 268 | + if (!empty($this->api_key)) { | |
| 269 | + $args['api_key'] = $this->api_key; | |
| 270 | + } | |
| 271 | + | |
| 272 | + $response = $this->http()->query( $type, $params, $args )->post(); | |
| 273 | + | |
| 274 | + if ( is_wp_error( $response ) ) { | |
| 275 | + return $response; | |
| 276 | + } | |
| 277 | + | |
| 278 | + if( empty( $response['data'] ) ) { | |
| 279 | + return $this->error( | |
| 280 | + 'invalid_response', | |
| 281 | + __('Item data not found', 'templately'), | |
| 282 | + '/items\/' . $id, | |
| 283 | + '404' | |
| 284 | + ); | |
| 285 | + } | |
| 286 | + | |
| 287 | + return current( $response['data'] ); | |
| 288 | + } | |
| 289 | + | |
| 184 | 290 | public function get_search_results( WP_REST_Request $request ) { |
| 185 | 291 | $keyword = $request->get_param( 'keyword' ); |
| 186 | 292 | $platform = $request->get_param( 'platform' ); |
| 187 | 293 | $query_type = $request->get_param( 'query_type' ); |
| @@ -274,9 +380,9 @@ | ||
| 274 | 380 | 'id' => $id, |
| 275 | 381 | 'type' => $type == 'block' || $type == 'page' ? 'item' : 'pack' |
| 276 | 382 | ]; |
| 277 | 383 | |
| 278 | - if( $response == 1 ) { | |
| 384 | + if( $response == 1 || $response === false ) { | |
| 279 | 385 | $_favourites = $this->utils('options')->get('favourites'); |
| 280 | 386 | $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites, true ); |
| 281 | 387 | $this->utils('options')->set('favourites', $_favourites); |
| 282 | 388 | |
| @@ -435,14 +541,14 @@ | ||
| 435 | 541 | * @param WP_REST_Request $request |
| 436 | 542 | * @return mixed |
| 437 | 543 | */ |
| 438 | 544 | public function featured_items(WP_REST_Request $request){ |
| 439 | - $defaults = Database::get_transient( 'featuredItems' ); | |
| 545 | + $platform = $request->get_param( 'platform' ); | |
| 546 | + $defaults = Database::get_transient( "featuredItems_{$platform}" ); | |
| 440 | 547 | if( $defaults ) { |
| 441 | 548 | return $defaults; |
| 442 | 549 | } |
| 443 | 550 | |
| 444 | - $platform = $request->get_param( 'platform' ); | |
| 445 | 551 | $funcArgs = [ |
| 446 | 552 | // 'page' => 1, |
| 447 | 553 | // 'per_page' => 10, |
| 448 | 554 | 'platform' => $platform, |
| @@ -448,9 +554,9 @@ | ||
| 448 | 554 | 'platform' => $platform, |
| 449 | 555 | ]; |
| 450 | 556 | $response = $this->http()->query( |
| 451 | 557 | 'featuredItems', |
| 452 | - 'data{ id, name, slug, price, type, thumbnail }', | |
| 558 | + 'data{ id, name, slug, price, type, thumbnail, badges }', | |
| 453 | 559 | $funcArgs |
| 454 | 560 | )->post(); |
| 455 | 561 | |
| 456 | 562 | if( ! is_wp_error( $response ) ) { |
| @@ -457,6 +563,85 @@ | ||
| 457 | 563 | Database::set_transient( 'featuredItems', $response ); |
| 458 | 564 | } |
| 459 | 565 | |
| 460 | 566 | return $response; |
| 567 | + } | |
| 568 | + | |
| 569 | + /** | |
| 570 | + * Get Trending Item List | |
| 571 | + * @param WP_REST_Request $request | |
| 572 | + * @return mixed | |
| 573 | + */ | |
| 574 | + public function trending_items(WP_REST_Request $request){ | |
| 575 | + $platform = $request->get_param( 'platform' ); | |
| 576 | + $defaults = Database::get_transient( "trendingItems_{$platform}" ); | |
| 577 | + if( $defaults ) { | |
| 578 | + return $defaults; | |
| 579 | + } | |
| 580 | + | |
| 581 | + $funcArgs = [ | |
| 582 | + // 'page' => 1, | |
| 583 | + // 'per_page' => 10, | |
| 584 | + 'platform' => $platform, | |
| 585 | + ]; | |
| 586 | + $response = $this->http()->query( | |
| 587 | + 'trendingItems', | |
| 588 | + 'data{ id, name, slug, price, type, thumbnail, badges }', | |
| 589 | + $funcArgs | |
| 590 | + )->post(); | |
| 591 | + | |
| 592 | + if( ! is_wp_error( $response ) ) { | |
| 593 | + Database::set_transient( 'trendingItems', $response ); | |
| 594 | + } | |
| 595 | + | |
| 596 | + return $response; | |
| 597 | + } | |
| 598 | + | |
| 599 | + /** | |
| 600 | + * Get Related Item List | |
| 601 | + * @param WP_REST_Request $request | |
| 602 | + * @return mixed | |
| 603 | + */ | |
| 604 | + public function related_items(WP_REST_Request $request){ | |
| 605 | + $item_id = (int) $request->get_param( 'item_id' ); | |
| 606 | + $type = $request->get_param( '_type' ); | |
| 607 | + | |
| 608 | + if ( empty( $item_id ) ) { | |
| 609 | + return $this->error( | |
| 610 | + 'invalid_item_id', | |
| 611 | + __( 'Item ID cannot be empty.', 'templately' ), | |
| 612 | + 'related-items', | |
| 613 | + '400' | |
| 614 | + ); | |
| 615 | + } | |
| 616 | + | |
| 617 | + if ( empty( $type ) ) { | |
| 618 | + return $this->error( | |
| 619 | + 'invalid_type', | |
| 620 | + __( 'Type cannot be empty.', 'templately' ), | |
| 621 | + 'related-items', | |
| 622 | + '400' | |
| 623 | + ); | |
| 624 | + } | |
| 625 | + | |
| 626 | + $funcArgs = [ | |
| 627 | + 'id' => $item_id, | |
| 628 | + 'type' => $type, | |
| 629 | + 'limit' => 4, // Limit to 3 related items | |
| 630 | + ]; | |
| 631 | + | |
| 632 | + $response = $this->http()->query( | |
| 633 | + 'relatedItems', | |
| 634 | + 'id, name, slug, price, type, thumbnail, badges', | |
| 635 | + $funcArgs | |
| 636 | + )->post(); | |
| 637 | + | |
| 638 | + if( is_wp_error( $response ) ) { | |
| 639 | + return $response; | |
| 640 | + } | |
| 641 | + | |
| 642 | + return [ | |
| 643 | + 'status' => 'success', | |
| 644 | + 'data' => $response, | |
| 645 | + ]; | |
| 461 | 646 | } |
| 462 | 647 | } |