PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
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 / ProductImageBlockEditor.php

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

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