| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\App\Hooks\Handlers\ShortCodes\PricingTableShortCode; |
| 8 |
use FluentCart\App\Services\Translations\TransStrings; |
| 9 |
use FluentCart\App\Vite; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class PricingTableBlockEditor extends BlockEditor |
| 13 |
{ |
| 14 |
|
| 15 |
protected static string $editorName = 'product-pricing-table'; |
| 16 |
|
| 17 |
public function getScripts(): array |
| 18 |
{ |
| 19 |
if (!App::isDevMode()) { |
| 20 |
return []; |
| 21 |
} |
| 22 |
return [ |
| 23 |
[ |
| 24 |
'source' => 'admin/BlockEditor/PricingTable/PricingTableBlockEditor.jsx', |
| 25 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 26 |
] |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
public function getStyles(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'admin/BlockEditor/PricingTable/style/pricing-table-block-editor.scss', |
| 34 |
'admin/BlockEditor/Components/style/fct-global-block-editor.scss' |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function localizeData(): array |
| 39 |
{ |
| 40 |
return [ |
| 41 |
$this->getLocalizationKey() => [ |
| 42 |
'slug' => $this->slugPrefix, |
| 43 |
'name' => static::getEditorName(), |
| 44 |
|
| 45 |
'trans' => TransStrings::getShopAppBlockEditorString(), |
| 46 |
'title' => __('Pricing Table', 'fluent-cart'), |
| 47 |
], |
| 48 |
'fluent_cart_block_editor_asset' => [ |
| 49 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 50 |
], |
| 51 |
'fluent_cart_block_translation' => TransStrings::blockStrings() |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
public function render(array $shortCodeAttribute, $block = null): string |
| 57 |
{ |
| 58 |
|
| 59 |
$groupBy = Arr::get($shortCodeAttribute, 'group_by', 'repeat_interval'); |
| 60 |
$showCheckoutButton = Arr::get($shortCodeAttribute, 'show_checkout_button', true) !== false; |
| 61 |
$showCartButton = Arr::get($shortCodeAttribute, 'show_cart_button', true) !== false; |
| 62 |
$iconVisibility = Arr::get($shortCodeAttribute, 'iconVisibility', true) !== false; |
| 63 |
$productPerRow = Arr::get($shortCodeAttribute, 'product_per_row', 0); |
| 64 |
|
| 65 |
$buttonOptions = Arr::get($shortCodeAttribute, 'buttonOptions', []); |
| 66 |
$checkoutButtonUrlParams = Arr::get($shortCodeAttribute, 'checkout_button_url_params', ''); |
| 67 |
|
| 68 |
$variants = Arr::get($shortCodeAttribute, 'variations', []); |
| 69 |
$variants = is_array($variants) ? $variants : []; |
| 70 |
$variantIds = implode(',', $variants); |
| 71 |
|
| 72 |
$activeVariant = Arr::get($shortCodeAttribute, 'active_variant', []); |
| 73 |
$activeVariant = is_array($activeVariant) ? $activeVariant : (is_object($activeVariant) ? (array)$activeVariant : []); |
| 74 |
|
| 75 |
$badge = Arr::get($shortCodeAttribute, 'badge', []); |
| 76 |
|
| 77 |
$colors = Arr::get($shortCodeAttribute, 'colors', []); |
| 78 |
|
| 79 |
// Initialize an empty array to store the formatted color strings |
| 80 |
$colorStrings = []; |
| 81 |
|
| 82 |
// Iterate over each color group (e.g., badgeColors, cardColors) |
| 83 |
foreach ($colors as $colorGroup) { |
| 84 |
// Iterate over each color within the color group |
| 85 |
foreach ($colorGroup as $key => $color) { |
| 86 |
// Append each color key and its value to the $colorStrings array |
| 87 |
$colorStrings[] = $key . '=' . $color['value']; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Implode the array into a comma-separated string |
| 92 |
$allColorStrings = implode(', ', $colorStrings); |
| 93 |
|
| 94 |
$badgeString = $this->assocArrayToString($badge); |
| 95 |
$activeVariantString = $this->assocArrayToString($activeVariant); |
| 96 |
$buttonOptionStrings = $this->assocArrayToString($buttonOptions); |
| 97 |
|
| 98 |
|
| 99 |
$activeTab = Arr::get($shortCodeAttribute, 'active_tab', 0); |
| 100 |
|
| 101 |
return PricingTableShortCode::make([ |
| 102 |
'variant_ids' => $variantIds, |
| 103 |
'show_cart_button' => (string) $showCartButton, |
| 104 |
'show_checkout_button' => (string) $showCheckoutButton, |
| 105 |
'group_by' => $groupBy, |
| 106 |
'active_tab' => $activeTab, |
| 107 |
'active_variant' => $activeVariantString, |
| 108 |
'badge' => $badgeString, |
| 109 |
'colors' => $allColorStrings, |
| 110 |
'product_per_row' => $productPerRow, |
| 111 |
'button_options' => $buttonOptionStrings, |
| 112 |
'url_params' => $checkoutButtonUrlParams, |
| 113 |
'icon_visibility' => (string) $iconVisibility, |
| 114 |
])->renderShortcode($block); |
| 115 |
} |
| 116 |
|
| 117 |
private function assocArrayToString(array $array, string $delimiter = ', '): string |
| 118 |
{ |
| 119 |
return implode($delimiter, array_map( |
| 120 |
function ($key, $value) { |
| 121 |
return $key . '=' . $value; |
| 122 |
}, |
| 123 |
array_keys($array), |
| 124 |
$array |
| 125 |
)); |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|