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

352 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 'file.php' => 'core/file',
67 'home-link.php' => 'core/home-link',
68 'image.php' => 'core/image',
69 'gallery.php' => 'core/gallery',
70 'latest-comments.php' => 'core/latest-comments',
71 'latest-posts.php' => 'core/latest-posts',
72 'loginout.php' => 'core/loginout',
73 'navigation.php' => 'core/navigation',
74 'navigation-link.php' => 'core/navigation-link',
75 'navigation-submenu.php' => 'core/navigation-submenu',
76 'page-list.php' => 'core/page-list',
77 'pattern.php' => 'core/pattern',
78 'post-author.php' => 'core/post-author',
79 'post-author-name.php' => 'core/post-author-name',
80 'post-author-biography.php' => 'core/post-author-biography',
81 'post-comment.php' => 'core/post-comment',
82 'post-comments.php' => 'core/post-comments',
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/style.css";
195 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
196
197 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
198 wp_deregister_style( "wp-block-{$block_name}" );
199 wp_register_style(
200 "wp-block-{$block_name}",
201 gutenberg_url( $style_path ),
202 array( 'global-styles' ),
203 $default_version
204 );
205 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
206
207 // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
208 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path );
209 } else {
210 wp_register_style( "wp-block-{$block_name}", false, array( 'global-styles' ) );
211 }
212
213 // If the current theme supports wp-block-styles, dequeue the full stylesheet
214 // and instead attach each block's theme-styles to their block styles stylesheet.
215 if ( current_theme_supports( 'wp-block-styles' ) ) {
216
217 // Dequeue the full stylesheet.
218 // Make sure this only runs once, it doesn't need to run for every block.
219 static $stylesheet_removed;
220 if ( ! $stylesheet_removed ) {
221 add_action(
222 'wp_enqueue_scripts',
223 function() {
224 wp_dequeue_style( 'wp-block-library-theme' );
225 }
226 );
227 $stylesheet_removed = true;
228 }
229
230 // Get the path to the block's stylesheet.
231 $theme_style_path = is_rtl()
232 ? "build/block-library/blocks/$block_name/theme-rtl.css"
233 : "build/block-library/blocks/$block_name/theme.css";
234
235 // If the file exists, enqueue it.
236 if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) {
237
238 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
239 // If there is a main stylesheet for this block, append the theme styles to main styles.
240 wp_add_inline_style(
241 "wp-block-{$block_name}",
242 file_get_contents( gutenberg_dir_path() . $theme_style_path )
243 );
244 } else {
245 // If there is no main stylesheet for this block, register theme style.
246 wp_register_style(
247 "wp-block-{$block_name}",
248 gutenberg_url( $theme_style_path ),
249 array( 'global-styles' ),
250 $default_version
251 );
252 wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $theme_style_path );
253 }
254 }
255 }
256
257 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
258 wp_deregister_style( "wp-block-{$block_name}-editor" );
259 wp_register_style(
260 "wp-block-{$block_name}-editor",
261 gutenberg_url( $editor_style_path ),
262 array(),
263 $default_version
264 );
265 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
266 } else {
267 wp_register_style( "wp-block-{$block_name}-editor", false );
268 }
269 }
270
271 /**
272 * Complements the implementation of block type `core/social-icon`, whether it
273 * be provided by core or the plugin, with derived block types for each
274 * "service" (WordPress, Twitter, etc.) supported by Social Links.
275 *
276 * This ensures backwards compatibility for any users running the Gutenberg
277 * plugin who have used Social Links prior to their conversion to block
278 * variations.
279 *
280 * This shim is INTENTIONALLY left out of core, as Social Links have never
281 * landed there.
282 *
283 * @see https://github.com/WordPress/gutenberg/pull/19887
284 */
285 function gutenberg_register_legacy_social_link_blocks() {
286 $services = array(
287 'amazon',
288 'bandcamp',
289 'behance',
290 'chain',
291 'codepen',
292 'deviantart',
293 'dribbble',
294 'dropbox',
295 'etsy',
296 'facebook',
297 'feed',
298 'fivehundredpx',
299 'flickr',
300 'foursquare',
301 'goodreads',
302 'google',
303 'github',
304 'instagram',
305 'lastfm',
306 'linkedin',
307 'mail',
308 'mastodon',
309 'meetup',
310 'medium',
311 'pinterest',
312 'pocket',
313 'reddit',
314 'skype',
315 'snapchat',
316 'soundcloud',
317 'spotify',
318 'tumblr',
319 'twitch',
320 'twitter',
321 'vimeo',
322 'vk',
323 'wordpress',
324 'yelp',
325 'youtube',
326 );
327
328 foreach ( $services as $service ) {
329 register_block_type(
330 'core/social-link-' . $service,
331 array(
332 'category' => 'widgets',
333 'attributes' => array(
334 'url' => array(
335 'type' => 'string',
336 ),
337 'service' => array(
338 'type' => 'string',
339 'default' => $service,
340 ),
341 'label' => array(
342 'type' => 'string',
343 ),
344 ),
345 'render_callback' => 'gutenberg_render_block_core_social_link',
346 )
347 );
348 }
349 }
350
351 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
352