| 1 |
<?php |
| 2 |
namespace CTB; |
| 3 |
|
| 4 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Patterns |
| 8 |
* |
| 9 |
* Handles Gutenberg block patterns registration from pattern.json file. |
| 10 |
* |
| 11 |
* @package CTB |
| 12 |
*/ |
| 13 |
class Patterns{ |
| 14 |
/** |
| 15 |
* Constructor. Sets up action hook for plugin init. |
| 16 |
*/ |
| 17 |
public function __construct(){ |
| 18 |
add_action('init', [$this, 'onPluginsLoaded']); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Decode pattern.json, filter Pro patterns if using the free version, |
| 23 |
* register block pattern category and individual block patterns. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
function onPluginsLoaded(){ |
| 28 |
$patterns = wp_json_file_decode( __DIR__ . '/pattern.json', [ 'associative' => true ] ); |
| 29 |
|
| 30 |
// Register Pattern Category |
| 31 |
if ( function_exists( 'register_block_pattern_category' ) ) { |
| 32 |
register_block_pattern_category( 'ctbPattern', [ 'label' => __( 'Countdown Time', 'countdown-time' ) ] ); |
| 33 |
} |
| 34 |
|
| 35 |
// Register Pattern |
| 36 |
if ( !empty( $patterns ) ) { |
| 37 |
foreach ( $patterns as $pattern ) { |
| 38 |
if ( function_exists( 'register_block_pattern' ) ) { |
| 39 |
register_block_pattern( $pattern['name'], [ |
| 40 |
'title' => $pattern['title'], |
| 41 |
'content' => $pattern['content'], |
| 42 |
'description' => $pattern['description'], |
| 43 |
'categories' => [ 'ctbPattern' ], |
| 44 |
'keywords' => $pattern['keywords'], |
| 45 |
'blockTypes' => $pattern['blockTypes'], |
| 46 |
'viewportWidth' => 1200 |
| 47 |
] ); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
new Patterns(); |