fluent-cart
/
app
/
Hooks
/
Handlers
/
BlockEditors
/
RelatedProduct
/
RelatedProductBlockEditor.php
RelatedProductBlockEditor.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Hooks/Handlers/BlockEditors/RelatedProduct/RelatedProductBlockEditor.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCart\App\Hooks\Handlers\BlockEditors\RelatedProduct; |
| 4 | |
| 5 | use FluentCart\App\Helpers\CurrenciesHelper; |
| 6 | use FluentCart\App\Helpers\Helper; |
| 7 | use FluentCart\App\Hooks\Handlers\BlockEditors\BlockEditor; |
| 8 | use FluentCart\App\Hooks\Handlers\BlockEditors\RelatedProduct\InnerBlocks\InnerBlocks; |
| 9 | use FluentCart\App\Modules\Templating\AssetLoader; |
| 10 | use FluentCart\App\Services\Translations\TransStrings; |
| 11 | use FluentCart\App\Vite; |
| 12 | use FluentCart\Framework\Support\Arr; |
| 13 | |
| 14 | class RelatedProductBlockEditor extends BlockEditor |
| 15 | { |
| 16 | protected static string $editorName = 'related-product'; |
| 17 | |
| 18 | public function init(): void |
| 19 | { |
| 20 | parent::init(); |
| 21 | |
| 22 | $this->registerInnerBlocks(); |
| 23 | } |
| 24 | |
| 25 | public function registerInnerBlocks() |
| 26 | { |
| 27 | InnerBlocks::register(); |
| 28 | } |
| 29 | |
| 30 | public function getScripts(): array |
| 31 | { |
| 32 | return [ |
| 33 | [ |
| 34 | 'source' => 'admin/BlockEditor/RelatedProduct/RelatedProductBlockEditor.jsx', |
| 35 | 'dependencies' => ['wp-blocks', 'wp-components'] |
| 36 | ] |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | public function getStyles(): array |
| 41 | { |
| 42 | return [ |
| 43 | 'admin/BlockEditor/RelatedProduct/style/related-product-block-editor.scss' |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function localizeData(): array |
| 48 | { |
| 49 | $currencyCode = Helper::shopConfig('currency'); |
| 50 | $currencySign = CurrenciesHelper::getCurrencySign($currencyCode); |
| 51 | return [ |
| 52 | $this->getLocalizationKey() => [ |
| 53 | 'slug' => $this->slugPrefix, |
| 54 | 'name' => static::getEditorName(), |
| 55 | 'title' => __('Related Products', 'fluent-cart'), |
| 56 | 'description' => __('Display related products.', 'fluent-cart'), |
| 57 | 'currency_sign' => $currencySign, |
| 58 | ], |
| 59 | 'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | public function provideContext(): array |
| 64 | { |
| 65 | return [ |
| 66 | 'fluent-cart/related_product_ids' => 'related_product_ids', |
| 67 | 'fluent-cart/related_products' => 'related_products', |
| 68 | 'fluent-cart/product_id' => 'product_id', |
| 69 | 'fluent-cart/order_by' => 'order_by', |
| 70 | 'fluent-cart/query_type' => 'query_type', |
| 71 | 'fluent-cart/related_by_categories' => 'related_by_categories', |
| 72 | 'fluent-cart/related_by_brands' => 'related_by_brands', |
| 73 | 'fluent-cart/columns' => 'columns', |
| 74 | 'fluent-cart/posts_per_page' => 'posts_per_page', |
| 75 | 'fluent-cart/show_image' => 'show_image', |
| 76 | 'fluent-cart/show_title' => 'show_title', |
| 77 | 'fluent-cart/show_price' => 'show_price', |
| 78 | 'fluent-cart/show_button' => 'show_button', |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | public function render(array $shortCodeAttribute, $block = null, $content = null): string |
| 83 | { |
| 84 | AssetLoader::loadProductArchiveAssets(); |
| 85 | |
| 86 | $queryType = Arr::get($shortCodeAttribute, 'query_type', 'custom'); |
| 87 | |
| 88 | if ($queryType === 'default') { |
| 89 | // Default mode: uses current product from page context |
| 90 | $product = fluent_cart_get_current_product(); |
| 91 | if (!$product) { |
| 92 | return ''; |
| 93 | } |
| 94 | } else { |
| 95 | // Custom mode: requires a product_id from block attributes |
| 96 | $productId = absint(Arr::get($shortCodeAttribute, 'product_id', 0)); |
| 97 | if (!$productId) { |
| 98 | return ''; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Return the rendered InnerBlocks content |
| 103 | return $content; |
| 104 | } |
| 105 | } |
| 106 |