| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse blocks from content and widgets |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* GhostKit_Parse_Blocks |
| 10 |
*/ |
| 11 |
class GhostKit_Parse_Blocks { |
| 12 |
/** |
| 13 |
* Array of content, that already parsed. |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
public static $parsed_content = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Array of reusable block IDs, that already parsed. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
public static $parsed_reusable_blocks = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* Init. |
| 28 |
*/ |
| 29 |
public static function init() { |
| 30 |
add_action( |
| 31 |
'wp', |
| 32 |
function() { |
| 33 |
// Parse methods [classic,modern]. |
| 34 |
$parse_methods = apply_filters( 'gkt_parse_blocks_methods', current_theme_supports( 'block-templates' ) ? array( 'modern' ) : array( 'classic' ) ); |
| 35 |
|
| 36 |
// Simple use `render_block` in FSE themes to enqueue assets. |
| 37 |
if ( in_array( 'modern', $parse_methods, true ) ) { |
| 38 |
// Parse all blocks. |
| 39 |
add_action( 'render_block', 'GhostKit_Parse_Blocks::render_block', 11, 2 ); |
| 40 |
} |
| 41 |
|
| 42 |
// Parse blocks manually from content and custom locations in Classic themes. |
| 43 |
if ( in_array( 'classic', $parse_methods, true ) ) { |
| 44 |
// parse blocks from post content. |
| 45 |
GhostKit_Parse_Blocks::maybe_parse_blocks_from_content(); |
| 46 |
|
| 47 |
// parse blocks from custom locations, that uses 'the_content' filter. |
| 48 |
add_filter( 'the_content', 'GhostKit_Parse_Blocks::maybe_parse_blocks_from_custom_location', 8 ); |
| 49 |
add_filter( 'widget_block_content', 'GhostKit_Parse_Blocks::maybe_parse_blocks_from_custom_location', 8 ); |
| 50 |
} |
| 51 |
} |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Standard callback to parse blocks (mostly solves problem with FSE themes and blocks inside templates). |
| 57 |
* |
| 58 |
* @param string $block_content - block content. |
| 59 |
* @param array $block - block data. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public static function render_block( $block_content, $block ) { |
| 64 |
// We don't need to parse inner blocks manually, because `render_block` filter will make it for us. |
| 65 |
$block['innerBlocks'] = false; |
| 66 |
|
| 67 |
self::parse_blocks( array( $block ), 'general' ); |
| 68 |
|
| 69 |
return $block_content; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Parse blocks from content. |
| 74 |
*/ |
| 75 |
public static function maybe_parse_blocks_from_content() { |
| 76 |
global $wp_query; |
| 77 |
|
| 78 |
if ( is_admin() || ! isset( $wp_query->posts ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// parse all posts content. |
| 83 |
foreach ( $wp_query->posts as $post ) { |
| 84 |
if ( isset( $post->post_content ) ) { |
| 85 |
self::maybe_parse_blocks( $post->post_content, 'content' ); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Parse blocks from custom locations. |
| 92 |
* |
| 93 |
* @param string $content - custom content. |
| 94 |
*/ |
| 95 |
public static function maybe_parse_blocks_from_custom_location( $content ) { |
| 96 |
if ( is_admin() ) { |
| 97 |
return $content; |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $content ) ) { |
| 101 |
self::maybe_parse_blocks( $content, 'content' ); |
| 102 |
} |
| 103 |
|
| 104 |
return $content; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Maybe parse blocks. |
| 109 |
* |
| 110 |
* @param string $content - content. |
| 111 |
* @param string $location - blocks location [content,widget]. |
| 112 |
*/ |
| 113 |
public static function maybe_parse_blocks( $content, $location = 'content' ) { |
| 114 |
if ( |
| 115 |
isset( $content ) && |
| 116 |
function_exists( 'has_blocks' ) && |
| 117 |
function_exists( 'parse_blocks' ) && |
| 118 |
$content && |
| 119 |
has_blocks( $content ) |
| 120 |
) { |
| 121 |
$is_parsed = false; |
| 122 |
|
| 123 |
// check if this content is already parsed. |
| 124 |
foreach ( self::$parsed_content as $parsed ) { |
| 125 |
$is_parsed = $is_parsed || $parsed === $content; |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! $is_parsed ) { |
| 129 |
$blocks = parse_blocks( $content ); |
| 130 |
self::parse_blocks( $blocks, $location ); |
| 131 |
|
| 132 |
self::$parsed_content[] = $content; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Parse blocks including reusable and InnerBlocks and call action `ghostkit_parse_blocks`. |
| 139 |
* |
| 140 |
* @param array $blocks - blocks array. |
| 141 |
* @param string $location - blocks location [content,widget]. |
| 142 |
* @param boolean $is_reusable - is from reusable block. |
| 143 |
* @param boolean $is_inner_blocks - is from inner blocks. |
| 144 |
*/ |
| 145 |
public static function parse_blocks( $blocks, $location = 'content', $is_reusable = false, $is_inner_blocks = false ) { |
| 146 |
if ( ! is_array( $blocks ) || empty( $blocks ) ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
do_action( 'ghostkit_parse_blocks', $blocks, $location, $is_reusable, $is_inner_blocks ); |
| 151 |
|
| 152 |
foreach ( $blocks as $block ) { |
| 153 |
// Reusable Blocks. |
| 154 |
if ( isset( $block['blockName'] ) && 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) { |
| 155 |
// Check if this reusable block already parsed. |
| 156 |
// Fixes possible error with nested reusable blocks. |
| 157 |
if ( ! in_array( $block['attrs']['ref'], self::$parsed_reusable_blocks, true ) ) { |
| 158 |
self::$parsed_reusable_blocks[] = $block['attrs']['ref']; |
| 159 |
|
| 160 |
$reusable_block = get_post( $block['attrs']['ref'] ); |
| 161 |
|
| 162 |
if ( has_blocks( $reusable_block ) && isset( $reusable_block->post_content ) ) { |
| 163 |
$post_blocks = parse_blocks( $reusable_block->post_content ); |
| 164 |
self::parse_blocks( $post_blocks, $location, true ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Inner blocks. |
| 170 |
if ( isset( $block['innerBlocks'] ) ) { |
| 171 |
self::parse_blocks( $block['innerBlocks'], $location, $is_reusable, true ); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
GhostKit_Parse_Blocks::init(); |
| 178 |
|