Geen omschrijving

layout.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Layout block support flag.
  4. *
  5. * @package WordPress
  6. * @since 5.8.0
  7. */
  8. /**
  9. * Registers the layout 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_layout_support( $block_type ) {
  17. $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );
  18. if ( $support_layout ) {
  19. if ( ! $block_type->attributes ) {
  20. $block_type->attributes = array();
  21. }
  22. if ( ! array_key_exists( 'layout', $block_type->attributes ) ) {
  23. $block_type->attributes['layout'] = array(
  24. 'type' => 'object',
  25. );
  26. }
  27. }
  28. }
  29. /**
  30. * Renders the layout config to the block wrapper.
  31. *
  32. * @since 5.8.0
  33. * @access private
  34. *
  35. * @param string $block_content Rendered block content.
  36. * @param array $block Block object.
  37. * @return string Filtered block content.
  38. */
  39. function wp_render_layout_support_flag( $block_content, $block ) {
  40. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  41. $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );
  42. if ( ! $support_layout || ! isset( $block['attrs']['layout'] ) ) {
  43. return $block_content;
  44. }
  45. $used_layout = $block['attrs']['layout'];
  46. if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
  47. $tree = WP_Theme_JSON_Resolver::get_merged_data();
  48. $default_layout = _wp_array_get( $tree->get_settings(), array( 'layout' ) );
  49. if ( ! $default_layout ) {
  50. return $block_content;
  51. }
  52. $used_layout = $default_layout;
  53. }
  54. $id = uniqid();
  55. $content_size = isset( $used_layout['contentSize'] ) ? $used_layout['contentSize'] : null;
  56. $wide_size = isset( $used_layout['wideSize'] ) ? $used_layout['wideSize'] : null;
  57. $all_max_width_value = $content_size ? $content_size : $wide_size;
  58. $wide_max_width_value = $wide_size ? $wide_size : $content_size;
  59. // Make sure there is a single CSS rule, and all tags are stripped for security.
  60. $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
  61. $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
  62. $style = '';
  63. if ( $content_size || $wide_size ) {
  64. $style = ".wp-container-$id > * {";
  65. $style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';';
  66. $style .= 'margin-left: auto !important;';
  67. $style .= 'margin-right: auto !important;';
  68. $style .= '}';
  69. $style .= ".wp-container-$id > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}';
  70. $style .= ".wp-container-$id .alignfull { max-width: none; }";
  71. }
  72. $style .= ".wp-container-$id .alignleft { float: left; margin-right: 2em; }";
  73. $style .= ".wp-container-$id .alignright { float: right; margin-left: 2em; }";
  74. // This assumes the hook only applies to blocks with a single wrapper.
  75. // I think this is a reasonable limitation for that particular hook.
  76. $content = preg_replace(
  77. '/' . preg_quote( 'class="', '/' ) . '/',
  78. 'class="wp-container-' . $id . ' ',
  79. $block_content,
  80. 1
  81. );
  82. return $content . '<style>' . $style . '</style>';
  83. }
  84. // Register the block support.
  85. WP_Block_Supports::get_instance()->register(
  86. 'layout',
  87. array(
  88. 'register_attribute' => 'wp_register_layout_support',
  89. )
  90. );
  91. add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
  92. /**
  93. * For themes without theme.json file, make sure
  94. * to restore the inner div for the group block
  95. * to avoid breaking styles relying on that div.
  96. *
  97. * @since 5.8.0
  98. * @access private
  99. *
  100. * @param string $block_content Rendered block content.
  101. * @param array $block Block object.
  102. *
  103. * @return string Filtered block content.
  104. */
  105. function wp_restore_group_inner_container( $block_content, $block ) {
  106. $group_with_inner_container_regex = '/(^\s*<div\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/';
  107. if (
  108. 'core/group' !== $block['blockName'] ||
  109. WP_Theme_JSON_Resolver::theme_has_support() ||
  110. 1 === preg_match( $group_with_inner_container_regex, $block_content )
  111. ) {
  112. return $block_content;
  113. }
  114. $replace_regex = '/(^\s*<div\b[^>]*wp-block-group[^>]*>)(.*)(<\/div>\s*$)/ms';
  115. $updated_content = preg_replace_callback(
  116. $replace_regex,
  117. function( $matches ) {
  118. return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
  119. },
  120. $block_content
  121. );
  122. return $updated_content;
  123. }
  124. add_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 );