elements.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Elements styles block support.
  4. *
  5. * @package WordPress
  6. * @since 5.8.0
  7. */
  8. /**
  9. * Get the elements class names.
  10. *
  11. * @since 6.0.0
  12. * @access private
  13. *
  14. * @param array $block Block object.
  15. * @return string The unique class name.
  16. */
  17. function wp_get_elements_class_name( $block ) {
  18. return 'wp-elements-' . md5( serialize( $block ) );
  19. }
  20. /**
  21. * Update the block content with elements class names.
  22. *
  23. * @since 5.8.0
  24. * @access private
  25. *
  26. * @param string $block_content Rendered block content.
  27. * @param array $block Block object.
  28. * @return string Filtered block content.
  29. */
  30. function wp_render_elements_support( $block_content, $block ) {
  31. if ( ! $block_content ) {
  32. return $block_content;
  33. }
  34. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  35. $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
  36. if ( $skip_link_color_serialization ) {
  37. return $block_content;
  38. }
  39. $link_color = null;
  40. if ( ! empty( $block['attrs'] ) ) {
  41. $link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null );
  42. }
  43. /*
  44. * For now we only care about link color.
  45. * This code in the future when we have a public API
  46. * should take advantage of WP_Theme_JSON::compute_style_properties
  47. * and work for any element and style.
  48. */
  49. if ( null === $link_color ) {
  50. return $block_content;
  51. }
  52. $class_name = wp_get_elements_class_name( $block );
  53. // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
  54. // Retrieve the opening tag of the first HTML element.
  55. $html_element_matches = array();
  56. preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
  57. $first_element = $html_element_matches[0][0];
  58. // If the first HTML element has a class attribute just add the new class
  59. // as we do on layout and duotone.
  60. if ( strpos( $first_element, 'class="' ) !== false ) {
  61. $content = preg_replace(
  62. '/' . preg_quote( 'class="', '/' ) . '/',
  63. 'class="' . $class_name . ' ',
  64. $block_content,
  65. 1
  66. );
  67. } else {
  68. // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
  69. $first_element_offset = $html_element_matches[0][1];
  70. $content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
  71. }
  72. return $content;
  73. }
  74. /**
  75. * Render the elements stylesheet.
  76. *
  77. * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
  78. * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
  79. * we want the descendant style to take priority, and this is done by loading it after, in DOM order.
  80. *
  81. * @since 6.0.0
  82. * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
  83. * @access private
  84. *
  85. * @param string|null $pre_render The pre-rendered content. Default null.
  86. * @param array $block The block being rendered.
  87. *
  88. * @return null
  89. */
  90. function wp_render_elements_support_styles( $pre_render, $block ) {
  91. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  92. $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
  93. /*
  94. * For now we only care about link color.
  95. */
  96. $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
  97. if ( $skip_link_color_serialization ) {
  98. return null;
  99. }
  100. $class_name = wp_get_elements_class_name( $block );
  101. $link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;
  102. wp_style_engine_get_styles(
  103. $link_block_styles,
  104. array(
  105. 'selector' => ".$class_name a",
  106. 'context' => 'block-supports',
  107. )
  108. );
  109. return null;
  110. }
  111. add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
  112. add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );