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, $is_ajax_render );
- $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
- $is_ajax_render(true|false)
- Whether or not this is an ACF AJAX render.
По умолчанию: false
Список изменений
Код acf_rendered_block() acf rendered block
ACF 6.4.2
function acf_rendered_block( $attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = false, $is_ajax_render = 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.
$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 );
// 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'];
}
} elseif ( ! $cached_block['form'] ) {
return $cached_block['html'];
}
}
}
ob_start();
$validation = false;
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 );
$block = acf_add_block_meta_values( $block, $post_id );
acf_setup_meta( $block['data'], $block['id'], 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']}" );
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>';
} 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 );
if ( $is_preview && ! $is_ajax_render ) {
/**
* If we're in preloaded preview, we need to get the validation state for a preview too.
* Because the block render resets meta once it's finished to not pollute $post_id, we need to redo that process here.
*/
$block = acf_prepare_block( $attributes );
$block = acf_add_block_meta_values( $block, $post_id );
acf_setup_meta( $block['data'], $block['id'], true );
if ( ! empty( $block['validate'] ) ) {
$validation = acf_get_block_validation_state( $block, false, false, true );
}
}
}
$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' ) ) {
// 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 );
}
$block_cache = array(
'form' => $form,
'html' => $html,
);
if ( $is_preview && $validation ) {
// If we're in the preview, also store the validation status in the block cache.
$block_cache['validation'] = $validation;
}
// Store in cache for preloading if we're in the backend.
acf_get_store( 'block-cache' )->set(
$attributes['id'],
$block_cache
);
// 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;
}