| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors\Buttons; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Helpers\CartHelper; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Hooks\Cart\WebCheckoutHandler; |
| 9 |
use FluentCart\App\Hooks\Handlers\BlockEditors\BlockEditor; |
| 10 |
use FluentCart\App\Http\Routes\WebRoutes; |
| 11 |
use FluentCart\App\Models\Cart; |
| 12 |
use FluentCart\App\Models\Product; |
| 13 |
use FluentCart\App\Models\ProductVariation; |
| 14 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 15 |
use FluentCart\App\Services\Renderer\ModalCheckoutRenderer; |
| 16 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 17 |
use FluentCart\App\Services\Translations\TransStrings; |
| 18 |
use FluentCart\App\Vite; |
| 19 |
use FluentCart\Framework\Support\Arr; |
| 20 |
|
| 21 |
class BuyNowButtonBlockEditor extends BlockEditor |
| 22 |
{ |
| 23 |
protected static string $editorName = 'buy-now-button'; |
| 24 |
|
| 25 |
public function getScripts(): array |
| 26 |
{ |
| 27 |
return [ |
| 28 |
[ |
| 29 |
'source' => 'admin/BlockEditor/Buttons/BuyNowButtonBlockEditor.jsx', |
| 30 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 31 |
] |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
public function supports(): array |
| 36 |
{ |
| 37 |
return [ |
| 38 |
'html' => false, |
| 39 |
'typography' => [ |
| 40 |
'fontSize' => true, |
| 41 |
'lineHeight' => true |
| 42 |
], |
| 43 |
'spacing' => [ |
| 44 |
'margin' => true, |
| 45 |
'padding' => true |
| 46 |
], |
| 47 |
'color' => [ |
| 48 |
'text' => true, |
| 49 |
'background' => true, |
| 50 |
], |
| 51 |
'__experimentalBorder' => [ |
| 52 |
'color' => true, |
| 53 |
'radius' => true, |
| 54 |
'style' => true, |
| 55 |
'width' => true, |
| 56 |
], |
| 57 |
'shadow' => true |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function getStyles(): array |
| 62 |
{ |
| 63 |
return [ |
| 64 |
'admin/BlockEditor/Buttons/style/button-block-editor.scss' |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public function localizeData(): array |
| 70 |
{ |
| 71 |
return [ |
| 72 |
$this->getLocalizationKey() => [ |
| 73 |
'slug' => $this->slugPrefix, |
| 74 |
'name' => static::getEditorName(), |
| 75 |
'title' => __('Buy Now', 'fluent-cart'), |
| 76 |
'description' => __('A custom button block with product selection and automatic link assignment.', 'fluent-cart'), |
| 77 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 78 |
], |
| 79 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 80 |
'fluent_cart_block_editor_asset' => [ |
| 81 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 82 |
], |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
public function render(array $shortCodeAttribute, $block = null) |
| 87 |
{ |
| 88 |
AssetLoader::loadSingleProductAssets(); |
| 89 |
|
| 90 |
$variantIds = Arr::get($shortCodeAttribute, 'variant_ids', []); |
| 91 |
$variantId = Arr::get($variantIds, 0); |
| 92 |
|
| 93 |
if (!$variantId) { |
| 94 |
if(Helper::isAdminUser()){ |
| 95 |
return '<p class="fct-admin-notice">' . esc_html__('No variant selected', 'fluent-cart') . '</p>'; |
| 96 |
} |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
$variant = ProductVariation::query()->find($variantId); |
| 101 |
|
| 102 |
if (!$variant) { |
| 103 |
if(Helper::isAdminUser()){ |
| 104 |
return '<p class="fct-admin-notice">' . esc_html__('Invalid variant', 'fluent-cart') . '</p>'; |
| 105 |
} |
| 106 |
|
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
$product = Product::query()->find($variant->post_id); |
| 111 |
|
| 112 |
if (!$product) { |
| 113 |
if(Helper::isAdminUser()){ |
| 114 |
return '<p class="fct-admin-notice">' . esc_html__('Product not found', 'fluent-cart') . '</p>'; |
| 115 |
} |
| 116 |
|
| 117 |
return ''; |
| 118 |
} |
| 119 |
|
| 120 |
ob_start(); |
| 121 |
(new ProductRenderer($product))->renderBuyNowButtonBlock($shortCodeAttribute); |
| 122 |
return ob_get_clean(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
} |
| 127 |
|