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

354 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 '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-area.php' => 'core/navigation-area',
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.php' => 'core/post-comments',
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 // 'table-of-contents.php' => 'core/table-of-contents',
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' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
195
196 $style_path = "build/block-library/blocks/$block_name/style.css";
197 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
198
199 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
200 wp_deregister_style( "wp-block-{$block_name}" );
201 wp_register_style(
202 "wp-block-{$block_name}",
203 gutenberg_url( $style_path ),
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', gutenberg_dir_path() . $style_path );
211 } else {
212 wp_register_style( "wp-block-{$block_name}", false );
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( gutenberg_dir_path() . $style_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 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