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

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