| 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->patterns = [ |
| 28 |
'default', |
| 29 |
'full-page', |
| 30 |
'simple', |
| 31 |
'sections', |
| 32 |
'two-column', |
| 33 |
'donation', |
| 34 |
'invoice', |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Bootstrap the service. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function bootstrap() { |
| 44 |
add_action( 'init', [ $this, 'registerPatternsAndCategories' ], 9 ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register block patterns and |
| 49 |
*/ |
| 50 |
public function registerPatternsAndCategories() { |
| 51 |
$this->categories = [ |
| 52 |
'surecart_form' => [ 'label' => __( 'Checkout Form', 'surecart' ) ], |
| 53 |
'surecart_shop' => [ 'label' => __( 'Shop', 'surecart' ) ], |
| 54 |
'surecart_product_page' => [ 'label' => __( 'Product Page', 'surecart' ) ], |
| 55 |
'surecart_related_products' => [ 'label' => __( 'Related Products', 'surecart' ) ], |
| 56 |
]; |
| 57 |
$this->registerCategories(); |
| 58 |
$this->registerPatterns(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Register block pattern categories. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function registerCategories() { |
| 67 |
/** |
| 68 |
* Filters the block pattern categories. |
| 69 |
* |
| 70 |
* @param array[] $categories { |
| 71 |
* An associative array of block pattern categories, keyed by category name. |
| 72 |
* |
| 73 |
* @type array[] $properties { |
| 74 |
* An array of block category properties. |
| 75 |
* |
| 76 |
* @type string $label A human-readable label for the pattern category. |
| 77 |
* } |
| 78 |
* } |
| 79 |
*/ |
| 80 |
$this->categories = apply_filters( 'surecart/blocks/pattern_categories', $this->categories ); |
| 81 |
|
| 82 |
foreach ( $this->categories as $name => $properties ) { |
| 83 |
if ( ! \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) { |
| 84 |
register_block_pattern_category( $name, $properties ); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register our block patterns. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function registerPatterns() { |
| 95 |
// register the block patterns from patterns directory. |
| 96 |
$patterns = glob( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'templates/patterns/*.php' ); |
| 97 |
|
| 98 |
// sort by priority key. |
| 99 |
usort( |
| 100 |
$patterns, |
| 101 |
function ( $a, $b ) { |
| 102 |
$a = require $a; |
| 103 |
$b = require $b; |
| 104 |
return ( $a['priority'] ?? 0 ) <=> ( $b['priority'] ?? 0 ); |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
foreach ( $patterns as $pattern_file ) { |
| 109 |
register_block_pattern( |
| 110 |
'surecart-' . basename( $pattern_file, '.php' ), |
| 111 |
require $pattern_file |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Filters the plugin block patterns. |
| 117 |
* |
| 118 |
* @param array $patterns List of block patterns by name. |
| 119 |
*/ |
| 120 |
$this->patterns = apply_filters( 'surecart/blocks/patterns', $this->patterns ); |
| 121 |
|
| 122 |
// loop through patterns and register. |
| 123 |
foreach ( $this->patterns as $block_pattern ) { |
| 124 |
$pattern_file = plugin_dir_path( SURECART_PLUGIN_FILE ) . 'templates/forms/' . $block_pattern . '.php'; |
| 125 |
|
| 126 |
register_block_pattern( |
| 127 |
'surecart/' . $block_pattern, |
| 128 |
require $pattern_file |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|