PluginProbe
Gutenberg / 7.3.0
Gutenberg v7.3.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.3.0, at lib/blocks.php

167 lines 5.8 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 * Retrieves registered social link blocks
10 *
11 * @return array Array of strings containing the registered social link block names.
12 */
13 function gutenberg_get_registered_social_link_blocks() {
14 $social_link_prefix = 'core/social-link';
15 $social_link_prefix_length = strlen( $social_link_prefix );
16
17 $registry = WP_Block_Type_Registry::get_instance();
18 $block_types = $registry->get_all_registered();
19
20 $registered_social_link_blocks = array();
21 foreach ( $block_types as $block_type ) {
22 // Block type name starts with $social_link_prefix.
23 if ( strncmp( $block_type->name, $social_link_prefix, $social_link_prefix_length ) === 0 ) {
24 $registered_social_link_blocks[] = $block_type->name;
25 }
26 }
27 return $registered_social_link_blocks;
28 }
29
30 /**
31 * Substitutes the implementation of a core-registered block type, if exists,
32 * with the built result from the plugin.
33 */
34 function gutenberg_reregister_core_block_types() {
35 // Blocks directory may not exist if working from a fresh clone.
36 $blocks_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/';
37 if ( ! file_exists( $blocks_dir ) ) {
38 return;
39 }
40
41 $block_names = array(
42 'archives.php' => 'core/archives',
43 'block.php' => 'core/block',
44 'calendar.php' => 'core/calendar',
45 'categories.php' => 'core/categories',
46 'latest-comments.php' => 'core/latest-comments',
47 'latest-posts.php' => 'core/latest-posts',
48 'legacy-widget.php' => 'core/legacy-widget',
49 'navigation.php' => 'core/navigation',
50 'rss.php' => 'core/rss',
51 'shortcode.php' => 'core/shortcode',
52 'search.php' => 'core/search',
53 'social-link.php' => gutenberg_get_registered_social_link_blocks(),
54 'tag-cloud.php' => 'core/tag-cloud',
55 'site-title.php' => 'core/site-title',
56 'template-part.php' => 'core/template-part',
57 'post-title.php' => 'core/post-title',
58 'post-content.php' => 'core/post-content',
59 'post-author.php' => 'core/post-author',
60 'post-date.php' => 'core/post-date',
61 'post-excerpt.php' => 'core/post-excerpt',
62 );
63
64 $registry = WP_Block_Type_Registry::get_instance();
65
66 foreach ( $block_names as $file => $block_names ) {
67 if ( ! file_exists( $blocks_dir . $file ) ) {
68 return;
69 }
70
71 if ( is_string( $block_names ) ) {
72 if ( $registry->is_registered( $block_names ) ) {
73 $registry->unregister( $block_names );
74 }
75 } elseif ( is_array( $block_names ) ) {
76 foreach ( $block_names as $block_name ) {
77 if ( $registry->is_registered( $block_name ) ) {
78 $registry->unregister( $block_name );
79 }
80 }
81 }
82
83 require $blocks_dir . $file;
84 }
85 }
86 add_action( 'init', 'gutenberg_reregister_core_block_types' );
87
88 if ( ! function_exists( 'register_block_style' ) ) {
89 /**
90 * Registers a new block style.
91 *
92 * @param string $block_name Block type name including namespace.
93 * @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).
94 *
95 * @return boolean True if the block style was registered with success and false otherwise.
96 */
97 function register_block_style( $block_name, $style_properties ) {
98 return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
99 }
100 }
101
102 if ( ! function_exists( 'unregister_block_style' ) ) {
103 /**
104 * Unregisters a block style.
105 *
106 * @param string $block_name Block type name including namespace.
107 * @param array $block_style_name Block style name.
108 *
109 * @return boolean True if the block style was unregistered with success and false otherwise.
110 */
111 function unregister_block_style( $block_name, $block_style_name ) {
112 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
113 }
114 }
115
116 if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) {
117 /**
118 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
119 */
120 function gutenberg_enqueue_block_styles_assets() {
121 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
122
123 foreach ( $block_styles as $styles ) {
124 foreach ( $styles as $style_properties ) {
125 if ( isset( $style_properties['style_handle'] ) ) {
126 wp_enqueue_style( $style_properties['style_handle'] );
127 }
128 if ( isset( $style_properties['inline_style'] ) ) {
129 wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
130 }
131 }
132 }
133 }
134 add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
135 }
136 if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) {
137 /**
138 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
139 */
140 function gutenberg_enqueue_editor_block_styles_assets() {
141 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
142
143 $register_script_lines = array( '( function() {' );
144 foreach ( $block_styles as $block_name => $styles ) {
145 foreach ( $styles as $style_properties ) {
146 $register_script_lines[] = sprintf(
147 ' wp.blocks.registerBlockStyle( \'%s\', %s );',
148 $block_name,
149 wp_json_encode(
150 array(
151 'name' => $style_properties['name'],
152 'label' => $style_properties['label'],
153 )
154 )
155 );
156 }
157 }
158 $register_script_lines[] = '} )();';
159 $inline_script = implode( "\n", $register_script_lines );
160
161 wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
162 wp_add_inline_script( 'wp-block-styles', $inline_script );
163 wp_enqueue_script( 'wp-block-styles' );
164 }
165 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' );
166 }
167