PluginProbe
Gutenberg / 16.0.0
Gutenberg v16.0.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 16.0.0, at lib/blocks.php

373 lines 12.3 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/block-library/blocks/' => array(
16 'block_folders' => array(
17 'audio',
18 'button',
19 'buttons',
20 'freeform',
21 'code',
22 'column',
23 'columns',
24 'comments',
25 'details',
26 'group',
27 'html',
28 'list',
29 'list-item',
30 'media-text',
31 'missing',
32 'more',
33 'nextpage',
34 'paragraph',
35 'preformatted',
36 'pullquote',
37 'quote',
38 'separator',
39 'social-links',
40 'spacer',
41 'table',
42 'table-of-contents',
43 'text-columns',
44 'verse',
45 'video',
46 'embed',
47 ),
48 'block_names' => array(
49 'archives.php' => 'core/archives',
50 'avatar.php' => 'core/avatar',
51 'block.php' => 'core/block',
52 'calendar.php' => 'core/calendar',
53 'categories.php' => 'core/categories',
54 'cover.php' => 'core/cover',
55 'comment-author-avatar.php' => 'core/comment-author-avatar',
56 'comment-author-name.php' => 'core/comment-author-name',
57 'comment-content.php' => 'core/comment-content',
58 'comment-date.php' => 'core/comment-date',
59 'comment-edit-link.php' => 'core/comment-edit-link',
60 'comment-reply-link.php' => 'core/comment-reply-link',
61 'comment-template.php' => 'core/comment-template',
62 'comments-pagination.php' => 'core/comments-pagination',
63 'comments-pagination-next.php' => 'core/comments-pagination-next',
64 'comments-pagination-numbers.php' => 'core/comments-pagination-numbers',
65 'comments-pagination-previous.php' => 'core/comments-pagination-previous',
66 'comments-title.php' => 'core/comments-title',
67 'comments.php' => 'core/comments',
68 'file.php' => 'core/file',
69 'home-link.php' => 'core/home-link',
70 'image.php' => 'core/image',
71 'gallery.php' => 'core/gallery',
72 'heading.php' => 'core/heading',
73 'latest-comments.php' => 'core/latest-comments',
74 'latest-posts.php' => 'core/latest-posts',
75 'loginout.php' => 'core/loginout',
76 'navigation.php' => 'core/navigation',
77 'navigation-link.php' => 'core/navigation-link',
78 'navigation-submenu.php' => 'core/navigation-submenu',
79 'page-list.php' => 'core/page-list',
80 'page-list-item.php' => 'core/page-list-item',
81 'pattern.php' => 'core/pattern',
82 'post-author.php' => 'core/post-author',
83 'post-author-name.php' => 'core/post-author-name',
84 'post-author-biography.php' => 'core/post-author-biography',
85 'post-comment.php' => 'core/post-comment',
86 'post-comments-count.php' => 'core/post-comments-count',
87 'post-comments-form.php' => 'core/post-comments-form',
88 'post-comments-link.php' => 'core/post-comments-link',
89 'post-content.php' => 'core/post-content',
90 'post-date.php' => 'core/post-date',
91 'post-excerpt.php' => 'core/post-excerpt',
92 'post-featured-image.php' => 'core/post-featured-image',
93 'post-navigation-link.php' => 'core/post-navigation-link',
94 'post-terms.php' => 'core/post-terms',
95 'post-time-to-read.php' => 'core/post-time-to-read',
96 'post-title.php' => 'core/post-title',
97 'query.php' => 'core/query',
98 'post-template.php' => 'core/post-template',
99 'query-no-results.php' => 'core/query-no-results',
100 'query-pagination.php' => 'core/query-pagination',
101 'query-pagination-next.php' => 'core/query-pagination-next',
102 'query-pagination-numbers.php' => 'core/query-pagination-numbers',
103 'query-pagination-previous.php' => 'core/query-pagination-previous',
104 'query-title.php' => 'core/query-title',
105 'read-more.php' => 'core/read-more',
106 'rss.php' => 'core/rss',
107 'search.php' => 'core/search',
108 'shortcode.php' => 'core/shortcode',
109 'social-link.php' => 'core/social-link',
110 'site-logo.php' => 'core/site-logo',
111 'site-tagline.php' => 'core/site-tagline',
112 'site-title.php' => 'core/site-title',
113 'tag-cloud.php' => 'core/tag-cloud',
114 'template-part.php' => 'core/template-part',
115 'term-description.php' => 'core/term-description',
116 ),
117 ),
118 __DIR__ . '/../build/edit-widgets/blocks/' => array(
119 'block_folders' => array(
120 'widget-area',
121 ),
122 'block_names' => array(),
123 ),
124 __DIR__ . '/../build/widgets/blocks/' => array(
125 'block_folders' => array(
126 'legacy-widget',
127 'widget-group',
128 ),
129 'block_names' => array(
130 'legacy-widget.php' => 'core/legacy-widget',
131 'widget-group.php' => 'core/widget-group',
132 ),
133 ),
134 );
135 foreach ( $blocks_dirs as $blocks_dir => $details ) {
136 $block_folders = $details['block_folders'];
137 $block_names = $details['block_names'];
138
139 foreach ( $block_folders as $folder_name ) {
140 $block_json_file = $blocks_dir . $folder_name . '/block.json';
141
142 // Ideally, all paths to block metadata files should be listed in
143 // WordPress core. In this place we should rather use filter
144 // to replace paths with overrides defined by the plugin.
145 $metadata = json_decode( file_get_contents( $block_json_file ), true );
146 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
147 continue;
148 }
149
150 gutenberg_deregister_core_block_and_assets( $metadata['name'] );
151 gutenberg_register_core_block_assets( $folder_name );
152 register_block_type_from_metadata( $block_json_file );
153 }
154
155 foreach ( $block_names as $file => $sub_block_names ) {
156 if ( ! file_exists( $blocks_dir . $file ) ) {
157 continue;
158 }
159
160 $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names;
161 foreach ( $sub_block_names_normalized as $block_name ) {
162 gutenberg_deregister_core_block_and_assets( $block_name );
163 gutenberg_register_core_block_assets( $block_name );
164 }
165
166 require_once $blocks_dir . $file;
167 }
168 }
169 }
170
171 add_action( 'init', 'gutenberg_reregister_core_block_types' );
172
173 /**
174 * Deregisters the existing core block type and its assets.
175 *
176 * @param string $block_name The name of the block.
177 *
178 * @return void
179 */
180 function gutenberg_deregister_core_block_and_assets( $block_name ) {
181 $registry = WP_Block_Type_Registry::get_instance();
182 if ( $registry->is_registered( $block_name ) ) {
183 $block_type = $registry->get_registered( $block_name );
184 if ( ! empty( $block_type->view_script_handles ) ) {
185 foreach ( $block_type->view_script_handles as $view_script_handle ) {
186 if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) {
187 wp_deregister_script( $view_script_handle );
188 }
189 }
190 }
191 $registry->unregister( $block_name );
192 }
193 }
194
195 /**
196 * Registers block styles for a core block.
197 *
198 * @param string $block_name The block-name.
199 *
200 * @return void
201 */
202 function gutenberg_register_core_block_assets( $block_name ) {
203 if ( ! wp_should_load_separate_core_block_assets() ) {
204 return;
205 }
206
207 $block_name = str_replace( 'core/', '', $block_name );
208
209 // When in production, use the plugin's version as the default asset version;
210 // else (for development or test) default to use the current time.
211 $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
212
213 $style_path = "build/block-library/blocks/$block_name/";
214 $stylesheet_url = gutenberg_url( $style_path . 'style.css' );
215 $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' );
216
217 if ( file_exists( $stylesheet_path ) ) {
218 wp_deregister_style( "wp-block-{$block_name}" );
219 wp_register_style(
220 "wp-block-{$block_name}",
221 $stylesheet_url,
222 array(),
223 $default_version
224 );
225 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
226
227 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
228 wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
229 } else {
230 wp_register_style( "wp-block-{$block_name}", false, array() );
231 }
232
233 // If the current theme supports wp-block-styles, dequeue the full stylesheet
234 // and instead attach each block's theme-styles to their block styles stylesheet.
235 if ( current_theme_supports( 'wp-block-styles' ) ) {
236
237 // Dequeue the full stylesheet.
238 // Make sure this only runs once, it doesn't need to run for every block.
239 static $stylesheet_removed;
240 if ( ! $stylesheet_removed ) {
241 add_action(
242 'wp_enqueue_scripts',
243 static function() {
244 wp_dequeue_style( 'wp-block-library-theme' );
245 }
246 );
247 $stylesheet_removed = true;
248 }
249
250 // Get the path to the block's stylesheet.
251 $theme_style_path = is_rtl()
252 ? "build/block-library/blocks/$block_name/theme-rtl.css"
253 : "build/block-library/blocks/$block_name/theme.css";
254
255 // If the file exists, enqueue it.
256 if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
257
258 if ( file_exists( $stylesheet_path ) ) {
259 // If there is a main stylesheet for this block, append the theme styles to main styles.
260 wp_add_inline_style(
261 "wp-block-{$block_name}",
262 file_get_contents( gutenberg_dir_path() . $theme_style_path )
263 );
264 } else {
265 // If there is no main stylesheet for this block, register theme style.
266 wp_register_style(
267 "wp-block-{$block_name}",
268 gutenberg_url( $theme_style_path ),
269 array(),
270 $default_version
271 );
272 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $theme_style_path );
273 }
274 }
275 }
276
277 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
278 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
279 wp_deregister_style( "wp-block-{$block_name}-editor" );
280 wp_register_style(
281 "wp-block-{$block_name}-editor",
282 gutenberg_url( $editor_style_path ),
283 array(),
284 $default_version
285 );
286 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
287 } else {
288 wp_register_style( "wp-block-{$block_name}-editor", false );
289 }
290 }
291
292 /**
293 * Complements the implementation of block type `core/social-icon`, whether it
294 * be provided by core or the plugin, with derived block types for each
295 * "service" (WordPress, Twitter, etc.) supported by Social Links.
296 *
297 * This ensures backwards compatibility for any users running the Gutenberg
298 * plugin who have used Social Links prior to their conversion to block
299 * variations.
300 *
301 * This shim is INTENTIONALLY left out of core, as Social Links have never
302 * landed there.
303 *
304 * @see https://github.com/WordPress/gutenberg/pull/19887
305 */
306 function gutenberg_register_legacy_social_link_blocks() {
307 $services = array(
308 'amazon',
309 'bandcamp',
310 'behance',
311 'chain',
312 'codepen',
313 'deviantart',
314 'dribbble',
315 'dropbox',
316 'etsy',
317 'facebook',
318 'feed',
319 'fivehundredpx',
320 'flickr',
321 'foursquare',
322 'goodreads',
323 'google',
324 'github',
325 'instagram',
326 'lastfm',
327 'linkedin',
328 'mail',
329 'mastodon',
330 'meetup',
331 'medium',
332 'pinterest',
333 'pocket',
334 'reddit',
335 'skype',
336 'snapchat',
337 'soundcloud',
338 'spotify',
339 'tumblr',
340 'twitch',
341 'twitter',
342 'vimeo',
343 'vk',
344 'wordpress',
345 'yelp',
346 'youtube',
347 );
348
349 foreach ( $services as $service ) {
350 register_block_type(
351 'core/social-link-' . $service,
352 array(
353 'category' => 'widgets',
354 'attributes' => array(
355 'url' => array(
356 'type' => 'string',
357 ),
358 'service' => array(
359 'type' => 'string',
360 'default' => $service,
361 ),
362 'label' => array(
363 'type' => 'string',
364 ),
365 ),
366 'render_callback' => 'gutenberg_render_block_core_social_link',
367 )
368 );
369 }
370 }
371
372 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
373