| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 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 ProductDescriptionBlockEditor extends BlockEditor |
| 12 |
{ |
| 13 |
protected static string $editorName = 'product-description'; |
| 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' => true, |
| 30 |
'typography' => [ |
| 31 |
'fontSize' => true, |
| 32 |
'lineHeight' => true, |
| 33 |
'__experimentalFontFamily' => true, |
| 34 |
'__experimentalFontWeight' => true, |
| 35 |
'__experimentalFontStyle' => true, |
| 36 |
'__experimentalTextTransform' => true, |
| 37 |
'__experimentalTextDecoration' => true, |
| 38 |
'__experimentalLetterSpacing' => true, |
| 39 |
'__experimentalDefaultControls' => [ |
| 40 |
'fontSize' => true, |
| 41 |
'lineHeight' => true, |
| 42 |
'fontWeight' => true, |
| 43 |
], |
| 44 |
], |
| 45 |
'color' => [ |
| 46 |
'text' => true, |
| 47 |
'background' => true, |
| 48 |
'link' => true, |
| 49 |
'gradients' => true, |
| 50 |
], |
| 51 |
'spacing' => [ |
| 52 |
'margin' => true, |
| 53 |
'padding' => true, |
| 54 |
], |
| 55 |
'__experimentalBorder' => [ |
| 56 |
'color' => true, |
| 57 |
'radius' => true, |
| 58 |
'style' => true, |
| 59 |
'width' => true, |
| 60 |
], |
| 61 |
'shadow' => true, |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
public function getScripts(): array |
| 66 |
{ |
| 67 |
return [ |
| 68 |
[ |
| 69 |
'source' => 'admin/BlockEditor/ProductDescription/ProductDescriptionBlockEditor.jsx', |
| 70 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 71 |
] |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
public function getStyles(): array |
| 76 |
{ |
| 77 |
return [ |
| 78 |
'admin/BlockEditor/ProductDescription/style/product-description-block-editor.scss' |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
public function localizeData(): array |
| 83 |
{ |
| 84 |
return [ |
| 85 |
$this->getLocalizationKey() => [ |
| 86 |
'slug' => $this->slugPrefix, |
| 87 |
'name' => static::getEditorName(), |
| 88 |
'title' => __('Product Description', 'fluent-cart'), |
| 89 |
'description' => __('This block will display the full product description.', 'fluent-cart'), |
| 90 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg') |
| 91 |
], |
| 92 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
public function render(array $shortCodeAttribute, $block = null) |
| 97 |
{ |
| 98 |
AssetLoader::loadSingleProductAssets(); |
| 99 |
|
| 100 |
$productId = absint(Arr::get($shortCodeAttribute, 'product_id', 0)); |
| 101 |
|
| 102 |
if ($productId) { |
| 103 |
$product = Product::query()->find($productId); |
| 104 |
} else { |
| 105 |
$product = fluent_cart_get_current_product(); |
| 106 |
} |
| 107 |
|
| 108 |
if (!$product || empty($product->post_content)) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
$wrapper_attributes = get_block_wrapper_attributes([ |
| 113 |
'class' => 'fct-product-description', |
| 114 |
]); |
| 115 |
|
| 116 |
return sprintf( |
| 117 |
'<div %s>%s</div>', |
| 118 |
$wrapper_attributes, |
| 119 |
wpautop(wp_kses_post($product->post_content)) |
| 120 |
); |
| 121 |
} |
| 122 |
} |