acf_rendered_block()ACF 5.7.13

Returns the rendered block HTML.

Возвращает

Строку. The block HTML.

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

acf_rendered_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.13 Введена.

Код acf_rendered_block() ACF 6.0.4

function acf_rendered_block( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false ) {
	$mode = isset( $attributes['mode'] ) ? $attributes['mode'] : 'auto';
	$form = ( 'edit' === $mode && $is_preview );

	// 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.
	$attributes['id'] = acf_get_block_id( $attributes, $context );

	// Check if we've already got a cache of this block ID and return it to save rendering if we're in the backend.
	if ( $is_preview ) {
		$cached_block = acf_get_store( 'block-cache' )->get( $attributes['id'] );
		if ( $cached_block ) {
			if ( $form ) {
				if ( $cached_block['form'] ) {
					return $cached_block['html'];
				}
			} else {
				if ( ! $cached_block['form'] ) {
					return $cached_block['html'];
				}
			}
		}
	}

	ob_start();

	if ( $form ) {
		// Load the block form since we're in edit mode.

		// Set flag for post REST cleanup of media enqueue count during preloads.
		acf_set_data( 'acf_did_render_block_form', true );

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

			echo '<div class="acf-block-fields acf-fields">';
			acf_render_fields( $fields, $block['id'], 'div', 'field' );
			echo '</div>';
		} else {
			echo acf_get_empty_block_form_html( $attributes['name'] ); //phpcs:ignore -- escaped in function.
		}
	} else {
		// Capture block render output.
		acf_render_block( $attributes, $content, $is_preview, $post_id, $wp_block, $context );
	}
	$html = ob_get_clean();

	// 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' ) ) {
		// Escape "$" character to avoid "capture group" interpretation.
		$content = str_replace( '$', '\$', $content );

		// Wrap content in our acf-inner-container wrapper if necessary.
		if ( $wp_block && $wp_block->block_type->acf_block_version > 1 && 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 = preg_replace( '/<InnerBlocks([\S\s]*?)\/>/', $content, $html );
	}

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

	// Prevent edit forms being output to rest endpoints.
	if ( $form && acf_get_data( 'acf_inside_rest_call' ) && apply_filters( 'acf/blocks/prevent_edit_forms_on_rest_endpoints', true ) ) {
		return '';
	}

	return $html;
}