PluginProbe
Gutenberg / 14.3.0
Gutenberg v14.3.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 14.3.0, at lib/blocks.php

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