align.php 1.7 KB

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