PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-parse-blocks.php

class-parse-blocks.php in Block Animations, Motion & Scroll Effects – Ghost Kit 2.25.0, at classes/class-parse-blocks.php

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