Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Created June 19, 2023 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacedmonkey/29c2af943da9d729138eb47ff7a18cff to your computer and use it in GitHub Desktop.
Save spacedmonkey/29c2af943da9d729138eb47ff7a18cff to your computer and use it in GitHub Desktop.
WordPress: Split block styles into maybe enqueues for classic themes.
<?php
/**
*
* @link https://www.spacedmonkey.co.uk
* @since 1.0.0
* @package Style_Splitter
*
* @wordpress-plugin
* Plugin Name: Block style splitter
* Plugin URI: https://www.spacedmonkey.co.uk
* Description: Split block styles into maybe enqueues for classic themes.
* Version: 1.0.0
* Author: Jonny Harris
* Author URI: https://www.spacedmonkey.co.uk
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: style-splitter
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
/**
* Get all post in main loop and sidebar widgets and get there styles.
*
*/
function maybe_inline_all_styles(){
global $wp_query, $wp_registered_widgets, $wp_registered_sidebars;
foreach( $wp_query->get_posts() as $post ){
render_contet_block_styles( $post->post_content );
}
$sidebars_widgets = get_option( 'sidebars_widgets' );
foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
if ( ! empty( $sidebars_widgets[ $index ] ) ) {
foreach ( (array) $sidebars_widgets[ $index ] as $widget ) {
if ( array_key_exists( $widget, $wp_registered_widgets ) ) {
if ( !array_key_exists( 'callback', $wp_registered_widgets ) ) {
continue;
}
foreach( $wp_registered_widgets[$widget]['callback'] as $callback ) {
if (!$callback instanceof WP_Widget_Block) {
continue;
}
foreach ($callback->get_settings() as $setting) {
if (isset($setting['content'])) {
render_contet_block_styles($setting['content']);
}
}
}
}
}
}
}
}
add_action( 'template_redirect', 'maybe_inline_all_styles' );
/**
* Parse content and convert into blocks and enqueue styles.
*
* @param string $content Content with blocks.
*/
function render_contet_block_styles( $content ){
global $post;
$blocks = parse_blocks( $content );
foreach( $blocks as $parsed_block ){
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $parsed_block, null );
$context = array();
if ( $post instanceof WP_Post ) {
$context['postId'] = $post->ID;
$context['postType'] = $post->post_type;
}
$context = apply_filters( 'render_block_context', $context, $parsed_block, null );
$block = new WP_Block( $parsed_block, $context );
if ( ( ! empty( $block->block_type->script_handles ) ) ) {
foreach ( $block->block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}
}
if ( ! empty( $block->block_type->view_script_handles ) ) {
foreach ( $block->block_type->view_script_handles as $view_script_handle ) {
wp_enqueue_script( $view_script_handle );
}
}
if ( ( ! empty( $block->block_type->style_handles ) ) ) {
foreach ( $block->block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
}
}
}
@JiveDig
Copy link

JiveDig commented Jul 5, 2023

Heyo! Got here from Twitter while doing my own testing of per-block styles only on pages where the block is loaded... what's the TL;DR of the benefits of your approach here? If you don't mind sharing ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment