PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.4
JetFormBuilder — Dynamic Blocks Form Builder v1.2.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 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
346 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
39 public function __construct() {
40 add_action( 'init', array( $this, 'init_jet_sm_block_manager' ) );
41 add_action( 'init', array( $this, 'register_block_types' ) );
42
43 add_action(
44 'jet-form-builder/editor-assets/after',
45 array( $this, 'register_block_types_for_form_editor' ),
46 10, 2
47 );
48
49 add_action(
50 'jet-form-builder/other-editor-assets/after',
51 array( $this, 'register_block_types_for_others' ),
52 10, 2
53 );
54
55 add_filter(
56 'jet-form-builder/post-type/args',
57 array( $this, 'add_default_fields_to_form' ),
58 99
59 );
60
61 add_filter( 'block_categories', array( $this, 'add_category' ), 10, 2 );
62
63 add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) );
64 add_action( 'wp_enqueue_scripts', array( $this, 'register_form_scripts' ) );
65 add_action( 'enqueue_block_editor_assets', array( $this, 'register_form_scripts' ) );
66 }
67
68 public function add_category( $categories, $post ) {
69 $categories[] = array(
70 'slug' => 'jet-form-builder-fields',
71 'title' => __( 'Jet Form Fields', 'jet-form-builder' ),
72 );
73
74 return $categories;
75 }
76
77 public function add_default_fields_to_form( $arguments ) {
78 $hidden_post_id = jet_form_builder()->form::NAMESPACE_FIELDS . 'hidden-field';
79 $submit_post_id = jet_form_builder()->form::NAMESPACE_FIELDS . 'submit-field';
80 $text_field = jet_form_builder()->form::NAMESPACE_FIELDS . 'text-field';
81
82 $arguments['template'] = array(
83 array(
84 $hidden_post_id,
85 array(
86 'name' => 'post_id',
87 'field_value' => 'post_id'
88 )
89 ),
90 array(
91 $text_field,
92 array(
93 'name' => 'text_field',
94 'label' => 'Text'
95 )
96 ),
97 array(
98 $submit_post_id,
99 array( 'label' => __( 'Submit', 'jet-form-builder' ) )
100 )
101 );
102
103 return $arguments;
104 }
105
106 public function init_jet_sm_block_manager() {
107 if ( Jet_Style_Manager::is_activated() ) {
108 $this->jet_sm__block_manager = Block_Manager::get_instance();
109 }
110 }
111
112 /**
113 * Register block types
114 *
115 * @return [type] [description]
116 */
117 public function register_block_types() {
118
119 $types = array(
120 new Types\Form(),
121 new Types\Select_Field(),
122 new Types\Text_Field(),
123 new Types\Hidden_Field(),
124 new Types\Radio_Field(),
125 new Types\Checkbox_Field(),
126 new Types\Number_Field(),
127 new Types\Date_Field(),
128 new Types\Time_Field(),
129 new Types\Calculated_Field(),
130 new Types\Media_Field(),
131 new Types\Wysiwyg_Field(),
132 new Types\Range_Field(),
133 new Types\Heading_Field(),
134 new Types\Textarea_Field(),
135 new Types\Submit_Field(),
136 new Types\Repeater_Field(),
137 new Types\Form_Break_Field(),
138 new Types\Group_Break_Field(),
139 new Types\Conditional_Block(),
140 new Types\Datetime_Field(),
141 );
142
143 foreach ( $types as $type ) {
144 $this->register_block_type( $type );
145 }
146
147 do_action( 'jet-form-builder/blocks/register', $this );
148
149 }
150
151 /**
152 * Register block types for editor
153 *
154 * @param $editor
155 * @param $handle
156 *
157 * @return void [type] [description]
158 */
159 public function register_block_types_for_form_editor( $editor, $handle ) {
160 foreach ( $this->_types[ self::FORM_EDITOR_STORAGE ] as $type ) {
161 $type->block_data( $editor, $handle );
162 }
163 }
164
165 /**
166 * Register block types for editor
167 *
168 * @param $editor
169 * @param $handle
170 *
171 * @return void [type] [description]
172 */
173 public function register_block_types_for_others( $editor, $handle ) {
174 foreach ( $this->_types[ self::OTHERS_STORAGE ] as $type ) {
175 $type->block_data( $editor, $handle );
176 }
177 }
178
179 public function enqueue_frontend_styles() {
180 wp_enqueue_style(
181 'jet-form-builder-frontend',
182 Plugin::instance()->plugin_url( 'assets/css/frontend.css' ),
183 array(),
184 Plugin::instance()->get_version()
185 );
186 }
187
188 /**
189 * Register form JS
190 * @return [type] [description]
191 */
192 public function enqueue_frontend_assets() {
193 $this->register_form_scripts();
194 $this->enqueue_frontend_styles();
195
196 wp_enqueue_script( 'jet-form-builder-frontend-forms' );
197
198 wp_localize_script( 'jet-form-builder-frontend-forms', 'JetFormBuilderSettings', array(
199 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
200 'form_action' => Plugin::instance()->form_handler->hook_key,
201 'devmode' => Dev_Mode\Manager::instance()->active()
202 ) );
203
204 }
205
206 public function register_form_scripts() {
207 if ( $this->_registered_scripts ) {
208 return;
209 }
210 wp_register_script(
211 'jet-form-builder-frontend-forms',
212 Plugin::instance()->plugin_url( 'assets/js/frontend-forms.js' ),
213 array( 'jquery' ),
214 Plugin::instance()->get_version(),
215 true
216 );
217
218 wp_register_script(
219 'jet-form-builder-sortable',
220 Plugin::instance()->plugin_url( 'assets/lib/jquery-sortable/sortable.js' ),
221 array(),
222 Plugin::instance()->get_version(),
223 true
224 );
225
226 wp_register_script(
227 'jet-form-builder-file-upload',
228 Plugin::instance()->plugin_url( 'assets/js/file-upload.js' ),
229 array( 'jet-form-builder-frontend-forms', 'jet-form-builder-sortable' ),
230 Plugin::instance()->get_version(),
231 true
232 );
233 $this->_registered_scripts = true;
234 }
235
236 /**
237 * Returns toolbar controls list from attributes
238 *
239 * @return [type] [description]
240 */
241 public function get_controls_list( $attributes = array(), $context = 'toolbar' ) {
242
243 $result = array();
244
245 foreach ( $attributes as $key => $data ) {
246 if ( ! empty( $data[ $context ] ) ) {
247 $result[] = array(
248 'key' => $key,
249 'type' => $data[ $context ]['type'],
250 'label' => $data[ $context ]['label'],
251 'options' => isset( $data[ $context ]['options'] ) ? $data[ $context ]['options'] : array(),
252 'condition' => isset( $data[ $context ]['condition'] ) ? $data[ $context ]['condition'] : false,
253 // for Submit field name
254 'show' => isset( $data[ $context ]['show'] ) ? $data[ $context ]['show'] : true,
255 // for Date and Time field
256 'help' => isset( $data[ $context ]['help'] ) ? $data[ $context ]['help'] : '',
257 );
258 }
259 }
260
261 return $result;
262 }
263
264 /**
265 * Register new block type
266 *
267 * @param [type] $block_type [description]
268 *
269 * @return [type] [description]
270 */
271 public function register_block_type( $block_type ) {
272 $this->_types[ $block_type->get_storage_name() ][ $block_type->get_name() ] = $block_type;
273 }
274
275 /**
276 * @param string $storage
277 *
278 * @return array
279 */
280 public function get_form_editor_types( $storage = self::FORM_EDITOR_STORAGE ) {
281 return $this->_types[ $storage ];
282 }
283
284 /**
285 * Returns block attributes list
286 */
287 public function get_block_atts( $block = null ) {
288
289 if ( ! $block ) {
290 return array();
291 }
292 $types = $this->get_form_editor_types();
293
294 $type = isset( $types[ $block ] ) ? $types[ $block ] : false;
295
296 if ( ! $type ) {
297 return array();
298 }
299
300 return $type->get_attributes();
301
302 }
303
304
305 public function get_field_by_name( $block_name, $storage = self::FORM_EDITOR_STORAGE ) {
306 $types = $this->get_form_editor_types( $storage );
307 $block = isset( $types[ $block_name ] ) ? $types[ $block_name ] : false;
308
309 if ( ! $block ) {
310 $block_name = explode( Plugin::instance()->form::NAMESPACE_FIELDS, $block_name );
311 $block = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
312 }
313
314 return $block;
315 }
316
317
318 public function get_field_attrs( $block_name, $attributes ) {
319
320 if ( ! $block_name ) {
321 return;
322 }
323 $types = $this->get_form_editor_types();
324 $block_name = explode( 'jet-forms/', $block_name );
325
326 $field = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
327
328 if ( ! $field ) {
329 return;
330 }
331
332 return array_merge( $field->get_default_attributes(), $attributes );
333 }
334
335 public function get_form_class() {
336 return $this->get_field_by_name( 'form-block', self::OTHERS_STORAGE );
337 }
338
339 public function render_callback( $instance ) {
340 return function ( array $attrs, $content = null, $wp_block = null ) use ( $instance ) {
341 return call_user_func( array( clone $instance, 'render_callback_field' ), $attrs, $content, $wp_block );
342 };
343 }
344
345 }
346