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