PluginProbe
Gutenberg / 6.8.0
Gutenberg v6.8.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 6.8.0, at lib/blocks.php

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