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