| @@ -37,8 +37,19 @@ | ||
| 37 | 37 | 'required' => false |
| 38 | 38 | ], |
| 39 | 39 | ] ); |
| 40 | 40 | |
| 41 | + // only number | |
| 42 | + $this->get( 'items/(?P<id>[0-9]+)', [ $this, 'get_item_by_id' ], [ | |
| 43 | + 'id' => [ | |
| 44 | + 'required' => true, | |
| 45 | + ], | |
| 46 | + 'type' => [ | |
| 47 | + 'default' => 'block' | |
| 48 | + ] | |
| 49 | + ] ); | |
| 50 | + | |
| 51 | + // with slug | |
| 41 | 52 | $this->get( 'items/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'get_item' ], [ |
| 42 | 53 | 'slug' => [ |
| 43 | 54 | 'required' => true, |
| 44 | 55 | ], |
| @@ -62,8 +73,10 @@ | ||
| 62 | 73 | $this->post( 'items/favourite', [ $this, 'set_favourite' ] ); |
| 63 | 74 | $this->post( 'items/rating', [ $this, 'set_rating' ] ); |
| 64 | 75 | $this->get( 'items-count', [ $this, 'get_counts' ] ); |
| 65 | 76 | $this->get( 'featured-items', [ $this, 'featured_items' ] ); |
| 77 | + $this->get( 'trending-items', [ $this, 'trending_items' ] ); | |
| 78 | + $this->get( 'related-items', [ $this, 'related_items' ]); | |
| 66 | 79 | } |
| 67 | 80 | |
| 68 | 81 | /** |
| 69 | 82 | * @param string $type |
| @@ -155,17 +168,24 @@ | ||
| 155 | 168 | if( $type === false ) { |
| 156 | 169 | return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); |
| 157 | 170 | } |
| 158 | 171 | |
| 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 }'; | |
| 172 | + $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 } }, live_url, template_type{ id, name, slug }'; | |
| 160 | 173 | $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }'; |
| 161 | 174 | |
| 162 | 175 | if ( $type == 'packs' ) { |
| 163 | - $params = 'data { id, fullsite_import, has_settings, name, rating, type, slug, live_url, price, features, favourite_count, thumbnail, downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }'; | |
| 176 | + $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 | 177 | } |
| 165 | 178 | |
| 166 | - $response = $this->http()->query( $type, $params, [ 'slug' => $slug ] )->post(); | |
| 179 | + $args = [ | |
| 180 | + 'slug' => $slug, | |
| 181 | + ]; | |
| 182 | + if (!empty($this->api_key)) { | |
| 183 | + $args['api_key'] = $this->api_key; | |
| 184 | + } | |
| 167 | 185 | |
| 186 | + $response = $this->http()->query( $type, $params, $args )->post(); | |
| 187 | + | |
| 168 | 188 | if ( is_wp_error( $response ) ) { |
| 169 | 189 | return $response; |
| 170 | 190 | } |
| 171 | 191 | |
| @@ -180,8 +200,58 @@ | ||
| 180 | 200 | |
| 181 | 201 | return current( $response['data'] ); |
| 182 | 202 | } |
| 183 | 203 | |
| 204 | + public function get_item_by_id() { | |
| 205 | + $id = (int) $this->get_param( 'id' ); | |
| 206 | + $_type = $this->get_param( 'type', 'blocks' ); | |
| 207 | + $type = $this->get_query_types( $_type ); | |
| 208 | + | |
| 209 | + if ( empty( $id ) ) { | |
| 210 | + return $this->error( | |
| 211 | + 'invalid_item_slug', | |
| 212 | + __( 'Items id cannot be empty.', 'templately' ), | |
| 213 | + 'items/:id', | |
| 214 | + '400' | |
| 215 | + ); | |
| 216 | + } | |
| 217 | + | |
| 218 | + if( $type === false ) { | |
| 219 | + return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) ); | |
| 220 | + } | |
| 221 | + | |
| 222 | + $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 } }, live_url, template_type{ id, name, slug }'; | |
| 223 | + $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }'; | |
| 224 | + | |
| 225 | + if ( $type == 'packs' ) { | |
| 226 | + $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 } }'; | |
| 227 | + } | |
| 228 | + | |
| 229 | + $args = [ | |
| 230 | + 'id' => $id, | |
| 231 | + ]; | |
| 232 | + if (!empty($this->api_key)) { | |
| 233 | + $args['api_key'] = $this->api_key; | |
| 234 | + } | |
| 235 | + | |
| 236 | + $response = $this->http()->query( $type, $params, $args )->post(); | |
| 237 | + | |
| 238 | + if ( is_wp_error( $response ) ) { | |
| 239 | + return $response; | |
| 240 | + } | |
| 241 | + | |
| 242 | + if( empty( $response['data'] ) ) { | |
| 243 | + return $this->error( | |
| 244 | + 'invalid_response', | |
| 245 | + __('Item data not found', 'templately'), | |
| 246 | + '/items\/' . $id, | |
| 247 | + '404' | |
| 248 | + ); | |
| 249 | + } | |
| 250 | + | |
| 251 | + return current( $response['data'] ); | |
| 252 | + } | |
| 253 | + | |
| 184 | 254 | public function get_search_results( WP_REST_Request $request ) { |
| 185 | 255 | $keyword = $request->get_param( 'keyword' ); |
| 186 | 256 | $platform = $request->get_param( 'platform' ); |
| 187 | 257 | $query_type = $request->get_param( 'query_type' ); |
| @@ -274,9 +344,9 @@ | ||
| 274 | 344 | 'id' => $id, |
| 275 | 345 | 'type' => $type == 'block' || $type == 'page' ? 'item' : 'pack' |
| 276 | 346 | ]; |
| 277 | 347 | |
| 278 | - if( $response == 1 ) { | |
| 348 | + if( $response == 1 || $response === false ) { | |
| 279 | 349 | $_favourites = $this->utils('options')->get('favourites'); |
| 280 | 350 | $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites, true ); |
| 281 | 351 | $this->utils('options')->set('favourites', $_favourites); |
| 282 | 352 | |
| @@ -435,14 +505,14 @@ | ||
| 435 | 505 | * @param WP_REST_Request $request |
| 436 | 506 | * @return mixed |
| 437 | 507 | */ |
| 438 | 508 | public function featured_items(WP_REST_Request $request){ |
| 439 | - $defaults = Database::get_transient( 'featuredItems' ); | |
| 509 | + $platform = $request->get_param( 'platform' ); | |
| 510 | + $defaults = Database::get_transient( "featuredItems_{$platform}" ); | |
| 440 | 511 | if( $defaults ) { |
| 441 | 512 | return $defaults; |
| 442 | 513 | } |
| 443 | 514 | |
| 444 | - $platform = $request->get_param( 'platform' ); | |
| 445 | 515 | $funcArgs = [ |
| 446 | 516 | // 'page' => 1, |
| 447 | 517 | // 'per_page' => 10, |
| 448 | 518 | 'platform' => $platform, |
| @@ -448,9 +518,9 @@ | ||
| 448 | 518 | 'platform' => $platform, |
| 449 | 519 | ]; |
| 450 | 520 | $response = $this->http()->query( |
| 451 | 521 | 'featuredItems', |
| 452 | - 'data{ id, name, slug, price, type, thumbnail }', | |
| 522 | + 'data{ id, name, slug, price, type, thumbnail, badges }', | |
| 453 | 523 | $funcArgs |
| 454 | 524 | )->post(); |
| 455 | 525 | |
| 456 | 526 | if( ! is_wp_error( $response ) ) { |
| @@ -457,6 +527,85 @@ | ||
| 457 | 527 | Database::set_transient( 'featuredItems', $response ); |
| 458 | 528 | } |
| 459 | 529 | |
| 460 | 530 | return $response; |
| 531 | + } | |
| 532 | + | |
| 533 | + /** | |
| 534 | + * Get Trending Item List | |
| 535 | + * @param WP_REST_Request $request | |
| 536 | + * @return mixed | |
| 537 | + */ | |
| 538 | + public function trending_items(WP_REST_Request $request){ | |
| 539 | + $platform = $request->get_param( 'platform' ); | |
| 540 | + $defaults = Database::get_transient( "trendingItems_{$platform}" ); | |
| 541 | + if( $defaults ) { | |
| 542 | + return $defaults; | |
| 543 | + } | |
| 544 | + | |
| 545 | + $funcArgs = [ | |
| 546 | + // 'page' => 1, | |
| 547 | + // 'per_page' => 10, | |
| 548 | + 'platform' => $platform, | |
| 549 | + ]; | |
| 550 | + $response = $this->http()->query( | |
| 551 | + 'trendingItems', | |
| 552 | + 'data{ id, name, slug, price, type, thumbnail, badges }', | |
| 553 | + $funcArgs | |
| 554 | + )->post(); | |
| 555 | + | |
| 556 | + if( ! is_wp_error( $response ) ) { | |
| 557 | + Database::set_transient( 'trendingItems', $response ); | |
| 558 | + } | |
| 559 | + | |
| 560 | + return $response; | |
| 561 | + } | |
| 562 | + | |
| 563 | + /** | |
| 564 | + * Get Related Item List | |
| 565 | + * @param WP_REST_Request $request | |
| 566 | + * @return mixed | |
| 567 | + */ | |
| 568 | + public function related_items(WP_REST_Request $request){ | |
| 569 | + $item_id = (int) $request->get_param( 'item_id' ); | |
| 570 | + $type = $request->get_param( '_type' ); | |
| 571 | + | |
| 572 | + if ( empty( $item_id ) ) { | |
| 573 | + return $this->error( | |
| 574 | + 'invalid_item_id', | |
| 575 | + __( 'Item ID cannot be empty.', 'templately' ), | |
| 576 | + 'related-items', | |
| 577 | + '400' | |
| 578 | + ); | |
| 579 | + } | |
| 580 | + | |
| 581 | + if ( empty( $type ) ) { | |
| 582 | + return $this->error( | |
| 583 | + 'invalid_type', | |
| 584 | + __( 'Type cannot be empty.', 'templately' ), | |
| 585 | + 'related-items', | |
| 586 | + '400' | |
| 587 | + ); | |
| 588 | + } | |
| 589 | + | |
| 590 | + $funcArgs = [ | |
| 591 | + 'id' => $item_id, | |
| 592 | + 'type' => $type, | |
| 593 | + 'limit' => 4, // Limit to 3 related items | |
| 594 | + ]; | |
| 595 | + | |
| 596 | + $response = $this->http()->query( | |
| 597 | + 'relatedItems', | |
| 598 | + 'id, name, slug, price, type, thumbnail, badges', | |
| 599 | + $funcArgs | |
| 600 | + )->post(); | |
| 601 | + | |
| 602 | + if( is_wp_error( $response ) ) { | |
| 603 | + return $response; | |
| 604 | + } | |
| 605 | + | |
| 606 | + return [ | |
| 607 | + 'status' => 'success', | |
| 608 | + 'data' => $response, | |
| 609 | + ]; | |
| 461 | 610 | } |
| 462 | 611 | } |