PluginProbe
Gutenberg / 4.6.1
Gutenberg v4.6.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / blocks.php

blocks.php in Gutenberg 4.6.1, at lib/blocks.php

272 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions related to editor blocks for the Gutenberg editor plugin.
4 *
5 * @package gutenberg
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 die( 'Silence is golden.' );
10 }
11
12 if ( ! function_exists( 'register_block_type' ) ) {
13 /**
14 * Registers a block type.
15 *
16 * @since 0.1.0
17 * @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter.
18 *
19 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
20 * complete WP_Block_Type instance. In case a WP_Block_Type
21 * is provided, the $args parameter will be ignored.
22 * @param array $args {
23 * Optional. Array of block type arguments. Any arguments may be defined, however the
24 * ones described below are supported by default. Default empty array.
25 *
26 * @type callable $render_callback Callback used to render blocks of this block type.
27 * }
28 * @return WP_Block_Type|false The registered block type on success, or false on failure.
29 */
30 function register_block_type( $name, $args = array() ) {
31 return WP_Block_Type_Registry::get_instance()->register( $name, $args );
32 }
33 }
34
35 if ( ! function_exists( 'unregister_block_type' ) ) {
36 /**
37 * Unregisters a block type.
38 *
39 * @since 0.1.0
40 * @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter.
41 *
42 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
43 * complete WP_Block_Type instance.
44 * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
45 */
46 function unregister_block_type( $name ) {
47 return WP_Block_Type_Registry::get_instance()->unregister( $name );
48 }
49 }
50
51 if ( ! function_exists( 'gutenberg_parse_blocks' ) ) {
52 /**
53 * Parses blocks out of a content string.
54 *
55 * @since 0.5.0
56 *
57 * @param string $content Post content.
58 * @return array Array of parsed block objects.
59 */
60 function gutenberg_parse_blocks( $content ) {
61 /**
62 * Filter to allow plugins to replace the server-side block parser
63 *
64 * @since 3.8.0
65 *
66 * @param string $parser_class Name of block parser class
67 */
68 $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
69 // Load default block parser for server-side parsing if the default parser class is being used.
70 if ( 'WP_Block_Parser' === $parser_class ) {
71 require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';
72 }
73 $parser = new $parser_class();
74 return $parser->parse( $content );
75 }
76 }
77
78 if ( ! function_exists( 'get_dynamic_block_names' ) ) {
79 /**
80 * Returns an array of the names of all registered dynamic block types.
81 *
82 * @return array Array of dynamic block names.
83 */
84 function get_dynamic_block_names() {
85 $dynamic_block_names = array();
86
87 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
88 foreach ( $block_types as $block_type ) {
89 if ( $block_type->is_dynamic() ) {
90 $dynamic_block_names[] = $block_type->name;
91 }
92 }
93
94 return $dynamic_block_names;
95 }
96 }
97
98 if ( ! function_exists( 'get_dynamic_blocks_regex' ) ) {
99 /**
100 * Retrieve the dynamic blocks regular expression for searching.
101 *
102 * @since 3.6.0
103 *
104 * @return string
105 */
106 function get_dynamic_blocks_regex() {
107 $dynamic_block_names = get_dynamic_block_names();
108 $dynamic_block_pattern = (
109 '/<!--\s+wp:(' .
110 str_replace(
111 '/',
112 '\/', // Escape namespace, not handled by preg_quote.
113 str_replace(
114 'core/',
115 '(?:core/)?', // Allow implicit core namespace, but don't capture.
116 implode(
117 '|', // Join block names into capture group alternation.
118 array_map(
119 'preg_quote', // Escape block name for regular expression.
120 $dynamic_block_names
121 )
122 )
123 )
124 ) .
125 ')(\s+(\{.*?\}))?\s+(\/)?-->/'
126 );
127
128 return $dynamic_block_pattern;
129 }
130 }
131
132 /**
133 * Renders a single block into a HTML string.
134 *
135 * @since 1.9.0
136 * @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string
137 * @global WP_Post $post The post to edit.
138 *
139 * @param array $block A single parsed block object.
140 * @return string String of rendered HTML.
141 */
142 function gutenberg_render_block( $block ) {
143 global $post;
144
145 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
146 $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
147 $inner_content = '';
148 $index = 0;
149
150 foreach ( $block['innerContent'] as $chunk ) {
151 $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] );
152 }
153
154 if ( $is_dynamic ) {
155 $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array();
156 $global_post = $post;
157 $output = $block_type->render( $attributes, $inner_content );
158 $post = $global_post;
159
160 return $output;
161 }
162
163 return $inner_content;
164 }
165
166 if ( ! function_exists( 'do_blocks' ) ) {
167 /**
168 * Parses dynamic blocks out of `post_content` and re-renders them.
169 *
170 * @since 0.1.0
171 * @since 4.4.0 performs full parse on input post content
172 *
173 * @param string $content Post content.
174 * @return string Updated post content.
175 */
176 function do_blocks( $content ) {
177 // If there are blocks in this content, we shouldn't run wpautop() on it later.
178 $priority = has_filter( 'the_content', 'wpautop' );
179 if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
180 remove_filter( 'the_content', 'wpautop', $priority );
181 add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
182 }
183
184 $blocks = gutenberg_parse_blocks( $content );
185 $output = '';
186
187 foreach ( $blocks as $block ) {
188 $output .= gutenberg_render_block( $block );
189 }
190
191 return $output;
192 }
193
194 add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed.
195 }
196
197 if ( ! function_exists( '_restore_wpautop_hook' ) ) {
198 /**
199 * If do_blocks() needs to remove wpautop() from the `the_content` filter,
200 * this re-adds it afterwards, for subsequent `the_content` usage.
201 *
202 * @access private
203 *
204 * @since 4.6.0
205 *
206 * @param string $content The post content running through this filter.
207 * @return string The unmodified content.
208 */
209 function _restore_wpautop_hook( $content ) {
210 $current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );
211
212 add_filter( 'the_content', 'wpautop', $current_priority - 1 );
213 remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
214
215 return $content;
216 }
217 }
218
219 if ( ! function_exists( 'strip_dynamic_blocks' ) ) {
220 /**
221 * Remove all dynamic blocks from the given content.
222 *
223 * @since 3.6.0
224 *
225 * @param string $content Content of the current post.
226 * @return string
227 */
228 function strip_dynamic_blocks( $content ) {
229 return preg_replace( get_dynamic_blocks_regex(), '', $content );
230 }
231 }
232
233 if ( ! function_exists( 'strip_dynamic_blocks_add_filter' ) ) {
234 /**
235 * Adds the content filter to strip dynamic blocks from excerpts.
236 *
237 * It's a bit hacky for now, but once this gets merged into core the function
238 * can just be called in `wp_trim_excerpt()`.
239 *
240 * @since 3.6.0
241 *
242 * @param string $text Excerpt.
243 * @return string
244 */
245 function strip_dynamic_blocks_add_filter( $text ) {
246 add_filter( 'the_content', 'strip_dynamic_blocks', 6 );
247
248 return $text;
249 }
250 add_filter( 'get_the_excerpt', 'strip_dynamic_blocks_add_filter', 9 ); // Before wp_trim_excerpt().
251 }
252
253 if ( ! function_exists( 'strip_dynamic_blocks_remove_filter' ) ) {
254 /**
255 * Removes the content filter to strip dynamic blocks from excerpts.
256 *
257 * It's a bit hacky for now, but once this gets merged into core the function
258 * can just be called in `wp_trim_excerpt()`.
259 *
260 * @since 3.6.0
261 *
262 * @param string $text Excerpt.
263 * @return string
264 */
265 function strip_dynamic_blocks_remove_filter( $text ) {
266 remove_filter( 'the_content', 'strip_dynamic_blocks', 6 );
267
268 return $text;
269 }
270 add_filter( 'wp_trim_excerpt', 'strip_dynamic_blocks_remove_filter', 0 ); // Before all other.
271 }
272