PluginProbe
Gutenberg / 7.5.0
Gutenberg v7.5.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 7.5.0, at lib/blocks.php

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