layout.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. * @since 6.1.0 Added `$block_spacing` param, use style engine to enqueue styles.
  34. * @access private
  35. *
  36. * @param string $selector CSS selector.
  37. * @param array $layout Layout object. The one that is passed has already checked
  38. * the existence of default block layout.
  39. * @param bool $has_block_gap_support Optional. Whether the theme has support for the block gap. Default false.
  40. * @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null.
  41. * @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
  42. * @param string $fallback_gap_value Optional. The block gap value to apply. Default '0.5em'.
  43. * @param array|null $block_spacing Optional. Custom spacing set on the block. Default null.
  44. * @return string CSS styles on success. Else, empty string.
  45. */
  46. 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', $block_spacing = null ) {
  47. $layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
  48. $layout_styles = array();
  49. if ( 'default' === $layout_type ) {
  50. if ( $has_block_gap_support ) {
  51. if ( is_array( $gap_value ) ) {
  52. $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
  53. }
  54. if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
  55. // Get spacing CSS variable from preset value if provided.
  56. if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
  57. $index_to_splice = strrpos( $gap_value, '|' ) + 1;
  58. $slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
  59. $gap_value = "var(--wp--preset--spacing--$slug)";
  60. }
  61. array_push(
  62. $layout_styles,
  63. array(
  64. 'selector' => "$selector > *",
  65. 'declarations' => array(
  66. 'margin-block-start' => '0',
  67. 'margin-block-end' => '0',
  68. ),
  69. ),
  70. array(
  71. 'selector' => "$selector$selector > * + *",
  72. 'declarations' => array(
  73. 'margin-block-start' => $gap_value,
  74. 'margin-block-end' => '0',
  75. ),
  76. )
  77. );
  78. }
  79. }
  80. } elseif ( 'constrained' === $layout_type ) {
  81. $content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : '';
  82. $wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : '';
  83. $justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'center';
  84. $all_max_width_value = $content_size ? $content_size : $wide_size;
  85. $wide_max_width_value = $wide_size ? $wide_size : $content_size;
  86. // Make sure there is a single CSS rule, and all tags are stripped for security.
  87. $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
  88. $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
  89. $margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important';
  90. $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
  91. if ( $content_size || $wide_size ) {
  92. array_push(
  93. $layout_styles,
  94. array(
  95. 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
  96. 'declarations' => array(
  97. 'max-width' => $all_max_width_value,
  98. 'margin-left' => $margin_left,
  99. 'margin-right' => $margin_right,
  100. ),
  101. ),
  102. array(
  103. 'selector' => "$selector > .alignwide",
  104. 'declarations' => array( 'max-width' => $wide_max_width_value ),
  105. ),
  106. array(
  107. 'selector' => "$selector .alignfull",
  108. 'declarations' => array( 'max-width' => 'none' ),
  109. )
  110. );
  111. if ( isset( $block_spacing ) ) {
  112. $block_spacing_values = wp_style_engine_get_styles(
  113. array(
  114. 'spacing' => $block_spacing,
  115. )
  116. );
  117. /*
  118. * Handle negative margins for alignfull children of blocks with custom padding set.
  119. * They're added separately because padding might only be set on one side.
  120. */
  121. if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
  122. $padding_right = $block_spacing_values['declarations']['padding-right'];
  123. $layout_styles[] = array(
  124. 'selector' => "$selector > .alignfull",
  125. 'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
  126. );
  127. }
  128. if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
  129. $padding_left = $block_spacing_values['declarations']['padding-left'];
  130. $layout_styles[] = array(
  131. 'selector' => "$selector > .alignfull",
  132. 'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
  133. );
  134. }
  135. }
  136. }
  137. if ( 'left' === $justify_content ) {
  138. $layout_styles[] = array(
  139. 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
  140. 'declarations' => array( 'margin-left' => '0 !important' ),
  141. );
  142. }
  143. if ( 'right' === $justify_content ) {
  144. $layout_styles[] = array(
  145. 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
  146. 'declarations' => array( 'margin-right' => '0 !important' ),
  147. );
  148. }
  149. if ( $has_block_gap_support ) {
  150. if ( is_array( $gap_value ) ) {
  151. $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
  152. }
  153. if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
  154. // Get spacing CSS variable from preset value if provided.
  155. if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
  156. $index_to_splice = strrpos( $gap_value, '|' ) + 1;
  157. $slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
  158. $gap_value = "var(--wp--preset--spacing--$slug)";
  159. }
  160. array_push(
  161. $layout_styles,
  162. array(
  163. 'selector' => "$selector > *",
  164. 'declarations' => array(
  165. 'margin-block-start' => '0',
  166. 'margin-block-end' => '0',
  167. ),
  168. ),
  169. array(
  170. 'selector' => "$selector$selector > * + *",
  171. 'declarations' => array(
  172. 'margin-block-start' => $gap_value,
  173. 'margin-block-end' => '0',
  174. ),
  175. )
  176. );
  177. }
  178. }
  179. } elseif ( 'flex' === $layout_type ) {
  180. $layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal';
  181. $justify_content_options = array(
  182. 'left' => 'flex-start',
  183. 'right' => 'flex-end',
  184. 'center' => 'center',
  185. );
  186. $vertical_alignment_options = array(
  187. 'top' => 'flex-start',
  188. 'center' => 'center',
  189. 'bottom' => 'flex-end',
  190. );
  191. if ( 'horizontal' === $layout_orientation ) {
  192. $justify_content_options += array( 'space-between' => 'space-between' );
  193. }
  194. if ( ! empty( $layout['flexWrap'] ) && 'nowrap' === $layout['flexWrap'] ) {
  195. $layout_styles[] = array(
  196. 'selector' => $selector,
  197. 'declarations' => array( 'flex-wrap' => 'nowrap' ),
  198. );
  199. }
  200. if ( $has_block_gap_support && isset( $gap_value ) ) {
  201. $combined_gap_value = '';
  202. $gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
  203. foreach ( $gap_sides as $gap_side ) {
  204. $process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
  205. // Get spacing CSS variable from preset value if provided.
  206. if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
  207. $index_to_splice = strrpos( $process_value, '|' ) + 1;
  208. $slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
  209. $process_value = "var(--wp--preset--spacing--$slug)";
  210. }
  211. $combined_gap_value .= "$process_value ";
  212. }
  213. $gap_value = trim( $combined_gap_value );
  214. if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
  215. $layout_styles[] = array(
  216. 'selector' => $selector,
  217. 'declarations' => array( 'gap' => $gap_value ),
  218. );
  219. }
  220. }
  221. if ( 'horizontal' === $layout_orientation ) {
  222. /*
  223. * Add this style only if is not empty for backwards compatibility,
  224. * since we intend to convert blocks that had flex layout implemented
  225. * by custom css.
  226. */
  227. if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
  228. $layout_styles[] = array(
  229. 'selector' => $selector,
  230. 'declarations' => array( 'justify-content' => $justify_content_options[ $layout['justifyContent'] ] ),
  231. );
  232. }
  233. if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
  234. $layout_styles[] = array(
  235. 'selector' => $selector,
  236. 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
  237. );
  238. }
  239. } else {
  240. $layout_styles[] = array(
  241. 'selector' => $selector,
  242. 'declarations' => array( 'flex-direction' => 'column' ),
  243. );
  244. if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
  245. $layout_styles[] = array(
  246. 'selector' => $selector,
  247. 'declarations' => array( 'align-items' => $justify_content_options[ $layout['justifyContent'] ] ),
  248. );
  249. } else {
  250. $layout_styles[] = array(
  251. 'selector' => $selector,
  252. 'declarations' => array( 'align-items' => 'flex-start' ),
  253. );
  254. }
  255. }
  256. }
  257. if ( ! empty( $layout_styles ) ) {
  258. /*
  259. * Add to the style engine store to enqueue and render layout styles.
  260. * Return compiled layout styles to retain backwards compatibility.
  261. * Since https://github.com/WordPress/gutenberg/pull/42452,
  262. * wp_enqueue_block_support_styles is no longer called in this block supports file.
  263. */
  264. return wp_style_engine_get_stylesheet_from_css_rules(
  265. $layout_styles,
  266. array(
  267. 'context' => 'block-supports',
  268. 'prettify' => false,
  269. )
  270. );
  271. }
  272. return '';
  273. }
  274. /**
  275. * Renders the layout config to the block wrapper.
  276. *
  277. * @since 5.8.0
  278. * @access private
  279. *
  280. * @param string $block_content Rendered block content.
  281. * @param array $block Block object.
  282. * @return string Filtered block content.
  283. */
  284. function wp_render_layout_support_flag( $block_content, $block ) {
  285. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  286. $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );
  287. if ( ! $support_layout ) {
  288. return $block_content;
  289. }
  290. $block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) );
  291. $global_layout_settings = wp_get_global_settings( array( 'layout' ) );
  292. $has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false;
  293. $default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
  294. $used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;
  295. if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
  296. if ( ! $global_layout_settings ) {
  297. return $block_content;
  298. }
  299. }
  300. $class_names = array();
  301. $layout_definitions = _wp_array_get( $global_layout_settings, array( 'definitions' ), array() );
  302. $block_classname = wp_get_block_default_classname( $block['blockName'] );
  303. $container_class = wp_unique_id( 'wp-container-' );
  304. $layout_classname = '';
  305. // Set the correct layout type for blocks using legacy content width.
  306. if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
  307. $used_layout['type'] = 'constrained';
  308. }
  309. if (
  310. wp_get_global_settings( array( 'useRootPaddingAwareAlignments' ) ) &&
  311. isset( $used_layout['type'] ) &&
  312. 'constrained' === $used_layout['type']
  313. ) {
  314. $class_names[] = 'has-global-padding';
  315. }
  316. /*
  317. * The following section was added to reintroduce a small set of layout classnames that were
  318. * removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is
  319. * not intended to provide an extended set of classes to match all block layout attributes
  320. * here.
  321. */
  322. if ( ! empty( $block['attrs']['layout']['orientation'] ) ) {
  323. $class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] );
  324. }
  325. if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
  326. $class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] );
  327. }
  328. if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) {
  329. $class_names[] = 'is-nowrap';
  330. }
  331. // Get classname for layout type.
  332. if ( isset( $used_layout['type'] ) ) {
  333. $layout_classname = _wp_array_get( $layout_definitions, array( $used_layout['type'], 'className' ), '' );
  334. } else {
  335. $layout_classname = _wp_array_get( $layout_definitions, array( 'default', 'className' ), '' );
  336. }
  337. if ( $layout_classname && is_string( $layout_classname ) ) {
  338. $class_names[] = sanitize_title( $layout_classname );
  339. }
  340. /*
  341. * Only generate Layout styles if the theme has not opted-out.
  342. * Attribute-based Layout classnames are output in all cases.
  343. */
  344. if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
  345. $gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
  346. /*
  347. * Skip if gap value contains unsupported characters.
  348. * Regex for CSS value borrowed from `safecss_filter_attr`, and used here
  349. * to only match against the value, not the CSS attribute.
  350. */
  351. if ( is_array( $gap_value ) ) {
  352. foreach ( $gap_value as $key => $value ) {
  353. $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  354. }
  355. } else {
  356. $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
  357. }
  358. $fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' );
  359. $block_spacing = _wp_array_get( $block, array( 'attrs', 'style', 'spacing' ), null );
  360. /*
  361. * If a block's block.json skips serialization for spacing or spacing.blockGap,
  362. * don't apply the user-defined value to the styles.
  363. */
  364. $should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
  365. $style = wp_get_layout_style(
  366. ".$block_classname.$container_class",
  367. $used_layout,
  368. $has_block_gap_support,
  369. $gap_value,
  370. $should_skip_gap_serialization,
  371. $fallback_gap_value,
  372. $block_spacing
  373. );
  374. // Only add container class and enqueue block support styles if unique styles were generated.
  375. if ( ! empty( $style ) ) {
  376. $class_names[] = $container_class;
  377. }
  378. }
  379. /*
  380. * This assumes the hook only applies to blocks with a single wrapper.
  381. * A limitation of this hook is that nested inner blocks wrappers are not yet supported.
  382. */
  383. $content = preg_replace(
  384. '/' . preg_quote( 'class="', '/' ) . '/',
  385. 'class="' . esc_attr( implode( ' ', $class_names ) ) . ' ',
  386. $block_content,
  387. 1
  388. );
  389. return $content;
  390. }
  391. // Register the block support.
  392. WP_Block_Supports::get_instance()->register(
  393. 'layout',
  394. array(
  395. 'register_attribute' => 'wp_register_layout_support',
  396. )
  397. );
  398. add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
  399. /**
  400. * For themes without theme.json file, make sure
  401. * to restore the inner div for the group block
  402. * to avoid breaking styles relying on that div.
  403. *
  404. * @since 5.8.0
  405. * @access private
  406. *
  407. * @param string $block_content Rendered block content.
  408. * @param array $block Block object.
  409. * @return string Filtered block content.
  410. */
  411. function wp_restore_group_inner_container( $block_content, $block ) {
  412. $tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div';
  413. $group_with_inner_container_regex = sprintf(
  414. '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
  415. preg_quote( $tag_name, '/' )
  416. );
  417. if (
  418. WP_Theme_JSON_Resolver::theme_has_support() ||
  419. 1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
  420. ( isset( $block['attrs']['layout']['type'] ) && 'flex' === $block['attrs']['layout']['type'] )
  421. ) {
  422. return $block_content;
  423. }
  424. $replace_regex = sprintf(
  425. '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
  426. preg_quote( $tag_name, '/' )
  427. );
  428. $updated_content = preg_replace_callback(
  429. $replace_regex,
  430. static function( $matches ) {
  431. return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
  432. },
  433. $block_content
  434. );
  435. return $updated_content;
  436. }
  437. add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
  438. /**
  439. * For themes without theme.json file, make sure
  440. * to restore the outer div for the aligned image block
  441. * to avoid breaking styles relying on that div.
  442. *
  443. * @since 6.0.0
  444. * @access private
  445. *
  446. * @param string $block_content Rendered block content.
  447. * @param array $block Block object.
  448. * @return string Filtered block content.
  449. */
  450. function wp_restore_image_outer_container( $block_content, $block ) {
  451. $image_with_align = "
  452. /# 1) everything up to the class attribute contents
  453. (
  454. ^\s*
  455. <figure\b
  456. [^>]*
  457. \bclass=
  458. [\"']
  459. )
  460. # 2) the class attribute contents
  461. (
  462. [^\"']*
  463. \bwp-block-image\b
  464. [^\"']*
  465. \b(?:alignleft|alignright|aligncenter)\b
  466. [^\"']*
  467. )
  468. # 3) everything after the class attribute contents
  469. (
  470. [\"']
  471. [^>]*
  472. >
  473. .*
  474. <\/figure>
  475. )/iUx";
  476. if (
  477. WP_Theme_JSON_Resolver::theme_has_support() ||
  478. 0 === preg_match( $image_with_align, $block_content, $matches )
  479. ) {
  480. return $block_content;
  481. }
  482. $wrapper_classnames = array( 'wp-block-image' );
  483. // If the block has a classNames attribute these classnames need to be removed from the content and added back
  484. // to the new wrapper div also.
  485. if ( ! empty( $block['attrs']['className'] ) ) {
  486. $wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) );
  487. }
  488. $content_classnames = explode( ' ', $matches[2] );
  489. $filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames );
  490. return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>';
  491. }
  492. add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );