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