PluginProbe
Gutenberg / 8.4.0
Gutenberg v8.4.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 8.4.0, at lib/blocks.php

299 lines 8.4 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_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/';
15 if ( ! file_exists( $blocks_dir ) ) {
16 return;
17 }
18
19 $block_folders = array(
20 'audio',
21 'button',
22 'buttons',
23 'classic',
24 'code',
25 'column',
26 'columns',
27 'file',
28 'gallery',
29 'group',
30 'heading',
31 'html',
32 'image',
33 'list',
34 'media-text',
35 'missing',
36 'more',
37 'navigation-link',
38 'nextpage',
39 'paragraph',
40 'preformatted',
41 'pullquote',
42 'quote',
43 'separator',
44 'social-links',
45 'spacer',
46 'subhead',
47 'table',
48 'text-columns',
49 'verse',
50 'video',
51 'widget-area',
52 );
53
54 $block_names = array(
55 'archives.php' => 'core/archives',
56 'block.php' => 'core/block',
57 'calendar.php' => 'core/calendar',
58 'categories.php' => 'core/categories',
59 'cover.php' => 'core/cover',
60 'latest-comments.php' => 'core/latest-comments',
61 'latest-posts.php' => 'core/latest-posts',
62 'legacy-widget.php' => 'core/legacy-widget',
63 'navigation.php' => 'core/navigation',
64 'rss.php' => 'core/rss',
65 'search.php' => 'core/search',
66 'shortcode.php' => 'core/shortcode',
67 'social-link.php' => 'core/social-link',
68 'tag-cloud.php' => 'core/tag-cloud',
69 );
70
71 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
72 $block_names = array_merge(
73 $block_names,
74 array(
75 'post-author.php' => 'core/post-author',
76 'post-comments.php' => 'core/post-comments',
77 'post-comments-count.php' => 'core/post-comments-count',
78 'post-comments-form.php' => 'core/post-comments-form',
79 'post-content.php' => 'core/post-content',
80 'post-date.php' => 'core/post-date',
81 'post-excerpt.php' => 'core/post-excerpt',
82 'post-featured-image.php' => 'core/post-featured-image',
83 'post-tags.php' => 'core/post-tags',
84 'post-title.php' => 'core/post-title',
85 'query.php' => 'core/query',
86 'query-loop.php' => 'core/query-loop',
87 'query-pagination.php' => 'core/query-pagination',
88 'site-title.php' => 'core/site-title',
89 'template-part.php' => 'core/template-part',
90 )
91 );
92 }
93
94 $registry = WP_Block_Type_Registry::get_instance();
95
96 foreach ( $block_folders as $folder_name ) {
97 $block_json_file = $blocks_dir . '/' . $folder_name . '/block.json';
98 if ( ! file_exists( $block_json_file ) ) {
99 return;
100 }
101
102 // Ideally, all paths to block metadata files should be listed in
103 // WordPress core. In this place we should rather use filter
104 // to replace paths with overrides defined by the plugin.
105 $metadata = json_decode( file_get_contents( $block_json_file ), true );
106 if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
107 return false;
108 }
109
110 if ( $registry->is_registered( $metadata['name'] ) ) {
111 $registry->unregister( $metadata['name'] );
112 }
113
114 register_block_type_from_metadata( $block_json_file );
115 }
116
117 foreach ( $block_names as $file => $block_names ) {
118 if ( ! file_exists( $blocks_dir . $file ) ) {
119 return;
120 }
121
122 if ( is_string( $block_names ) ) {
123 if ( $registry->is_registered( $block_names ) ) {
124 $registry->unregister( $block_names );
125 }
126 } elseif ( is_array( $block_names ) ) {
127 foreach ( $block_names as $block_name ) {
128 if ( $registry->is_registered( $block_name ) ) {
129 $registry->unregister( $block_name );
130 }
131 }
132 }
133
134 require $blocks_dir . $file;
135 }
136 }
137 add_action( 'init', 'gutenberg_reregister_core_block_types' );
138
139 /**
140 * Complements the implementation of block type `core/social-icon`, whether it
141 * be provided by core or the plugin, with derived block types for each
142 * "service" (WordPress, Twitter, etc.) supported by Social Links.
143 *
144 * This ensures backwards compatibility for any users running the Gutenberg
145 * plugin who have used Social Links prior to their conversion to block
146 * variations.
147 *
148 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
149 * landed there.
150 *
151 * @see https://github.com/WordPress/gutenberg/pull/19887
152 */
153 function gutenberg_register_legacy_social_link_blocks() {
154 $services = array(
155 'amazon',
156 'bandcamp',
157 'behance',
158 'chain',
159 'codepen',
160 'deviantart',
161 'dribbble',
162 'dropbox',
163 'etsy',
164 'facebook',
165 'feed',
166 'fivehundredpx',
167 'flickr',
168 'foursquare',
169 'goodreads',
170 'google',
171 'github',
172 'instagram',
173 'lastfm',
174 'linkedin',
175 'mail',
176 'mastodon',
177 'meetup',
178 'medium',
179 'pinterest',
180 'pocket',
181 'reddit',
182 'skype',
183 'snapchat',
184 'soundcloud',
185 'spotify',
186 'tumblr',
187 'twitch',
188 'twitter',
189 'vimeo',
190 'vk',
191 'wordpress',
192 'yelp',
193 'youtube',
194 );
195
196 foreach ( $services as $service ) {
197 register_block_type(
198 'core/social-link-' . $service,
199 array(
200 'category' => 'widgets',
201 'attributes' => array(
202 'url' => array(
203 'type' => 'string',
204 ),
205 'service' => array(
206 'type' => 'string',
207 'default' => $service,
208 ),
209 'label' => array(
210 'type' => 'string',
211 ),
212 ),
213 'render_callback' => 'gutenberg_render_block_core_social_link',
214 )
215 );
216 }
217 }
218 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
219
220 if ( ! function_exists( 'register_block_style' ) ) {
221 /**
222 * Registers a new block style.
223 *
224 * @param string $block_name Block type name including namespace.
225 * @param array $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).
226 *
227 * @return boolean True if the block style was registered with success and false otherwise.
228 */
229 function register_block_style( $block_name, $style_properties ) {
230 return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
231 }
232 }
233
234 if ( ! function_exists( 'unregister_block_style' ) ) {
235 /**
236 * Unregisters a block style.
237 *
238 * @param string $block_name Block type name including namespace.
239 * @param array $block_style_name Block style name.
240 *
241 * @return boolean True if the block style was unregistered with success and false otherwise.
242 */
243 function unregister_block_style( $block_name, $block_style_name ) {
244 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
245 }
246 }
247
248 if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) {
249 /**
250 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
251 */
252 function gutenberg_enqueue_block_styles_assets() {
253 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
254
255 foreach ( $block_styles as $styles ) {
256 foreach ( $styles as $style_properties ) {
257 if ( isset( $style_properties['style_handle'] ) ) {
258 wp_enqueue_style( $style_properties['style_handle'] );
259 }
260 if ( isset( $style_properties['inline_style'] ) ) {
261 wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
262 }
263 }
264 }
265 }
266 add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
267 }
268 if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) {
269 /**
270 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
271 */
272 function gutenberg_enqueue_editor_block_styles_assets() {
273 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
274
275 $register_script_lines = array( '( function() {' );
276 foreach ( $block_styles as $block_name => $styles ) {
277 foreach ( $styles as $style_properties ) {
278 $register_script_lines[] = sprintf(
279 ' wp.blocks.registerBlockStyle( \'%s\', %s );',
280 $block_name,
281 wp_json_encode(
282 array(
283 'name' => $style_properties['name'],
284 'label' => $style_properties['label'],
285 )
286 )
287 );
288 }
289 }
290 $register_script_lines[] = '} )();';
291 $inline_script = implode( "\n", $register_script_lines );
292
293 wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
294 wp_add_inline_script( 'wp-block-styles', $inline_script );
295 wp_enqueue_script( 'wp-block-styles' );
296 }
297 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' );
298 }
299