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