| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\App\Models\Product; |
| 7 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 8 |
use FluentCart\App\Services\Translations\TransStrings; |
| 9 |
use FluentCart\App\Vite; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class StockBlock extends BlockEditor |
| 13 |
{ |
| 14 |
protected static string $editorName = 'stock'; |
| 15 |
|
| 16 |
public function supports(): array |
| 17 |
{ |
| 18 |
return [ |
| 19 |
'html' => false, |
| 20 |
'align' => ['left', 'center', 'right'], |
| 21 |
'typography' => [ |
| 22 |
'fontSize' => true, |
| 23 |
'lineHeight' => true |
| 24 |
], |
| 25 |
'spacing' => [ |
| 26 |
'margin' => true |
| 27 |
], |
| 28 |
'color' => [ |
| 29 |
'text' => true, |
| 30 |
] |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function getScripts(): array |
| 35 |
{ |
| 36 |
return [ |
| 37 |
[ |
| 38 |
'source' => 'admin/BlockEditor/Stock/StockBlock.jsx', |
| 39 |
'dependencies' => ['wp-blocks', 'wp-components', 'wp-data', 'wp-block-editor', 'wp-element'] |
| 40 |
] |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function getStyles(): array |
| 45 |
{ |
| 46 |
// 'admin/BlockEditor/Stock/style/stock-block-editor.scss' |
| 47 |
return []; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public function localizeData(): array |
| 52 |
{ |
| 53 |
return [ |
| 54 |
$this->getLocalizationKey() => [ |
| 55 |
'slug' => $this->slugPrefix, |
| 56 |
'name' => static::getEditorName(), |
| 57 |
'title' => __('Stock', 'fluent-cart'), |
| 58 |
'description' => __('This block will display the stock.', 'fluent-cart'), |
| 59 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 60 |
'supports' => $this->supports() |
| 61 |
], |
| 62 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function render(array $shortCodeAttribute, $block = null) |
| 67 |
{ |
| 68 |
$product = null; |
| 69 |
$insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no'); |
| 70 |
$queryType = Arr::get($shortCodeAttribute, 'query_type', 'default'); |
| 71 |
|
| 72 |
if ($insideProductInfo === 'yes' || $queryType === 'default') { |
| 73 |
$product = fluent_cart_get_current_product(); |
| 74 |
|
| 75 |
} else { |
| 76 |
$productId = Arr::get($shortCodeAttribute, 'product_id', false); |
| 77 |
if ($productId) { |
| 78 |
$product = Product::query()->with(['variants'])->find($productId); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (!$product) { |
| 83 |
return ''; |
| 84 |
} |
| 85 |
|
| 86 |
$wrapper_attributes = get_block_wrapper_attributes( |
| 87 |
[ |
| 88 |
'class' => 'fct-product-stock wc-block-grid__product-stock', |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
ob_start(); |
| 93 |
(new ProductRenderer($product)) |
| 94 |
->renderStockAvailability($wrapper_attributes); |
| 95 |
|
| 96 |
return ob_get_clean(); |
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|