PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.10.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.10.0
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / blocks / register.php
register.php
88 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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