PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.2
JetFormBuilder — Dynamic Blocks Form Builder v1.3.2
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 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
333 lines
1 <?php
2
3 namespace Jet_Form_Builder\Blocks;
4
5 use Jet_Form_Builder\Blocks\Types;
6 use Jet_Form_Builder\Compatibility\Jet_Style_Manager;
7 use Jet_Form_Builder\Dev_Mode;
8 use Jet_Form_Builder\Plugin;
9 use JET_SM\Gutenberg\Block_Manager;
10
11
12 // If this file is called directly, abort.
13
14 if ( ! defined( 'WPINC' ) ) {
15 die;
16 }
17
18 /**
19 * Define Manager class
20 */
21 class Manager {
22
23 private $_types = array();
24 public $base_control;
25
26 const FORM_EDITOR_STORAGE = 'form_editor';
27 const OTHERS_STORAGE = 'others';
28 /**
29 * @var bool
30 */
31 private $_registered_scripts = false;
32
33 public function __construct() {
34 add_action( 'init', array( $this, 'init_jet_sm_block_manager' ) );
35 add_action( 'init', array( $this, 'register_block_types' ) );
36
37 add_action(
38 'jet-form-builder/editor-assets/after',
39 array( $this, 'register_block_types_for_form_editor' ),
40 10,
41 2
42 );
43
44 add_action(
45 'jet-form-builder/other-editor-assets/after',
46 array( $this, 'register_block_types_for_others' ),
47 10,
48 2
49 );
50
51 add_filter(
52 'jet-form-builder/post-type/args',
53 array( $this, 'add_default_fields_to_form' ),
54 99
55 );
56
57 if ( class_exists( 'WP_Block_Editor_Context' ) ) {
58 add_filter( 'block_categories_all', array( $this, 'add_category' ), 10, 2 );
59 } else {
60 add_filter( 'block_categories', array( $this, 'add_category' ), 10, 2 );
61 }
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 Block_Manager::get_instance();
109 }
110 }
111
112 /**
113 * Register block types
114 *
115 * @return void
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 new Types\Color_Picker_Field(),
142 );
143
144 foreach ( $types as $type ) {
145 $this->register_block_type( $type );
146 }
147
148 do_action( 'jet-form-builder/blocks/register', $this );
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_register_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 *
191 * @return void
192 */
193 public function enqueue_frontend_assets() {
194 $this->register_form_scripts();
195 $this->enqueue_frontend_styles();
196
197 wp_enqueue_script( 'jet-form-builder-frontend-forms' );
198
199 wp_localize_script(
200 'jet-form-builder-frontend-forms',
201 'JetFormBuilderSettings',
202 apply_filters(
203 'jet-form-builder/frontend-settings',
204 array(
205 'ajaxurl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
206 'form_action' => Plugin::instance()->form_handler->hook_key,
207 'devmode' => Dev_Mode\Manager::instance()->active(),
208 'scrollOffset' => - 50,
209 )
210 )
211 );
212 }
213
214 public function register_form_scripts() {
215 if ( $this->_registered_scripts ) {
216 return;
217 }
218 wp_register_script(
219 'jet-form-builder-frontend-forms',
220 Plugin::instance()->plugin_url( 'assets/js/frontend-forms.js' ),
221 array( 'jquery' ),
222 Plugin::instance()->get_version(),
223 true
224 );
225
226 wp_register_script(
227 'jet-form-builder-sortable',
228 Plugin::instance()->plugin_url( 'assets/lib/jquery-sortable/sortable.js' ),
229 array(),
230 Plugin::instance()->get_version(),
231 true
232 );
233
234 wp_register_script(
235 'jet-form-builder-file-upload',
236 Plugin::instance()->plugin_url( 'assets/js/file-upload.js' ),
237 array( 'jet-form-builder-frontend-forms', 'jet-form-builder-sortable' ),
238 Plugin::instance()->get_version(),
239 true
240 );
241 $this->_registered_scripts = true;
242 }
243
244 /**
245 * Returns toolbar controls list from attributes
246 *
247 * @return [type] [description]
248 */
249 public function get_controls_list( $attributes = array(), $context = 'toolbar' ) {
250
251 $result = array();
252
253 foreach ( $attributes as $key => $data ) {
254 if ( ! empty( $data[ $context ] ) ) {
255 $result[] = array(
256 'key' => $key,
257 'type' => $data[ $context ]['type'],
258 'label' => $data[ $context ]['label'],
259 'options' => $data[ $context ]['options'] ?? array(),
260 'condition' => $data[ $context ]['condition'] ?? false,
261 // for Submit field name.
262 'show' => $data[ $context ]['show'] ?? true,
263 // for Date and Time field.
264 'help' => $data[ $context ]['help'] ?? '',
265 );
266 }
267 }
268
269 return $result;
270 }
271
272 /**
273 * Register new block type
274 *
275 * @param Types\Base $block_type
276 *
277 * @return void
278 */
279 public function register_block_type( Types\Base $block_type ) {
280 $this->_types[ $block_type->get_storage_name() ][ $block_type->get_name() ] = $block_type;
281 }
282
283 /**
284 * @param string $storage
285 *
286 * @return array
287 */
288 public function get_form_editor_types( $storage = self::FORM_EDITOR_STORAGE ) {
289 return $this->_types[ $storage ];
290 }
291
292
293 public function get_field_by_name( $block_name, $storage = self::FORM_EDITOR_STORAGE ) {
294 $types = $this->get_form_editor_types( $storage );
295 $block = isset( $types[ $block_name ] ) ? clone $types[ $block_name ] : false;
296
297 if ( ! $block ) {
298 $block_name = explode( Plugin::instance()->form::NAMESPACE_FIELDS, $block_name );
299 $block = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
300 }
301
302 return $block;
303 }
304
305
306 public function get_field_attrs( $block_name, $attributes ) {
307 if ( ! $block_name ) {
308 return array();
309 }
310 $types = $this->get_form_editor_types();
311 $block_name = explode( 'jet-forms/', $block_name );
312
313 $field = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false;
314
315 if ( ! $field ) {
316 return array();
317 }
318
319 return array_merge( $field->get_default_attributes(), $attributes );
320 }
321
322 public function get_form_class() {
323 return $this->get_field_by_name( 'form-block', self::OTHERS_STORAGE );
324 }
325
326 public function render_callback( $instance ) {
327 return function ( array $attrs, $content = null, $wp_block = null ) use ( $instance ) {
328 return call_user_func( array( clone $instance, 'render_callback_field' ), $attrs, $content, $wp_block );
329 };
330 }
331
332 }
333