acf_ajax_fetch_block()ACF 5.7.13

acf_ajax_fetch_block

Handles the ajax request for block data.

Хуков нет.

Возвращает

null. Ничего.

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

acf_ajax_fetch_block();

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

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

Код acf_ajax_fetch_block() ACF 5.10.2

function acf_ajax_fetch_block() {

	// Validate ajax request.
	if ( ! acf_verify_ajax() ) {
		 wp_send_json_error();
	}

	// Get request args.
	extract(
		acf_request_args(
			array(
				'block'   => false,
				'post_id' => 0,
				'query'   => array(),
			)
		)
	);

	// Bail ealry if no block.
	if ( ! $block ) {
		wp_send_json_error();
	}

	// Unslash and decode $_POST data.
	$block = wp_unslash( $block );
	$block = json_decode( $block, true );

	// Prepare block ensuring all settings and attributes exist.
	if ( ! $block = acf_prepare_block( $block ) ) {
		wp_send_json_error();
	}

	// Load field defaults when first previewing a block.
	if ( ! empty( $query['preview'] ) && ! $block['data'] ) {
		$fields = acf_get_block_fields( $block );
		foreach ( $fields as $field ) {
			$block['data'][ "_{$field['name']}" ] = $field['key'];
		}
	}

	// Setup postdata allowing form to load meta.
	acf_setup_meta( $block['data'], $block['id'], true );

	// Vars.
	$response = array();

	// Query form.
	if ( ! empty( $query['form'] ) ) {

		// Load fields for form.
		$fields = acf_get_block_fields( $block );

		// Prefix field inputs to avoid multiple blocks using the same name/id attributes.
		acf_prefix_fields( $fields, "acf-{$block['id']}" );

		// Start Capture.
		ob_start();

		// Render.
		echo '<div class="acf-block-fields acf-fields">';
			acf_render_fields( $fields, $block['id'], 'div', 'field' );
		echo '</div>';

		// Store Capture.
		$response['form'] = ob_get_contents();
		ob_end_clean();
	}

	// Query preview.
	if ( ! empty( $query['preview'] ) ) {

		// Render_callback vars.
		$content    = '';
		$is_preview = true;

		// Render and store HTML.
		$response['preview'] = acf_rendered_block( $block, $content, $is_preview, $post_id );
	}

	// Send repsonse.
	wp_send_json_success( $response );
}