| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Blocks; |
| 6 |
|
| 7 |
use Yatra\Services\BlockDataService; |
| 8 |
|
| 9 |
/** |
| 10 |
* Destination Block |
| 11 |
* |
| 12 |
* Gutenberg block for displaying destination listings |
| 13 |
* Maintains backward compatibility with old yatra/destination block |
| 14 |
* |
| 15 |
* @package Yatra\Blocks |
| 16 |
* @since 3.0.0 |
| 17 |
*/ |
| 18 |
class DestinationBlock |
| 19 |
{ |
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->register(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Block render callback |
| 27 |
* Uses BlockDataService to share logic with shortcodes |
| 28 |
* |
| 29 |
* @param array<string, mixed> $attributes Block attributes |
| 30 |
* @param string $content Block content |
| 31 |
*/ |
| 32 |
public function renderCallback(array $attributes, string $content): string |
| 33 |
{ |
| 34 |
wp_enqueue_style('yatra-destination-shortcode', \YATRA_PLUGIN_URL . 'assets/css/shortcodes/destination-shortcode.css', [], YATRA_VERSION); |
| 35 |
|
| 36 |
return BlockDataService::renderDestinationBlock($attributes); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register the block (metadata + render callback) |
| 41 |
*/ |
| 42 |
public function register(): void |
| 43 |
{ |
| 44 |
BlockEditorScript::reclaimBlockName('yatra/destination'); |
| 45 |
|
| 46 |
$editorDeps = BlockEditorScript::editorDependencies(); |
| 47 |
if (! BlockEditorScript::register('yatra-destination-block-editor', 'destination', $editorDeps)) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
wp_register_style( |
| 52 |
'yatra-listing', |
| 53 |
\YATRA_PLUGIN_URL . 'assets/css/listing.css', |
| 54 |
[], |
| 55 |
YATRA_VERSION |
| 56 |
); |
| 57 |
|
| 58 |
wp_register_style( |
| 59 |
'yatra-destination-shortcode', |
| 60 |
\YATRA_PLUGIN_URL . 'assets/css/shortcodes/destination-shortcode.css', |
| 61 |
[], |
| 62 |
YATRA_VERSION |
| 63 |
); |
| 64 |
|
| 65 |
wp_register_script( |
| 66 |
'yatra-destination-shortcode', |
| 67 |
\YATRA_PLUGIN_URL . 'assets/js/destination-shortcode.js', |
| 68 |
['jquery'], |
| 69 |
YATRA_VERSION, |
| 70 |
true |
| 71 |
); |
| 72 |
|
| 73 |
$block = register_block_type_from_metadata( |
| 74 |
BlockEditorScript::blockJsonPath('destination'), |
| 75 |
[ |
| 76 |
'render_callback' => [$this, 'renderCallback'], |
| 77 |
'editor_style' => ['yatra-listing', 'yatra-destination-shortcode'], |
| 78 |
'style' => ['yatra-destination-shortcode'], |
| 79 |
'script' => 'yatra-destination-shortcode', |
| 80 |
] |
| 81 |
); |
| 82 |
|
| 83 |
if ($block === false) { |
| 84 |
register_block_type('yatra/destination', [ |
| 85 |
'api_version' => 3, |
| 86 |
'title' => __('Destination', 'yatra'), |
| 87 |
'category' => 'yatra', |
| 88 |
'description' => __('Display destination listings with customizable options', 'yatra'), |
| 89 |
'icon' => 'admin-site', |
| 90 |
'keywords' => ['destination', 'place', 'location', 'yatra'], |
| 91 |
'render_callback' => [$this, 'renderCallback'], |
| 92 |
'editor_script' => 'yatra-destination-block-editor', |
| 93 |
'editor_style' => ['yatra-listing', 'yatra-destination-shortcode'], |
| 94 |
'style' => ['yatra-destination-shortcode'], |
| 95 |
'script' => 'yatra-destination-shortcode', |
| 96 |
'supports' => [ |
| 97 |
'align' => ['wide', 'full'], |
| 98 |
'html' => false, |
| 99 |
'inserter' => true, |
| 100 |
], |
| 101 |
]); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|