acf_rendered_block_v3()ACF 6.8

Returns the rendered block HTML for v3 blocks.

Returns

String. The block HTML.

Usage

acf_rendered_block_v3( $attributes, $content, $is_preview, $post_id, $wp_block, $context );
$attributes(array) (required)
The block attributes.
$content(string)
The block content.
Default: ''
$is_preview(true|false)
Whether or not the block is being rendered for editing preview.
Default: false
$post_id(int)
The current post being edited or viewed.
$wp_block(WP_Block)
The block instance (since WP 5.5).
Default: null
$context(array)
The block context array.
Default: false

Changelog

Since 6.8 Introduced.

acf_rendered_block_v3() code ACF 6.8.8

function acf_rendered_block_v3( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false ) {
	// If context is available from the WP_Block class object and we have no context of our own, use that.
	if ( empty( $context ) && ! empty( $wp_block->context ) ) {
		$context = $wp_block->context;
	}

	// Check if we need to generate a block ID.
	$force_new_id = false;
	if ( acf_block_uses_post_meta( $attributes ) && ! empty( $attributes['id'] ) && empty( $attributes['data'] ) ) {
		$force_new_id = true;
	}

	$attributes['id'] = acf_get_block_id( $attributes, $context, $force_new_id );

	// Only use the cache in the block editor for preloading.
	if ( $is_preview ) {
		$cached_block = acf_get_store( 'block-cache' )->get( $attributes['id'] );
		if ( $cached_block ) {
			return $cached_block['html'];
		}
	}

	$validation = false;
	$form       = '';
	$fields     = false;

	$block = acf_prepare_block( $attributes );
	$block = acf_add_block_meta_values( $block, $post_id );
	acf_setup_meta( $block['data'], $block['id'], true );

	// Only render the block form in the admin/preview context to avoid
	// enqueueing editor assets (e.g. wp_editor) on the front end.
	if ( $is_preview ) {
		// Load the block form
		// Set flag for post REST cleanup of media enqueue count during preloads.
		acf_set_data( 'acf_did_render_block_form', true );

		if ( ! empty( $block['validate'] ) ) {
			$validation = acf_get_block_validation_state( $block, false, false, true );
		}

		$fields = acf_get_block_fields( $block );
		if ( $fields ) {
			acf_prefix_fields( $fields, "acf-{$block['id']}" );

			ob_start();
			echo '<div class="acf-block-fields acf-fields" data-block-id="' . esc_attr( $block['id'] ) . '">';
			acf_render_fields( $fields, acf_ensure_block_id_prefix( $block['id'] ), 'div', 'field' );
			echo '</div>';
			$form = ob_get_clean();
		} else {
			ob_start();
			echo acf_get_empty_block_form_html( $attributes['name'] ); //phpcs:ignore -- Output of acf_get_empty_block_form_html() is already escaped via acf_esc_html() for use as text within this HTML container.
			$form = ob_get_clean();
		}

		// Now that the form has been rendered, reset field values
		// as they may need to be different depending on if acf_doing_block_preview is true or false.
		// An example of this is the flexible content field, which shows disabled fields in the form, but not the preview.
		acf_get_store( 'values' )->reset();
	}

	// Capture block render output.
	acf_set_data( 'acf_doing_block_preview', true );

	ob_start();
	acf_render_block( $attributes, $content, $is_preview, $post_id, $wp_block, $context );
	acf_set_data( 'acf_doing_block_preview', false );
	$html = ob_get_clean();
	$html = is_string( $html ) ? $html : '';

	// Replace <InnerBlocks /> placeholder on front-end, or if we're rendering an ACF block inside another ACF block template.
	if ( ! $is_preview || doing_action( 'acf_block_render_template' ) ) {

		// Wrap content in our acf-inner-container wrapper if necessary.
		if ( apply_filters( 'acf/blocks/wrap_frontend_innerblocks', true, $attributes['name'] ) ) {
			// Check for a class (or className) provided in the template to become the InnerBlocks wrapper class.
			$matches = array();
			if ( preg_match( '/<InnerBlocks(?:[^<]+?)(?:class|className)=(?:["\']\W+\s*(?:\w+)\()?["\']([^\'"]+)[\'"]/', $html, $matches ) ) {
				$class = isset( $matches[1] ) ? $matches[1] : 'acf-innerblocks-container';
			} else {
				$class = 'acf-innerblocks-container';
			}
			$content = '<div class="' . $class . '">' . $content . '</div>';
		}

		$html = acf_replace_inner_blocks_in_block_content( $content, $html );
	}

	$block_cache = array(
		'form'   => $form,
		'html'   => $html,
		'fields' => $fields,
	);

	if ( $validation ) {
		// Also store the validation status in the block cache.
		$block_cache['validation'] = $validation;
	}

	$block_toolbar_fields = acf_process_block_toolbar_fields( apply_filters( 'acf/blocks/top_toolbar_fields', array(), $block, $content, $is_preview, $post_id, $wp_block, $context ) );
	if ( ! empty( $block_toolbar_fields ) ) {
		$block_cache['blockToolbarFields'] = $block_toolbar_fields;
	}

	// Store in cache for preloading if we're in the backend.
	acf_get_store( 'block-cache' )->set(
		$attributes['id'],
		$block_cache
	);

	return $html;
}