acf_field_gallery::ajax_get_sort_order()publicACF 5.0.0

ajax_get_sort_order

description

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

Хуков нет.

Возвращает

$post_id. (int)

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

$acf_field_gallery = new acf_field_gallery();
$acf_field_gallery->ajax_get_sort_order();

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

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

Код acf_field_gallery::ajax_get_sort_order() ACF 6.0.4

function ajax_get_sort_order() {

	// vars
	$r     = array();
	$order = 'DESC';
	$args  = acf_parse_args(
		$_POST, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified below.
		array(
			'ids'       => 0,
			'sort'      => 'date',
			'field_key' => '',
			'nonce'     => '',
		)
	);

	// validate
	if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {

		wp_send_json_error();

	}

	// reverse
	if ( $args['sort'] == 'reverse' ) {

		$ids = array_reverse( $args['ids'] );

		wp_send_json_success( $ids );

	}

	if ( $args['sort'] == 'title' ) {

		$order = 'ASC';

	}

	// find attachments (DISTINCT POSTS)
	$ids = get_posts(
		array(
			'post_type'   => 'attachment',
			'numberposts' => -1,
			'post_status' => 'any',
			'post__in'    => $args['ids'],
			'order'       => $order,
			'orderby'     => $args['sort'],
			'fields'      => 'ids',
		)
	);

	// success
	if ( ! empty( $ids ) ) {

		wp_send_json_success( $ids );

	}

	// failure
	wp_send_json_error();

}