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

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