| 1 |
<?php |
| 2 |
/** |
| 3 |
* Align block support flag. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the align block attribute for block types that support it. |
| 10 |
* |
| 11 |
* @param WP_Block_Type $block_type Block Type. |
| 12 |
*/ |
| 13 |
function gutenberg_register_alignment_support( $block_type ) { |
| 14 |
$has_align_support = false; |
| 15 |
if ( property_exists( $block_type, 'supports' ) ) { |
| 16 |
$has_align_support = gutenberg_experimental_get( $block_type->supports, array( 'align' ), false ); |
| 17 |
} |
| 18 |
if ( $has_align_support ) { |
| 19 |
if ( ! $block_type->attributes ) { |
| 20 |
$block_type->attributes = array(); |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! array_key_exists( 'align', $block_type->attributes ) ) { |
| 24 |
$block_type->attributes['align'] = array( |
| 25 |
'type' => 'string', |
| 26 |
'enum' => array( 'left', 'center', 'right', 'wide', 'full', '' ), |
| 27 |
); |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add CSS classes for block alignment to the incoming attributes array. |
| 34 |
* This will be applied to the block markup in the front-end. |
| 35 |
* |
| 36 |
* @param WP_Block_Type $block_type Block Type. |
| 37 |
* @param array $block_attributes Block attributes. |
| 38 |
* |
| 39 |
* @return array Block alignment CSS classes and inline styles. |
| 40 |
*/ |
| 41 |
function gutenberg_apply_alignment_support( $block_type, $block_attributes ) { |
| 42 |
$attributes = array(); |
| 43 |
$has_align_support = false; |
| 44 |
if ( property_exists( $block_type, 'supports' ) ) { |
| 45 |
$has_align_support = gutenberg_experimental_get( $block_type->supports, array( 'align' ), false ); |
| 46 |
} |
| 47 |
if ( $has_align_support ) { |
| 48 |
$has_block_alignment = array_key_exists( 'align', $block_attributes ); |
| 49 |
|
| 50 |
if ( $has_block_alignment ) { |
| 51 |
$attributes['class'] = sprintf( 'align%s', $block_attributes['align'] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $attributes; |
| 56 |
} |
| 57 |
|
| 58 |
// Register the block support. |
| 59 |
WP_Block_Supports::get_instance()->register( |
| 60 |
'align', |
| 61 |
array( |
| 62 |
'register_attribute' => 'gutenberg_register_alignment_support', |
| 63 |
'apply' => 'gutenberg_apply_alignment_support', |
| 64 |
) |
| 65 |
); |
| 66 |
|