custom-classname.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Custom classname block support flag.
  4. *
  5. * @package WordPress
  6. * @since 5.6.0
  7. */
  8. /**
  9. * Registers the custom classname 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_custom_classname_support( $block_type ) {
  17. $has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
  18. if ( $has_custom_classname_support ) {
  19. if ( ! $block_type->attributes ) {
  20. $block_type->attributes = array();
  21. }
  22. if ( ! array_key_exists( 'className', $block_type->attributes ) ) {
  23. $block_type->attributes['className'] = array(
  24. 'type' => 'string',
  25. );
  26. }
  27. }
  28. }
  29. /**
  30. * Add the custom classnames to the output.
  31. *
  32. * @since 5.6.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 CSS classes and inline styles.
  39. */
  40. function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
  41. $has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
  42. $attributes = array();
  43. if ( $has_custom_classname_support ) {
  44. $has_custom_classnames = array_key_exists( 'className', $block_attributes );
  45. if ( $has_custom_classnames ) {
  46. $attributes['class'] = $block_attributes['className'];
  47. }
  48. }
  49. return $attributes;
  50. }
  51. // Register the block support.
  52. WP_Block_Supports::get_instance()->register(
  53. 'custom-classname',
  54. array(
  55. 'register_attribute' => 'wp_register_custom_classname_support',
  56. 'apply' => 'wp_apply_custom_classname_support',
  57. )
  58. );