acf_render_block()ACF 5.7.12

Renders the block HTML.

Хуки из функции

Возвращает

void|string.

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

acf_render_block( $attributes, $content, $is_preview, $post_id, $wp_block, $context );
$attributes(массив) (обязательный)
The block attributes.
$content(строка)
The block content.
По умолчанию: ''
$is_preview(true|false)
Whether or not the block is being rendered for editing preview.
По умолчанию: false
$post_id(int)
The current post being edited or viewed.
$wp_block(WP_Block)
The block instance (since WP 5.5).
По умолчанию: null
$context(массив)
The block context array.
По умолчанию: false

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

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

Код acf_render_block() ACF 6.8.8

function acf_render_block( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false ) {

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

	// Track the current block version so acf_inline_text_editing_attrs() and
	// acf_inline_toolbar_editing_attrs() can access it during rendering.
	// Save/restore handles nested blocks (including InnerBlocks) correctly.
	$previous_version = acf_get_data( 'acf_current_block_version' );
	acf_set_data( 'acf_current_block_version', $block['acf_block_version'] ?? null );

	// Find post_id if not defined.
	if ( ! $post_id ) {
		$post_id = get_the_ID();
	}

	// Enqueue block type assets.
	acf_enqueue_block_type_assets( $block );

	$block = acf_add_block_meta_values( $block, $post_id );

	// Setup postdata allowing get_field() to work.
	acf_setup_meta( $block['data'], $block['id'], true );

	// Call render_callback.
	if ( is_callable( $block['render_callback'] ) ) {
		if ( $is_preview && ! empty( $block['auto_inline_editing'] ) && ! empty( $block['acf_block_version'] ) && $block['acf_block_version'] >= 3 ) {
			// In order to allow block render templates to support any html tags,
			// we must assume that escaping has already been properly handled by the block render template here.
			// Typically we'd use something like wp_kses here, but that would limit the HTML tags that can be used.
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo apply_inline_editing_attributes_to_render_callback( $block['render_callback'], $block, $content, $is_preview, $post_id, $wp_block, $context );
		} else {
			call_user_func( $block['render_callback'], $block, $content, $is_preview, $post_id, $wp_block, $context );
		}

		// Or include template.
	} elseif ( $block['render_template'] ) {
		do_action( 'acf_block_render_template', $block, $content, $is_preview, $post_id, $wp_block, $context );
	}

	// Reset postdata.
	acf_reset_meta( $block['id'] );

	// Restore the previous block version for the parent block's context.
	acf_set_data( 'acf_current_block_version', $previous_version );
}