acf_field_select::get_ajax_querypublicACF 5.0.9

This function will return an array of data formatted for use in a select2 AJAX response

Метод класса: acf_field_select{}

Возвращает

Массив. A select2 compatible array of options.

Использование

$acf_field_select = new acf_field_select();
$acf_field_select->get_ajax_query( $options );
$options(массив)
An array of options.
По умолчанию: array()

Список изменений

С версии 5.0.9 Введена.

Код acf_field_select::get_ajax_query() ACF 6.4.2

public function get_ajax_query( $options = array() ) {
	$options = acf_parse_args(
		$options,
		array(
			'post_id'   => 0,
			's'         => '',
			'field_key' => '',
			'paged'     => 1,
		)
	);

	$shortcut = apply_filters( 'acf/fields/select/query', array(), $options );
	$shortcut = apply_filters( 'acf/fields/select/query/key=' . $options['field_key'], $shortcut, $options );
	if ( ! empty( $shortcut ) ) {
		return $shortcut;
	}

	// load field.
	$field = acf_get_field( $options['field_key'] );
	if ( ! $field ) {
		return false;
	}

	// get choices.
	$choices = acf_get_array( $field['choices'] );
	if ( empty( $field['choices'] ) ) {
		return false;
	}

	$results = array();
	$s       = null;

	// search.
	if ( $options['s'] !== '' ) {

		// strip slashes (search may be integer)
		$s = strval( $options['s'] );
		$s = wp_unslash( $s );
	}

	foreach ( $field['choices'] as $k => $v ) {

		// ensure $v is a string.
		$v = strval( $v );

		// if searching, but doesn't exist.
		if ( is_string( $s ) && stripos( $v, $s ) === false ) {
			continue;
		}

		// append results.
		$results[] = array(
			'id'   => $k,
			'text' => $v,
		);
	}

	$response = array(
		'results' => $results,
	);

	return $response;
}