| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\ShortCodes; |
| 4 |
|
| 5 |
use FluentCart\App\CPT\FluentProducts; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\App\Vite; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
use FluentCart\Framework\Support\Collection; |
| 10 |
use FluentCart\App\Services\Renderer\SearchBarRenderer; |
| 11 |
|
| 12 |
class SearchBarShortCode extends ShortCode |
| 13 |
{ |
| 14 |
protected static string $shortCodeName = 'fluent_products_search_bar'; |
| 15 |
|
| 16 |
public function getScripts(): array |
| 17 |
{ |
| 18 |
return [ |
| 19 |
[ |
| 20 |
'source' => 'public/search-bar-app/SearchBarApp.js', |
| 21 |
'dependencies' => ['jquery'] |
| 22 |
] |
| 23 |
]; |
| 24 |
} |
| 25 |
|
| 26 |
public function getStyles(): array |
| 27 |
{ |
| 28 |
return [ |
| 29 |
'public/search-bar-app/style/style.scss' |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
public function localizeData(): array |
| 34 |
{ |
| 35 |
return [ |
| 36 |
'fluentcart_search_bar_vars' => [ |
| 37 |
'rest' => Helper::getRestInfo(), |
| 38 |
] |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function viewData(): ?array |
| 43 |
{ |
| 44 |
return [ |
| 45 |
'url_mode' => Arr::get($this->shortCodeAttributes, 'url_mode'), |
| 46 |
'category_mode' => Arr::get($this->shortCodeAttributes, 'category_mode'), |
| 47 |
'termData' => $this->getTermsData('categories'), |
| 48 |
'link_with_shop_app' => Arr::get($this->shortCodeAttributes, 'link_with_shop_app'), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
public function render(?array $viewData = null) |
| 53 |
{ |
| 54 |
(new SearchBarRenderer($viewData))->render(); |
| 55 |
} |
| 56 |
|
| 57 |
private function getTermsData($key): array |
| 58 |
{ |
| 59 |
if (!$key) { |
| 60 |
return []; |
| 61 |
} |
| 62 |
$options = get_categories([ |
| 63 |
'taxonomy' => 'product-' . $key, |
| 64 |
'post_type' => FluentProducts::CPT_NAME |
| 65 |
]); |
| 66 |
|
| 67 |
return Collection::make($options)->map(function ($meta) { |
| 68 |
return [ |
| 69 |
'termId' => $meta->term_id, |
| 70 |
'termName' => $meta->name, |
| 71 |
]; |
| 72 |
})->toArray(); |
| 73 |
} |
| 74 |
} |
| 75 |
|