PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.4
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.4
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / includes / generators / registry.php

registry.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.5.4, at includes/generators/registry.php

312 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Generator Registry - centralized registration and management of option generators.
4 *
5 * @package Jet_Form_Builder\Generators
6 */
7
8 namespace Jet_Form_Builder\Generators;
9
10 use Jet_Form_Builder\Plugin;
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Registry class for option generators.
19 *
20 * Provides centralized registration, retrieval, and schema export
21 * for all option generators (both legacy and V2).
22 *
23 * This class integrates with the existing Form_Manager system
24 * by reading generators from the 'jet-form-builder/forms/options-generators' filter.
25 */
26 class Registry {
27
28 /**
29 * Singleton instance.
30 *
31 * @var Registry|null
32 */
33 private static $instance = null;
34
35 /**
36 * Cached generators from Form_Manager.
37 *
38 * @var Base[]|null
39 */
40 private $generators = null;
41
42 /**
43 * Get singleton instance.
44 *
45 * @return Registry
46 */
47 public static function instance(): Registry {
48 if ( null === self::$instance ) {
49 self::$instance = new self();
50 }
51
52 return self::$instance;
53 }
54
55 /**
56 * Private constructor for singleton.
57 */
58 private function __construct() {
59 // Generators are loaded lazily from Form_Manager
60 }
61
62 /**
63 * Load generators from Form_Manager.
64 * This integrates with the existing system that uses
65 * 'jet-form-builder/forms/options-generators' filter.
66 */
67 private function load_generators(): void {
68 if ( null !== $this->generators ) {
69 return;
70 }
71
72 $this->generators = array();
73
74 // Get generators from Form_Manager (which applies the filter)
75 if ( class_exists( Plugin::class ) && Plugin::instance()->form ) {
76 $generators = Plugin::instance()->form->get_options_generators();
77
78 foreach ( $generators as $id => $generator ) {
79 $this->generators[ $id ] = $generator;
80 }
81 }
82 }
83
84 /**
85 * Get a generator by ID.
86 *
87 * @param string $id Generator ID.
88 *
89 * @return Base|null Generator instance or null if not found.
90 */
91 public function get( string $id ): ?Base {
92 $this->load_generators();
93
94 return $this->generators[ $id ] ?? null;
95 }
96
97 /**
98 * Check if a generator exists.
99 *
100 * @param string $id Generator ID.
101 *
102 * @return bool
103 */
104 public function has( string $id ): bool {
105 $this->load_generators();
106
107 return isset( $this->generators[ $id ] );
108 }
109
110 /**
111 * Get all registered generators.
112 *
113 * @return Base[]
114 */
115 public function get_all(): array {
116 $this->load_generators();
117
118 return $this->generators;
119 }
120
121 /**
122 * Get generators list for select control (id => name format).
123 *
124 * @return array
125 */
126 public function get_list(): array {
127 $this->load_generators();
128
129 $list = array(
130 array(
131 'value' => '',
132 'label' => __( '-- Select generator --', 'jet-form-builder' ),
133 ),
134 );
135
136 foreach ( $this->generators as $generator ) {
137 if ( ! $generator->can_generate() ) {
138 continue;
139 }
140
141 $list[] = array(
142 'value' => $generator->get_id(),
143 'label' => $generator->get_name(),
144 );
145 }
146
147 return $list;
148 }
149
150 /**
151 * Get all schemas for JS localization.
152 * Returns detailed schema information for editor UI generation.
153 *
154 * @return array
155 */
156 public function get_schemas_for_js(): array {
157 $this->load_generators();
158
159 $schemas = array();
160
161 foreach ( $this->generators as $id => $generator ) {
162 if ( $generator instanceof Base_V2 ) {
163 // New V2 generator with full schema support
164 $schemas[ $id ] = $generator->get_schema_for_js();
165 } else {
166 // Legacy generator - create minimal schema
167 $schemas[ $id ] = $this->create_legacy_schema( $generator );
168 }
169 }
170
171 return $schemas;
172 }
173
174 /**
175 * Creates minimal schema for legacy generators.
176 *
177 * @param Base $generator Legacy generator instance.
178 *
179 * @return array
180 */
181 private function create_legacy_schema( Base $generator ): array {
182 $id = $generator->get_id();
183
184 // Special handling for known legacy generators
185 switch ( $id ) {
186 case 'num_range_manual':
187 return array(
188 'id' => $id,
189 'name' => $generator->get_name(),
190 'schema' => array(
191 'generator_numbers_min' => array(
192 'type' => 'number',
193 'default' => 1,
194 'label' => __( 'Start of range', 'jet-form-builder' ),
195 'control' => 'number',
196 'step' => 0.01,
197 ),
198 'generator_numbers_max' => array(
199 'type' => 'number',
200 'default' => 10,
201 'label' => __( 'End of range', 'jet-form-builder' ),
202 'control' => 'number',
203 'step' => 0.01,
204 ),
205 'generator_numbers_step' => array(
206 'type' => 'number',
207 'default' => 1,
208 'label' => __( 'Step', 'jet-form-builder' ),
209 'control' => 'number',
210 'step' => 0.01,
211 ),
212 ),
213 'supports_update' => false,
214 'legacy' => false, // Has dedicated schema
215 );
216
217 default:
218 // Generic legacy generator with single field
219 return array(
220 'id' => $id,
221 'name' => $generator->get_name(),
222 'schema' => array(
223 'generator_field' => array(
224 'type' => 'string',
225 'default' => '',
226 'label' => __( 'Field Name', 'jet-form-builder' ),
227 'control' => 'text',
228 ),
229 ),
230 'supports_update' => false,
231 'legacy' => true,
232 );
233 }
234 }
235
236 /**
237 * Get combined block attributes from all V2 generators.
238 * Can be used to dynamically extend block.json.
239 *
240 * @return array
241 */
242 public function get_all_block_attributes(): array {
243 $this->load_generators();
244
245 $attributes = array();
246
247 foreach ( $this->generators as $generator ) {
248 if ( $generator instanceof Base_V2 ) {
249 $attributes = array_merge( $attributes, $generator->get_block_attributes() );
250 }
251 }
252
253 return $attributes;
254 }
255
256 /**
257 * Generate options using specified generator.
258 *
259 * @param string $generator_id Generator ID.
260 * @param array $block_attrs Block attributes.
261 * @param array $context Optional context for auto-update.
262 *
263 * @return array Generated options or empty array if generator not found.
264 */
265 public function generate( string $generator_id, array $block_attrs, array $context = array() ): array {
266 $generator = $this->get( $generator_id );
267
268 if ( ! $generator ) {
269 return array();
270 }
271
272 if ( ! $generator->can_generate() ) {
273 return array();
274 }
275
276 // Use context-aware generation for V2 generators
277 if ( $generator instanceof Base_V2 && ! empty( $context ) ) {
278 // Priority: generator_args object (new format) > prefixed attrs (intermediate) > defaults
279 if ( ! empty( $block_attrs['generator_args'] ) && is_array( $block_attrs['generator_args'] ) ) {
280 $settings = $generator->parse_generator_args( $block_attrs['generator_args'] );
281 } else {
282 $settings = $generator->parse_settings( $block_attrs );
283 }
284
285 if ( isset( $block_attrs['_jfb_runtime'] ) && is_array( $block_attrs['_jfb_runtime'] ) ) {
286 $settings['_jfb_runtime'] = $block_attrs['_jfb_runtime'];
287 }
288
289 $settings['_jfb_block_attrs'] = $block_attrs;
290
291 return $generator->generate_with_context( $settings, $context );
292 }
293
294 // Standard generation
295 return $generator->get_values( $block_attrs );
296 }
297
298 /**
299 * Prevent cloning of singleton.
300 */
301 private function __clone() {}
302
303 /**
304 * Prevent unserialization of singleton.
305 *
306 * @throws \Exception Always.
307 */
308 public function __wakeup() {
309 throw new \Exception( 'Cannot unserialize singleton' );
310 }
311 }
312