PluginProbe
Gutenberg / 9.9.0
Gutenberg v9.9.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 9.9.0, at lib/blocks.php

275 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block and style registration functions.
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 'file',
25 'gallery',
26 'group',
27 'heading',
28 'html',
29 'image',
30 'list',
31 'media-text',
32 'missing',
33 'more',
34 'navigation-link',
35 'nextpage',
36 'paragraph',
37 'preformatted',
38 'pullquote',
39 'quote',
40 'separator',
41 'social-links',
42 'spacer',
43 'subhead',
44 'table',
45 'text-columns',
46 'verse',
47 'video',
48 'embed',
49 ),
50 'block_names' => array_merge(
51 array(
52 'archives.php' => 'core/archives',
53 'block.php' => 'core/block',
54 'calendar.php' => 'core/calendar',
55 'categories.php' => 'core/categories',
56 'cover.php' => 'core/cover',
57 'latest-comments.php' => 'core/latest-comments',
58 'latest-posts.php' => 'core/latest-posts',
59 'navigation.php' => 'core/navigation',
60 'navigation-link.php' => 'core/navigation-link',
61 'rss.php' => 'core/rss',
62 'search.php' => 'core/search',
63 'shortcode.php' => 'core/shortcode',
64 'social-link.php' => 'core/social-link',
65 'tag-cloud.php' => 'core/tag-cloud',
66 'post-author.php' => 'core/post-author',
67 'post-comment.php' => 'core/post-comment',
68 'post-comment-author.php' => 'core/post-comment-author',
69 'post-comment-content.php' => 'core/post-comment-content',
70 'post-comment-date.php' => 'core/post-comment-date',
71 'post-comments.php' => 'core/post-comments',
72 'post-comments-count.php' => 'core/post-comments-count',
73 'post-comments-form.php' => 'core/post-comments-form',
74 'post-content.php' => 'core/post-content',
75 'post-date.php' => 'core/post-date',
76 'post-excerpt.php' => 'core/post-excerpt',
77 'post-featured-image.php' => 'core/post-featured-image',
78 'post-hierarchical-terms.php' => 'core/post-hierarchical-terms',
79 'post-tags.php' => 'core/post-tags',
80 'post-title.php' => 'core/post-title',
81 'query.php' => 'core/query',
82 'query-loop.php' => 'core/query-loop',
83 'query-pagination.php' => 'core/query-pagination',
84 'query-pagination-next.php' => 'core/query-pagination-next',
85 'query-pagination-numbers.php' => 'core/query-pagination-numbers',
86 'query-pagination-previous.php' => 'core/query-pagination-previous',
87 'site-logo.php' => 'core/site-logo',
88 'site-tagline.php' => 'core/site-tagline',
89 'site-title.php' => 'core/site-title',
90 'template-part.php' => 'core/template-part',
91 )
92 ),
93 ),
94 __DIR__ . '/../build/edit-widgets/blocks/' => array(
95 'block_folders' => array(
96 'legacy-widget',
97 'widget-area',
98 ),
99 'block_names' => array(
100 'legacy-widget.php' => 'core/legacy-widget',
101 'widget-area.php' => 'core/widget-area',
102 ),
103 ),
104 );
105 foreach ( $blocks_dirs as $blocks_dir => $details ) {
106 $block_folders = $details['block_folders'];
107 $block_names = $details['block_names'];
108
109 $registry = WP_Block_Type_Registry::get_instance();
110
111 foreach ( $block_folders as $folder_name ) {
112 $block_json_file = $blocks_dir . $folder_name . '/block.json';
113
114 // Ideally, all paths to block metadata files should be listed in
115 // WordPress core. In this place we should rather use filter
116 // to replace paths with overrides defined by the plugin.
117 $metadata = json_decode( file_get_contents( $block_json_file ), true );
118 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
119 return false;
120 }
121
122 if ( $registry->is_registered( $metadata['name'] ) ) {
123 $registry->unregister( $metadata['name'] );
124 }
125
126 gutenberg_register_core_block_styles( $folder_name );
127 register_block_type_from_metadata( $block_json_file );
128 }
129
130 foreach ( $block_names as $file => $block_names ) {
131 if ( ! file_exists( $blocks_dir . $file ) ) {
132 return;
133 }
134
135 if ( is_string( $block_names ) ) {
136 if ( $registry->is_registered( $block_names ) ) {
137 $registry->unregister( $block_names );
138 }
139 gutenberg_register_core_block_styles( $block_names );
140 } elseif ( is_array( $block_names ) ) {
141 foreach ( $block_names as $block_name ) {
142 if ( $registry->is_registered( $block_name ) ) {
143 $registry->unregister( $block_name );
144 }
145 gutenberg_register_core_block_styles( $block_name );
146 }
147 }
148
149 require $blocks_dir . $file;
150 }
151 }
152 }
153
154 add_action( 'init', 'gutenberg_reregister_core_block_types' );
155
156 /**
157 * Registers block styles for a core block.
158 *
159 * @param string $block_name The block-name.
160 *
161 * @return void
162 */
163 function gutenberg_register_core_block_styles( $block_name ) {
164 if ( ! gutenberg_should_load_separate_block_styles() ) {
165 return;
166 }
167
168 $block_name = str_replace( 'core/', '', $block_name );
169
170 $style_path = "build/block-library/blocks/$block_name/style.css";
171 $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";
172
173 if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
174 wp_register_style(
175 "wp-block-{$block_name}",
176 gutenberg_url( $style_path ),
177 array(),
178 filemtime( gutenberg_dir_path() . $style_path )
179 );
180 wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );
181 }
182
183 if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
184 wp_register_style(
185 "wp-block-{$block_name}-editor",
186 gutenberg_url( $editor_style_path ),
187 array(),
188 filemtime( gutenberg_dir_path() . $editor_style_path )
189 );
190 wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
191 }
192 }
193
194 /**
195 * Complements the implementation of block type `core/social-icon`, whether it
196 * be provided by core or the plugin, with derived block types for each
197 * "service" (WordPress, Twitter, etc.) supported by Social Links.
198 *
199 * This ensures backwards compatibility for any users running the Gutenberg
200 * plugin who have used Social Links prior to their conversion to block
201 * variations.
202 *
203 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
204 * landed there.
205 *
206 * @see https://github.com/WordPress/gutenberg/pull/19887
207 */
208 function gutenberg_register_legacy_social_link_blocks() {
209 $services = array(
210 'amazon',
211 'bandcamp',
212 'behance',
213 'chain',
214 'codepen',
215 'deviantart',
216 'dribbble',
217 'dropbox',
218 'etsy',
219 'facebook',
220 'feed',
221 'fivehundredpx',
222 'flickr',
223 'foursquare',
224 'goodreads',
225 'google',
226 'github',
227 'instagram',
228 'lastfm',
229 'linkedin',
230 'mail',
231 'mastodon',
232 'meetup',
233 'medium',
234 'pinterest',
235 'pocket',
236 'reddit',
237 'skype',
238 'snapchat',
239 'soundcloud',
240 'spotify',
241 'tumblr',
242 'twitch',
243 'twitter',
244 'vimeo',
245 'vk',
246 'wordpress',
247 'yelp',
248 'youtube',
249 );
250
251 foreach ( $services as $service ) {
252 register_block_type(
253 'core/social-link-' . $service,
254 array(
255 'category' => 'widgets',
256 'attributes' => array(
257 'url' => array(
258 'type' => 'string',
259 ),
260 'service' => array(
261 'type' => 'string',
262 'default' => $service,
263 ),
264 'label' => array(
265 'type' => 'string',
266 ),
267 ),
268 'render_callback' => 'gutenberg_render_block_core_social_link',
269 )
270 );
271 }
272 }
273
274 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
275