PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.3.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.3.1
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 / includes / blocks / module.php
module.php
360 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks;
5
6 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
7 use Jet_Form_Builder\Classes\Compatibility;
8 use Jet_Form_Builder\Classes\Http\Http_Tools;
9 use Jet_Form_Builder\Exceptions\Repository_Exception;
10 use Jet_Form_Builder\Plugin;
11 use JET_SM\Gutenberg\Block_Manager;
12 use JFB_Components\Module\Base_Module_It;
13
14 // If this file is called directly, abort.
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 final class Module implements Base_Module_It {
20
21 const MAIN_SCRIPT_HANDLE = 'jet-form-builder-frontend-forms';
22 const LISTING_OPTIONS_HANDLE = 'jet-form-builder-listing-options';
23
24 // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
25 private $_builder_blocks_repository;
26 private $_default_blocks_repository;
27
28 // phpcs:enable PSR2.Classes.PropertyDeclaration.Underscore
29
30 public function rep_item_id() {
31 return 'blocks';
32 }
33
34 public function condition(): bool {
35 return true;
36 }
37
38 public function init_hooks() {
39 add_action( 'init', array( $this, 'init_jet_sm_block_manager' ) );
40 add_action( 'init', array( $this, 'register_block_types' ) );
41
42 add_action(
43 'jet-form-builder/editor-assets/after',
44 array( $this, 'register_block_types_for_form_editor' ),
45 10,
46 2
47 );
48
49 add_action(
50 'jet-form-builder/other-editor-assets/after',
51 array( $this, 'register_block_types_for_others' ),
52 10,
53 2
54 );
55
56 if ( class_exists( 'WP_Block_Editor_Context' ) ) {
57 add_filter( 'block_categories_all', array( $this, 'add_categories' ), 999, 2 );
58 } else {
59 add_filter( 'block_categories', array( $this, 'add_categories' ), 999, 2 );
60 }
61
62 add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) );
63 add_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ), 8 );
64 add_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) );
65
66 /**
67 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
68 */
69 add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_form_scripts' ) );
70 }
71
72 public function remove_hooks() {
73 remove_action( 'init', array( $this, 'init_jet_sm_block_manager' ) );
74 remove_action( 'init', array( $this, 'register_block_types' ) );
75
76 remove_action(
77 'jet-form-builder/editor-assets/after',
78 array( $this, 'register_block_types_for_form_editor' )
79 );
80
81 remove_action(
82 'jet-form-builder/other-editor-assets/after',
83 array( $this, 'register_block_types_for_others' )
84 );
85
86 if ( class_exists( 'WP_Block_Editor_Context' ) ) {
87 remove_filter( 'block_categories_all', array( $this, 'add_categories' ), 999 );
88 } else {
89 remove_filter( 'block_categories', array( $this, 'add_categories' ), 999 );
90 }
91
92 remove_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) );
93 remove_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ), 8 );
94 remove_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) );
95
96 /**
97 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
98 */
99 remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_form_scripts' ) );
100 }
101
102 /**
103 * Register block types
104 *
105 * @return void
106 */
107 public function register_block_types() {
108 $this->default_repository()->rep_install();
109 $this->builder_repository()->rep_install();
110
111 do_action( 'jet-form-builder/blocks/register', $this );
112 }
113
114 /**
115 * Register new block type
116 *
117 * @param Types\Base $block_type
118 *
119 * @return void
120 */
121 public function register_block_type( Types\Base $block_type ) {
122 $this->builder_repository()->rep_install_item_soft( $block_type );
123 }
124
125 /**
126 * Register new block type
127 *
128 * @param Types\Base $block_type
129 *
130 * @return void
131 */
132 public function register_default_block_type( Types\Base $block_type ) {
133 $this->default_repository()->rep_install_item_soft( $block_type );
134 }
135
136
137 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
138 public function add_categories( $categories, $post ): array {
139 return array_merge(
140 array(
141 array(
142 'slug' => 'jet-form-builder-fields',
143 'title' => __( 'Jet Form Fields', 'jet-form-builder' ),
144 ),
145 ),
146 array(
147 array(
148 'slug' => 'jet-form-builder-elements',
149 'title' => __( 'Jet Form Elements', 'jet-form-builder' ),
150 ),
151 ),
152 $categories
153 );
154 }
155
156 /**
157 * Register block types for editor
158 *
159 * @param $editor
160 * @param $handle
161 *
162 * @return void [type] [description]
163 */
164 public function register_block_types_for_form_editor( $editor, $handle ) {
165 foreach ( $this->builder_repository()->rep_get_items() as $type ) {
166 $type->block_data( $editor, $handle );
167 }
168 }
169
170 /**
171 * Register block types for editor
172 *
173 * @param $editor
174 * @param $handle
175 *
176 * @return void [type] [description]
177 */
178 public function register_block_types_for_others( $editor, $handle ) {
179 foreach ( $this->default_repository()->rep_get_items() as $type ) {
180 $type->block_data( $editor, $handle );
181 }
182 }
183
184 public function enqueue_frontend_styles() {
185 wp_register_style(
186 'jet-form-builder-frontend',
187 Plugin::instance()->plugin_url( 'assets/build/frontend/main.css' ),
188 array(),
189 Plugin::instance()->get_version()
190 );
191 }
192
193 /**
194 * Register form JS
195 *
196 * @return void
197 */
198 public function enqueue_frontend_assets() {
199 $this->register_form_scripts();
200
201 wp_enqueue_script( self::MAIN_SCRIPT_HANDLE );
202 }
203
204 public function register_form_scripts() {
205 /** @var \JFB_Modules\Jet_Plugins\Module $jet_plugins */
206 /** @noinspection PhpUnhandledExceptionInspection */
207 $jet_plugins = jet_form_builder()->module( 'jet-plugins' );
208 $jet_plugins->register_scripts();
209
210 $script_asset = require_once jet_form_builder()->plugin_dir( 'assets/build/frontend/main.asset.php' );
211
212 if ( true === $script_asset ) {
213 return;
214 }
215
216 $script_asset['dependencies'][] = $jet_plugins::HANDLE;
217
218 wp_register_script(
219 self::MAIN_SCRIPT_HANDLE,
220 Plugin::instance()->plugin_url( 'assets/build/frontend/main.js' ),
221 $script_asset['dependencies'],
222 $script_asset['version'],
223 true
224 );
225
226 $options = Tab_Handler_Manager::get_options( 'options-tab' );
227
228 wp_localize_script(
229 self::MAIN_SCRIPT_HANDLE,
230 'JetFormBuilderSettings',
231 apply_filters(
232 'jet-form-builder/frontend-settings',
233 array_merge(
234 array(
235 'ajaxurl' => Http_Tools::get_form_action_url(
236 array( 'method' => 'ajax' )
237 ),
238 'adminajaxurl' => admin_url( 'admin-ajax.php' ),
239 'devmode' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
240 'replaceAttrs' => array(
241 'href',
242 'src',
243 'alt',
244 'title',
245 ),
246 ),
247 $options
248 )
249 )
250 );
251 }
252
253 /**
254 * Returns toolbar controls list from attributes
255 *
256 * @return [type] [description]
257 */
258 public function get_controls_list( $attributes = array(), $context = 'toolbar' ): array {
259
260 $result = array();
261
262 foreach ( $attributes as $key => $data ) {
263 if ( ! empty( $data[ $context ] ) ) {
264 $result[] = array(
265 'key' => $key,
266 'type' => $data[ $context ]['type'],
267 'label' => $data[ $context ]['label'],
268 'options' => $data[ $context ]['options'] ?? array(),
269 'condition' => $data[ $context ]['condition'] ?? false,
270 // for Submit field name.
271 'show' => $data[ $context ]['show'] ?? true,
272 // for Date and Time field.
273 'help' => $data[ $context ]['help'] ?? '',
274 );
275 }
276 }
277
278 return $result;
279 }
280
281 /**
282 * @param $block_name
283 * @param $attributes
284 *
285 * @return array
286 */
287 public function get_field_attrs( $block_name, $attributes ): array {
288 if ( ! $block_name ) {
289 return array();
290 }
291 $block_id = Block_Helper::delete_namespace( $block_name );
292
293 try {
294 $field = $this->builder_repository()->rep_get_item( $block_id );
295 } catch ( Repository_Exception $exception ) {
296 return array();
297 }
298
299 return array_merge( $field->get_default_attributes(), $attributes );
300 }
301
302
303 /**
304 * @param $block_name
305 *
306 * @return Types\Base|bool
307 */
308 public function get_field_by_name( $block_name ) {
309 $block_id = Block_Helper::delete_namespace( $block_name );
310
311 try {
312 return $this->builder_repository()->rep_clone_item( $block_id );
313 } catch ( Repository_Exception $exception ) {
314 return false;
315 }
316 }
317
318
319 /**
320 * @return Types\Form|bool
321 */
322 public function get_form_class() {
323 try {
324 return $this->default_repository()->rep_get_item( 'form-block' );
325 } catch ( Repository_Exception $exception ) {
326 return false;
327 }
328 }
329
330 public function render_callback( $instance ) {
331 return static function ( array $attrs, $content = null, $wp_block = null ) use ( $instance ) {
332 return call_user_func( array( clone $instance, 'render_callback_field' ), $attrs, $content, $wp_block );
333 };
334 }
335
336 public function builder_repository(): Form_Builder_Blocks_Repository {
337 if ( ! $this->_builder_blocks_repository ) {
338 $this->_builder_blocks_repository = new Form_Builder_Blocks_Repository();
339 }
340
341 return $this->_builder_blocks_repository;
342 }
343
344 public function default_repository(): Default_Blocks_Repository {
345 if ( ! $this->_default_blocks_repository ) {
346 $this->_default_blocks_repository = new Default_Blocks_Repository();
347 }
348
349 return $this->_default_blocks_repository;
350 }
351
352 public function init_jet_sm_block_manager() {
353 if ( ! Compatibility::has_jet_sm() ) {
354 return;
355 }
356
357 Block_Manager::get_instance();
358 }
359 }
360