Nenhuma Descrição

gallery.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  50. }
  51. } else {
  52. $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
  53. }
  54. $class = wp_unique_id( 'wp-block-gallery-' );
  55. $content = preg_replace(
  56. '/' . preg_quote( 'class="', '/' ) . '/',
  57. 'class="' . $class . ' ',
  58. $content,
  59. 1
  60. );
  61. // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
  62. // gap on the gallery.
  63. $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
  64. $gap_value = $gap ? $gap : $fallback_gap;
  65. $gap_column = $gap_value;
  66. if ( is_array( $gap_value ) ) {
  67. $gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;
  68. $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap;
  69. $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
  70. }
  71. // Set the CSS variable to the column value, and the `gap` property to the combined gap value.
  72. $style = '.' . $class . '{ --wp--style--unstable-gallery-gap: ' . $gap_column . '; gap: ' . $gap_value . '}';
  73. // Ideally styles should be loaded in the head, but blocks may be parsed
  74. // after that, so loading in the footer for now.
  75. // See https://core.trac.wordpress.org/ticket/53494.
  76. add_action(
  77. 'wp_footer',
  78. function () use ( $style ) {
  79. echo '<style> ' . $style . '</style>';
  80. }
  81. );
  82. return $content;
  83. }
  84. /**
  85. * Registers the `core/gallery` block on server.
  86. */
  87. function register_block_core_gallery() {
  88. register_block_type_from_metadata(
  89. __DIR__ . '/gallery',
  90. array(
  91. 'render_callback' => 'block_core_gallery_render',
  92. )
  93. );
  94. }
  95. add_action( 'init', 'register_block_core_gallery' );