PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.8.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.8.0
1.8.0 1.7.1 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 All 52 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / WriteAReviewButtonBlockEditor.php

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

164 lines 6.5 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\ProductReviewService;
7 use FluentCart\App\Services\Renderer\ProductReviewRenderer;
8 use FluentCart\App\Services\Translations\TransStrings;
9 use FluentCart\Framework\Support\Arr;
10
11 class WriteAReviewButtonBlockEditor extends BlockEditor
12 {
13 protected static string $editorName = 'write-a-review-button';
14
15 public function supports(): array
16 {
17 return [
18 'html' => false,
19 'align' => true,
20 'typography' => [
21 'fontSize' => true,
22 'lineHeight' => true,
23 '__experimentalFontFamily' => true,
24 '__experimentalFontWeight' => true,
25 '__experimentalDefaultControls' => [
26 'fontSize' => true,
27 ],
28 ],
29 'color' => [
30 'text' => true,
31 'background' => true,
32 ],
33 'spacing' => [
34 'margin' => true,
35 'padding' => true,
36 ],
37 '__experimentalBorder' => [
38 'color' => true,
39 'radius' => true,
40 'style' => true,
41 'width' => true,
42 ],
43 ];
44 }
45
46 public function blockAttributes(): array
47 {
48 return [
49 // Must mirror the JS registration — WordPress prepares dynamic
50 // block attributes against this server-side schema, so an
51 // attribute missing here never reaches the render callback.
52 // starColor was dropped with the trigger-button redesign, so it
53 // is deliberately absent here too.
54 'query_type' => ['type' => 'string', 'default' => 'default'],
55 'product_id' => ['type' => ['string', 'number'], 'default' => ''],
56 // The block is the trigger; these two decide what it opens.
57 // 'container' is where the form appears, 'layout' is how it
58 // paces the fields once it is there.
59 'container' => ['type' => 'string', 'default' => 'drawer'],
60 'layout' => ['type' => 'string', 'default' => 'inline'],
61 'addReviewButtonText' => ['type' => 'string', 'default' => ''],
62 'editReviewButtonText' => ['type' => 'string', 'default' => ''],
63 'loginReviewButtonText' => ['type' => 'string', 'default' => ''],
64 ];
65 }
66
67 public function getScripts(): array
68 {
69 return [
70 [
71 'source' => 'admin/BlockEditor/WriteAReviewButton/WriteAReviewButtonBlockEditor.jsx',
72 'dependencies' => ['wp-blocks', 'wp-components', 'wp-data', 'wp-block-editor', 'wp-element']
73 ]
74 ];
75 }
76
77 public function getStyles(): array
78 {
79 return [
80 'admin/BlockEditor/WriteAReviewButton/style/write-a-review-button-block-editor.scss',
81 // Shared review-block preview stylesheet — the standalone
82 // skeleton preview uses its classes.
83 'admin/BlockEditor/ProductReviews/style/product-reviews-block-editor.scss'
84 ];
85 }
86
87 public function localizeData(): array
88 {
89 return [
90 $this->getLocalizationKey() => [
91 'slug' => $this->slugPrefix,
92 'name' => static::getEditorName(),
93 'title' => __('Write a Review', 'fluent-cart'),
94 'description' => __('A button that opens the review submission drawer for a product.', 'fluent-cart'),
95 ],
96 'fluent_cart_block_translation' => TransStrings::blockStrings(),
97 ];
98 }
99
100 public function useContext()
101 {
102 return ['fluent-cart/review_container_query'];
103 }
104
105 public function render(array $shortCodeAttribute, $block = null)
106 {
107 // Nested under a review container (server-visible via the provided
108 // context key) the parent owns the product — a stale custom pick
109 // saved on this child must not win. Standalone, the block resolves
110 // its own query.
111 $insideContainer = $block instanceof \WP_Block
112 && isset($block->context['fluent-cart/review_container_query']);
113
114 // An All products or Multiple products container has no single product to show.
115 if ($insideContainer && ProductReviewService::isMultiProductQuery($block->context['fluent-cart/review_container_query'])) {
116 return '';
117 }
118
119 $product = $insideContainer ? fluent_cart_get_current_product() : $this->resolveProduct($shortCodeAttribute);
120
121 if (!$product) {
122 return '';
123 }
124
125 // The form's own script and stylesheet: the block is a trigger and
126 // the form it opens, and needs none of the gallery, product card or
127 // review list the full single-product bundle carries. On a product
128 // page that bundle is enqueued anyway, under the same form handle.
129 AssetLoader::loadReviewSubmissionFormAssets();
130
131 $wrapper_attributes = get_block_wrapper_attributes([
132 'class' => 'fct-write-a-review-button-block',
133 ]);
134
135 $container = Arr::get($shortCodeAttribute, 'container', 'drawer');
136 if (!\in_array($container, ['drawer', 'modal'], true)) {
137 $container = 'drawer';
138 }
139
140 $layout = Arr::get($shortCodeAttribute, 'layout', 'inline');
141 if (!\in_array($layout, ['steps', 'inline'], true)) {
142 $layout = 'inline';
143 }
144
145 ob_start();
146 // The block is a button plus the overlay it opens: the trigger always
147 // renders (it is the block's whole purpose), and the section below
148 // carries the drawer or modal — clicking any CTA opens it via
149 // delegation. The form itself, standalone, is its own block.
150 $renderer = new ProductReviewRenderer($product->ID, [
151 'container' => $container,
152 'layout' => $layout,
153 'ctaAddText' => sanitize_text_field(Arr::get($shortCodeAttribute, 'addReviewButtonText', '')),
154 'ctaEditText' => sanitize_text_field(Arr::get($shortCodeAttribute, 'editReviewButtonText', '')),
155 'ctaLoginText' => sanitize_text_field(Arr::get($shortCodeAttribute, 'loginReviewButtonText', '')),
156 ]);
157 $renderer->renderWriteReviewCta();
158 $renderer->renderForm();
159 $inner = ob_get_clean();
160
161 return static::wrapUnlessEmpty($wrapper_attributes, $inner);
162 }
163 }
164