Nenhuma Descrição

spacing.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Spacing block support flag.
  4. * For backwards compatibility, this remains separate to the dimensions.php
  5. * block support despite both belonging under a single panel in the editor.
  6. *
  7. * @package WordPress
  8. * @since 5.8.0
  9. */
  10. /**
  11. * Registers the style block attribute for block types that support it.
  12. *
  13. * @since 5.8.0
  14. * @access private
  15. *
  16. * @param WP_Block_Type $block_type Block Type.
  17. */
  18. function wp_register_spacing_support( $block_type ) {
  19. $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
  20. // Setup attributes and styles within that if needed.
  21. if ( ! $block_type->attributes ) {
  22. $block_type->attributes = array();
  23. }
  24. if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  25. $block_type->attributes['style'] = array(
  26. 'type' => 'object',
  27. );
  28. }
  29. }
  30. /**
  31. * Add CSS classes for block spacing to the incoming attributes array.
  32. * This will be applied to the block markup in the front-end.
  33. *
  34. * @since 5.8.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 spacing CSS classes and inline styles.
  40. */
  41. function wp_apply_spacing_support( $block_type, $block_attributes ) {
  42. if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
  43. return array();
  44. }
  45. $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
  46. $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
  47. $skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
  48. $skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
  49. $styles = array();
  50. if ( $has_padding_support && ! $skip_padding ) {
  51. $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
  52. if ( is_array( $padding_value ) ) {
  53. foreach ( $padding_value as $key => $value ) {
  54. $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
  55. }
  56. } elseif ( null !== $padding_value ) {
  57. $styles[] = sprintf( 'padding: %s;', $padding_value );
  58. }
  59. }
  60. if ( $has_margin_support && ! $skip_margin ) {
  61. $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
  62. if ( is_array( $margin_value ) ) {
  63. foreach ( $margin_value as $key => $value ) {
  64. $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
  65. }
  66. } elseif ( null !== $margin_value ) {
  67. $styles[] = sprintf( 'margin: %s;', $margin_value );
  68. }
  69. }
  70. return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
  71. }
  72. // Register the block support.
  73. WP_Block_Supports::get_instance()->register(
  74. 'spacing',
  75. array(
  76. 'register_attribute' => 'wp_register_spacing_support',
  77. 'apply' => 'wp_apply_spacing_support',
  78. )
  79. );