PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
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 22.3.0, at lib/blocks.php

386 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block functions specific for the Gutenberg editor plugin.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Substitutes the implementation of a core-registered block type, if exists,
10 * with the built result from the plugin.
11 */
12 function gutenberg_reregister_core_block_types() {
13 // Blocks directory may not exist if working from a fresh clone.
14 $blocks_dirs = array(
15 __DIR__ . '/../build/scripts/block-library/',
16 __DIR__ . '/../build/scripts/edit-widgets/blocks/',
17 __DIR__ . '/../build/scripts/widgets/blocks/',
18 );
19
20 foreach ( $blocks_dirs as $blocks_dir ) {
21 if ( ! is_dir( $blocks_dir ) ) {
22 continue;
23 }
24
25 // Scan for block directories (those with block.json).
26 $block_json_files = glob( $blocks_dir . '*/block.json' );
27
28 foreach ( $block_json_files as $block_json_file ) {
29 $block_folder = dirname( $block_json_file );
30 $block_name_folder = basename( $block_folder );
31
32 // Read block.json to get block name.
33 $metadata = json_decode( file_get_contents( $block_json_file ), true );
34 if ( ! is_array( $metadata ) || ! isset( $metadata['name'] ) ) {
35 continue;
36 }
37
38 // Deregister core version.
39 gutenberg_deregister_core_block_and_assets( $metadata['name'] );
40 gutenberg_register_core_block_assets( $block_name_folder );
41 $php_file = dirname( $block_folder ) . '/' . $block_name_folder . '.php';
42 if ( file_exists( $php_file ) ) {
43 require_once $php_file;
44 } else {
45 register_block_type_from_metadata( $block_json_file );
46 }
47 }
48 }
49 }
50
51 add_action( 'init', 'gutenberg_reregister_core_block_types' );
52
53 /**
54 * Adds the defer loading strategy to all registered blocks.
55 *
56 * This function would not be part of core merge. Instead, the register_block_script_handle() function would be patched
57 * as follows.
58 *
59 * ```
60 * --- a/wp-includes/blocks.php
61 * +++ b/wp-includes/blocks.php
62 * @ @ -153,7 +153,8 @ @ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
63 * $script_handle,
64 * $script_uri,
65 * $script_dependencies,
66 * - isset( $script_asset['version'] ) ? $script_asset['version'] : false
67 * + isset( $script_asset['version'] ) ? $script_asset['version'] : false,
68 * + array( 'strategy' => 'defer' )
69 * );
70 * if ( ! $result ) {
71 * return false;
72 * ```
73 *
74 * @see register_block_script_handle()
75 */
76 function gutenberg_defer_block_view_scripts() {
77 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
78 foreach ( $block_types as $block_type ) {
79 foreach ( $block_type->view_script_handles as $view_script_handle ) {
80 wp_script_add_data( $view_script_handle, 'strategy', 'defer' );
81 }
82 }
83 }
84
85 add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 );
86
87 /**
88 * Deregisters the existing core block type and its assets.
89 *
90 * @param string $block_name The name of the block.
91 *
92 * @return void
93 */
94 function gutenberg_deregister_core_block_and_assets( $block_name ) {
95 $registry = WP_Block_Type_Registry::get_instance();
96 if ( $registry->is_registered( $block_name ) ) {
97 $block_type = $registry->get_registered( $block_name );
98 if ( ! empty( $block_type->view_script_handles ) ) {
99 foreach ( $block_type->view_script_handles as $view_script_handle ) {
100 if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) {
101 wp_deregister_script( $view_script_handle );
102 }
103 }
104 }
105 $registry->unregister( $block_name );
106 }
107 }
108
109 /**
110 * Registers block styles for a core block.
111 *
112 * @param string $block_name The block-name.
113 *
114 * @return void
115 */
116 function gutenberg_register_core_block_assets( $block_name ) {
117 if ( ! wp_should_load_separate_core_block_assets() ) {
118 return;
119 }
120
121 $block_name = str_replace( 'core/', '', $block_name );
122
123 // When in production, use the plugin's version as the default asset version;
124 // else (for development or test) default to use the current time.
125 $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
126
127 $style_path = "build/styles/block-library/$block_name/";
128 $stylesheet_url = gutenberg_url( $style_path . 'style.css' );
129 $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' );
130
131 if ( file_exists( $stylesheet_path ) ) {
132
133 wp_deregister_style( "wp-block-{$block_name}" );
134 wp_register_style(
135 "wp-block-{$block_name}",
136 $stylesheet_url,
137 array(),
138 $default_version
139 );
140 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
141 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
142 wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
143 } else {
144 wp_register_style( "wp-block-{$block_name}", false, array() );
145 }
146
147 /*
148 * If the current theme supports wp-block-styles, dequeue the core styles
149 * and enqueue the plugin ones instead.
150 */
151 if ( current_theme_supports( 'wp-block-styles' ) ) {
152
153 // Get the path to the block's stylesheet.
154 $theme_style_path = is_rtl()
155 ? "build/styles/block-library/$block_name/theme-rtl.css"
156 : "build/styles/block-library/$block_name/theme.css";
157
158 // If the file exists, enqueue it.
159 if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
160 wp_deregister_style( "wp-block-{$block_name}-theme" );
161 wp_register_style(
162 "wp-block-{$block_name}-theme",
163 gutenberg_url( $theme_style_path ),
164 array(),
165 $default_version
166 );
167 wp_style_add_data( "wp-block-{$block_name}-theme", 'path', gutenberg_dir_path() . $theme_style_path );
168 }
169 }
170
171 $editor_style_path = "build/styles/block-library/$block_name/style-editor.css";
172 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
173 wp_deregister_style( "wp-block-{$block_name}-editor" );
174 wp_register_style(
175 "wp-block-{$block_name}-editor",
176 gutenberg_url( $editor_style_path ),
177 array(),
178 $default_version
179 );
180 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
181 } else {
182 wp_register_style( "wp-block-{$block_name}-editor", false );
183 }
184 }
185
186 /**
187 * Complements the implementation of block type `core/social-icon`, whether it
188 * be provided by core or the plugin, with derived block types for each
189 * "service" (WordPress, Twitter, etc.) supported by Social Links.
190 *
191 * This ensures backwards compatibility for any users running the Gutenberg
192 * plugin who have used Social Links prior to their conversion to block
193 * variations.
194 *
195 * This shim is INTENTIONALLY left out of core, as Social Links have never
196 * landed there.
197 *
198 * @see https://github.com/WordPress/gutenberg/pull/19887
199 */
200 function gutenberg_register_legacy_social_link_blocks() {
201 $services = array(
202 'amazon',
203 'bandcamp',
204 'behance',
205 'chain',
206 'codepen',
207 'deviantart',
208 'dribbble',
209 'dropbox',
210 'etsy',
211 'facebook',
212 'feed',
213 'fivehundredpx',
214 'flickr',
215 'foursquare',
216 'goodreads',
217 'google',
218 'github',
219 'instagram',
220 'lastfm',
221 'linkedin',
222 'mail',
223 'mastodon',
224 'meetup',
225 'medium',
226 'pinterest',
227 'pocket',
228 'reddit',
229 'skype',
230 'snapchat',
231 'soundcloud',
232 'spotify',
233 'tumblr',
234 'twitch',
235 'twitter',
236 'vimeo',
237 'vk',
238 'wordpress',
239 'yelp',
240 'youtube',
241 );
242
243 foreach ( $services as $service ) {
244 register_block_type(
245 'core/social-link-' . $service,
246 array(
247 'category' => 'widgets',
248 'attributes' => array(
249 'url' => array(
250 'type' => 'string',
251 ),
252 'service' => array(
253 'type' => 'string',
254 'default' => $service,
255 ),
256 'label' => array(
257 'type' => 'string',
258 ),
259 ),
260 'render_callback' => 'gutenberg_render_block_core_social_link',
261 )
262 );
263 }
264 }
265
266 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
267
268 /**
269 * Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1).
270 *
271 * This filter is INTENTIONALLY left out of core as the meta key was fist introduced to core in 6.3 as `wp_pattern_sync_status`.
272 * see https://github.com/WordPress/gutenberg/pull/52232
273 *
274 * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single.
275 * @param int $object_id ID of the object metadata is for.
276 * @param string $meta_key Metadata key.
277 * @param bool $single Whether to return only the first value of the specified $meta_key.
278 */
279 function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) {
280 if ( 'wp_pattern_sync_status' !== $meta_key ) {
281 return $value;
282 }
283
284 $sync_status = get_post_meta( $object_id, 'sync_status', $single );
285
286 if ( $single && 'unsynced' === $sync_status ) {
287 return $sync_status;
288 } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) {
289 return $sync_status;
290 }
291
292 return $value;
293 }
294
295 add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 );
296
297
298
299 /**
300 * Strips all HTML from the content of footnotes, and sanitizes the ID.
301 *
302 * This function expects slashed data on the footnotes content.
303 *
304 * @access private
305 *
306 * @param string $footnotes JSON encoded string of an array containing the content and ID of each footnote.
307 * @return string Filtered content without any HTML on the footnote content and with the sanitized id.
308 */
309 function _gutenberg_filter_post_meta_footnotes( $footnotes ) {
310 $footnotes_decoded = json_decode( $footnotes, true );
311 if ( ! is_array( $footnotes_decoded ) ) {
312 return '';
313 }
314 $footnotes_sanitized = array();
315 foreach ( $footnotes_decoded as $footnote ) {
316 if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
317 $footnotes_sanitized[] = array(
318 'id' => sanitize_key( $footnote['id'] ),
319 'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
320 );
321 }
322 }
323 return wp_json_encode( $footnotes_sanitized );
324 }
325
326 /**
327 * Adds the filters to filter footnotes meta field.
328 *
329 * @access private
330 */
331 function _gutenberg_footnotes_kses_init_filters() {
332 add_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
333 }
334
335 /**
336 * Removes the filters that filter footnotes meta field.
337 *
338 * @access private
339 */
340 function _gutenberg_footnotes_remove_filters() {
341 remove_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' );
342 }
343
344 /**
345 * Registers the filter of footnotes meta field if the user does not have unfiltered_html capability.
346 *
347 * @access private
348 */
349 function _gutenberg_footnotes_kses_init() {
350 if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
351 return;
352 }
353 _gutenberg_footnotes_remove_filters();
354 if ( ! current_user_can( 'unfiltered_html' ) ) {
355 _gutenberg_footnotes_kses_init_filters();
356 }
357 }
358
359 /**
360 * Initializes footnotes meta field filters when imported data should be filtered.
361 *
362 * This filter is the last being executed on force_filtered_html_on_import.
363 * If the input of the filter is true it means we are in an import situation and should
364 * enable kses, independently of the user capabilities.
365 * So in that case we call _gutenberg_footnotes_kses_init_filters;
366 *
367 * @access private
368 *
369 * @param string $arg Input argument of the filter.
370 * @return string Input argument of the filter.
371 */
372 function _gutenberg_footnotes_force_filtered_html_on_import_filter( $arg ) {
373 if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) {
374 return;
375 }
376 // force_filtered_html_on_import is true we need to init the global styles kses filters.
377 if ( $arg ) {
378 _gutenberg_footnotes_kses_init_filters();
379 }
380 return $arg;
381 }
382
383 add_action( 'init', '_gutenberg_footnotes_kses_init' );
384 add_action( 'set_current_user', '_gutenberg_footnotes_kses_init' );
385 add_filter( 'force_filtered_html_on_import', '_gutenberg_footnotes_force_filtered_html_on_import_filter', 999 );
386