PluginProbe
Gutenberg / 15.7.0
Gutenberg v15.7.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 15.7.0, at lib/blocks.php

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