| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Hooks\Handlers\ShortCodes\ProductCardShortCode; |
| 6 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 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 |
use FluentCart\App\Models\Product; |
| 12 |
use FluentCart\App\Helpers\Helper; |
| 13 |
use FluentCart\App\App; |
| 14 |
use FluentCart\Api\StoreSettings; |
| 15 |
|
| 16 |
class ProductGalleryBlockEditor extends BlockEditor |
| 17 |
{ |
| 18 |
protected static string $editorName = 'product-gallery'; |
| 19 |
|
| 20 |
public function getScripts(): array |
| 21 |
{ |
| 22 |
return [ |
| 23 |
[ |
| 24 |
'source' => 'admin/BlockEditor/ProductGallery/ProductGalleryBlockEditor.jsx', |
| 25 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 26 |
] |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function getStyles(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'admin/BlockEditor/ProductGallery/style/product-gallery-block-editor.scss' |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
public function localizeData(): array |
| 38 |
{ |
| 39 |
return [ |
| 40 |
$this->getLocalizationKey() => [ |
| 41 |
'slug' => $this->slugPrefix, |
| 42 |
'name' => static::getEditorName(), |
| 43 |
'title' => __('Product Gallery', 'fluent-cart'), |
| 44 |
'description' => __('This block will display the product gallery.', 'fluent-cart'), |
| 45 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 46 |
], |
| 47 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function render(array $shortCodeAttribute, $block = null) |
| 52 |
{ |
| 53 |
AssetLoader::loadSingleProductAssets(); |
| 54 |
|
| 55 |
$product = null; |
| 56 |
$insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no'); |
| 57 |
$queryType = Arr::get($shortCodeAttribute, 'query_type', 'default'); |
| 58 |
$enableImageZoom = Arr::get($shortCodeAttribute, 'enableImageZoom', 'yes'); |
| 59 |
|
| 60 |
if ($insideProductInfo === 'yes' || $queryType === 'default') { |
| 61 |
$product = fluent_cart_get_current_product(); |
| 62 |
|
| 63 |
} else { |
| 64 |
$productId = Arr::get($shortCodeAttribute, 'product_id', false); |
| 65 |
if ($productId) { |
| 66 |
$product = Product::query()->with(['variants'])->find($productId); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (!$product) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
// import xzoom |
| 74 |
// Vite::enqueueStaticScript( |
| 75 |
// 'fluentcart-zoom-js', |
| 76 |
// 'public/lib/xzoom/xzoom.js', |
| 77 |
// [] |
| 78 |
// ); |
| 79 |
// Vite::enqueueStaticStyle( |
| 80 |
// 'fluentcart-zoom-css', |
| 81 |
// 'public/lib/xzoom/xzoom.css', |
| 82 |
// ); |
| 83 |
// |
| 84 |
// wp_enqueue_style( |
| 85 |
// 'fluentcart-single-product', |
| 86 |
// Vite::getAssetUrl('public/single-product/single-product.scss'), |
| 87 |
// [], |
| 88 |
// '' |
| 89 |
// ); |
| 90 |
// |
| 91 |
// |
| 92 |
// Vite::enqueueStyle( |
| 93 |
// 'fluentcart-add-to-cart-btn-css', |
| 94 |
// 'public/buttons/add-to-cart/style/style.scss' |
| 95 |
// ); |
| 96 |
// Vite::enqueueStyle( |
| 97 |
// 'fluentcart-direct-checkout-btn-css', |
| 98 |
// 'public/buttons/direct-checkout/style/style.scss' |
| 99 |
// ); |
| 100 |
|
| 101 |
$thumbPosition = Arr::get($shortCodeAttribute, 'thumbPosition', 'bottom'); |
| 102 |
if (!in_array($thumbPosition, ['bottom', 'top', 'left', 'right'], true)) { |
| 103 |
$thumbPosition = 'bottom'; |
| 104 |
} |
| 105 |
$scrollableThumbs = Arr::get($shortCodeAttribute, 'scrollableThumbs', 'no'); |
| 106 |
if (!in_array($scrollableThumbs, ['yes', 'no'], true)) { |
| 107 |
$scrollableThumbs = 'no'; |
| 108 |
} |
| 109 |
$maxThumbnails = Arr::get($shortCodeAttribute, 'maxThumbnails', ''); |
| 110 |
$maxThumbnails = $maxThumbnails !== '' ? (int) $maxThumbnails : null; |
| 111 |
|
| 112 |
ob_start(); |
| 113 |
(new ProductRenderer($product))->renderGallery([ |
| 114 |
'thumbnail_mode' => 'all', |
| 115 |
'thumb_position' => $thumbPosition, |
| 116 |
'scrollable_thumbs' => $scrollableThumbs, |
| 117 |
'max_thumbnails' => $maxThumbnails, |
| 118 |
]); |
| 119 |
return ob_get_clean(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
|