|
1 <?php |
|
2 /** |
|
3 * Generated classname block support flag. |
|
4 * |
|
5 * @package WordPress |
|
6 * @since 5.6.0 |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Get the generated classname from a given block name. |
|
11 * |
|
12 * @since 5.6.0 |
|
13 * |
|
14 * @access private |
|
15 * |
|
16 * @param string $block_name Block Name. |
|
17 * @return string Generated classname. |
|
18 */ |
|
19 function wp_get_block_default_classname( $block_name ) { |
|
20 // Generated HTML classes for blocks follow the `wp-block-{name}` nomenclature. |
|
21 // Blocks provided by WordPress drop the prefixes 'core/' or 'core-' (historically used in 'core-embed/'). |
|
22 $classname = 'wp-block-' . preg_replace( |
|
23 '/^core-/', |
|
24 '', |
|
25 str_replace( '/', '-', $block_name ) |
|
26 ); |
|
27 |
|
28 /** |
|
29 * Filters the default block className for server rendered blocks. |
|
30 * |
|
31 * @since 5.6.0 |
|
32 * |
|
33 * @param string $class_name The current applied classname. |
|
34 * @param string $block_name The block name. |
|
35 */ |
|
36 $classname = apply_filters( 'block_default_classname', $classname, $block_name ); |
|
37 |
|
38 return $classname; |
|
39 } |
|
40 |
|
41 /** |
|
42 * Add the generated classnames to the output. |
|
43 * |
|
44 * @since 5.6.0 |
|
45 * |
|
46 * @access private |
|
47 * |
|
48 * @param WP_Block_Type $block_type Block Type. |
|
49 * |
|
50 * @return array Block CSS classes and inline styles. |
|
51 */ |
|
52 function wp_apply_generated_classname_support( $block_type ) { |
|
53 $attributes = array(); |
|
54 $has_generated_classname_support = block_has_support( $block_type, array( 'className' ), true ); |
|
55 if ( $has_generated_classname_support ) { |
|
56 $block_classname = wp_get_block_default_classname( $block_type->name ); |
|
57 |
|
58 if ( $block_classname ) { |
|
59 $attributes['class'] = $block_classname; |
|
60 } |
|
61 } |
|
62 |
|
63 return $attributes; |
|
64 } |
|
65 |
|
66 // Register the block support. |
|
67 WP_Block_Supports::get_instance()->register( |
|
68 'generated-classname', |
|
69 array( |
|
70 'apply' => 'wp_apply_generated_classname_support', |
|
71 ) |
|
72 ); |