dimensions.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Dimensions block support flag.
  4. *
  5. * This does not include the `spacing` block support even though that visually
  6. * appears under the "Dimensions" panel in the editor. It remains in its
  7. * original `spacing.php` file for compatibility with core.
  8. *
  9. * @package WordPress
  10. * @since 5.9.0
  11. */
  12. /**
  13. * Registers the style block attribute for block types that support it.
  14. *
  15. * @since 5.9.0
  16. * @access private
  17. *
  18. * @param WP_Block_Type $block_type Block Type.
  19. */
  20. function wp_register_dimensions_support( $block_type ) {
  21. // Setup attributes and styles within that if needed.
  22. if ( ! $block_type->attributes ) {
  23. $block_type->attributes = array();
  24. }
  25. // Check for existing style attribute definition e.g. from block.json.
  26. if ( array_key_exists( 'style', $block_type->attributes ) ) {
  27. return;
  28. }
  29. $has_dimensions_support = block_has_support( $block_type, array( '__experimentalDimensions' ), false );
  30. // Future block supports such as height & width will be added here.
  31. if ( $has_dimensions_support ) {
  32. $block_type->attributes['style'] = array(
  33. 'type' => 'object',
  34. );
  35. }
  36. }
  37. /**
  38. * Adds CSS classes for block dimensions to the incoming attributes array.
  39. * This will be applied to the block markup in the front-end.
  40. *
  41. * @since 5.9.0
  42. * @access private
  43. *
  44. * @param WP_Block_Type $block_type Block Type.
  45. * @param array $block_attributes Block attributes.
  46. * @return array Block dimensions CSS classes and inline styles.
  47. */
  48. function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  49. if ( wp_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) {
  50. return array();
  51. }
  52. $styles = array();
  53. // Height support to be added in near future.
  54. // Width support to be added in near future.
  55. return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
  56. }
  57. // Register the block support.
  58. WP_Block_Supports::get_instance()->register(
  59. 'dimensions',
  60. array(
  61. 'register_attribute' => 'wp_register_dimensions_support',
  62. 'apply' => 'wp_apply_dimensions_support',
  63. )
  64. );