fields
4 years ago
current-parsers.php
4 years ago
field-data-parser.php
4 years ago
parser-manager.php
4 years ago
request-handler.php
4 years ago
parser-manager.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 8 | use Jet_Form_Builder\Plugin; |
| 9 | use Jet_Form_Builder\Request\Fields; |
| 10 | |
| 11 | /** |
| 12 | * @method static Parser_Manager instance() |
| 13 | * |
| 14 | * Class Parser_Manager |
| 15 | * @package Jet_Form_Builder\Request |
| 16 | */ |
| 17 | class Parser_Manager { |
| 18 | |
| 19 | private $_parsers; |
| 20 | private $response; |
| 21 | public $inside_conditional = false; |
| 22 | |
| 23 | use Instance_Trait; |
| 24 | |
| 25 | private function __construct() { |
| 26 | $this->register_request_parsers(); |
| 27 | } |
| 28 | |
| 29 | private function register_request_parsers() { |
| 30 | $parsers = apply_filters( 'jet-form-builder/parsers-request/register', array( |
| 31 | new Fields\Date_Field_Parser(), |
| 32 | new Fields\Repeater_Field_Parser(), |
| 33 | new Fields\Wysiwyg_Field_Parser(), |
| 34 | new Fields\Text_Field_Parser(), |
| 35 | new Fields\Repeater_Field_Parser(), |
| 36 | new Fields\Media_Field_Parser(), |
| 37 | ) ); |
| 38 | |
| 39 | foreach ( $parsers as $parser ) { |
| 40 | $this->_parsers[ $parser->type() ] = $parser; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function get_values_fields( $fields, $request, $inside_conditional = false ) { |
| 45 | $response = array(); |
| 46 | |
| 47 | foreach ( $fields as $field ) { |
| 48 | if ( empty( $field['blockName'] ) ) { |
| 49 | continue; |
| 50 | } |
| 51 | if ( ! empty( $field['innerBlocks'] ) && ! $this->isset_parser_by_block_name( $field['blockName'] ) ) { |
| 52 | if ( strpos( $field['blockName'], 'conditional-block' ) ) { |
| 53 | $inside_conditional = true; |
| 54 | } |
| 55 | |
| 56 | $response = array_merge( $response, $this->get_values_fields( |
| 57 | $field['innerBlocks'], |
| 58 | $request, |
| 59 | $inside_conditional |
| 60 | ) ); |
| 61 | |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | $settings = $field['attrs']; |
| 66 | $name = isset( $settings['name'] ) ? $settings['name'] : 'field_name'; |
| 67 | |
| 68 | $response[ $name ] = $this->get_parsed_value( $field, $request, $name, $inside_conditional ); |
| 69 | } |
| 70 | |
| 71 | return $response; |
| 72 | } |
| 73 | |
| 74 | public function get_parsed_value( $field, $request, $name, $inside_conditional ) { |
| 75 | |
| 76 | if ( ! $this->is_field_visible( $field['attrs'] ) ) { |
| 77 | return null; |
| 78 | } |
| 79 | $value = isset( $request[ $name ] ) ? $request[ $name ] : ''; |
| 80 | $parser = $this->get_parser_by_block_name( $field['blockName'] ); |
| 81 | |
| 82 | if ( $parser instanceof Field_Data_Parser ) { |
| 83 | $parser->init( $value, $field, $inside_conditional ); |
| 84 | |
| 85 | return $parser->response(); |
| 86 | } |
| 87 | |
| 88 | return $value; |
| 89 | } |
| 90 | |
| 91 | private function isset_parser_by_block_name( $block_name ) { |
| 92 | $type = Plugin::instance()->form->field_name( $block_name ); |
| 93 | |
| 94 | return isset( $this->_parsers[ $type ] ); |
| 95 | } |
| 96 | |
| 97 | private function get_parser_by_block_name( $block_name ) { |
| 98 | $type = Plugin::instance()->form->field_name( $block_name ); |
| 99 | |
| 100 | return $this->get_parser( $type ); |
| 101 | } |
| 102 | |
| 103 | private function get_parser( $type ) { |
| 104 | return isset( $this->_parsers[ $type ] ) ? clone $this->_parsers[ $type ] : false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns true if field is visible |
| 109 | * |
| 110 | * @param array $field [description] |
| 111 | * |
| 112 | * @return boolean [description] |
| 113 | */ |
| 114 | public function is_field_visible( $field = array() ) { |
| 115 | |
| 116 | // For backward compatibility and hidden fields |
| 117 | if ( empty( $field['visibility'] ) ) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // If is visible for all - show field |
| 122 | if ( 'all' === $field['visibility'] ) { |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // If is visible for logged in users and user is logged in - show field |
| 127 | if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | // If is visible for not logged in users and user is not logged in - show field |
| 132 | if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) { |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | } |