spacing.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
  36. * @access private
  37. *
  38. * @param WP_Block_Type $block_type Block Type.
  39. * @param array $block_attributes Block attributes.
  40. * @return array Block spacing CSS classes and inline styles.
  41. */
  42. function wp_apply_spacing_support( $block_type, $block_attributes ) {
  43. if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
  44. return array();
  45. }
  46. $attributes = array();
  47. $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
  48. $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
  49. $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
  50. if ( ! $block_styles ) {
  51. return $attributes;
  52. }
  53. $skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
  54. $skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
  55. $spacing_block_styles = array();
  56. $spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
  57. $spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null;
  58. $styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
  59. if ( ! empty( $styles['css'] ) ) {
  60. $attributes['style'] = $styles['css'];
  61. }
  62. return $attributes;
  63. }
  64. // Register the block support.
  65. WP_Block_Supports::get_instance()->register(
  66. 'spacing',
  67. array(
  68. 'register_attribute' => 'wp_register_spacing_support',
  69. 'apply' => 'wp_apply_spacing_support',
  70. )
  71. );