| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Blocks_V2\Repeater_Field; |
| 4 |
|
| 5 |
// If this file is called directly, abort. |
| 6 |
use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 |
use Jet_Form_Builder\Blocks\Render\Base; |
| 8 |
use Jet_Form_Builder\Classes\Builder_Helper; |
| 9 |
|
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @property Block_Type block_type |
| 16 |
* |
| 17 |
* Class Repeater_Field_Render |
| 18 |
* @package Jet_Form_Builder\Blocks\Render |
| 19 |
*/ |
| 20 |
class Block_Render extends Base { |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return 'repeater-field'; |
| 24 |
} |
| 25 |
|
| 26 |
public function render( $wp_block = null, $template = null ) { |
| 27 |
if ( empty( $wp_block['innerBlocks'] ) ) { |
| 28 |
return ''; |
| 29 |
} |
| 30 |
|
| 31 |
$this->add_attribute( 'class', 'jet-form-builder-repeater jet-form-builder__field' ); |
| 32 |
$this->add_attribute( 'class', $this->block_type->block_attrs['class_name'] ); |
| 33 |
$this->add_attribute( 'data-required', $this->block_type->get_required_val() ); |
| 34 |
$this->add_attribute( 'data-jfb-sync' ); |
| 35 |
|
| 36 |
$attrs = Builder_Helper::attrs( |
| 37 |
array( |
| 38 |
array( 'data-field-name', esc_attr( $this->block_type->block_attrs['name'] ) ), |
| 39 |
array( 'name', esc_attr( $this->block_type->block_attrs['name'] ) ), |
| 40 |
array( 'data-calc-type', esc_attr( $this->block_type->calc_type ) ), |
| 41 |
array( 'data-formula', esc_attr( $this->block_type->get_calculated_formula() ) ), |
| 42 |
array( 'data-manage-items', esc_attr( $this->block_type->manage_items ) ), |
| 43 |
array( 'data-items-field', esc_attr( $this->block_type->items_field ) ), |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
$html = '<div class="jet-form-builder__field-wrap">'; |
| 48 |
$content = ''; |
| 49 |
|
| 50 |
foreach ( $this->iterate_rows_html( $wp_block ) as $part ) { |
| 51 |
$content .= $part; |
| 52 |
} |
| 53 |
|
| 54 |
$html .= sprintf( |
| 55 |
'<div data-repeater="1" %1$s %2$s>%3$s', |
| 56 |
$attrs, |
| 57 |
$this->get_attributes_string(), |
| 58 |
$content |
| 59 |
); |
| 60 |
|
| 61 |
if ( ! $this->block_type->manage_items ) { |
| 62 |
$html .= sprintf( |
| 63 |
'<div class="jet-form-builder-repeater__actions"> |
| 64 |
<button type="button" class="jet-form-builder-repeater__new">%1$s</button> |
| 65 |
</div>', |
| 66 |
wp_kses_post( $this->block_type->new_item_label ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$html .= '</div>'; |
| 71 |
$html .= '</div>'; |
| 72 |
|
| 73 |
return parent::render( null, $html ); |
| 74 |
} |
| 75 |
|
| 76 |
protected function iterate_rows_html( $wp_block ): \Generator { |
| 77 |
foreach ( $this->populate_blocks_or_html( $wp_block ) as $row ) { |
| 78 |
// just html |
| 79 |
if ( is_string( $row ) ) { |
| 80 |
yield $row; |
| 81 |
|
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
yield Block_Helper::render_with_context( |
| 86 |
$row, |
| 87 |
$this->block_type->block_context + array( |
| 88 |
Block_Type::CONTEXT_NAME => $this->block_type->block_attrs['name'] ?? '', |
| 89 |
Block_Type::CONTEXT_MANAGE_ITEMS => $this->block_type->manage_items, |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
protected function populate_blocks_or_html( $wp_block ): \Generator { |
| 96 |
// repeater template should always render |
| 97 |
yield array( |
| 98 |
'blockName' => 'jet-forms/repeater-row', |
| 99 |
'attrs' => array( |
| 100 |
'isTemplate' => true, |
| 101 |
), |
| 102 |
'innerBlocks' => $wp_block['innerBlocks'], |
| 103 |
'innerContent' => $wp_block['innerContent'], |
| 104 |
); |
| 105 |
|
| 106 |
$repeater_rows = $this->block_type->block_attrs['default']; |
| 107 |
|
| 108 |
if ( ! $repeater_rows || ! is_array( $repeater_rows ) ) { |
| 109 |
yield '<div class="jet-form-builder-repeater__items"></div>'; |
| 110 |
|
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
yield '<div class="jet-form-builder-repeater__items">'; |
| 115 |
|
| 116 |
$repeater_rows = array_values( $repeater_rows ); |
| 117 |
$counter = 0; |
| 118 |
|
| 119 |
foreach ( $repeater_rows as $row ) { |
| 120 |
yield array( |
| 121 |
'blockName' => 'jet-forms/repeater-row', |
| 122 |
'attrs' => array( |
| 123 |
'rowDefault' => $row, |
| 124 |
'currentIndex' => $counter, |
| 125 |
), |
| 126 |
'innerBlocks' => $wp_block['innerBlocks'], |
| 127 |
'innerContent' => $wp_block['innerContent'], |
| 128 |
); |
| 129 |
++$counter; |
| 130 |
} |
| 131 |
|
| 132 |
yield '</div>'; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
protected function get_fields_label_tag(): string { |
| 141 |
return 'div'; |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|