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

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