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