PluginProbe
Gutenberg / 10.0.1
Gutenberg v10.0.1
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 10.0.1, at lib/blocks.php

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