actions
2 years ago
addons
2 years ago
admin
2 years ago
blocks
2 years ago
classes
2 years ago
db-queries
2 years ago
exceptions
2 years ago
form-messages
2 years ago
form-patterns
2 years ago
form-response
2 years ago
generators
2 years ago
integrations
2 years ago
migrations
2 years ago
presets
2 years ago
request
2 years ago
autoloader.php
2 years ago
file-upload.php
2 years ago
form-break.php
2 years ago
form-handler.php
2 years ago
form-manager.php
2 years ago
functions.php
2 years ago
live-form.php
2 years ago
plugin.php
2 years ago
form-manager.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Generators\Get_From_DB; |
| 8 | use Jet_Form_Builder\Generators\Num_Range; |
| 9 | use Jet_Form_Builder\Generators\Num_Range_Manual; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die(); |
| 14 | } |
| 15 | |
| 16 | class Form_Manager { |
| 17 | |
| 18 | public $generators = false; |
| 19 | public $builder; |
| 20 | |
| 21 | const NAMESPACE_FIELDS = 'jet-forms/'; |
| 22 | |
| 23 | /** |
| 24 | * Returns all instances of options generators classes |
| 25 | * |
| 26 | * @return [type] [description] |
| 27 | */ |
| 28 | public function get_options_generators() { |
| 29 | |
| 30 | if ( false === $this->generators ) { |
| 31 | |
| 32 | $instances = apply_filters( |
| 33 | 'jet-form-builder/forms/options-generators', |
| 34 | array( |
| 35 | new Num_Range(), |
| 36 | new Num_Range_Manual(), |
| 37 | new Get_From_DB(), |
| 38 | ), |
| 39 | $this |
| 40 | ); |
| 41 | |
| 42 | $this->generators = array(); |
| 43 | |
| 44 | foreach ( $instances as $instance ) { |
| 45 | if ( $instance->can_generate() ) { |
| 46 | $this->generators[ $instance->get_id() ] = $instance; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return $this->generators; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Returns generators list |
| 57 | * |
| 58 | * @return [type] [description] |
| 59 | */ |
| 60 | public function get_generators_list() { |
| 61 | |
| 62 | $generators = $this->get_options_generators(); |
| 63 | $result = array( |
| 64 | 0 => __( 'Select function...', 'jet-form-builder' ), |
| 65 | ); |
| 66 | |
| 67 | foreach ( $generators as $id => $generator ) { |
| 68 | $result[ $id ] = $generator->get_name(); |
| 69 | } |
| 70 | |
| 71 | return $result; |
| 72 | } |
| 73 | |
| 74 | public function get_only_form_fields( $form_id ): array { |
| 75 | $content = Block_Helper::get_blocks_by_post( $form_id ); |
| 76 | |
| 77 | return Block_Helper::filter_blocks_by_namespace( $content ); |
| 78 | } |
| 79 | |
| 80 | public function get_field_by_name( $form_id, $field_name, $blocks = array() ): array { |
| 81 | if ( ! $blocks ) { |
| 82 | $blocks = Block_Helper::get_blocks_by_post( $form_id ); |
| 83 | } |
| 84 | |
| 85 | return Block_Helper::find_block_by_name( $field_name, $blocks ); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 |