gallery.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/gallery` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Handles backwards compatibility for Gallery Blocks,
  9. * whose images feature a `data-id` attribute.
  10. *
  11. * Now that the Gallery Block contains inner Image Blocks,
  12. * we add a custom `data-id` attribute before rendering the gallery
  13. * so that the Image Block can pick it up in its render_callback.
  14. *
  15. * @param array $parsed_block The block being rendered.
  16. * @return array The migrated block object.
  17. */
  18. function block_core_gallery_data_id_backcompatibility( $parsed_block ) {
  19. if ( 'core/gallery' === $parsed_block['blockName'] ) {
  20. foreach ( $parsed_block['innerBlocks'] as $key => $inner_block ) {
  21. if ( 'core/image' === $inner_block['blockName'] ) {
  22. if ( ! isset( $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] ) && isset( $inner_block['attrs']['id'] ) ) {
  23. $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] = esc_attr( $inner_block['attrs']['id'] );
  24. }
  25. }
  26. }
  27. }
  28. return $parsed_block;
  29. }
  30. add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );
  31. /**
  32. * Adds a style tag for the --wp--style--unstable-gallery-gap var.
  33. *
  34. * The Gallery block needs to recalculate Image block width based on
  35. * the current gap setting in order to maintain the number of flex columns
  36. * so a css var is added to allow this.
  37. *
  38. * @param array $attributes Attributes of the block being rendered.
  39. * @param string $content Content of the block being rendered.
  40. * @return string The content of the block being rendered.
  41. */
  42. function block_core_gallery_render( $attributes, $content ) {
  43. $gap = _wp_array_get( $attributes, array( 'style', 'spacing', 'blockGap' ) );
  44. // Skip if gap value contains unsupported characters.
  45. // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
  46. // because we only want to match against the value, not the CSS attribute.
  47. if ( is_array( $gap ) ) {
  48. foreach ( $gap as $key => $value ) {
  49. // Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
  50. $value = is_string( $value ) ? $value : '';
  51. $value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  52. // Get spacing CSS variable from preset value if provided.
  53. if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) {
  54. $index_to_splice = strrpos( $value, '|' ) + 1;
  55. $slug = _wp_to_kebab_case( substr( $value, $index_to_splice ) );
  56. $value = "var(--wp--preset--spacing--$slug)";
  57. }
  58. $gap[ $key ] = $value;
  59. }
  60. } else {
  61. // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
  62. $gap = is_string( $gap ) ? $gap : '';
  63. $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
  64. // Get spacing CSS variable from preset value if provided.
  65. if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
  66. $index_to_splice = strrpos( $gap, '|' ) + 1;
  67. $slug = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );
  68. $gap = "var(--wp--preset--spacing--$slug)";
  69. }
  70. }
  71. $class = wp_unique_id( 'wp-block-gallery-' );
  72. $content = preg_replace(
  73. '/' . preg_quote( 'class="', '/' ) . '/',
  74. 'class="' . $class . ' ',
  75. $content,
  76. 1
  77. );
  78. // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
  79. // gap on the gallery.
  80. $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
  81. $gap_value = $gap ? $gap : $fallback_gap;
  82. $gap_column = $gap_value;
  83. if ( is_array( $gap_value ) ) {
  84. $gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;
  85. $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap;
  86. $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
  87. }
  88. // The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.
  89. if ( '0' === $gap_column ) {
  90. $gap_column = '0px';
  91. }
  92. // Set the CSS variable to the column value, and the `gap` property to the combined gap value.
  93. $style = '.wp-block-gallery.' . $class . '{ --wp--style--unstable-gallery-gap: ' . $gap_column . '; gap: ' . $gap_value . '}';
  94. wp_enqueue_block_support_styles( $style, 11 );
  95. return $content;
  96. }
  97. /**
  98. * Registers the `core/gallery` block on server.
  99. */
  100. function register_block_core_gallery() {
  101. register_block_type_from_metadata(
  102. __DIR__ . '/gallery',
  103. array(
  104. 'render_callback' => 'block_core_gallery_render',
  105. )
  106. );
  107. }
  108. add_action( 'init', 'register_block_core_gallery' );