| 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 |
$namespace = 'SRFM\\Inc\\Blocks'; |
| 31 |
$blocks_dir = glob( SRFM_DIR . 'inc/blocks/**/*.php' ); |
| 32 |
$base = 'Block'; |
| 33 |
$this->register_block( $blocks_dir, $namespace, $base ); |
| 34 |
|
| 35 |
if ( defined( 'SRFM_PRO_VER' ) ) { |
| 36 |
$blocks_dir = glob( SRFM_PRO_DIR . 'inc/blocks/**/*.php' ); |
| 37 |
$namespace = 'SRFM_PRO\\Inc\\Blocks'; |
| 38 |
$base = 'Block'; |
| 39 |
$this->register_block( $blocks_dir, $namespace, $base ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register Blocks |
| 45 |
* |
| 46 |
* @param array<int, string>|false $blocks_dir Block directory. |
| 47 |
* @param string $namespace Namespace. |
| 48 |
* @param string $base Base. |
| 49 |
* @return void |
| 50 |
* @since 0.0.1 |
| 51 |
*/ |
| 52 |
public static function register_block( $blocks_dir, $namespace, $base ) { |
| 53 |
if ( ! empty( $blocks_dir ) ) { |
| 54 |
foreach ( $blocks_dir as $filename ) { |
| 55 |
// Include the file. |
| 56 |
require_once $filename; |
| 57 |
|
| 58 |
// Replace hyphens with underscores. |
| 59 |
$classname = str_replace( '-', '_', basename( dirname( $filename ) ) ); |
| 60 |
|
| 61 |
// Convert to title case (capitalizes the first letter of each word). |
| 62 |
$classname = ucwords( $classname, '_' ); |
| 63 |
|
| 64 |
$full_class_name = $namespace . '\\' . $classname . '\\' . $base; |
| 65 |
|
| 66 |
// Check if the class exists. |
| 67 |
if ( class_exists( $full_class_name ) ) { |
| 68 |
$block = new $full_class_name(); |
| 69 |
|
| 70 |
// Check if the register method exists. |
| 71 |
if ( method_exists( $block, 'register' ) ) { |
| 72 |
// Call register on the block object. |
| 73 |
$block->register(); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|