| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 6 |
use FluentCart\App\Services\Renderer\ProductReviewRenderer; |
| 7 |
use FluentCart\App\Services\Translations\TransStrings; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class ProductReviewSummaryBlockEditor extends BlockEditor |
| 11 |
{ |
| 12 |
protected static string $editorName = 'product-review-summary'; |
| 13 |
|
| 14 |
/** |
| 15 |
* The bare card lives only inside Rating Summary with Review — the |
| 16 |
* inserter offers that group at the top level instead. |
| 17 |
*/ |
| 18 |
public function ancestor(): array |
| 19 |
{ |
| 20 |
return [ |
| 21 |
'fluent-cart/product-review-summary-group', |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
public function supports(): array |
| 26 |
{ |
| 27 |
return [ |
| 28 |
'html' => false, |
| 29 |
'align' => true, |
| 30 |
'typography' => [ |
| 31 |
'fontSize' => true, |
| 32 |
'lineHeight' => true, |
| 33 |
'__experimentalFontFamily' => true, |
| 34 |
'__experimentalFontWeight' => true, |
| 35 |
'__experimentalDefaultControls' => [ |
| 36 |
'fontSize' => true, |
| 37 |
], |
| 38 |
], |
| 39 |
'color' => [ |
| 40 |
'text' => true, |
| 41 |
'background' => true, |
| 42 |
], |
| 43 |
'spacing' => [ |
| 44 |
'margin' => true, |
| 45 |
'padding' => true, |
| 46 |
], |
| 47 |
'__experimentalBorder' => [ |
| 48 |
'color' => true, |
| 49 |
'radius' => true, |
| 50 |
'style' => true, |
| 51 |
'width' => true, |
| 52 |
], |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
public function blockAttributes(): array |
| 57 |
{ |
| 58 |
return [ |
| 59 |
// Must mirror the JS registration — WordPress prepares dynamic |
| 60 |
// block attributes against this server-side schema, so an |
| 61 |
// attribute missing here never reaches the render callback. |
| 62 |
'query_type' => ['type' => 'string', 'default' => 'default'], |
| 63 |
'product_id' => ['type' => ['string', 'number'], 'default' => ''], |
| 64 |
'starColor' => ['type' => 'string', 'default' => '#f59e0b'], |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
public function getScripts(): array |
| 69 |
{ |
| 70 |
return [ |
| 71 |
[ |
| 72 |
'source' => 'admin/BlockEditor/ProductReviewSummary/ProductReviewSummaryBlockEditor.jsx', |
| 73 |
'dependencies' => ['wp-blocks', 'wp-components', 'wp-data', 'wp-block-editor', 'wp-element'] |
| 74 |
] |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
public function getStyles(): array |
| 79 |
{ |
| 80 |
return [ |
| 81 |
// The review block editor previews share one stylesheet — it is |
| 82 |
// scoped to every review block wrapper. Declaring it here keeps |
| 83 |
// this block styled on production even if the Product Reviews |
| 84 |
// container block ever stops enqueuing it. |
| 85 |
'admin/BlockEditor/ProductReviews/style/product-reviews-block-editor.scss' |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
public function localizeData(): array |
| 90 |
{ |
| 91 |
return [ |
| 92 |
$this->getLocalizationKey() => [ |
| 93 |
'slug' => $this->slugPrefix, |
| 94 |
'name' => static::getEditorName(), |
| 95 |
'title' => __('Rating Summary', 'fluent-cart'), |
| 96 |
'description' => __('Average rating and star distribution for a product.', 'fluent-cart'), |
| 97 |
], |
| 98 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
public function useContext() |
| 103 |
{ |
| 104 |
return ['fluent-cart/review_container_query']; |
| 105 |
} |
| 106 |
|
| 107 |
public function render(array $shortCodeAttribute, $block = null) |
| 108 |
{ |
| 109 |
// Nested under a review container (server-visible via the provided |
| 110 |
// context key) the parent owns the product — a stale custom pick |
| 111 |
// saved on this child must not win. Standalone, the block resolves |
| 112 |
// its own query. |
| 113 |
$insideContainer = $block instanceof \WP_Block |
| 114 |
&& isset($block->context['fluent-cart/review_container_query']); |
| 115 |
|
| 116 |
$product = $insideContainer ? fluent_cart_get_current_product() : $this->resolveProduct($shortCodeAttribute); |
| 117 |
|
| 118 |
if (!$product) { |
| 119 |
return ''; |
| 120 |
} |
| 121 |
|
| 122 |
AssetLoader::loadSingleProductAssets(); |
| 123 |
|
| 124 |
$wrapper_attributes = get_block_wrapper_attributes([ |
| 125 |
'class' => 'fct-review-summary-block', |
| 126 |
]); |
| 127 |
|
| 128 |
ob_start(); |
| 129 |
(new ProductReviewRenderer($product->ID, [ |
| 130 |
'starColor' => Arr::get($shortCodeAttribute, 'starColor', '#f59e0b'), |
| 131 |
]))->renderSummarySection(); |
| 132 |
$inner = ob_get_clean(); |
| 133 |
|
| 134 |
return static::wrapUnlessEmpty($wrapper_attributes, $inner); |
| 135 |
} |
| 136 |
} |
| 137 |
|