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