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