| 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 |
protected function allows_array_value(): bool { |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @return int|void |
| 26 |
* @throws Sanitize_Value_Exception |
| 27 |
*/ |
| 28 |
public function get_response() { |
| 29 |
$indexes = $this->get_indexes(); |
| 30 |
|
| 31 |
if ( ! $indexes ) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! $this->get_inner_template() ) { |
| 36 |
return -1; |
| 37 |
} |
| 38 |
|
| 39 |
foreach ( $indexes as $index ) { |
| 40 |
$context = clone $this->get_inner_template(); |
| 41 |
$context->set_index_in_parent( $this->add_inner( $context ) ); |
| 42 |
|
| 43 |
$context->set_request( $this->value[ $index ] ?? array() ) |
| 44 |
->set_files( $this->file[ $index ] ?? array() ); |
| 45 |
|
| 46 |
$context->apply(); |
| 47 |
} |
| 48 |
|
| 49 |
$this->set_file( false ); |
| 50 |
|
| 51 |
return count( $indexes ); |
| 52 |
} |
| 53 |
|
| 54 |
protected function get_indexes(): array { |
| 55 |
if ( ! is_array( $this->value ) ) { |
| 56 |
$this->value = array(); |
| 57 |
} |
| 58 |
if ( ! is_array( $this->file ) ) { |
| 59 |
$this->file = array(); |
| 60 |
} |
| 61 |
|
| 62 |
$indexes = array_keys( $this->value ); |
| 63 |
$file_indexes = array_keys( $this->file ); |
| 64 |
|
| 65 |
/** |
| 66 |
* @see https://github.com/Crocoblock/issues-tracker/issues/2095 |
| 67 |
*/ |
| 68 |
return max( $indexes, $file_indexes ); |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|