acf_enqueue_block_assets()
Enqueues and localizes block scripts and styles.
Hooks from the function
Returns
null. Nothing (null).
Usage
acf_enqueue_block_assets();
Changelog
| Since 5.7.13 | Introduced. |
acf_enqueue_block_assets() acf enqueue block assets code ACF 6.8.8
function acf_enqueue_block_assets() {
// Localize text.
acf_localize_text(
array(
'Switch to Edit' => __( 'Switch to Edit', 'acf' ),
'Switch to Preview' => __( 'Switch to Preview', 'acf' ),
'Change content alignment' => __( 'Change content alignment', 'acf' ),
'Error previewing block' => __( 'An error occurred when loading the preview for this block.', 'acf' ),
'Error loading block form' => __( 'An error occurred when loading the block in edit mode.', 'acf' ),
'Edit Block' => __( 'Edit Block', 'acf' ),
'Open Expanded Editor' => __( 'Open Expanded Editor', 'acf' ),
'Error previewing block v3' => __( 'The preview for this block couldn’t be loaded. Review its content or settings for issues.', 'acf' ),
'ACF Block' => __( 'ACF Block', 'acf' ),
'Done' => __( 'Done', 'acf' ),
/* translators: %s: Block type title */
'%s settings' => __( '%s settings', 'acf' ),
)
);
// Get block types.
$block_types = array_map(
function ( $block ) {
// Render Callback may contain a incompatible class for JSON encoding. Turn it into a boolean for the frontend.
$block['render_callback'] = ! empty( $block['render_callback'] );
// Get the expanded editor button text from block.json (if set).
$block_json_text = ! empty( $block['expanded_editor_button_text'] ) ? $block['expanded_editor_button_text'] : '';
/**
* Filters the default expanded editor button text for blocks.
*
* This filter provides a global default for the "Open Expanded Editor" button text.
* The block.json `expandedEditorButtonText` setting takes precedence over this filter.
*
* @since 6.8.0
*
* @param string $button_text The default button text. Empty string uses "Open Expanded Editor" translation.
* @param array $block The block configuration array.
* @return string The filtered default button text.
*/
$default_text = apply_filters( 'acf/blocks/default_expanded_editor_button_text', '', $block );
// Block.json setting takes precedence over the filter default.
$button_text = $block_json_text ? $block_json_text : $default_text;
// Sanitize the button text to prevent XSS.
$block['expanded_editor_button_text'] = is_string( $button_text ) ? acf_esc_html( $button_text ) : '';
return $block;
},
acf_get_block_types()
);
// Localize data.
acf_localize_data(
array(
'blockTypes' => array_values( $block_types ),
'postType' => get_post_type(),
)
);
// Enqueue script.
$min = defined( 'ACF_DEVELOPMENT_MODE' ) && ACF_DEVELOPMENT_MODE ? '' : '.min';
$blocks_js_path = acf_get_url( "assets/build/js/pro/acf-pro-blocks{$min}.js" );
wp_enqueue_script( 'acf-blocks', $blocks_js_path, array( 'acf-input', 'wp-blocks' ), ACF_VERSION, true );
// Enqueue block assets.
array_map( 'acf_enqueue_block_type_assets', $block_types );
// During the edit screen loading, WordPress renders all blocks in its own attempt to preload data.
// Retrieve any cached block HTML and include this in the localized data.
if ( acf_get_setting( 'preload_blocks' ) ) {
$preloaded_blocks = acf_get_store( 'block-cache' )->get_data();
acf_localize_data(
array(
'preloadedBlocks' => $preloaded_blocks,
)
);
}
}