Sin descripción

align.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * Add 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. *
  40. * @return array Block alignment CSS classes and inline styles.
  41. */
  42. function wp_apply_alignment_support( $block_type, $block_attributes ) {
  43. $attributes = array();
  44. $has_align_support = block_has_support( $block_type, array( 'align' ), false );
  45. if ( $has_align_support ) {
  46. $has_block_alignment = array_key_exists( 'align', $block_attributes );
  47. if ( $has_block_alignment ) {
  48. $attributes['class'] = sprintf( 'align%s', $block_attributes['align'] );
  49. }
  50. }
  51. return $attributes;
  52. }
  53. // Register the block support.
  54. WP_Block_Supports::get_instance()->register(
  55. 'align',
  56. array(
  57. 'register_attribute' => 'wp_register_alignment_support',
  58. 'apply' => 'wp_apply_alignment_support',
  59. )
  60. );