PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.2
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 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
jetformbuilder / modules / option-field / module.php
module.php
380 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Modules\Option_Field;
5
6 use Jet_Form_Builder\Blocks\Module as BlocksModule;
7 use JFB_Modules\Option_Field\Interfaces\Support_Option_Query_It;
8 use Jet_Form_Builder\Blocks\Types\Base;
9 use Jet_Form_Builder\Classes\Tools;
10 use Jet_Form_Builder\Plugin;
11 use JFB_Components\Module\Base_Module_Dir_It;
12 use JFB_Components\Module\Base_Module_Dir_Trait;
13 use JFB_Components\Module\Base_Module_Handle_It;
14 use JFB_Components\Module\Base_Module_Handle_Trait;
15 use JFB_Components\Module\Base_Module_It;
16 use JFB_Components\Module\Base_Module_Url_It;
17 use JFB_Components\Module\Base_Module_Url_Trait;
18 use JFB_Modules\Option_Query\Legacy_Generator_Query;
19
20 // If this file is called directly, abort.
21 if ( ! defined( 'WPINC' ) ) {
22 die;
23 }
24
25 final class Module implements
26 Base_Module_It,
27 Base_Module_Url_It,
28 Base_Module_Handle_It,
29 Base_Module_Dir_It {
30
31 use Base_Module_Url_Trait;
32 use Base_Module_Dir_Trait;
33 use Base_Module_Handle_Trait;
34
35 /**
36 * REST API controller instance.
37 *
38 * @var Rest_Api\Rest_Api_Controller
39 */
40 private $rest;
41
42 /**
43 * HTML attributes injector instance.
44 *
45 * @var Html_Attributes_Injector
46 */
47 private $html_injector;
48
49 public function rep_item_id() {
50 return 'option-field';
51 }
52
53 public function condition(): bool {
54 return true;
55 }
56
57 public function init_hooks() {
58 add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
59 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) );
60 add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
61 add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
62 add_action( 'jet-form-builder/after-start-form-row', array( $this, 'apply_field_options' ) );
63 add_action(
64 'jet-form-builder/option-query/set-in-block',
65 array( $this, 'on_set_in_block' ),
66 0
67 );
68 add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
69
70 // Initialize HTML attributes injector for auto-update
71 $this->html_injector = new Html_Attributes_Injector();
72 }
73
74 public function remove_hooks() {
75 remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
76 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) );
77 remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
78 remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
79 remove_action(
80 'jet-form-builder/option-query/set-in-block',
81 array( $this, 'on_set_in_block' ),
82 0
83 );
84 remove_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
85 }
86
87 public function add_blocks_types( array $block_types ): array {
88 array_push(
89 $block_types,
90 new Blocks\Select\Block_Type(),
91 new Blocks\Checkbox\Block_Type(),
92 new Blocks\Radio\Block_Type()
93 );
94
95 return $block_types;
96 }
97
98 /**
99 * @param Base $block
100 */
101 public function apply_field_options( Base $block ) {
102 if ( ! ( $block instanceof Support_Option_Query_It ) ) {
103 return;
104 }
105 $block->block_attrs['field_options'] = iterator_to_array( $block->get_query()->fetch() );
106 }
107
108 public function enqueue_admin_assets() {
109 $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' );
110
111 array_push(
112 $script_asset['dependencies'],
113 'jet-fb-components'
114 );
115
116 // for all blocks
117 wp_enqueue_script(
118 $this->get_handle(),
119 $this->get_url( 'assets/build/editor.js' ),
120 $script_asset['dependencies'],
121 $script_asset['version'],
122 true
123 );
124
125 wp_enqueue_style(
126 $this->get_handle(),
127 $this->get_url( 'assets/build/editor.css' ),
128 array(),
129 $script_asset['version']
130 );
131
132 wp_localize_script(
133 $this->get_handle(),
134 'JetFormOptionFieldData',
135 $this->get_localize_data()
136 );
137 }
138
139 public function register_frontend_scripts() {
140 // select
141 $script_asset = require_once $this->get_dir( 'assets/build/select.asset.php' );
142
143 if ( true === $script_asset ) {
144 return;
145 }
146
147 array_push(
148 $script_asset['dependencies'],
149 BlocksModule::MAIN_SCRIPT_HANDLE
150 );
151
152 wp_register_script(
153 $this->get_handle( 'select' ),
154 $this->get_url( 'assets/build/select.js' ),
155 $script_asset['dependencies'],
156 $script_asset['version'],
157 true
158 );
159 wp_register_style(
160 $this->get_handle( 'select' ),
161 $this->get_url( 'assets/build/select.css' ),
162 array(),
163 $script_asset['version']
164 );
165
166 // checkbox
167 $script_asset = require_once $this->get_dir( 'assets/build/checkbox.asset.php' );
168
169 array_push(
170 $script_asset['dependencies'],
171 BlocksModule::MAIN_SCRIPT_HANDLE
172 );
173
174 wp_register_script(
175 Blocks\Checkbox\Block_Type::HANDLE,
176 $this->get_url( 'assets/build/checkbox.js' ),
177 $script_asset['dependencies'],
178 $script_asset['version'],
179 true
180 );
181
182 wp_register_style(
183 $this->get_handle( 'checkbox' ),
184 $this->get_url( 'assets/build/checkbox.css' ),
185 array(),
186 $script_asset['version']
187 );
188
189 // options restrictions
190 $script_asset = require_once $this->get_dir(
191 'assets/build/custom.options.restrictions.asset.php'
192 );
193
194 array_push(
195 $script_asset['dependencies'],
196 BlocksModule::MAIN_SCRIPT_HANDLE
197 );
198
199 wp_register_script(
200 Blocks\Checkbox\Block_Type::HANDLE_CUSTOM,
201 $this->get_url( 'assets/build/custom.options.restrictions.js' ),
202 $script_asset['dependencies'],
203 $script_asset['version'],
204 true
205 );
206
207 // radio
208 $script_asset = require_once $this->get_dir( 'assets/build/radio.asset.php' );
209
210 array_push(
211 $script_asset['dependencies'],
212 BlocksModule::MAIN_SCRIPT_HANDLE
213 );
214
215 wp_register_script(
216 Blocks\Radio\Block_Type::HANDLE,
217 $this->get_url( 'assets/build/radio.js' ),
218 $script_asset['dependencies'],
219 $script_asset['version'],
220 true
221 );
222 wp_register_style(
223 $this->get_handle( 'radio' ),
224 $this->get_url( 'assets/build/radio.css' ),
225 array(),
226 $script_asset['version']
227 );
228
229 // auto-update
230 $auto_update_asset_file = $this->get_dir( 'assets/build/auto-update.asset.php' );
231
232 if ( file_exists( $auto_update_asset_file ) ) {
233 $auto_update_asset = require $auto_update_asset_file;
234
235 if ( is_array( $auto_update_asset ) ) {
236 array_push(
237 $auto_update_asset['dependencies'],
238 BlocksModule::MAIN_SCRIPT_HANDLE
239 );
240
241 wp_register_script(
242 $this->get_handle( 'auto-update' ),
243 $this->get_url( 'assets/build/auto-update.js' ),
244 $auto_update_asset['dependencies'],
245 $auto_update_asset['version'],
246 true
247 );
248
249 wp_add_inline_script(
250 $this->get_handle( 'auto-update' ),
251 'window.JFBOptionFieldAutoUpdate = window.JFBOptionFieldAutoUpdate || {};'
252 . 'window.JFBOptionFieldAutoUpdate.endpoint = '
253 . wp_json_encode( Rest_Api\Generator_Update_Endpoint::rest_url() )
254 . ';',
255 'before'
256 );
257
258 wp_register_style(
259 $this->get_handle( 'auto-update' ),
260 $this->get_url( 'assets/build/auto-update.css' ),
261 array(),
262 $auto_update_asset['version']
263 );
264 }
265 }
266 }
267
268 /**
269 * @param Base|Support_Option_Query_It $block
270 */
271 public function on_set_in_block( Base $block ) {
272 $value_from = ! empty( $block->block_attrs['value_from_key'] )
273 ? $block->block_attrs['value_from_key']
274 : false;
275 $calc_from = ! empty( $block->block_attrs['calculated_value_from_key'] )
276 ? $block->block_attrs['calculated_value_from_key']
277 : false;
278
279 switch ( $block->get_query()->rep_item_id() ) {
280 case 'manual_input':
281 $block->get_query()->set_query(
282 'options',
283 $block->block_attrs['field_options'] ?? array()
284 );
285
286 break;
287 case 'posts':
288 $block->get_query()->set_setting( 'value_from', $value_from );
289 $block->get_query()->set_setting( 'calc_from', $calc_from );
290 $block->get_query()->set_query(
291 'post_type',
292 $block->block_attrs['field_options_post_type'] ?? ''
293 );
294 break;
295 case 'terms':
296 $block->get_query()->set_setting( 'value_from', $value_from );
297 $block->get_query()->set_setting( 'calc_from', $calc_from );
298 $block->get_query()->set_query(
299 'taxonomy',
300 $block->block_attrs['field_options_tax'] ?? ''
301 );
302 break;
303 case 'meta_field':
304 $block->get_query()->set_query(
305 'meta_key',
306 $block->block_attrs['field_options_key'] ?? ''
307 );
308 break;
309 case 'generate':
310 /** @var Legacy_Generator_Query $generator_query */
311 $generator_query = $block->get_query();
312 $generator_query->set_block( $block );
313 $generator_query->set_settings( $block->block_attrs );
314 $generator_query->set_setting( 'value_from', $value_from );
315 $generator_query->set_setting( 'calc_from', $calc_from );
316 break;
317 }
318 }
319
320 /**
321 * Return data for Select, Checkboxes and Radio fields
322 *
323 * @param array $merged
324 *
325 * @return array
326 */
327 protected function get_localize_data( $merged = array() ): array {
328 $options = array(
329 'post_types_list' => Tools::get_post_types_for_options(),
330 'taxonomies_list' => Tools::get_taxonomies_for_js(),
331 'generators_list' => Tools::get_generators_list_for_js(),
332 'generator_schemas' => Tools::get_generator_schemas_for_js(),
333 );
334
335 $active_jet_engine = false !== Tools::get_jet_engine_version();
336
337 $options['glossaries_list'] = $active_jet_engine
338 ? $this->get_glossaries_list()
339 : array();
340
341 $options['listings_list'] = $active_jet_engine
342 ? jet_engine()->listings->get_listings_for_options( 'blocks' )
343 : array();
344
345 return array_merge( $options, $merged );
346 }
347
348 private function get_glossaries_list(): array {
349 return array_map(
350 function ( $glossary ) {
351 return array(
352 'label' => $glossary['name'],
353 'value' => $glossary['id'],
354 );
355 },
356 jet_engine()->glossaries->settings->get()
357 );
358 }
359
360 /**
361 * Get or create REST API controller instance.
362 *
363 * @return Rest_Api\Rest_Api_Controller
364 */
365 private function get_rest(): Rest_Api\Rest_Api_Controller {
366 if ( ! $this->rest ) {
367 $this->rest = new Rest_Api\Rest_Api_Controller();
368 }
369
370 return $this->rest;
371 }
372
373 /**
374 * Register REST API routes.
375 */
376 public function register_rest_routes() {
377 $this->get_rest()->register_routes();
378 }
379 }
380