-
Notifications
You must be signed in to change notification settings - Fork 7
/
acf-fancy-repeater-field-v5.php
269 lines (198 loc) · 7.61 KB
/
acf-fancy-repeater-field-v5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
class ACF_Fancy_Repeater_Field_V5 {
private static $instance;
public static function register() {
if ( self::$instance == null ) {
self::$instance = new ACF_Fancy_Repeater_Field_V5();
}
}
public function __construct() {
// vars
$this->name = 'fancyrepeater';
$this->label = __( "Fancy Repeater", 'acf' );
$this->category = 'layout';
$this->defaults = array(
'sub_fields' => array(),
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => __( "Add Row", 'acf' ),
);
$this->l10n = array(
'min' => __( "Minimum rows reached ({min} rows)", 'acf' ),
'max' => __( "Maximum rows reached ({max} rows)", 'acf' ),
);
add_filter( "acf/load_field/type=repeater", array($this, 'load_field'), 10, 1 );
add_action( "acf/render_field_settings/type=repeater", array($this, 'render_field_settings'), 10, 1 );
add_action( "acf/render_field_settings/type=fancyrepeater", array($this, 'render_field_settings'), 10, 1 );
add_filter( "acf/update_field/type=fancyrepeater", array($this, 'update_field'), 5, 1 );
add_filter( "acf/duplicate_field/type=fancyrepeater", array($this, 'duplicate_field'), 5, 1 );
add_action( "acf/delete_field/type=fancyrepeater", array($this, 'delete_field'), 5, 1 );
add_action( "acf/render_field/type=fancyrepeater", array($this, 'render_field'), 5, 1 );
add_filter( "acf/load_value/type=fancyrepeater", array($this, 'load_value'), 10, 3 );
add_filter( "acf/update_value/type=fancyrepeater", array($this, 'update_value'), 10, 3 );
add_filter( "acf/format_value/type=fancyrepeater", array($this, 'format_value'), 10, 3 );
add_filter( "acf/validate_value/type=fancyrepeater", array($this, 'validate_value'), 10, 4 );
add_action( "acf/delete_value/type=fancyrepeater", array($this, 'delete_value'), 10, 2 );
}
public function load_field( $field ) {
global $post;
$field['use_fancy_repeater'] = isset( $field['use_fancy_repeater'] ) ? $field['use_fancy_repeater'] : 'no';
if ( empty( $post ) || ( $post && $post->post_type != 'acf-field-group' ) ) {
$field['use_fancy_repeater'] = isset( $field['use_fancy_repeater'] ) ? $field['use_fancy_repeater'] : 'no';
$field['forced_fancy_repeater'] = false;
if ( $field['use_fancy_repeater'] == 'yes' ) {
$field['type'] = 'fancyrepeater';
$field['forced_fancy_repeater'] = true;
}
}
return $field;
}
public function render_field_settings( $field ) {
// layout
acf_render_field_setting( $field, array(
'label' => __( 'Fancy Repeater', 'acf' ),
'instructions' => '',
'class' => 'acf-repeater-use-fancy-repeater',
'type' => 'radio',
'name' => 'use_fancy_repeater',
'layout' => 'horizontal',
'default_value' => 'no',
'value' => isset( $field['use_fancy_repeater'] ) ? $field['use_fancy_repeater'] : 'no',
'choices' => array(
'yes' => __( 'Yes', 'acf' ),
'no' => __( 'No', 'acf' )
)
) );
return $field;
}
public function load_value( $value, $post_id, $field ) {
return apply_filters( 'acf/load_value/type=repeater', $value, $post_id, $field );
}
public function format_value( $value, $post_id, $field ) {
return apply_filters( 'acf/format_value/type=repeater', $value, $post_id, $field );
}
public function validate_value( $valid, $value, $field, $input ) {
return apply_filters( 'acf/validate_value/type=repeater', $valid, $value, $field, $input );
}
public function update_value( $value, $post_id, $field ) {
return apply_filters( 'acf/update_value/type=repeater', $value, $post_id, $field );
}
public function delete_field( $field ) {
return do_action( 'acf/delete_field/type=repeater', $field );
}
public function update_field( $field ) {
if ( isset( $field['forced_fancy_repeater'] ) && !empty( $field['forced_fancy_repeater'] ) ) {
unset( $field['forced_fancy_repeater'] );
}
return apply_filters( 'acf/update_field/type=repeater', $field );
}
public function duplicate_field( $field ) {
return apply_filters( 'acf/duplicate_field/type=repeater', $field );
}
function render_field( $field ) {
// ensure value is an array
if ( empty( $field['value'] ) ) {
$field['value'] = array();
}
// rows
$field['min'] = empty( $field['min'] ) ? 0 : $field['min'];
$field['max'] = empty( $field['max'] ) ? 0 : $field['max'];
// populate the empty row data (used for acfcloneindex and min setting)
$empty_row = array();
if ( isset( $field['sub_fields'] ) && !empty( $field['sub_fields'] ) ) {
foreach ( $field['sub_fields'] as $f ) {
$empty_row[$f['key']] = isset( $f['default_value'] ) ? $f['default_value'] : false;
}
}
// If there are less values than min, populate the extra values
if ( $field['min'] ) {
for ( $i = 0; $i < $field['min']; $i++ ) {
// continue if already have a value
if ( array_key_exists( $i, $field['value'] ) ) {
continue;
}
// populate values
$field['value'][$i] = $empty_row;
}
}
// If there are more values than man, remove some values
if ( $field['max'] ) {
for ( $i = 0; $i < count( $field['value'] ); $i++ ) {
if ( $i >= $field['max'] ) {
unset( $field['value'][$i] );
}
}
}
// setup values for row clone
$field['value']['acfcloneindex'] = $empty_row;
// show columns
$show_order = true;
$show_add = true;
$show_remove = true;
if ( $field['max'] ) {
if ( $field['max'] == 1 ) {
$show_order = false;
}
if ( $field['max'] <= $field['min'] ) {
$show_remove = false;
$show_add = false;
}
}
// field wrap
$el = 'td';
$before_fields = '';
$after_fields = '';
if ( $field['layout'] == 'row' ) {
$el = 'tr';
$before_fields = '<td class="acf-table-wrap"><table class="acf-table">';
$after_fields = '</table></td>';
} elseif ( $field['layout'] == 'block' ) {
$el = 'div';
$before_fields = '<td class="acf-fields">';
$after_fields = '</td>';
}
// hidden input
acf_hidden_input( array(
'type' => 'hidden',
'name' => $field['name'],
) );
?>
<div <?php acf_esc_attr_e( array('class' => 'acf-repeater acf-fancyrepeater', 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-titlefieldkey' => $this->get_title_field_key( $field )) ); ?>>
<div class="acf-fancyrepeater-list-wrap">
<ul class="acf-hl acf-thead">
<li class="li-fancyrepeater-order"><?php _e( 'Order', 'acf_child_post_field' ); ?></li>
<li class="li-fancyrepeater-label"><?php _e( 'Item', 'acf_child_post_field' ); ?></li>
<li class="li-fancyrepeater-name"></li>
<li class="li-fancyrepeater-type"></li>
</ul>
<div class="acf-fancyrepeater-list">
<?php foreach ( $field['value'] as $i => $row ): ?>
<?php $clone_class = ($i === 'acfcloneindex') ? ' acf-clone' : ''; ?>
<div class="acf-fancyrepeater-object <?php echo $clone_class; ?>">
<?php $this->render_child_table( $field, $row, $i, $before_fields, $after_fields, $show_order, $el ); ?>
</div>
<?php endforeach; ?>
</div>
<ul class="acf-hl acf-tfoot">
<li class="comic-sans"><i class="acf-sprite-arrow"></i><?php _e( 'Drag and drop to reorder', 'acf' ); ?></li>
<li class="acf-fr">
<a href="#" class="acf-button blue acf-fancyrepeater-add-row">+ <?php echo $field['button_label']; ?></a>
</li>
</ul>
</div>
</div>
<?php
}
private function render_child_table( $field, $row, $i, $before_fields, $after_fields, $show_order, $el ) {
$title_input_key = $this->get_title_field_key( $field );
include 'views/repeater-table.php';
}
private function get_title_field_key( $field ) {
if ( isset( $field['sub_fields'] ) ) {
return $field['sub_fields'][0]['key'];
} else {
return '';
}
}
}