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

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

145 lines 4.7 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\Services\Translations\TransStrings;
7 use FluentCart\App\Vite;
8 use FluentCart\Framework\Support\Arr;
9 use FluentCart\App\Models\Product;
10
11 class SoldOutBadgeBlockEditor extends BlockEditor
12 {
13 protected static string $editorName = 'sold-out-badge';
14
15 public function ancestor(): array
16 {
17 return [
18 'fluent-cart/products',
19 'fluent-cart/product-carousel',
20 'fluent-cart/product-info',
21 'fluent-cart/related-product',
22 ];
23 }
24
25 public function supports(): array
26 {
27 return [
28 'html' => false,
29 'align' => ['left', 'center', 'right'],
30 'color' => [
31 'text' => true,
32 'background' => true,
33 'gradients' => true,
34 ],
35 'typography' => [
36 'fontSize' => true,
37 '__experimentalFontWeight' => true,
38 '__experimentalLetterSpacing' => true,
39 '__experimentalTextTransform' => true,
40 '__experimentalDefaultControls' => [
41 'fontSize' => true,
42 ],
43 ],
44 'spacing' => [
45 'padding' => true,
46 'margin' => true,
47 ],
48 '__experimentalBorder' => [
49 'color' => true,
50 'radius' => true,
51 'style' => true,
52 'width' => true,
53 ],
54 'shadow' => true,
55 ];
56 }
57
58 public function getScripts(): array
59 {
60 return [
61 [
62 'source' => 'admin/BlockEditor/SoldOutBadge/SoldOutBadgeBlockEditor.jsx',
63 'dependencies' => ['wp-blocks', 'wp-components']
64 ]
65 ];
66 }
67
68 public function getStyles(): array
69 {
70 return [
71 'admin/BlockEditor/SoldOutBadge/style/sold-out-badge-block-editor.scss'
72 ];
73 }
74
75 public function localizeData(): array
76 {
77 return [
78 $this->getLocalizationKey() => [
79 'slug' => $this->slugPrefix,
80 'name' => static::getEditorName(),
81 'title' => __('Sold Out Badge', 'fluent-cart'),
82 'description' => __('Displays a sold out badge when a product is out of stock.', 'fluent-cart'),
83 ],
84 'fluent_cart_block_translation' => TransStrings::blockStrings(),
85 ];
86 }
87
88 public function render(array $shortCodeAttribute, $block = null): string
89 {
90 // Enqueue frontend styles
91 Vite::enqueueStyle(
92 'fluent-cart-sold-out-badge',
93 'admin/BlockEditor/SoldOutBadge/style/sold-out-badge-block-editor.scss'
94 );
95
96 // Validate attributes
97 $badgeStyle = Arr::get($shortCodeAttribute, 'badge_style', 'badge');
98 if (!in_array($badgeStyle, ['badge', 'ribbon', 'tag'], true)) {
99 $badgeStyle = 'badge';
100 }
101
102 $badgePosition = Arr::get($shortCodeAttribute, 'badge_position', 'top-left');
103 if (!in_array($badgePosition, ['top-left', 'top-right', 'bottom-left', 'bottom-right'], true)) {
104 $badgePosition = 'top-left';
105 }
106
107 $badgeText = sanitize_text_field(Arr::get($shortCodeAttribute, 'badge_text', __('Out of Stock', 'fluent-cart')));
108
109 // Get product — explicit product_id takes priority, then current context
110 $productId = absint(Arr::get($shortCodeAttribute, 'product_id', 0));
111 if ($productId) {
112 $product = Product::query()->where('post_status', 'publish')->with(['detail'])->find($productId);
113 } else {
114 $product = fluent_cart_get_current_product();
115 }
116
117 if (!$product || !$product->detail) {
118 return '';
119 }
120
121 // Check if product is out of stock
122 $isOutOfStock = $product->detail->stock_availability === Helper::OUT_OF_STOCK;
123
124 // If in stock, render nothing
125 if (!$isOutOfStock) {
126 return '';
127 }
128
129 // Build CSS classes (values are pre-validated via in_array above)
130 $classes = ['fct-sold-out-badge'];
131 $classes[] = 'fct-sold-out-badge--' . esc_attr($badgeStyle);
132 $classes[] = 'fct-sold-out-badge--' . esc_attr($badgePosition);
133
134 $wrapper_attributes = get_block_wrapper_attributes([
135 'class' => implode(' ', $classes),
136 ]);
137
138 return sprintf(
139 '<span %s>%s</span>',
140 $wrapper_attributes,
141 esc_html($badgeText)
142 );
143 }
144 }
145