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

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