| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Providers; |
| 6 |
|
| 7 |
use Yatra\Blocks\TourBlock; |
| 8 |
use Yatra\Blocks\ActivityBlock; |
| 9 |
use Yatra\Blocks\DestinationBlock; |
| 10 |
use Yatra\Blocks\TripCategoryBlock; |
| 11 |
use Yatra\Core\Container; |
| 12 |
use Yatra\Core\ServiceProvider; |
| 13 |
|
| 14 |
/** |
| 15 |
* Block Service Provider |
| 16 |
* |
| 17 |
* Registers all Gutenberg blocks for Yatra |
| 18 |
* Maintains backward compatibility with old plugin blocks |
| 19 |
* |
| 20 |
* @package Yatra\Providers |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
class BlockServiceProvider extends ServiceProvider |
| 24 |
{ |
| 25 |
public function __construct(Container $container) |
| 26 |
{ |
| 27 |
parent::__construct($container); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register blocks |
| 32 |
*/ |
| 33 |
public function register(): void |
| 34 |
{ |
| 35 |
// Priority 100 so other plugins (or old stubs) register first; we reclaim + register full assets last. |
| 36 |
if (did_action('init')) { |
| 37 |
$this->registerBlocksOnInit(); |
| 38 |
} else { |
| 39 |
add_action('init', [$this, 'registerBlocksOnInit'], 100); |
| 40 |
} |
| 41 |
|
| 42 |
// Register Yatra block category |
| 43 |
add_filter('block_categories_all', [$this, 'registerBlockCategory'], 10, 2); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register blocks on init hook |
| 48 |
*/ |
| 49 |
public function registerBlocksOnInit(): void |
| 50 |
{ |
| 51 |
// Initialize all blocks |
| 52 |
new TourBlock(); |
| 53 |
new ActivityBlock(); |
| 54 |
new DestinationBlock(); |
| 55 |
new TripCategoryBlock(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Register Yatra block category |
| 60 |
* |
| 61 |
* @param array $categories Existing block categories |
| 62 |
* @param \WP_Block_Editor_Context|null $context Block editor context |
| 63 |
* @return array Modified categories |
| 64 |
*/ |
| 65 |
public function registerBlockCategory(array $categories, ?\WP_Block_Editor_Context $context = null): array |
| 66 |
{ |
| 67 |
// Add Yatra category at the beginning |
| 68 |
array_unshift($categories, [ |
| 69 |
'slug' => 'yatra', |
| 70 |
'title' => __('Yatra', 'yatra'), |
| 71 |
'icon' => '<svg width="20" height="20" viewBox="0 0 24 24" fill="none"><path d="M12 2L2 7v10c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V7l-10-5z" fill="currentColor"/></svg>', |
| 72 |
]); |
| 73 |
|
| 74 |
return $categories; |
| 75 |
} |
| 76 |
} |
| 77 |
|