| 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()->post_type->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_by_id( $form_id ) { |
| 87 |
return $this->get_fields( get_post( $form_id )->post_content ); |
| 88 |
} |
| 89 |
|
| 90 |
public function get_only_form_fields( $form_id ) { |
| 91 |
$content = $this->get_form_by_id( $form_id ); |
| 92 |
|
| 93 |
$this->result_fields = array(); |
| 94 |
$this->get_inner_fields( $content ); |
| 95 |
|
| 96 |
return $this->result_fields; |
| 97 |
} |
| 98 |
|
| 99 |
function get_inner_fields( $source ) { |
| 100 |
foreach ( $source as $block ) { |
| 101 |
|
| 102 |
if ( ! $this->is_not_field( $block['blockName'] ) ) { |
| 103 |
$this->result_fields[] = $block; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! empty( $block['innerBlocks'] ) ) { |
| 107 |
$this->get_inner_fields( $block['innerBlocks'] ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/** |
| 114 |
* Returns generators list |
| 115 |
* |
| 116 |
* @return [type] [description] |
| 117 |
*/ |
| 118 |
public function get_generators_list() { |
| 119 |
|
| 120 |
$generators = $this->get_options_generators(); |
| 121 |
$result = array( |
| 122 |
0 => __( 'Select function...', 'jet-form-builder' ), |
| 123 |
); |
| 124 |
|
| 125 |
foreach ( $generators as $id => $generator ) { |
| 126 |
$result[ $id ] = $generator->get_name(); |
| 127 |
} |
| 128 |
|
| 129 |
return $result; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
public function field_name( $blockName ) { |
| 134 |
return explode( self::NAMESPACE_FIELDS, $blockName )[1]; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
} |