PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.3
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.3
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 1.3.19 trunk All 48 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / ProductImageBlockEditor.php

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

153 lines 4.8 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;
4
5 use FluentCart\App\Modules\Templating\AssetLoader;
6 use FluentCart\App\Services\Renderer\ProductCardRender;
7 use FluentCart\App\Services\Translations\TransStrings;
8 use FluentCart\App\Vite;
9 use FluentCart\Framework\Support\Arr;
10 use FluentCart\App\Models\Product;
11
12 class ProductImageBlockEditor extends BlockEditor
13 {
14 protected static string $editorName = 'product-image';
15
16 public function ancestor(): array
17 {
18 return [
19 'fluent-cart/products',
20 'fluent-cart/product-carousel',
21 'fluent-cart/product-info',
22 'fluent-cart/related-product',
23 ];
24 }
25
26 public function supports(): array
27 {
28 return [
29 'html' => false,
30 'align' => true,
31 '__experimentalBorder' => [
32 'color' => true,
33 'radius' => true,
34 'style' => true,
35 'width' => true,
36 ],
37 'spacing' => [
38 'margin' => true,
39 'padding' => true,
40 ],
41 'shadow' => true,
42 '__experimentalFilter' => [
43 'duotone' => true,
44 ],
45 ];
46 }
47
48 public function getScripts(): array
49 {
50 return [
51 [
52 'source' => 'admin/BlockEditor/ProductImage/ProductImageBlockEditor.jsx',
53 'dependencies' => ['wp-blocks', 'wp-components']
54 ]
55 ];
56 }
57
58 public function getStyles(): array
59 {
60 return [
61 'admin/BlockEditor/ProductImage/style/product-image-block-editor.scss'
62 ];
63 }
64
65 public function localizeData(): array
66 {
67 return [
68 $this->getLocalizationKey() => [
69 'slug' => $this->slugPrefix,
70 'name' => static::getEditorName(),
71 'title' => __('Product Image', 'fluent-cart'),
72 'description' => __('This block will display the product image.', 'fluent-cart'),
73 'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg')
74 ],
75 'fluent_cart_block_translation' => TransStrings::blockStrings(),
76 ];
77 }
78
79 protected function skipInnerBlocks(): bool
80 {
81 return true;
82 }
83
84 public function render(array $shortCodeAttribute, $block = null)
85 {
86 AssetLoader::loadSingleProductAssets();
87
88 Vite::enqueueStyle(
89 'fluent-cart-product-image-block',
90 'admin/BlockEditor/ProductImage/style/product-image-block-editor.scss'
91 );
92
93 $product = null;
94 $insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no');
95 if (!in_array($insideProductInfo, ['yes', 'no'], true)) {
96 $insideProductInfo = 'no';
97 }
98 $queryType = Arr::get($shortCodeAttribute, 'query_type', 'default');
99 if (!in_array($queryType, ['default', 'custom'], true)) {
100 $queryType = 'default';
101 }
102
103 if ($insideProductInfo === 'yes' || $queryType === 'default') {
104 $product = fluent_cart_get_current_product();
105 }
106
107 if (!$product) {
108 $productId = Arr::get($shortCodeAttribute, 'product_id', false);
109 if ($productId) {
110 $product = Product::query()->where('post_status', 'publish')->with(['detail', 'variants'])->find($productId);
111 }
112 }
113
114 if (!$product) {
115 return '';
116 }
117
118 $render = new ProductCardRender($product);
119
120 // Handle inner blocks (for overlay content like badges, titles on image)
121 $innerBlocksContent = '';
122 if ($block instanceof \WP_Block && !empty($block->inner_blocks)) {
123 $blockContext = $block->context ?? [];
124 foreach ($block->inner_blocks as $inner_block) {
125 if (isset($inner_block->parsed_block)) {
126 $innerContext = array_merge($inner_block->context, $blockContext);
127 $instance = new \WP_Block($inner_block->parsed_block, $innerContext);
128 $innerBlocksContent .= $instance->render();
129 }
130 }
131 }
132
133 $wrapper_attributes = get_block_wrapper_attributes([
134 'class' => 'fct-product-image-block',
135 ]);
136
137 ob_start();
138 $render->renderProductImage();
139 $renderedImage = ob_get_clean();
140
141 if (!empty($innerBlocksContent)) {
142 return sprintf(
143 '<div %s>%s<div class="fct-product-image-inner-blocks">%s</div></div>',
144 $wrapper_attributes,
145 $renderedImage,
146 $innerBlocksContent
147 );
148 }
149
150 return sprintf('<div %s>%s</div>', $wrapper_attributes, $renderedImage);
151 }
152 }
153