| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors\ShopApp; |
| 4 |
|
| 5 |
use FluentCart\Api\Taxonomy; |
| 6 |
use FluentCart\App\CPT\FluentProducts; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Hooks\Handlers\BlockEditors\BlockEditor; |
| 9 |
use FluentCart\App\Hooks\Handlers\BlockEditors\ShopApp\InnerBlocks\InnerBlocks; |
| 10 |
use FluentCart\App\Hooks\Handlers\ShortCodes\ShopAppHandler; |
| 11 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 12 |
use FluentCart\App\Services\Translations\TransStrings; |
| 13 |
use FluentCart\App\Vite; |
| 14 |
use FluentCart\Framework\Support\Arr; |
| 15 |
use FluentCart\Framework\Support\Collection; |
| 16 |
use FluentCart\Framework\Support\Str; |
| 17 |
|
| 18 |
class ShopAppBlockEditor extends BlockEditor |
| 19 |
{ |
| 20 |
protected static string $editorName = 'products'; |
| 21 |
|
| 22 |
protected ?string $localizationKey = 'fluent_cart_shop_app_block_editor_data'; |
| 23 |
|
| 24 |
public function getScripts(): array |
| 25 |
{ |
| 26 |
return [ |
| 27 |
[ |
| 28 |
'source' => 'admin/BlockEditor/ShopApp/ShopAppBlockEditor.jsx', |
| 29 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 30 |
] |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function getStyles(): array |
| 35 |
{ |
| 36 |
return ['admin/BlockEditor/ShopApp/style/shop-app-block-editor.css']; |
| 37 |
} |
| 38 |
|
| 39 |
public function init(): void |
| 40 |
{ |
| 41 |
parent::init(); |
| 42 |
|
| 43 |
$this->registerInnerBlocks(); |
| 44 |
} |
| 45 |
|
| 46 |
public function registerInnerBlocks() |
| 47 |
{ |
| 48 |
InnerBlocks::register(); |
| 49 |
} |
| 50 |
|
| 51 |
public function localizeData(): array |
| 52 |
{ |
| 53 |
$taxonomies = Taxonomy::getTaxonomies(); |
| 54 |
|
| 55 |
$taxonomies = Collection::make($taxonomies) |
| 56 |
->map(function ($taxonomy) { |
| 57 |
return [ |
| 58 |
'name' => $taxonomy, |
| 59 |
'label' => Str::headline($taxonomy), |
| 60 |
'parent' => 0, |
| 61 |
'terms' => Taxonomy::getFormattedTerms($taxonomy, false, null, 'value', 'label'), |
| 62 |
]; |
| 63 |
}); |
| 64 |
|
| 65 |
return [ |
| 66 |
$this->getLocalizationKey() => [ |
| 67 |
'rest' => Helper::getRestInfo(), |
| 68 |
'slug' => $this->slugPrefix, |
| 69 |
'name' => static::getEditorName(), |
| 70 |
'product_categories' => $this->getMetaFilterOptions('categories'), |
| 71 |
'trans' => TransStrings::getShopAppBlockEditorString(), |
| 72 |
'taxonomies' => $taxonomies, |
| 73 |
'title' => __('Products', 'fluent-cart'), |
| 74 |
], |
| 75 |
'fluent_cart_block_editor_asset' => [ |
| 76 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 77 |
], |
| 78 |
'fluent_cart_block_translation' => TransStrings::blockStrings() |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
private function getMetaFilterOptions($key): array |
| 83 |
{ |
| 84 |
$options = get_categories([ |
| 85 |
'taxonomy' => 'product-' . $key, |
| 86 |
'post_type' => FluentProducts::CPT_NAME |
| 87 |
]); |
| 88 |
|
| 89 |
return Collection::make($options)->map(function ($meta) { |
| 90 |
return [ |
| 91 |
'value' => $meta->term_id, |
| 92 |
'label' => $meta->name, |
| 93 |
]; |
| 94 |
})->toArray(); |
| 95 |
} |
| 96 |
|
| 97 |
public function provideContext(): array |
| 98 |
{ |
| 99 |
// in which name the data will be received => which attr |
| 100 |
return [ |
| 101 |
'fluent-cart/paginator' => 'paginator', |
| 102 |
'fluent-cart/per_page' => 'per_page', |
| 103 |
'fluent-cart/enable_filter' => 'enable_filter', |
| 104 |
'fluent-cart/product_box_grid_size' => 'product_box_grid_size', |
| 105 |
'fluent-cart/view_mode' => 'view_mode', |
| 106 |
'fluent-cart/filters' => 'filters', |
| 107 |
'fluent-cart/default_filters' => 'default_filters', |
| 108 |
'fluent-cart/order_type' => 'order_type', |
| 109 |
'fluent-cart/order_by' => 'order_by', |
| 110 |
'fluent-cart/live_filter' => 'live_filter', |
| 111 |
'fluent-cart/price_format' => 'price_format', |
| 112 |
'fluent-cart/enable_wildcard_filter' => 'enable_wildcard_filter', |
| 113 |
|
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
public function render(array $shortCodeAttribute, $block = null, $content = null): string |
| 118 |
{ |
| 119 |
AssetLoader::loadProductArchiveAssets(); |
| 120 |
return $content; |
| 121 |
} |
| 122 |
} |
| 123 |
|