manager.php
331 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 | 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 | foreach ( $this->_types[ self::FORM_EDITOR_STORAGE ] as $type ) { |
| 156 | $type->block_data( $editor, $handle ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Register block types for editor |
| 162 | * |
| 163 | * @param $editor |
| 164 | * @param $handle |
| 165 | * |
| 166 | * @return void [type] [description] |
| 167 | */ |
| 168 | public function register_block_types_for_others( $editor, $handle ) { |
| 169 | foreach ( $this->_types[ self::OTHERS_STORAGE ] as $type ) { |
| 170 | $type->block_data( $editor, $handle ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public function enqueue_frontend_styles() { |
| 175 | wp_enqueue_style( |
| 176 | 'jet-form-builder-frontend', |
| 177 | Plugin::instance()->plugin_url( 'assets/css/frontend.css' ), |
| 178 | array(), |
| 179 | Plugin::instance()->get_version() |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Register form JS |
| 185 | * @return [type] [description] |
| 186 | */ |
| 187 | public function enqueue_frontend_assets() { |
| 188 | |
| 189 | $this->enqueue_frontend_styles(); |
| 190 | |
| 191 | wp_enqueue_script( 'jet-form-builder-frontend-forms' ); |
| 192 | |
| 193 | wp_localize_script( 'jet-form-builder-frontend-forms', 'JetFormBuilderSettings', array( |
| 194 | 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ), |
| 195 | 'form_action' => Plugin::instance()->form_handler->hook_key, |
| 196 | 'devmode' => Dev_Mode\Manager::instance()->active() |
| 197 | ) ); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | public function register_form_scripts() { |
| 202 | wp_register_script( |
| 203 | 'jet-form-builder-frontend-forms', |
| 204 | Plugin::instance()->plugin_url( 'assets/js/frontend-forms.js' ), |
| 205 | array( 'jquery' ), |
| 206 | Plugin::instance()->get_version(), |
| 207 | true |
| 208 | ); |
| 209 | |
| 210 | wp_register_script( |
| 211 | 'jet-form-builder-sortable', |
| 212 | Plugin::instance()->plugin_url( 'assets/lib/jquery-sortable/sortable.js' ), |
| 213 | array(), |
| 214 | Plugin::instance()->get_version(), |
| 215 | true |
| 216 | ); |
| 217 | |
| 218 | wp_register_script( |
| 219 | 'jet-form-builder-file-upload', |
| 220 | Plugin::instance()->plugin_url( 'assets/js/file-upload.js' ), |
| 221 | array( 'jet-form-builder-frontend-forms', 'jet-form-builder-sortable' ), |
| 222 | Plugin::instance()->get_version(), |
| 223 | true |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns toolbar controls list from attributes |
| 229 | * |
| 230 | * @return [type] [description] |
| 231 | */ |
| 232 | public function get_controls_list( $attributes = array(), $context = 'toolbar' ) { |
| 233 | |
| 234 | $result = array(); |
| 235 | |
| 236 | foreach ( $attributes as $key => $data ) { |
| 237 | if ( ! empty( $data[ $context ] ) ) { |
| 238 | $result[] = array( |
| 239 | 'key' => $key, |
| 240 | 'type' => $data[ $context ]['type'], |
| 241 | 'label' => $data[ $context ]['label'], |
| 242 | 'options' => isset( $data[ $context ]['options'] ) ? $data[ $context ]['options'] : array(), |
| 243 | 'condition' => isset( $data[ $context ]['condition'] ) ? $data[ $context ]['condition'] : false, |
| 244 | // for Submit field name |
| 245 | 'show' => isset( $data[ $context ]['show'] ) ? $data[ $context ]['show'] : true, |
| 246 | // for Date and Time field |
| 247 | 'help' => isset( $data[ $context ]['help'] ) ? $data[ $context ]['help'] : '', |
| 248 | ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return $result; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Register new block type |
| 257 | * |
| 258 | * @param [type] $block_type [description] |
| 259 | * |
| 260 | * @return [type] [description] |
| 261 | */ |
| 262 | public function register_block_type( $block_type ) { |
| 263 | $this->_types[ $block_type->get_storage_name() ][ $block_type->get_name() ] = $block_type; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @param string $storage |
| 268 | * |
| 269 | * @return array |
| 270 | */ |
| 271 | public function get_form_editor_types( $storage = self::FORM_EDITOR_STORAGE ) { |
| 272 | return $this->_types[ $storage ]; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns block attributes list |
| 277 | */ |
| 278 | public function get_block_atts( $block = null ) { |
| 279 | |
| 280 | if ( ! $block ) { |
| 281 | return array(); |
| 282 | } |
| 283 | $types = $this->get_form_editor_types(); |
| 284 | |
| 285 | $type = isset( $types[ $block ] ) ? $types[ $block ] : false; |
| 286 | |
| 287 | if ( ! $type ) { |
| 288 | return array(); |
| 289 | } |
| 290 | |
| 291 | return $type->get_attributes(); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | |
| 296 | public function get_field_by_name( $block_name, $storage = self::FORM_EDITOR_STORAGE ) { |
| 297 | $types = $this->get_form_editor_types( $storage ); |
| 298 | $block = isset( $types[ $block_name ] ) ? $types[ $block_name ] : false; |
| 299 | |
| 300 | if ( ! $block ) { |
| 301 | $block_name = explode( Plugin::instance()->form::NAMESPACE_FIELDS, $block_name ); |
| 302 | $block = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false; |
| 303 | } |
| 304 | |
| 305 | return $block; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | public function get_field_attrs( $block_name, $attributes ) { |
| 310 | |
| 311 | if ( ! $block_name ) { |
| 312 | return; |
| 313 | } |
| 314 | $types = $this->get_form_editor_types(); |
| 315 | $block_name = explode( 'jet-forms/', $block_name ); |
| 316 | |
| 317 | $field = isset( $types[ $block_name[1] ] ) ? $types[ $block_name[1] ] : false; |
| 318 | |
| 319 | if ( ! $field ) { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | return array_merge( $field->get_default_attributes(), $attributes ); |
| 324 | } |
| 325 | |
| 326 | public function get_form_class() { |
| 327 | return $this->get_field_by_name( 'form-block', self::OTHERS_STORAGE ); |
| 328 | } |
| 329 | |
| 330 | } |
| 331 |