PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.5
JetFormBuilder — Dynamic Blocks Form Builder v1.2.5
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 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / blocks / manager.php
jetformbuilder / includes / blocks Last commit date
modules 4 years ago render 4 years ago types 4 years ago manager.php 4 years ago
manager.php
353 lines
1 <?php
2
3 namespace Jet_Form_Builder\Blocks;
4
5 use Jet_Form_Builder\Blocks\Types;
6
7
8 use Jet_Form_Builder\Compatibility\Jet_Style_Manager;
9 use Jet_Form_Builder\Plugin;
10 use JET_SM\Gutenberg\Block_Manager;
11 use Jet_Form_Builder\Dev_Mode;
12
13 // If this file is called directly, abort.
14
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 /**
20 * Define Manager class
21 */
22 class Manager {
23
24 private $_types = array();
25 public $base_control;
26
27 /**
28 * @var Block_Manager
29 */
30 public $jet_sm__block_manager;
31
32 const FORM_EDITOR_STORAGE = 'form_editor';
33 const OTHERS_STORAGE = 'others';
34 /**
35 * @var bool
36 */
37 private $_registered_scripts = false;
38 private $_added_category = false;
39
40 public function __construct() {
41 global $wp_version;
42
43 add_action( 'init', array( $this, 'init_jet_sm_block_manager' ) );
44 add_action( 'init', array( $this, 'register_block_types' ) );
45
46 add_action(
47 'jet-form-builder/editor-assets/after',
48 array( $this, 'register_block_types_for_form_editor' ),
49 10, 2
50 );
51
52 add_action(
53 'jet-form-builder/other-editor-assets/after',
54 array( $this, 'register_block_types_for_others' ),
55 10, 2
56 );
57
58 add_filter(
59 'jet-form-builder/post-type/args',
60 array( $this, 'add_default_fields_to_form' ),
61 99
62 );
63
64 if ( class_exists( 'WP_Block_Editor_Context' ) ) {
65 add_filter( 'block_categories_all', array( $this, 'add_category' ), 10, 2 );
66 } else {
67 add_filter( 'block_categories', array( $this, 'add_category' ), 10, 2 );
68 }
69
70 add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) );
71 add_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ) );
72 add_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) );
73 }
74
75 public function add_category( $categories, $post ) {
76 $categories[] = array(
77 'slug' => 'jet-form-builder-fields',
78 'title' => __( 'Jet Form Fields', 'jet-form-builder' ),
79 );
80
81 return $categories;
82 }
83
84 public function add_default_fields_to_form( $arguments ) {
85 $hidden_post_id = jet_form_builder()->form::NAMESPACE_FIELDS . 'hidden-field';
86 $submit_post_id = jet_form_builder()->form::NAMESPACE_FIELDS . 'submit-field';
87 $text_field = jet_form_builder()->form::NAMESPACE_FIELDS . 'text-field';
88
89 $arguments['template'] = array(
90 array(
91 $hidden_post_id,
92 array(
93 'name' => 'post_id',
94 'field_value' => 'post_id'
95 )
96 ),
97 array(
98 $text_field,
99 array(
100 'name' => 'text_field',
101 'label' => 'Text'
102 )
103 ),
104 array(
105 $submit_post_id,
106 array( 'label' => __( 'Submit', 'jet-form-builder' ) )
107 )
108 );
109
110 return $arguments;
111 }
112
113 public function init_jet_sm_block_manager() {
114 if ( Jet_Style_Manager::is_activated() ) {
115 $this->jet_sm__block_manager = Block_Manager::get_instance();
116 }
117 }
118
119 /**
120 * Register block types
121 *
122 * @return [type] [description]
123 */
124 public function register_block_types() {
125
126 $types = array(
127 new Types\Form(),
128 new Types\Select_Field(),
129 new Types\Text_Field(),
130 new Types\Hidden_Field(),
131 new Types\Radio_Field(),
132 new Types\Checkbox_Field(),
133 new Types\Number_Field(),
134 new Types\Date_Field(),
135 new Types\Time_Field(),
136 new Types\Calculated_Field(),
137 new Types\Media_Field(),
138 new Types\Wysiwyg_Field(),
139 new Types\Range_Field(),
140 new Types\Heading_Field(),
141 new Types\Textarea_Field(),
142 new Types\Submit_Field(),
143 new Types\Repeater_Field(),
144 new Types\Form_Break_Field(),
145 new Types\Group_Break_Field(),
146 new Types\Conditional_Block(),
147 new Types\Datetime_Field(),
148 );
149
150 foreach ( $types as $type ) {
151 $this->register_block_type( $type );
152 }
153
154 do_action( 'jet-form-builder/blocks/register', $this );
155
156 }
157
158 /**
159 * Register block types for editor
160 *
161 * @param $editor
162 * @param $handle
163 *
164 * @return void [type] [description]
165 */
166 public function register_block_types_for_form_editor( $editor, $handle ) {
167 foreach ( $this->_types[ self::FORM_EDITOR_STORAGE ] as $type ) {
168 $type->block_data( $editor, $handle );
169 }
170 }
171
172 /**
173 * Register block types for editor
174 *
175 * @param $editor
176 * @param $handle
177 *
178 * @return void [type] [description]
179 */
180 public function register_block_types_for_others( $editor, $handle ) {
181 foreach ( $this->_types[ self::OTHERS_STORAGE ] as $type ) {
182 $type->block_data( $editor, $handle );
183 }
184 }
185
186 public function enqueue_frontend_styles() {
187 wp_enqueue_style(
188 'jet-form-builder-frontend',
189 Plugin::instance()->plugin_url( 'assets/css/frontend.css' ),
190 array(),
191 Plugin::instance()->get_version()
192 );
193 }
194
195 /**
196 * Register form JS
197 * @return [type] [description]
198 */
199 public function enqueue_frontend_assets() {
200 $this->register_form_scripts();
201 $this->enqueue_frontend_styles();
202
203 wp_enqueue_script( 'jet-form-builder-frontend-forms' );
204
205 wp_localize_script( 'jet-form-builder-frontend-forms', 'JetFormBuilderSettings', array(
206 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
207 'form_action' => Plugin::instance()->form_handler->hook_key,
208 'devmode' => Dev_Mode\Manager::instance()->active()
209 ) );
210
211 }
212
213 public function register_form_scripts() {
214 if ( $this->_registered_scripts ) {
215 return;
216 }
217 wp_register_script(
218 'jet-form-builder-frontend-forms',
219 Plugin::instance()->plugin_url( 'assets/js/frontend-forms.js' ),
220 array( 'jquery' ),
221 Plugin::instance()->get_version(),
222 true
223 );
224
225 wp_register_script(
226 'jet-form-builder-sortable',
227 Plugin::instance()->plugin_url( 'assets/lib/jquery-sortable/sortable.js' ),
228 array(),
229 Plugin::instance()->get_version(),
230 true
231 );
232
233 wp_register_script(
234 'jet-form-builder-file-upload',
235 Plugin::instance()->plugin_url( 'assets/js/file-upload.js' ),
236 array( 'jet-form-builder-frontend-forms', 'jet-form-builder-sortable' ),
237 Plugin::instance()->get_version(),
238 true
239 );
240 $this->_registered_scripts = true;
241 }
242
243 /**
244 * Returns toolbar controls list from attributes
245 *
246 * @return [type] [description]
247 */
248 public function get_controls_list( $attributes = array(), $context = 'toolbar' ) {
249
250 $result = array();
251
252 foreach ( $attributes as $key => $data ) {
253 if ( ! empty( $data[ $context ] ) ) {
254 $result[] = array(
255 'key' => $key,
256 'type' => $data[ $context ]['type'],
257 'label' => $data[ $context ]['label'],
258 'options' => isset( $data[ $context ]['options'] ) ? $data[ $context ]['options'] : array(),
259 'condition' => isset( $data[ $context ]['condition'] ) ? $data[ $context ]['condition'] : false,
260 // for Submit field name
261 'show' => isset( $data[ $context ]['show'] ) ? $data[ $context ]['show'] : true,
262 // for Date and Time field
263 'help' => isset( $data[ $context ]['help'] ) ? $data[ $context ]['help'] : '',
264 );
265 }
266 }
267
268 return $result;
269 }
270
271 /**
272 * Register new block type
273 *
274 * @param [type] $block_type [description]
275 *
276 * @return [type] [description]
277 */
278 public function register_block_type( $block_type ) {
279 $this->_types[ $block_type->get_storage_name() ][ $block_type->get_name() ] = $block_type;
280 }
281
282 /**
283 * @param string $storage
284 *
285 * @return array
286 */
287 public function get_form_editor_types( $storage = self::FORM_EDITOR_STORAGE ) {
288 return $this->_types[ $storage ];
289 }
290
291 /**
292 * Returns block attributes list
293 */
294 public function get_block_atts( $block = null ) {
295
296 if ( ! $block ) {
297 return array();
298 }
299 $types = $this->get_form_editor_types();
300
301 $type = isset( $types[ $block ] ) ? $types[ $block ] : false;
302
303 if ( ! $type ) {
304 return array();
305 }
306
307 return $type->get_attributes();
308
309 }
310
311
312 public function get_field_by_name( $block_name, $storage = self::FORM_EDITOR_STORAGE ) {
313 $types = $this->get_form_editor_types( $storage );
314 $block = isset( $types[ $block_name ] ) ? $types[ $block_name ] : false;
315
316 if ( ! $block ) {
317 $block_name = explode( Plugin::instance()->form::NAMESPACE_FIELDS, $block_name );
318 $block = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
319 }
320
321 return $block;
322 }
323
324
325 public function get_field_attrs( $block_name, $attributes ) {
326
327 if ( ! $block_name ) {
328 return;
329 }
330 $types = $this->get_form_editor_types();
331 $block_name = explode( 'jet-forms/', $block_name );
332
333 $field = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
334
335 if ( ! $field ) {
336 return;
337 }
338
339 return array_merge( $field->get_default_attributes(), $attributes );
340 }
341
342 public function get_form_class() {
343 return $this->get_field_by_name( 'form-block', self::OTHERS_STORAGE );
344 }
345
346 public function render_callback( $instance ) {
347 return function ( array $attrs, $content = null, $wp_block = null ) use ( $instance ) {
348 return call_user_func( array( clone $instance, 'render_callback_field' ), $attrs, $content, $wp_block );
349 };
350 }
351
352 }
353