PluginProbe
Gutenberg / 7.1.0
Gutenberg v7.1.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.1.0, at lib/blocks.php

164 lines 5.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 * 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 );
60
61 $registry = WP_Block_Type_Registry::get_instance();
62
63 foreach ( $block_names as $file => $block_names ) {
64 if ( ! file_exists( $blocks_dir . $file ) ) {
65 return;
66 }
67
68 if ( is_string( $block_names ) ) {
69 if ( $registry->is_registered( $block_names ) ) {
70 $registry->unregister( $block_names );
71 }
72 } elseif ( is_array( $block_names ) ) {
73 foreach ( $block_names as $block_name ) {
74 if ( $registry->is_registered( $block_name ) ) {
75 $registry->unregister( $block_name );
76 }
77 }
78 }
79
80 require $blocks_dir . $file;
81 }
82 }
83 add_action( 'init', 'gutenberg_reregister_core_block_types' );
84
85 if ( ! function_exists( 'register_block_style' ) ) {
86 /**
87 * Registers a new block style.
88 *
89 * @param string $block_name Block type name including namespace.
90 * @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).
91 *
92 * @return boolean True if the block style was registered with success and false otherwise.
93 */
94 function register_block_style( $block_name, $style_properties ) {
95 return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
96 }
97 }
98
99 if ( ! function_exists( 'unregister_block_style' ) ) {
100 /**
101 * Unregisters a block style.
102 *
103 * @param string $block_name Block type name including namespace.
104 * @param array $block_style_name Block style name.
105 *
106 * @return boolean True if the block style was unregistered with success and false otherwise.
107 */
108 function unregister_block_style( $block_name, $block_style_name ) {
109 return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
110 }
111 }
112
113 if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) {
114 /**
115 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
116 */
117 function gutenberg_enqueue_block_styles_assets() {
118 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
119
120 foreach ( $block_styles as $styles ) {
121 foreach ( $styles as $style_properties ) {
122 if ( isset( $style_properties['style_handle'] ) ) {
123 wp_enqueue_style( $style_properties['style_handle'] );
124 }
125 if ( isset( $style_properties['inline_style'] ) ) {
126 wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
127 }
128 }
129 }
130 }
131 add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
132 }
133 if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) {
134 /**
135 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
136 */
137 function gutenberg_enqueue_editor_block_styles_assets() {
138 $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
139
140 $register_script_lines = array( '( function() {' );
141 foreach ( $block_styles as $block_name => $styles ) {
142 foreach ( $styles as $style_properties ) {
143 $register_script_lines[] = sprintf(
144 ' wp.blocks.registerBlockStyle( \'%s\', %s );',
145 $block_name,
146 wp_json_encode(
147 array(
148 'name' => $style_properties['name'],
149 'label' => $style_properties['label'],
150 )
151 )
152 );
153 }
154 }
155 $register_script_lines[] = '} )();';
156 $inline_script = implode( "\n", $register_script_lines );
157
158 wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
159 wp_add_inline_script( 'wp-block-styles', $inline_script );
160 wp_enqueue_script( 'wp-block-styles' );
161 }
162 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' );
163 }
164