Bez popisu

layout.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * Generates the CSS corresponding to the provided layout.
  31. *
  32. * @since 5.9.0
  33. * @access private
  34. *
  35. * @param string $selector CSS selector.
  36. * @param array $layout Layout object. The one that is passed has already checked
  37. * the existence of default block layout.
  38. * @param boolean $has_block_gap_support Whether the theme has support for the block gap.
  39. * @param string $gap_value The block gap value to apply.
  40. * @param boolean $should_skip_gap_serialization Whether to skip applying the user-defined value set in the editor.
  41. * @param string $fallback_gap_value The custom fallback value for block gap.
  42. * @return string CSS style.
  43. */
  44. function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) {
  45. $layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
  46. $style = '';
  47. if ( 'default' === $layout_type ) {
  48. $content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : '';
  49. $wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : '';
  50. $all_max_width_value = $content_size ? $content_size : $wide_size;
  51. $wide_max_width_value = $wide_size ? $wide_size : $content_size;
  52. // Make sure there is a single CSS rule, and all tags are stripped for security.
  53. $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
  54. $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
  55. if ( $content_size || $wide_size ) {
  56. $style = "$selector > :where(:not(.alignleft):not(.alignright)) {";
  57. $style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';';
  58. $style .= 'margin-left: auto !important;';
  59. $style .= 'margin-right: auto !important;';
  60. $style .= '}';
  61. $style .= "$selector > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}';
  62. $style .= "$selector .alignfull { max-width: none; }";
  63. }
  64. $style .= "$selector > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }";
  65. $style .= "$selector > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }";
  66. $style .= "$selector > .aligncenter { margin-left: auto !important; margin-right: auto !important; }";
  67. if ( $has_block_gap_support ) {
  68. if ( is_array( $gap_value ) ) {
  69. $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
  70. }
  71. $gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : 'var( --wp--style--block-gap )';
  72. $style .= "$selector > * { margin-block-start: 0; margin-block-end: 0; }";
  73. $style .= "$selector > * + * { margin-block-start: $gap_style; margin-block-end: 0; }";
  74. }
  75. } elseif ( 'flex' === $layout_type ) {
  76. $layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal';
  77. $justify_content_options = array(
  78. 'left' => 'flex-start',
  79. 'right' => 'flex-end',
  80. 'center' => 'center',
  81. );
  82. if ( 'horizontal' === $layout_orientation ) {
  83. $justify_content_options += array( 'space-between' => 'space-between' );
  84. }
  85. $flex_wrap_options = array( 'wrap', 'nowrap' );
  86. $flex_wrap = ! empty( $layout['flexWrap'] ) && in_array( $layout['flexWrap'], $flex_wrap_options, true ) ?
  87. $layout['flexWrap'] :
  88. 'wrap';
  89. $style = "$selector {";
  90. $style .= 'display: flex;';
  91. if ( $has_block_gap_support ) {
  92. if ( is_array( $gap_value ) ) {
  93. $gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap_value;
  94. $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap_value;
  95. $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
  96. }
  97. $gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : "var( --wp--style--block-gap, $fallback_gap_value )";
  98. $style .= "gap: $gap_style;";
  99. } else {
  100. $style .= "gap: $fallback_gap_value;";
  101. }
  102. $style .= "flex-wrap: $flex_wrap;";
  103. if ( 'horizontal' === $layout_orientation ) {
  104. $style .= 'align-items: center;';
  105. /**
  106. * Add this style only if is not empty for backwards compatibility,
  107. * since we intend to convert blocks that had flex layout implemented
  108. * by custom css.
  109. */
  110. if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
  111. $style .= "justify-content: {$justify_content_options[ $layout['justifyContent'] ]};";
  112. }
  113. } else {
  114. $style .= 'flex-direction: column;';
  115. if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
  116. $style .= "align-items: {$justify_content_options[ $layout['justifyContent'] ]};";
  117. } else {
  118. $style .= 'align-items: flex-start;';
  119. }
  120. }
  121. $style .= '}';
  122. $style .= "$selector > * { margin: 0; }";
  123. }
  124. return $style;
  125. }
  126. /**
  127. * Renders the layout config to the block wrapper.
  128. *
  129. * @since 5.8.0
  130. * @access private
  131. *
  132. * @param string $block_content Rendered block content.
  133. * @param array $block Block object.
  134. * @return string Filtered block content.
  135. */
  136. function wp_render_layout_support_flag( $block_content, $block ) {
  137. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  138. $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );
  139. if ( ! $support_layout ) {
  140. return $block_content;
  141. }
  142. $block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) );
  143. $default_layout = wp_get_global_settings( array( 'layout' ) );
  144. $has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false;
  145. $default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
  146. $used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;
  147. if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
  148. if ( ! $default_layout ) {
  149. return $block_content;
  150. }
  151. $used_layout = $default_layout;
  152. }
  153. $class_name = wp_unique_id( 'wp-container-' );
  154. $gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
  155. // Skip if gap value contains unsupported characters.
  156. // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
  157. // because we only want to match against the value, not the CSS attribute.
  158. if ( is_array( $gap_value ) ) {
  159. foreach ( $gap_value as $key => $value ) {
  160. $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  161. }
  162. } else {
  163. $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
  164. }
  165. $fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' );
  166. // If a block's block.json skips serialization for spacing or spacing.blockGap,
  167. // don't apply the user-defined value to the styles.
  168. $should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
  169. $style = wp_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value );
  170. // This assumes the hook only applies to blocks with a single wrapper.
  171. // I think this is a reasonable limitation for that particular hook.
  172. $content = preg_replace(
  173. '/' . preg_quote( 'class="', '/' ) . '/',
  174. 'class="' . esc_attr( $class_name ) . ' ',
  175. $block_content,
  176. 1
  177. );
  178. wp_enqueue_block_support_styles( $style );
  179. return $content;
  180. }
  181. // Register the block support.
  182. WP_Block_Supports::get_instance()->register(
  183. 'layout',
  184. array(
  185. 'register_attribute' => 'wp_register_layout_support',
  186. )
  187. );
  188. add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
  189. /**
  190. * For themes without theme.json file, make sure
  191. * to restore the inner div for the group block
  192. * to avoid breaking styles relying on that div.
  193. *
  194. * @since 5.8.0
  195. * @access private
  196. *
  197. * @param string $block_content Rendered block content.
  198. * @param array $block Block object.
  199. * @return string Filtered block content.
  200. */
  201. function wp_restore_group_inner_container( $block_content, $block ) {
  202. $tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div';
  203. $group_with_inner_container_regex = sprintf(
  204. '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
  205. preg_quote( $tag_name, '/' )
  206. );
  207. if (
  208. WP_Theme_JSON_Resolver::theme_has_support() ||
  209. 1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
  210. ( isset( $block['attrs']['layout']['type'] ) && 'default' !== $block['attrs']['layout']['type'] )
  211. ) {
  212. return $block_content;
  213. }
  214. $replace_regex = sprintf(
  215. '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
  216. preg_quote( $tag_name, '/' )
  217. );
  218. $updated_content = preg_replace_callback(
  219. $replace_regex,
  220. static function( $matches ) {
  221. return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
  222. },
  223. $block_content
  224. );
  225. return $updated_content;
  226. }
  227. add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
  228. /**
  229. * For themes without theme.json file, make sure
  230. * to restore the outer div for the aligned image block
  231. * to avoid breaking styles relying on that div.
  232. *
  233. * @since 6.0.0
  234. * @access private
  235. *
  236. * @param string $block_content Rendered block content.
  237. * @param array $block Block object.
  238. * @return string Filtered block content.
  239. */
  240. function wp_restore_image_outer_container( $block_content, $block ) {
  241. $image_with_align = "
  242. /# 1) everything up to the class attribute contents
  243. (
  244. ^\s*
  245. <figure\b
  246. [^>]*
  247. \bclass=
  248. [\"']
  249. )
  250. # 2) the class attribute contents
  251. (
  252. [^\"']*
  253. \bwp-block-image\b
  254. [^\"']*
  255. \b(?:alignleft|alignright|aligncenter)\b
  256. [^\"']*
  257. )
  258. # 3) everything after the class attribute contents
  259. (
  260. [\"']
  261. [^>]*
  262. >
  263. .*
  264. <\/figure>
  265. )/iUx";
  266. if (
  267. WP_Theme_JSON_Resolver::theme_has_support() ||
  268. 0 === preg_match( $image_with_align, $block_content, $matches )
  269. ) {
  270. return $block_content;
  271. }
  272. $wrapper_classnames = array( 'wp-block-image' );
  273. // If the block has a classNames attribute these classnames need to be removed from the content and added back
  274. // to the new wrapper div also.
  275. if ( ! empty( $block['attrs']['className'] ) ) {
  276. $wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) );
  277. }
  278. $content_classnames = explode( ' ', $matches[2] );
  279. $filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames );
  280. return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>';
  281. }
  282. add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );