acf_field_page_link::ajax_query()publicACF 5.0.0

ajax_query

description

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

Возвращает

$post_id. (int)

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

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

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

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

Код acf_field_page_link::ajax_query() ACF 6.0.4

function ajax_query() {

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

	// defaults
	$options = acf_parse_args(
		$_POST,
		array(
			'post_id'   => 0,
			's'         => '',
			'field_key' => '',
			'paged'     => 1,
		)
	);

	// vars
	$results   = array();
	$args      = array();
	$s         = false;
	$is_search = false;

	// paged
	$args['posts_per_page'] = 20;
	$args['paged']          = $options['paged'];

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

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

		// update vars
		$args['s'] = $s;
		$is_search = true;

	}

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

	// update $args
	if ( ! empty( $field['post_type'] ) ) {

		$args['post_type'] = acf_get_array( $field['post_type'] );

	} else {

		$args['post_type'] = acf_get_post_types();

	}

	// create tax queries
	if ( ! empty( $field['taxonomy'] ) ) {

		// append to $args
		$args['tax_query'] = array();

		// decode terms
		$taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] );

		// now create the tax queries
		foreach ( $taxonomies as $taxonomy => $terms ) {

			$args['tax_query'][] = array(
				'taxonomy' => $taxonomy,
				'field'    => 'slug',
				'terms'    => $terms,
			);

		}
	}

	// filters
	$args = apply_filters( 'acf/fields/page_link/query', $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id'] );
	$args = apply_filters( 'acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id'] );

	// add archives to $results
	if ( $field['allow_archives'] && $args['paged'] == 1 ) {

		// Generate unique list of URLs.
		$links   = array();
		$links[] = home_url();
		foreach ( $args['post_type'] as $post_type ) {
			$links[] = get_post_type_archive_link( $post_type );
		}
		$links = array_filter( $links );
		$links = array_unique( $links );

		// Convert list into choices.
		$children = array();
		foreach ( $links as $link ) {

			// Ignore if search does not match.
			if ( $is_search && stripos( $link, $s ) === false ) {
				continue;
			}
			$children[] = array(
				'id'   => $link,
				'text' => $link,
			);
		}
		if ( $children ) {
			$results[] = array(
				'text'     => __( 'Archives', 'acf' ),
				'children' => $children,
			);
		}
	}

	// get posts grouped by post type
	$groups = acf_get_grouped_posts( $args );

	// loop
	if ( ! empty( $groups ) ) {

		foreach ( array_keys( $groups ) as $group_title ) {

			// vars
			$posts = acf_extract_var( $groups, $group_title );

			// data
			$data = array(
				'text'     => $group_title,
				'children' => array(),
			);

			// convert post objects to post titles
			foreach ( array_keys( $posts ) as $post_id ) {

				$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );

			}

			// order posts by search
			if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {

				$posts = acf_order_by_search( $posts, $args['s'] );

			}

			// append to $data
			foreach ( array_keys( $posts ) as $post_id ) {

				$data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );

			}

			// append to $results
			$results[] = $data;

		}
	}

	// return
	acf_send_ajax_results(
		array(
			'results' => $results,
			'limit'   => $args['posts_per_page'],
		)
	);

}