field = $field;
$this->sub_fields = $field['sub_fields'];
// Default to non-paginated repeaters.
if ( empty( $this->field['pagination'] ) ) {
$this->field['pagination'] = false;
}
// We don't yet support pagination inside other repeaters or flexible content fields.
if ( ! empty( $this->field['parent_repeater'] ) || ! empty( $this->field['parent_layout'] ) ) {
$this->field['pagination'] = false;
}
// We don't yet support pagination in frontend forms or inside blocks.
if ( ! is_admin() || acf_get_data( 'acf_inside_rest_call' ) || doing_action( 'wp_ajax_acf/ajax/fetch-block' ) ) {
$this->field['pagination'] = false;
}
$this->setup();
}
/**
* Sets up the field for rendering.
*
* @since 6.0.0
*
* @return void
*/
private function setup() {
if ( $this->field['collapsed'] ) {
foreach ( $this->sub_fields as &$sub_field ) {
// Add target class.
if ( $sub_field['key'] == $this->field['collapsed'] ) {
$sub_field['wrapper']['class'] .= ' -collapsed-target';
}
}
}
if ( $this->field['max'] ) {
// If max 1 row, don't show order.
if ( 1 == $this->field['max'] ) {
$this->show_order = false;
}
// If max == min, don't show add or remove buttons.
if ( $this->field['max'] <= $this->field['min'] ) {
$this->show_remove = false;
$this->show_add = false;
}
}
if ( empty( $this->field['rows_per_page'] ) ) {
$this->field['rows_per_page'] = 20;
}
if ( (int) $this->field['rows_per_page'] < 1 ) {
$this->field['rows_per_page'] = 20;
}
$this->value = $this->prepare_value();
}
/**
* Prepares the repeater values for rendering.
*
* @since 6.0.0
*
* @return array
*/
private function prepare_value() {
$value = is_array( $this->field['value'] ) ? $this->field['value'] : array();
if ( empty( $this->field['pagination'] ) ) {
// If there are fewer values than min, populate the extra values.
if ( $this->field['min'] ) {
$value = array_pad( $value, $this->field['min'], array() );
}
// If there are more values than max, remove some values.
if ( $this->field['max'] ) {
$value = array_slice( $value, 0, $this->field['max'] );
}
}
$value['acfcloneindex'] = array();
return $value;
}
/**
* Renders the full repeater table.
*
* @since 6.0.0
*
* @return void
*/
public function render() {
// Attributes for main wrapper div.
$div = array(
'class' => 'acf-repeater -' . $this->field['layout'],
'data-min' => $this->field['min'],
'data-max' => $this->field['max'],
'data-pagination' => ! empty( $this->field['pagination'] ),
);
if ( $this->field['pagination'] ) {
$div['data-per_page'] = $this->field['rows_per_page'];
$div['data-total_rows'] = $this->field['total_rows'];
$div['data-orig_name'] = $this->field['orig_name'];
}
if ( empty( $this->value ) ) {
$div['class'] .= ' -empty';
}
?>
>
$this->field['name'],
'value' => '',
'class' => 'acf-repeater-hidden-input',
)
);
?>
table_actions(); ?>
field['layout'] ) {
return;
}
?>
show_order ) : ?>
|
sub_fields as $sub_field ) :
// Prepare field (allow sub fields to be removed).
$sub_field = acf_prepare_field( $sub_field );
if ( ! $sub_field ) {
continue;
}
// Define attrs.
$attrs = array(
'class' => 'acf-th',
'data-name' => $sub_field['_name'],
'data-type' => $sub_field['type'],
'data-key' => $sub_field['key'],
);
if ( $sub_field['wrapper']['width'] ) {
$attrs['data-width'] = $sub_field['wrapper']['width'];
$attrs['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
}
// Remove "id" to avoid "for" attribute on
value['acfcloneindex'] ) ) {
unset( $this->value['acfcloneindex'] );
}
foreach ( $this->value as $i => $row ) {
$rows[ $i ] = $this->row( $i, $row, $return );
}
if ( $return ) {
return $rows;
}
echo implode( PHP_EOL, $rows );
}
/**
* Renders an individual row.
*
* @since 6.0.0
*
* @param int $i The row number.
* @param array $row An array containing the row values.
* @param bool $return If we should return the row or render it.
* @return string|void
*/
public function row( $i, $row, $return = false ) {
if ( $return ) {
ob_start();
}
$id = "row-$i";
$class = 'acf-row';
if ( 'acfcloneindex' === $i ) {
$id = 'acfcloneindex';
$class .= ' acf-clone';
}
$el = 'td';
$before_fields = '';
$after_fields = '';
if ( 'row' === $this->field['layout'] ) {
$el = 'div';
$before_fields = '';
$after_fields = ' | ';
} elseif ( 'block' === $this->field['layout'] ) {
$el = 'div';
$before_fields = '';
$after_fields = ' | ';
}
printf(
'',
esc_attr( $class ),
esc_attr( $id )
);
$this->row_handle( $i );
echo $before_fields;
foreach ( $this->sub_fields as $sub_field ) {
if ( isset( $row[ $sub_field['key'] ] ) ) {
$sub_field['value'] = $row[ $sub_field['key'] ];
} elseif ( isset( $sub_field['default_value'] ) ) {
$sub_field['value'] = $sub_field['default_value'];
}
// Update prefix to allow for nested values.
$sub_field['prefix'] = $this->field['name'] . '[' . $id . ']';
acf_render_field_wrap( $sub_field, $el );
}
echo $after_fields;
$this->row_actions();
echo '
';
if ( $return ) {
return ob_get_clean();
}
}
/**
* Renders the row handle at the start of each row.
*
* @since 6.0.0
*
* @param int $i The current row number.
* @return void
*/
public function row_handle( $i ) {
if ( ! $this->show_order ) {
return;
}
$hr_row_num = intval( $i ) + 1;
$classes = 'acf-row-handle order';
$title = __( 'Drag to reorder', 'acf' );
$row_num_html = sprintf(
'%d',
__( 'Click to reorder', 'acf' ),
$hr_row_num
);
if ( ! empty( $this->field['pagination'] ) ) {
$classes .= ' pagination';
$title = '';
$input = sprintf( '', $hr_row_num );
$row_num_html = '' . $input . $row_num_html . '
';
}
?>
field['collapsed'] ) : ?>
|
show_remove ) {
return;
}
?>
|
show_add ) {
return;
}
?>
field['pagination'] ) ) {
return;
}
$total_rows = isset( $this->field['total_rows'] ) ? (int) $this->field['total_rows'] : 0;
$total_pages = ceil( $total_rows / (int) $this->field['rows_per_page'] );
$total_pages = max( $total_pages, 1 );
$html_current_page = sprintf(
"%s",
'',
1,
strlen( $total_pages )
);
$html_total_pages = sprintf( "%s", number_format_i18n( $total_pages ) );
?>