acf_field_clone::ajax_query()publicACF 5.3.8

ajax_query

description

@type    function

{} Это метод класса: acf_field_clone{}

Хуков нет.

Возвращает

$post_id. (int)

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

$acf_field_clone = new acf_field_clone();
$acf_field_clone->ajax_query();

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

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

Код acf_field_clone::ajax_query() ACF 5.10.2

function ajax_query() {

	// validate
	if ( ! acf_verify_ajax() ) {
		die();
	}

	// disable field to allow clone fields to appear selectable
	acf_disable_filter( 'clone' );

	// options
	$options = acf_parse_args(
		$_POST,
		array(
			'post_id' => 0,
			'paged'   => 0,
			's'       => '',
			'title'   => '',
			'fields'  => array(),
		)
	);

	// vars
	$results     = array();
	$s           = false;
	$i           = -1;
	$limit       = 20;
	$range_start = $limit * ( $options['paged'] - 1 );  // 0,  20, 40
	$range_end   = $range_start + ( $limit - 1 );         // 19, 39, 59

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

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

	}

	// load groups
	$field_groups = acf_get_field_groups();
	$field_group  = false;

	// bail early if no field groups
	if ( empty( $field_groups ) ) {
		die();
	}

	// move current field group to start
	foreach ( array_keys( $field_groups ) as $j ) {

		// check ID
		if ( $field_groups[ $j ]['ID'] !== $options['post_id'] ) {
			continue;
		}

		// extract field group and move to start
		$field_group = acf_extract_var( $field_groups, $j );

		// field group found, stop looking
		break;

	}

	// if field group was not found, this is a new field group (not yet saved)
	if ( ! $field_group ) {

		$field_group = array(
			'ID'    => $options['post_id'],
			'title' => $options['title'],
			'key'   => '',
		);

	}

	// move current field group to start of list
	array_unshift( $field_groups, $field_group );

	// loop
	foreach ( $field_groups as $field_group ) {

		// vars
		$fields   = false;
		$ignore_s = false;
		$data     = array(
			'text'     => $field_group['title'],
			'children' => array(),
		);

		// get fields
		if ( $field_group['ID'] == $options['post_id'] ) {

			$fields = $options['fields'];

		} else {

			$fields = acf_get_fields( $field_group );
			$fields = acf_prepare_fields_for_import( $fields );

		}

		// bail early if no fields
		if ( ! $fields ) {
			continue;
		}

		// show all children for field group search match
		if ( $s !== false && stripos( $data['text'], $s ) !== false ) {

			$ignore_s = true;

		}

		// populate children
		$children   = array();
		$children[] = $field_group['key'];
		foreach ( $fields as $field ) {
			$children[] = $field['key']; }

		// loop
		foreach ( $children as $child ) {

			// bail ealry if no key (fake field group or corrupt field)
			if ( ! $child ) {
				continue;
			}

			// vars
			$text = false;

			// bail early if is search, and $text does not contain $s
			if ( $s !== false && ! $ignore_s ) {

				// get early
				$text = $this->get_clone_setting_choice( $child );

				// search
				if ( stripos( $text, $s ) === false ) {
					continue;
				}
			}

			// $i
			$i++;

			// bail early if $i is out of bounds
			if ( $i < $range_start || $i > $range_end ) {
				continue;
			}

			// load text
			if ( $text === false ) {
				$text = $this->get_clone_setting_choice( $child );
			}

			// append
			$data['children'][] = array(
				'id'   => $child,
				'text' => $text,
			);

		}

		// bail early if no children
		// - this group contained fields, but none shown on this page
		if ( empty( $data['children'] ) ) {
			continue;
		}

		// append
		$results[] = $data;

		// end loop if $i is out of bounds
		// - no need to look further
		if ( $i > $range_end ) {
			break;
		}
	}

	// return
	acf_send_ajax_results(
		array(
			'results' => $results,
			'limit'   => $limit,
		)
	);

}