Нет описания

spacing.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Spacing block support flag.
  4. *
  5. * @package WordPress
  6. * @since 5.8.0
  7. */
  8. /**
  9. * Registers the style block attribute for block types that support it.
  10. *
  11. * @since 5.8.0
  12. * @access private
  13. *
  14. * @param WP_Block_Type $block_type Block Type.
  15. */
  16. function wp_register_spacing_support( $block_type ) {
  17. $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
  18. // Setup attributes and styles within that if needed.
  19. if ( ! $block_type->attributes ) {
  20. $block_type->attributes = array();
  21. }
  22. if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  23. $block_type->attributes['style'] = array(
  24. 'type' => 'object',
  25. );
  26. }
  27. }
  28. /**
  29. * Add CSS classes for block spacing to the incoming attributes array.
  30. * This will be applied to the block markup in the front-end.
  31. *
  32. * @since 5.8.0
  33. * @access private
  34. *
  35. * @param WP_Block_Type $block_type Block Type.
  36. * @param array $block_attributes Block attributes.
  37. *
  38. * @return array Block spacing CSS classes and inline styles.
  39. */
  40. function wp_apply_spacing_support( $block_type, $block_attributes ) {
  41. $has_padding_support = wp_has_spacing_feature_support( $block_type, 'padding' );
  42. $has_margin_support = wp_has_spacing_feature_support( $block_type, 'margin' );
  43. $styles = array();
  44. if ( $has_padding_support ) {
  45. $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
  46. if ( null !== $padding_value ) {
  47. foreach ( $padding_value as $key => $value ) {
  48. $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
  49. }
  50. }
  51. }
  52. if ( $has_margin_support ) {
  53. $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
  54. if ( null !== $margin_value ) {
  55. foreach ( $margin_value as $key => $value ) {
  56. $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
  57. }
  58. }
  59. }
  60. return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
  61. }
  62. /**
  63. * Checks whether the current block type supports the spacing feature requested.
  64. *
  65. * @since 5.8.0
  66. * @access private
  67. *
  68. * @param WP_Block_Type $block_type Block type to check for support.
  69. * @param string $feature Name of the feature to check support for.
  70. * @param mixed $default Fallback value for feature support, defaults to false.
  71. *
  72. * @return boolean Whether or not the feature is supported.
  73. */
  74. function wp_has_spacing_feature_support( $block_type, $feature, $default = false ) {
  75. // Check if the specific feature has been opted into individually
  76. // via nested flag under `spacing`.
  77. return block_has_support( $block_type, array( 'spacing', $feature ), $default );
  78. }
  79. // Register the block support.
  80. WP_Block_Supports::get_instance()->register(
  81. 'spacing',
  82. array(
  83. 'register_attribute' => 'wp_register_spacing_support',
  84. 'apply' => 'wp_apply_spacing_support',
  85. )
  86. );