date-field-parser.php
2 years ago
datetime-field-parser.php
2 years ago
datetime-trait.php
2 years ago
default-parser.php
2 years ago
hidden-field-parser.php
2 years ago
media-field-parser.php
1 year ago
repeater-field-parser.php
2 years ago
text-field-parser.php
2 years ago
wysiwyg-field-parser.php
2 years ago
repeater-field-parser.php
68 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Block_Parsers\Fields; |
| 5 | |
| 6 | use Jet_Form_Builder\Request\Exceptions\Sanitize_Value_Exception; |
| 7 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Repeater_Field_Parser extends Field_Data_Parser { |
| 15 | |
| 16 | public function type() { |
| 17 | return 'repeater-field'; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @return int|void |
| 22 | * @throws Sanitize_Value_Exception |
| 23 | */ |
| 24 | public function get_response() { |
| 25 | $indexes = $this->get_indexes(); |
| 26 | |
| 27 | if ( ! $indexes ) { |
| 28 | return ''; |
| 29 | } |
| 30 | |
| 31 | if ( ! $this->get_inner_template() ) { |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | foreach ( $indexes as $index ) { |
| 36 | $context = clone $this->get_inner_template(); |
| 37 | $context->set_index_in_parent( $this->add_inner( $context ) ); |
| 38 | |
| 39 | $context->set_request( $this->value[ $index ] ?? array() ) |
| 40 | ->set_files( $this->file[ $index ] ?? array() ); |
| 41 | |
| 42 | $context->apply(); |
| 43 | } |
| 44 | |
| 45 | $this->set_file( false ); |
| 46 | |
| 47 | return count( $indexes ); |
| 48 | } |
| 49 | |
| 50 | protected function get_indexes(): array { |
| 51 | if ( ! is_array( $this->value ) ) { |
| 52 | $this->value = array(); |
| 53 | } |
| 54 | if ( ! is_array( $this->file ) ) { |
| 55 | $this->file = array(); |
| 56 | } |
| 57 | |
| 58 | $indexes = array_keys( $this->value ); |
| 59 | $file_indexes = array_keys( $this->file ); |
| 60 | |
| 61 | /** |
| 62 | * @see https://github.com/Crocoblock/issues-tracker/issues/2095 |
| 63 | */ |
| 64 | return max( $indexes, $file_indexes ); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 |