PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 0.0.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v0.0.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / blocks / register.php

register.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 0.0.1, at inc/blocks/register.php

183 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Blocks Register
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\Blocks;
9
10 use SureDonation\Inc\Payments\Stripe\Stripe_Helper;
11 use SureDonation\Inc\Traits\Get_Instance;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Register class for blocks.
20 *
21 * @since 0.0.1
22 */
23 class Register {
24 use Get_Instance;
25
26 /**
27 * Constructor.
28 *
29 * @since 0.0.1
30 */
31 public function __construct() {
32 add_action( 'init', [ $this, 'register_blocks' ] );
33 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
34 add_filter( 'block_categories_all', [ $this, 'register_block_category' ], 10, 2 );
35 }
36
37 /**
38 * Register custom block category for SureDonation blocks.
39 *
40 * Only adds the category on the donation form editor.
41 *
42 * @param array<int, array<string, mixed>> $categories Existing block categories.
43 * @param \WP_Block_Editor_Context $context Block editor context.
44 * @return array<int, array<string, mixed>> Modified block categories.
45 * @since 0.0.1
46 */
47 public function register_block_category( $categories, $context ) {
48 // Only show category on donation form editor.
49 if ( ! isset( $context->post ) || 'suredonation_form' !== $context->post->post_type ) {
50 return $categories;
51 }
52
53 // Add SureDonation category at the beginning.
54 return array_merge(
55 [
56 [
57 'slug' => 'suredonation',
58 'title' => __( 'General Fields', 'suredonation' ),
59 'icon' => null,
60 ],
61 ],
62 $categories
63 );
64 }
65
66 /**
67 * Enqueue block editor assets.
68 *
69 * Only loads on the donation form editor.
70 *
71 * @return void
72 * @since 0.0.1
73 */
74 public function enqueue_editor_assets() {
75 $screen = get_current_screen();
76
77 // Only load on donation form editor.
78 if ( ! $screen || 'suredonation_form' !== $screen->post_type ) {
79 return;
80 }
81
82 // Enqueue the blocks script.
83 wp_enqueue_script(
84 'suredonation-blocks',
85 SUREDONATION_URL . 'assets/build/blocks.js',
86 [ 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n', 'wp-block-editor', 'wp-data' ],
87 SUREDONATION_VER,
88 true
89 );
90
91 // Localize script with admin data for blocks.
92 wp_localize_script(
93 'suredonation-blocks',
94 'suredonation_admin',
95 [
96 'payments' => [
97 'stripe_connected' => Stripe_Helper::is_stripe_connected(),
98 'stripe_connect_url' => Stripe_Helper::get_stripe_connect_url(),
99 ],
100 ]
101 );
102 }
103
104 /**
105 * Register all blocks.
106 *
107 * @return void
108 * @since 0.0.1
109 */
110 public function register_blocks() {
111 $blocks = [
112 [
113 'dir' => SUREDONATION_DIR . 'inc/blocks/**/*.php',
114 'namespace' => 'SureDonation\\Inc\\Blocks',
115 ],
116 ];
117
118 /**
119 * Filter to add and register additional blocks.
120 *
121 * @param array<int, array<string, string>> $additional_blocks Additional blocks to register.
122 */
123 $additional_blocks = apply_filters( 'suredonation_register_additional_blocks', [] );
124
125 if ( ! empty( $additional_blocks ) && is_array( $additional_blocks ) && count( $additional_blocks ) > 0 ) {
126 $blocks = [ ...$blocks, ...$additional_blocks ];
127 }
128
129 foreach ( $blocks as $block ) {
130 if ( ! is_array( $block ) || ! isset( $block['dir'] ) || ! isset( $block['namespace'] ) ) {
131 continue;
132 }
133 $block_files = glob( $block['dir'] );
134 if ( is_array( $block_files ) ) {
135 $this->register_block( $block_files, $block['namespace'], 'Block' );
136 }
137 }
138 }
139
140 /**
141 * Register blocks from directory.
142 *
143 * @param array<int, string> $blocks_dir Array of block file paths.
144 * @param string $block_namespace Block namespace.
145 * @param string $base Base class name.
146 * @return void
147 * @since 0.0.1
148 */
149 public function register_block( $blocks_dir, $block_namespace, $base ) {
150 if ( empty( $blocks_dir ) ) {
151 return;
152 }
153
154 foreach ( $blocks_dir as $filename ) {
155 // Skip base.php and register.php.
156 $basename = basename( $filename );
157 if ( 'base.php' === $basename || 'register.php' === $basename ) {
158 continue;
159 }
160
161 require_once $filename;
162
163 // Replace hyphens with underscores in directory name.
164 $classname = str_replace( '-', '_', basename( dirname( $filename ) ) );
165
166 // Convert to title case.
167 $classname = ucwords( $classname, '_' );
168
169 $full_class_name = $block_namespace . '\\' . $classname . '\\' . $base;
170
171 // Check if the class exists.
172 if ( class_exists( $full_class_name ) ) {
173 $block = new $full_class_name();
174
175 // Call register on the block object.
176 if ( method_exists( $block, 'register' ) ) {
177 $block->register();
178 }
179 }
180 }
181 }
182 }
183