BlockPatternsService.php
3 years ago
BlockService.php
3 years ago
BlockServiceProvider.php
3 years ago
BlockPatternsService.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\BlockLibrary; |
| 4 | |
| 5 | /** |
| 6 | * Provide general block-related functionality. |
| 7 | */ |
| 8 | class BlockPatternsService { |
| 9 | /** |
| 10 | * Block patterns to register. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | protected $patterns = []; |
| 15 | |
| 16 | /** |
| 17 | * Block patterns categories to register. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $categories = []; |
| 22 | |
| 23 | /** |
| 24 | * Set categories and patterns. |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $this->categories = [ |
| 28 | 'surecart_form' => [ 'label' => __( 'Checkout Form', 'surecart' ) ], |
| 29 | ]; |
| 30 | |
| 31 | $this->patterns = [ |
| 32 | 'default', |
| 33 | 'full-page', |
| 34 | 'simple', |
| 35 | 'sections', |
| 36 | 'two-column', |
| 37 | 'donation', |
| 38 | 'invoice', |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Bootstrap the service. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function bootstrap() { |
| 48 | add_action( 'init', [ $this, 'registerPatternsAndCategories' ], 9 ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Register block patterns and |
| 53 | */ |
| 54 | public function registerPatternsAndCategories() { |
| 55 | // $this->registerCategories(); |
| 56 | $this->registerPatterns(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Register block pattern categories. |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public function registerCategories() { |
| 65 | /** |
| 66 | * Filters the block pattern categories. |
| 67 | * |
| 68 | * @param array[] $categories { |
| 69 | * An associative array of block pattern categories, keyed by category name. |
| 70 | * |
| 71 | * @type array[] $properties { |
| 72 | * An array of block category properties. |
| 73 | * |
| 74 | * @type string $label A human-readable label for the pattern category. |
| 75 | * } |
| 76 | * } |
| 77 | */ |
| 78 | $this->categories = apply_filters( 'surecart/blocks/pattern_categories', $this->categories ); |
| 79 | |
| 80 | foreach ( $this->categories as $name => $properties ) { |
| 81 | if ( ! \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) { |
| 82 | register_block_pattern_category( $name, $properties ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Register our block patterns. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public function registerPatterns() { |
| 93 | /** |
| 94 | * Filters the plugin block patterns. |
| 95 | * |
| 96 | * @param array $patterns List of block patterns by name. |
| 97 | */ |
| 98 | $this->patterns = apply_filters( 'surecart/blocks/patterns', $this->patterns ); |
| 99 | |
| 100 | // loop through patterns and register. |
| 101 | foreach ( $this->patterns as $block_pattern ) { |
| 102 | $pattern_file = plugin_dir_path( SURECART_PLUGIN_FILE ) . 'templates/forms/' . $block_pattern . '.php'; |
| 103 | |
| 104 | register_block_pattern( |
| 105 | 'surecart/' . $block_pattern, |
| 106 | require $pattern_file |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |