| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder\Blocks\Render; |
| 4 |
|
| 5 |
// If this file is called directly, abort. |
| 6 |
use Jet_Form_Builder\Live_Form; |
| 7 |
use Jet_Form_Builder\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Define text field renderer class |
| 15 |
*/ |
| 16 |
class Repeater_Field_Render extends Base { |
| 17 |
|
| 18 |
public $current_repeater_i = false; |
| 19 |
public $current_repeater; |
| 20 |
|
| 21 |
public function get_name() { |
| 22 |
return 'repeater-field'; |
| 23 |
} |
| 24 |
|
| 25 |
public function render( $wp_block = null, $template = null ) { |
| 26 |
if ( empty( $wp_block['innerBlocks'] ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$this->current_repeater = $this->block_type->block_attrs; |
| 31 |
|
| 32 |
$template = sprintf( |
| 33 |
'<template class="jet-form-builder-repeater__initial">%1$s</template>', |
| 34 |
$this->render_repeater_row( $wp_block, false ) |
| 35 |
); |
| 36 |
|
| 37 |
$html = '<div class="jet-form-builder__field-wrap">'; |
| 38 |
|
| 39 |
$html .= sprintf( |
| 40 |
'<div class="jet-form-builder-repeater jet-form-builder__field %5$s" data-repeater="1" |
| 41 |
data-field-name="%1$s" name="%1$s" data-settings="%2$s" %3$s>%4$s', |
| 42 |
$this->block_type->block_attrs['name'], |
| 43 |
$this->block_type->settings, |
| 44 |
$this->block_type->calc_dataset, |
| 45 |
$template, |
| 46 |
$this->maybe_get_error_class( $this->block_type->block_attrs ) |
| 47 |
); |
| 48 |
|
| 49 |
$html .= '<div class="jet-form-builder-repeater__items">'; |
| 50 |
|
| 51 |
if ( ! empty( $this->current_repeater['default'] ) && is_array( $this->current_repeater['default'] ) ) { |
| 52 |
$i = 0; |
| 53 |
foreach ( $this->current_repeater['default'] as $item ) { |
| 54 |
$this->current_repeater['values'] = $item; |
| 55 |
$html .= $this->render_repeater_row( $wp_block, $i ); |
| 56 |
$i++; |
| 57 |
} |
| 58 |
$this->current_repeater['values'] = false; |
| 59 |
} |
| 60 |
|
| 61 |
$html .= '</div>'; |
| 62 |
|
| 63 |
if ( 'manually' === $this->block_type->manage_items ) { |
| 64 |
$html .= sprintf( |
| 65 |
'<div class="jet-form-builder-repeater__actions"> |
| 66 |
<button type="button" class="jet-form-builder-repeater__new">%1$s</button> |
| 67 |
</div>', |
| 68 |
$this->block_type->new_item_label |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$html .= '</div>'; |
| 73 |
$html .= $this->maybe_render_error( $this->block_type->block_attrs ); |
| 74 |
$html .= '</div>'; |
| 75 |
|
| 76 |
$this->current_repeater = false; |
| 77 |
|
| 78 |
return parent::render( null, $html ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Render current repeater row |
| 83 |
* |
| 84 |
* @param $wp_block |
| 85 |
* @param bool $index |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function render_repeater_row( $wp_block, $index = false ) { |
| 90 |
|
| 91 |
if ( false !== $index ) { |
| 92 |
$this->current_repeater_i = $index; |
| 93 |
} else { |
| 94 |
$index = 0; |
| 95 |
} |
| 96 |
|
| 97 |
$html = '<div class="jet-form-builder-repeater__row" data-repeater-row="1" data-index="' . $index . '"' . $this->block_type->calc_dataset . '>'; |
| 98 |
$html .= '<div class="jet-form-builder-repeater__row-fields">'; |
| 99 |
|
| 100 |
Live_Form::instance()->set_repeater( $this->current_repeater, $this->current_repeater_i ); |
| 101 |
|
| 102 |
foreach ( $wp_block['innerBlocks'] as $block ) { |
| 103 |
$html .= render_block( $block ); |
| 104 |
} |
| 105 |
|
| 106 |
$html .= '</div>'; |
| 107 |
|
| 108 |
Live_Form::instance()->set_repeater( false ); |
| 109 |
|
| 110 |
if ( 'manually' === $this->block_type->manage_items ) { |
| 111 |
$html .= '<div class="jet-form-builder-repeater__row-remove">'; |
| 112 |
$html .= '<button type="button" class="jet-form-builder-repeater__remove">×</button>'; |
| 113 |
$html .= '</div>'; |
| 114 |
} |
| 115 |
|
| 116 |
return $html . '</div>'; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
|