acf_field_repeater::ajax_get_rows
Returns an array of rows used to populate the repeater table over AJAX.
Method of the class: acf_field_repeater{}
No Hooks.
Returns
null|WP_Error.
Usage
$acf_field_repeater = new acf_field_repeater(); $acf_field_repeater->ajax_get_rows();
Changelog
| Since 6.0.0 | Introduced. |
acf_field_repeater::ajax_get_rows() acf field repeater::ajax get rows code ACF 6.8.8
public function ajax_get_rows() {
$args = acf_request_args(
array(
'field_name' => '',
'field_key' => '',
'field_prefix' => '',
'post_id' => 0,
'rows_per_page' => 0,
'refresh' => false,
'nonce' => '',
'options_page_slug' => '',
)
);
if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'], true, 'repeater' ) ) {
$error = array( 'error' => __( 'Invalid nonce.', 'acf' ) );
wp_send_json_error( $error, 401 );
}
$post_id_info = acf_decode_post_id( acf_get_valid_post_id( $args['post_id'] ) );
if ( ! acf_current_user_can_edit_in_context( $post_id_info, sanitize_text_field( $args['options_page_slug'] ) ) ) {
$error = array( 'error' => __( 'Sorry, you do not have permission to do that.', 'acf' ) );
wp_send_json_error( $error, 403 );
}
if ( '' === $args['field_name'] || '' === $args['field_key'] ) {
$error = array( 'error' => __( 'Invalid field key or name.', 'acf' ) );
wp_send_json_error( $error, 404 );
}
$field = acf_get_field( $args['field_key'] );
$post_id = acf_get_valid_post_id( $args['post_id'] );
$response = array();
if ( ! $field || ! $post_id ) {
$error = array( 'error' => __( 'There was an error retrieving the field.', 'acf' ) );
wp_send_json_error( $error, 404 );
}
// Make sure we have a valid field.
$field = acf_validate_field( $field );
if ( empty( $field['pagination'] ) ) {
$error = array( 'error' => __( 'Pagination is not enabled for this field.', 'acf' ) );
wp_send_json_error( $error, 400 );
}
// Make sure that we only get a subset of the rows.
$this->is_rendering = true;
$args['rows_per_page'] = (int) $args['rows_per_page'];
if ( $args['rows_per_page'] ) {
$field['rows_per_page'] = $args['rows_per_page'];
}
/**
* We have to swap out the field name and prefix with the ones sent via JS,
* as the repeater could be inside a subfield.
*/
$field['name'] = $args['field_name'];
$field['prefix'] = $args['field_prefix'];
$field['value'] = acf_get_value( $post_id, $field );
if ( $args['refresh'] ) {
$response['total_rows'] = (int) acf_get_metadata_by_field( $post_id, $field );
}
// Render the rows to be sent back via AJAX.
$field = acf_prepare_field( $field );
$repeater_table = new ACF_Repeater_Table( $field );
$response['rows'] = $repeater_table->rows( true );
wp_send_json_success( $response );
}