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