PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / trunk
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler vtrunk
1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 All 50 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / RelatedProduct / RelatedProductBlockEditor.php

RelatedProductBlockEditor.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler trunk, at app/Hooks/Handlers/BlockEditors/RelatedProduct/RelatedProductBlockEditor.php

111 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Related products render via per-product inner blocks rather than
103 // ProductCardRender, so fire the advanced-variation asset hook here too
104 // — Pro's advanced-variation selector assets load for related items.
105 do_action('fluent_cart/advanced_variation/enqueue_assets');
106
107 // Return the rendered InnerBlocks content
108 return $content;
109 }
110 }
111