PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.7.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.7.2
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
← All changes | classes/class-parse-blocks.php +180 -136 2.25.03.7.2 View file →
@@ -8,166 +8,210 @@
8 8 /**
9 9 * GhostKit_Parse_Blocks
10 10 */
11 11 class GhostKit_Parse_Blocks {
12 - /**
13 - * Array of content, that already parsed.
14 - *
15 - * @var array
16 - */
17 - public static $parsed_content = array();
12 + /**
13 + * Array of content, that already parsed.
14 + *
15 + * @var array
16 + */
17 + public static $parsed_content = array();
18 18
19 - /**
20 - * Array of reusable block IDs, that already parsed.
21 - *
22 - * @var array
23 - */
24 - public static $parsed_reusable_blocks = array();
19 + /**
20 + * Array of reusable block IDs, that already parsed.
21 + *
22 + * @var array
23 + */
24 + public static $parsed_reusable_blocks = array();
25 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 );
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' ) );
37 35
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();
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 + }
42 41
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 - }
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();
50 46
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;
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 + } else {
51 + // FSE / modern-only: extra sources (e.g. Visual Portfolio) are not in the main query.
52 + self::maybe_parse_blocks_from_content_sources();
53 + }
54 + }
55 + );
56 + }
62 57
63 - self::parse_blocks( array( $block ), 'general' );
58 + /**
59 + * Standard callback to parse blocks (mostly solves problem with FSE themes and blocks inside templates).
60 + *
61 + * @param string $block_content - block content.
62 + * @param array $block - block data.
63 + *
64 + * @return string
65 + */
66 + public static function render_block( $block_content, $block ) {
67 + // We don't need to parse inner blocks manually, because `render_block` filter will make it for us.
68 + $block['innerBlocks'] = false;
64 69
65 - return $block_content;
66 - }
70 + self::parse_blocks( array( $block ), 'general' );
67 71
68 - /**
69 - * Parse blocks from content.
70 - */
71 - public static function maybe_parse_blocks_from_content() {
72 - global $wp_query;
72 + return $block_content;
73 + }
73 74
74 - if ( is_admin() || ! isset( $wp_query->posts ) ) {
75 - return;
76 - }
75 + /**
76 + * Parse blocks from content.
77 + */
78 + public static function maybe_parse_blocks_from_content() {
79 + global $wp_query;
77 80
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 - }
81 + if ( is_admin() || ! isset( $wp_query->posts ) ) {
82 + return;
83 + }
85 84
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 - }
85 + // parse all posts content.
86 + foreach ( $wp_query->posts as $post ) {
87 + if ( isset( $post->post_content ) ) {
88 + self::maybe_parse_blocks( $post->post_content, 'content' );
89 + }
90 + }
95 91
96 - if ( isset( $content ) ) {
97 - self::maybe_parse_blocks( $content, 'content' );
98 - }
92 + self::maybe_parse_blocks_from_content_sources();
93 + }
99 94
100 - return $content;
101 - }
95 + /**
96 + * Parse block content from additional sources (themes, Visual Portfolio, etc.).
97 + *
98 + * Filter `gkt_parse_blocks_content_sources` returns an array of sources:
99 + *
100 + * array(
101 + * array(
102 + * 'content' => string, // Required. Raw post_content–style block markup.
103 + * 'location' => string, // Optional. Default 'content'. Also 'widget'.
104 + * ),
105 + * )
106 + *
107 + * Runs on classic themes with post content and on modern-only themes via `wp`.
108 + */
109 + public static function maybe_parse_blocks_from_content_sources() {
110 + $sources = apply_filters( 'gkt_parse_blocks_content_sources', array() );
102 111
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;
112 + if ( ! is_array( $sources ) || empty( $sources ) ) {
113 + return;
114 + }
118 115
119 - // check if this content is already parsed.
120 - foreach ( self::$parsed_content as $parsed ) {
121 - $is_parsed = $is_parsed || $parsed === $content;
122 - }
116 + foreach ( $sources as $source ) {
117 + if ( ! is_array( $source ) || empty( $source['content'] ) || ! is_string( $source['content'] ) ) {
118 + continue;
119 + }
123 120
124 - if ( ! $is_parsed ) {
125 - $blocks = parse_blocks( $content );
126 - self::parse_blocks( $blocks, $location );
121 + $location = 'content';
122 + if ( ! empty( $source['location'] ) && is_string( $source['location'] ) ) {
123 + $location = $source['location'];
124 + }
127 125
128 - self::$parsed_content[] = $content;
129 - }
130 - }
131 - }
126 + self::maybe_parse_blocks( $source['content'], $location );
127 + }
128 + }
132 129
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 - }
130 + /**
131 + * Parse blocks from custom locations.
132 + *
133 + * @param string $content - custom content.
134 + */
135 + public static function maybe_parse_blocks_from_custom_location( $content ) {
136 + if ( is_admin() ) {
137 + return $content;
138 + }
145 139
146 - do_action( 'ghostkit_parse_blocks', $blocks, $location, $is_reusable, $is_inner_blocks );
140 + if ( isset( $content ) ) {
141 + self::maybe_parse_blocks( $content, 'content' );
142 + }
147 143
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'];
144 + return $content;
145 + }
155 146
156 - $reusable_block = get_post( $block['attrs']['ref'] );
147 + /**
148 + * Maybe parse blocks.
149 + *
150 + * @param string $content - content.
151 + * @param string $location - blocks location [content,widget].
152 + */
153 + public static function maybe_parse_blocks( $content, $location = 'content' ) {
154 + if (
155 + isset( $content ) &&
156 + function_exists( 'has_blocks' ) &&
157 + function_exists( 'parse_blocks' ) &&
158 + $content &&
159 + has_blocks( $content )
160 + ) {
161 + $is_parsed = false;
157 162
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 - }
163 + // check if this content is already parsed.
164 + foreach ( self::$parsed_content as $parsed ) {
165 + $is_parsed = $is_parsed || $parsed === $content;
166 + }
164 167
165 - // Inner blocks.
166 - if ( isset( $block['innerBlocks'] ) ) {
167 - self::parse_blocks( $block['innerBlocks'], $location, $is_reusable, true );
168 - }
169 - }
170 - }
168 + if ( ! $is_parsed ) {
169 + $blocks = parse_blocks( $content );
170 + self::parse_blocks( $blocks, $location );
171 +
172 + self::$parsed_content[] = $content;
173 + }
174 + }
175 + }
176 +
177 + /**
178 + * Parse blocks including reusable and InnerBlocks and call action `ghostkit_parse_blocks`.
179 + *
180 + * @param array $blocks - blocks array.
181 + * @param string $location - blocks location [content,widget].
182 + * @param boolean $is_reusable - is from reusable block.
183 + * @param boolean $is_inner_blocks - is from inner blocks.
184 + */
185 + public static function parse_blocks( $blocks, $location = 'content', $is_reusable = false, $is_inner_blocks = false ) {
186 + if ( ! is_array( $blocks ) || empty( $blocks ) ) {
187 + return;
188 + }
189 +
190 + do_action( 'ghostkit_parse_blocks', $blocks, $location, $is_reusable, $is_inner_blocks );
191 +
192 + foreach ( $blocks as $block ) {
193 + // Reusable Blocks.
194 + if ( isset( $block['blockName'] ) && 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) {
195 + // Check if this reusable block already parsed.
196 + // Fixes possible error with nested reusable blocks.
197 + if ( ! in_array( $block['attrs']['ref'], self::$parsed_reusable_blocks, true ) ) {
198 + self::$parsed_reusable_blocks[] = $block['attrs']['ref'];
199 +
200 + $reusable_block = get_post( $block['attrs']['ref'] );
201 +
202 + if ( has_blocks( $reusable_block ) && isset( $reusable_block->post_content ) ) {
203 + $post_blocks = parse_blocks( $reusable_block->post_content );
204 + self::parse_blocks( $post_blocks, $location, true );
205 + }
206 + }
207 + }
208 +
209 + // Inner blocks.
210 + if ( isset( $block['innerBlocks'] ) ) {
211 + self::parse_blocks( $block['innerBlocks'], $location, $is_reusable, true );
212 + }
213 + }
214 + }
171 215 }
172 216
173 217 GhostKit_Parse_Blocks::init();