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