PluginProbe
Gutenberg / 8.2.0
Gutenberg v8.2.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.2.0, at lib/blocks.php

243 lines 7.3 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_names = array(
20 'archives.php' => 'core/archives',
21 'block.php' => 'core/block',
22 'calendar.php' => 'core/calendar',
23 'categories.php' => 'core/categories',
24 'cover.php' => 'core/cover',
25 'latest-comments.php' => 'core/latest-comments',
26 'latest-posts.php' => 'core/latest-posts',
27 'legacy-widget.php' => 'core/legacy-widget',
28 'navigation.php' => 'core/navigation',
29 'rss.php' => 'core/rss',
30 'shortcode.php' => 'core/shortcode',
31 'search.php' => 'core/search',
32 'social-link.php' => 'core/social-link',
33 'tag-cloud.php' => 'core/tag-cloud',
34 );
35
36 if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
37 $block_names = array_merge(
38 $block_names,
39 array(
40 'post-title.php' => 'core/post-title',
41 'post-content.php' => 'core/post-content',
42 'post-author.php' => 'core/post-author',
43 'post-comments.php' => 'core/post-comments',
44 'post-comments-count.php' => 'core/post-comments-count',
45 'post-comments-form.php' => 'core/post-comments-form',
46 'post-date.php' => 'core/post-date',
47 'post-excerpt.php' => 'core/post-excerpt',
48 'post-featured-image.php' => 'core/post-featured-image',
49 'post-tags.php' => 'core/post-tags',
50 'site-title.php' => 'core/site-title',
51 'template-part.php' => 'core/template-part',
52 'query.php' => 'core/query',
53 'query-loop.php' => 'core/query-loop',
54 'query-pagination.php' => 'core/query-pagination',
55 )
56 );
57 }
58
59 $registry = WP_Block_Type_Registry::get_instance();
60
61 foreach ( $block_names as $file => $block_names ) {
62 if ( ! file_exists( $blocks_dir . $file ) ) {
63 return;
64 }
65
66 if ( is_string( $block_names ) ) {
67 if ( $registry->is_registered( $block_names ) ) {
68 $registry->unregister( $block_names );
69 }
70 } elseif ( is_array( $block_names ) ) {
71 foreach ( $block_names as $block_name ) {
72 if ( $registry->is_registered( $block_name ) ) {
73 $registry->unregister( $block_name );
74 }
75 }
76 }
77
78 require $blocks_dir . $file;
79 }
80 }
81 add_action( 'init', 'gutenberg_reregister_core_block_types' );
82
83 /**
84 * Complements the implementation of block type `core/social-icon`, whether it
85 * be provided by core or the plugin, with derived block types for each
86 * "service" (WordPress, Twitter, etc.) supported by Social Links.
87 *
88 * This ensures backwards compatibility for any users running the Gutenberg
89 * plugin who have used Social Links prior to their conversion to block
90 * variations.
91 *
92 * This shim is INTENTIONALLY left out of core, as Social Links haven't yet
93 * landed there.
94 *
95 * @see https://github.com/WordPress/gutenberg/pull/19887
96 */
97 function gutenberg_register_legacy_social_link_blocks() {
98 $services = array(
99 'amazon',
100 'bandcamp',
101 'behance',
102 'chain',
103 'codepen',
104 'deviantart',
105 'dribbble',
106 'dropbox',
107 'etsy',
108 'facebook',
109 'feed',
110 'fivehundredpx',
111 'flickr',
112 'foursquare',
113 'goodreads',
114 'google',
115 'github',
116 'instagram',
117 'lastfm',
118 'linkedin',
119 'mail',
120 'mastodon',
121 'meetup',
122 'medium',
123 'pinterest',
124 'pocket',
125 'reddit',
126 'skype',
127 'snapchat',
128 'soundcloud',
129 'spotify',
130 'tumblr',
131 'twitch',
132 'twitter',
133 'vimeo',
134 'vk',
135 'wordpress',
136 'yelp',
137 'youtube',
138 );
139
140 foreach ( $services as $service ) {
141 register_block_type(
142 'core/social-link-' . $service,
143 array(
144 'category' => 'widgets',
145 'attributes' => array(
146 'url' => array(
147 'type' => 'string',
148 ),
149 'service' => array(
150 'type' => 'string',
151 'default' => $service,
152 ),
153 'label' => array(
154 'type' => 'string',
155 ),
156 ),
157 'render_callback' => 'gutenberg_render_block_core_social_link',
158 )
159 );
160 }
161 }
162 add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
163
164 if ( ! function_exists( 'register_block_style' ) ) {
165 /**
166 * Registers a new block style.
167 *
168 * @param string $block_name Block type name including namespace.
169 * @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).
170 *
171 * @return boolean True if the block style was registered with success and false otherwise.
172 */
173 function register_block_style( $block_name, $style_properties ) {
174 return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
175 }
176 }
177
178 if ( ! function_exists( 'unregister_block_style' ) ) {
179 /**
180 * Unregisters a block style.
181 *
182 * @param string $block_name Block type name including namespace.
183 * @param array $block_style_name Block style name.
184 *
185 * @return boolean True if the block style was unregistered with success and false otherwise.
186 */
187 function unregister_block_style( $block_name, $block_style_name ) {
188 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
189 }
190 }
191
192 if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) {
193 /**
194 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
195 */
196 function gutenberg_enqueue_block_styles_assets() {
197 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
198
199 foreach ( $block_styles as $styles ) {
200 foreach ( $styles as $style_properties ) {
201 if ( isset( $style_properties['style_handle'] ) ) {
202 wp_enqueue_style( $style_properties['style_handle'] );
203 }
204 if ( isset( $style_properties['inline_style'] ) ) {
205 wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
206 }
207 }
208 }
209 }
210 add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
211 }
212 if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) {
213 /**
214 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
215 */
216 function gutenberg_enqueue_editor_block_styles_assets() {
217 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
218
219 $register_script_lines = array( '( function() {' );
220 foreach ( $block_styles as $block_name => $styles ) {
221 foreach ( $styles as $style_properties ) {
222 $register_script_lines[] = sprintf(
223 ' wp.blocks.registerBlockStyle( \'%s\', %s );',
224 $block_name,
225 wp_json_encode(
226 array(
227 'name' => $style_properties['name'],
228 'label' => $style_properties['label'],
229 )
230 )
231 );
232 }
233 }
234 $register_script_lines[] = '} )();';
235 $inline_script = implode( "\n", $register_script_lines );
236
237 wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
238 wp_add_inline_script( 'wp-block-styles', $inline_script );
239 wp_enqueue_script( 'wp-block-styles' );
240 }
241 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' );
242 }
243