| 1 |
<?php |
| 2 |
/** |
| 3 |
* Call block registration. |
| 4 |
* |
| 5 |
* @package SureForms |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SRFM\Inc\Blocks; |
| 9 |
|
| 10 |
use SRFM\Inc\Traits\Get_Instance; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Manage Blocks registrations. |
| 18 |
* |
| 19 |
* @since 0.0.1 |
| 20 |
*/ |
| 21 |
class Register { |
| 22 |
use Get_Instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor |
| 26 |
* |
| 27 |
* @since 0.0.1 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$blocks = [ |
| 31 |
[ |
| 32 |
'dir' => SRFM_DIR . 'inc/blocks/**/*.php', |
| 33 |
'namespace' => 'SRFM\\Inc\\Blocks', |
| 34 |
], |
| 35 |
]; |
| 36 |
|
| 37 |
// Filter to add and register additional blocks. Like Signature block. |
| 38 |
$additional_blocks = apply_filters( 'srfm_register_additional_blocks', [] ); |
| 39 |
|
| 40 |
// Merge additional blocks with the default blocks. When the additional blocks are not empty. |
| 41 |
if ( ! empty( $additional_blocks ) && count( $additional_blocks ) > 0 ) { |
| 42 |
$blocks = [ ...$blocks, ...$additional_blocks ]; |
| 43 |
} |
| 44 |
|
| 45 |
foreach ( $blocks as $block ) { |
| 46 |
// Register the block. |
| 47 |
$this->register_block( glob( $block['dir'] ), $block['namespace'], 'Block' ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register Blocks |
| 53 |
* |
| 54 |
* @param array<int, string>|false $blocks_dir Block directory. |
| 55 |
* @param string $namespace Namespace. |
| 56 |
* @param string $base Base. |
| 57 |
* @return void |
| 58 |
* @since 0.0.1 |
| 59 |
*/ |
| 60 |
public static function register_block( $blocks_dir, $namespace, $base ) { |
| 61 |
if ( ! empty( $blocks_dir ) ) { |
| 62 |
foreach ( $blocks_dir as $filename ) { |
| 63 |
// Include the file. |
| 64 |
require_once $filename; |
| 65 |
|
| 66 |
// Replace hyphens with underscores. |
| 67 |
$classname = str_replace( '-', '_', basename( dirname( $filename ) ) ); |
| 68 |
|
| 69 |
// Convert to title case (capitalizes the first letter of each word). |
| 70 |
$classname = ucwords( $classname, '_' ); |
| 71 |
|
| 72 |
$full_class_name = $namespace . '\\' . $classname . '\\' . $base; |
| 73 |
|
| 74 |
// Check if the class exists. |
| 75 |
if ( class_exists( $full_class_name ) ) { |
| 76 |
$block = new $full_class_name(); |
| 77 |
|
| 78 |
// Check if the register method exists. |
| 79 |
if ( method_exists( $block, 'register' ) ) { |
| 80 |
// Call register on the block object. |
| 81 |
$block->register(); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|