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

356 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-time-to-read.php' => 'core/post-time-to-read',
94 'post-title.php' => 'core/post-title',
95 'query.php' => 'core/query',
96 'post-template.php' => 'core/post-template',
97 'query-no-results.php' => 'core/query-no-results',
98 'query-pagination.php' => 'core/query-pagination',
99 'query-pagination-next.php' => 'core/query-pagination-next',
100 'query-pagination-numbers.php' => 'core/query-pagination-numbers',
101 'query-pagination-previous.php' => 'core/query-pagination-previous',
102 'query-title.php' => 'core/query-title',
103 'read-more.php' => 'core/read-more',
104 'rss.php' => 'core/rss',
105 'search.php' => 'core/search',
106 'shortcode.php' => 'core/shortcode',
107 'social-link.php' => 'core/social-link',
108 'site-logo.php' => 'core/site-logo',
109 'site-tagline.php' => 'core/site-tagline',
110 'site-title.php' => 'core/site-title',
111 'tag-cloud.php' => 'core/tag-cloud',
112 'template-part.php' => 'core/template-part',
113 'term-description.php' => 'core/term-description',
114 ),
115 ),
116 __DIR__ . '/../build/edit-widgets/blocks/' => array(
117 'block_folders' => array(
118 'widget-area',
119 ),
120 'block_names' => array(),
121 ),
122 __DIR__ . '/../build/widgets/blocks/' => array(
123 'block_folders' => array(
124 'legacy-widget',
125 'widget-group',
126 ),
127 'block_names' => array(
128 'legacy-widget.php' => 'core/legacy-widget',
129 'widget-group.php' => 'core/widget-group',
130 ),
131 ),
132 );
133 foreach ( $blocks_dirs as $blocks_dir => $details ) {
134 $block_folders = $details['block_folders'];
135 $block_names = $details['block_names'];
136
137 $registry = WP_Block_Type_Registry::get_instance();
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 if ( $registry->is_registered( $metadata['name'] ) ) {
151 $registry->unregister( $metadata['name'] );
152 }
153
154 gutenberg_register_core_block_assets( $folder_name );
155 register_block_type_from_metadata( $block_json_file );
156 }
157
158 foreach ( $block_names as $file => $sub_block_names ) {
159 if ( ! file_exists( $blocks_dir . $file ) ) {
160 continue;
161 }
162
163 $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names;
164 foreach ( $sub_block_names_normalized as $block_name ) {
165 if ( $registry->is_registered( $block_name ) ) {
166 $registry->unregister( $block_name );
167 }
168 gutenberg_register_core_block_assets( $block_name );
169 }
170
171 require_once $blocks_dir . $file;
172 }
173 }
174 }
175
176 add_action( 'init', 'gutenberg_reregister_core_block_types' );
177
178 /**
179 * Registers block styles for a core block.
180 *
181 * @param string $block_name The block-name.
182 *
183 * @return void
184 */
185 function gutenberg_register_core_block_assets( $block_name ) {
186 if ( ! wp_should_load_separate_core_block_assets() ) {
187 return;
188 }
189
190 $block_name = str_replace( 'core/', '', $block_name );
191
192 // When in production, use the plugin's version as the default asset version;
193 // else (for development or test) default to use the current time.
194 $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
195
196 $style_path = "build/block-library/blocks/$block_name/";
197 $stylesheet_url = gutenberg_url( $style_path . 'style.css' );
198 $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' );
199
200 if ( file_exists( $stylesheet_path ) ) {
201 wp_deregister_style( "wp-block-{$block_name}" );
202 wp_register_style(
203 "wp-block-{$block_name}",
204 $stylesheet_url,
205 array(),
206 $default_version
207 );
208 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
209
210 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
211 wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
212 } else {
213 wp_register_style( "wp-block-{$block_name}", false, array() );
214 }
215
216 // If the current theme supports wp-block-styles, dequeue the full stylesheet
217 // and instead attach each block's theme-styles to their block styles stylesheet.
218 if ( current_theme_supports( 'wp-block-styles' ) ) {
219
220 // Dequeue the full stylesheet.
221 // Make sure this only runs once, it doesn't need to run for every block.
222 static $stylesheet_removed;
223 if ( ! $stylesheet_removed ) {
224 add_action(
225 'wp_enqueue_scripts',
226 function() {
227 wp_dequeue_style( 'wp-block-library-theme' );
228 }
229 );
230 $stylesheet_removed = true;
231 }
232
233 // Get the path to the block's stylesheet.
234 $theme_style_path = is_rtl()
235 ? "build/block-library/blocks/$block_name/theme-rtl.css"
236 : "build/block-library/blocks/$block_name/theme.css";
237
238 // If the file exists, enqueue it.
239 if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
240
241 if ( file_exists( $stylesheet_path ) ) {
242 // If there is a main stylesheet for this block, append the theme styles to main styles.
243 wp_add_inline_style(
244 "wp-block-{$block_name}",
245 file_get_contents( gutenberg_dir_path() . $theme_style_path )
246 );
247 } else {
248 // If there is no main stylesheet for this block, register theme style.
249 wp_register_style(
250 "wp-block-{$block_name}",
251 gutenberg_url( $theme_style_path ),
252 array(),
253 $default_version
254 );
255 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $theme_style_path );
256 }
257 }
258 }
259
260 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
261 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
262 wp_deregister_style( "wp-block-{$block_name}-editor" );
263 wp_register_style(
264 "wp-block-{$block_name}-editor",
265 gutenberg_url( $editor_style_path ),
266 array(),
267 $default_version
268 );
269 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
270 } else {
271 wp_register_style( "wp-block-{$block_name}-editor", false );
272 }
273 }
274
275 /**
276 * Complements the implementation of block type `core/social-icon`, whether it
277 * be provided by core or the plugin, with derived block types for each
278 * "service" (WordPress, Twitter, etc.) supported by Social Links.
279 *
280 * This ensures backwards compatibility for any users running the Gutenberg
281 * plugin who have used Social Links prior to their conversion to block
282 * variations.
283 *
284 * This shim is INTENTIONALLY left out of core, as Social Links have never
285 * landed there.
286 *
287 * @see https://github.com/WordPress/gutenberg/pull/19887
288 */
289 function gutenberg_register_legacy_social_link_blocks() {
290 $services = array(
291 'amazon',
292 'bandcamp',
293 'behance',
294 'chain',
295 'codepen',
296 'deviantart',
297 'dribbble',
298 'dropbox',
299 'etsy',
300 'facebook',
301 'feed',
302 'fivehundredpx',
303 'flickr',
304 'foursquare',
305 'goodreads',
306 'google',
307 'github',
308 'instagram',
309 'lastfm',
310 'linkedin',
311 'mail',
312 'mastodon',
313 'meetup',
314 'medium',
315 'pinterest',
316 'pocket',
317 'reddit',
318 'skype',
319 'snapchat',
320 'soundcloud',
321 'spotify',
322 'tumblr',
323 'twitch',
324 'twitter',
325 'vimeo',
326 'vk',
327 'wordpress',
328 'yelp',
329 'youtube',
330 );
331
332 foreach ( $services as $service ) {
333 register_block_type(
334 'core/social-link-' . $service,
335 array(
336 'category' => 'widgets',
337 'attributes' => array(
338 'url' => array(
339 'type' => 'string',
340 ),
341 'service' => array(
342 'type' => 'string',
343 'default' => $service,
344 ),
345 'label' => array(
346 'type' => 'string',
347 ),
348 ),
349 'render_callback' => 'gutenberg_render_block_core_social_link',
350 )
351 );
352 }
353 }
354
355 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
356