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

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