actions
5 years ago
admin
5 years ago
blocks
5 years ago
classes
5 years ago
compatibility
5 years ago
dev-mode
5 years ago
exceptions
5 years ago
form-actions
5 years ago
form-messages
5 years ago
form-patterns
5 years ago
form-response
5 years ago
gateways
5 years ago
generators
5 years ago
integrations
5 years ago
license
5 years ago
presets
5 years ago
request
5 years ago
shortcodes
5 years ago
widgets
5 years ago
autoloader.php
5 years ago
file-upload.php
5 years ago
form-handler.php
5 years ago
form-manager.php
5 years ago
live-form.php
5 years ago
plugin.php
5 years ago
post-type.php
5 years ago
form-manager.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder; |
| 5 | |
| 6 | use Jet_Form_Builder\Gateways\Gateway_Manager; |
| 7 | use Jet_Form_Builder\Generators\Get_From_DB; |
| 8 | use Jet_Form_Builder\Generators\Get_From_Field; |
| 9 | use Jet_Form_Builder\Generators\Num_Range; |
| 10 | use Jet_Form_Builder\Shortcodes\Manager; |
| 11 | |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die(); |
| 16 | } |
| 17 | |
| 18 | class Form_Manager { |
| 19 | public $generators = false; |
| 20 | public $builder; |
| 21 | private $result_fields = array(); |
| 22 | |
| 23 | const NAMESPACE_FIELDS = 'jet-forms/'; |
| 24 | |
| 25 | public function __construct() { |
| 26 | if ( Plugin::instance()->allow_gateways ) { |
| 27 | Gateway_Manager::instance(); |
| 28 | } |
| 29 | |
| 30 | Manager::instance(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns all instances of options generators classes |
| 35 | * |
| 36 | * @return [type] [description] |
| 37 | */ |
| 38 | public function get_options_generators() { |
| 39 | |
| 40 | if ( false === $this->generators ) { |
| 41 | |
| 42 | $instances = array( |
| 43 | new Num_Range(), |
| 44 | new Get_From_DB(), |
| 45 | new Get_From_Field(), |
| 46 | ); |
| 47 | |
| 48 | $instances = apply_filters( 'jet-form-builder/forms/options-generators', $instances, $this ); |
| 49 | |
| 50 | foreach ( $instances as $instance ) { |
| 51 | if ( $instance->can_generate() ) { |
| 52 | $this->generators[ $instance->get_id() ] = $instance; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | return $this->generators; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns form fields, |
| 63 | * parsed from post_content |
| 64 | * |
| 65 | * @param $content |
| 66 | * |
| 67 | * @return array[] |
| 68 | */ |
| 69 | public function get_fields( $content ) { |
| 70 | return parse_blocks( $content ); |
| 71 | } |
| 72 | |
| 73 | public function is_not_field( $block_name ) { |
| 74 | return ( |
| 75 | stripos( |
| 76 | $block_name, |
| 77 | self::NAMESPACE_FIELDS |
| 78 | ) === false |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | public function is_field( $block_name, $needle ) { |
| 83 | return stristr( $block_name, $needle ); |
| 84 | } |
| 85 | |
| 86 | public function get_form_blocks( $form_id ) { |
| 87 | return $this->get_fields( get_post( $form_id )->post_content ); |
| 88 | } |
| 89 | |
| 90 | public function prepare_fields_names( $block_name ) { |
| 91 | return "jet-forms/$block_name"; |
| 92 | } |
| 93 | |
| 94 | public function get_only_form_fields( $form_id, $exclude = array(), $recursive = true ) { |
| 95 | $content = $this->get_form_blocks( $form_id ); |
| 96 | |
| 97 | return $this->only_form_fields( $content, $exclude, $recursive ); |
| 98 | } |
| 99 | |
| 100 | public function only_form_fields( $content, $exclude = array(), $recursive = true ) { |
| 101 | $exclude = array_map( array( $this, 'prepare_fields_names' ), $exclude ); |
| 102 | |
| 103 | $this->result_fields = array(); |
| 104 | $this->get_inner_fields( $content, $exclude, $recursive ); |
| 105 | |
| 106 | $response = $this->result_fields; |
| 107 | $this->result_fields = array(); |
| 108 | |
| 109 | return $response; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | private function get_inner_fields( $source, $exclude, $recursive = true ) { |
| 114 | foreach ( $source as $block ) { |
| 115 | |
| 116 | if ( ! $this->is_not_field( $block['blockName'] ) && ! in_array( $block['blockName'], $exclude ) ) { |
| 117 | $this->result_fields[] = $block; |
| 118 | } |
| 119 | |
| 120 | if ( $recursive && ! empty( $block['innerBlocks'] ) ) { |
| 121 | $this->get_inner_fields( $block['innerBlocks'], $exclude, true ); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Returns generators list |
| 129 | * |
| 130 | * @return [type] [description] |
| 131 | */ |
| 132 | public function get_generators_list() { |
| 133 | |
| 134 | $generators = $this->get_options_generators(); |
| 135 | $result = array( |
| 136 | 0 => __( 'Select function...', 'jet-form-builder' ), |
| 137 | ); |
| 138 | |
| 139 | foreach ( $generators as $id => $generator ) { |
| 140 | $result[ $id ] = $generator->get_name(); |
| 141 | } |
| 142 | |
| 143 | return $result; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | public function field_name( $blockName ) { |
| 148 | return explode( self::NAMESPACE_FIELDS, $blockName )[1]; |
| 149 | } |
| 150 | |
| 151 | public function get_form_content( $form_id ) { |
| 152 | return get_post( $form_id )->post_content; |
| 153 | } |
| 154 | |
| 155 | public function get_field_by_name( $form_id, $field_name, $blocks = array() ): array { |
| 156 | if ( ! $blocks ) { |
| 157 | $blocks = $this->get_only_form_fields( $form_id ); |
| 158 | } |
| 159 | return $this->_get_field_by_name( $field_name, $blocks ); |
| 160 | } |
| 161 | |
| 162 | private function _get_field_by_name( $field_name, $blocks ): array { |
| 163 | foreach ( $blocks as $block ) { |
| 164 | $name = isset( $block['attrs']['name'] ) && $block['attrs']['name'] |
| 165 | ? $block['attrs']['name'] |
| 166 | : ''; |
| 167 | |
| 168 | if ( ! $name ) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | if ( $name === $field_name ) { |
| 173 | return $block; |
| 174 | } |
| 175 | |
| 176 | if ( 0 < count( $block['innerBlocks'] ) ) { |
| 177 | return $this->_get_field_by_name( $field_name, $block['innerBlocks'] ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return array(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | } |