| 1 |
<?php |
| 2 |
/** |
| 3 |
* Custom classname block support flag. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the custom classname block attribute for block types that support it. |
| 10 |
* |
| 11 |
* @param WP_Block_Type $block_type Block Type. |
| 12 |
*/ |
| 13 |
function gutenberg_register_custom_classname_support( $block_type ) { |
| 14 |
$has_custom_classname_support = true; |
| 15 |
if ( property_exists( $block_type, 'supports' ) ) { |
| 16 |
$has_custom_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'customClassName' ), true ); |
| 17 |
} |
| 18 |
if ( $has_custom_classname_support ) { |
| 19 |
if ( ! $block_type->attributes ) { |
| 20 |
$block_type->attributes = array(); |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! array_key_exists( 'className', $block_type->attributes ) ) { |
| 24 |
$block_type->attributes['className'] = array( |
| 25 |
'type' => 'string', |
| 26 |
); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Add the custom classnames to the output. |
| 33 |
* |
| 34 |
* @param WP_Block_Type $block_type Block Type. |
| 35 |
* @param array $block_attributes Block attributes. |
| 36 |
* |
| 37 |
* @return array Block CSS classes and inline styles. |
| 38 |
*/ |
| 39 |
function gutenberg_apply_custom_classname_support( $block_type, $block_attributes ) { |
| 40 |
$has_custom_classname_support = true; |
| 41 |
$attributes = array(); |
| 42 |
if ( property_exists( $block_type, 'supports' ) ) { |
| 43 |
$has_custom_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'customClassName' ), true ); |
| 44 |
} |
| 45 |
if ( $has_custom_classname_support ) { |
| 46 |
$has_custom_classnames = array_key_exists( 'className', $block_attributes ); |
| 47 |
|
| 48 |
if ( $has_custom_classnames ) { |
| 49 |
$attributes['class'] = $block_attributes['className']; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $attributes; |
| 54 |
} |
| 55 |
|
| 56 |
// Register the block support. |
| 57 |
WP_Block_Supports::get_instance()->register( |
| 58 |
'custom-classname', |
| 59 |
array( |
| 60 |
'register_attribute' => 'gutenberg_register_custom_classname_support', |
| 61 |
'apply' => 'gutenberg_apply_custom_classname_support', |
| 62 |
) |
| 63 |
); |
| 64 |
|