PluginProbe
Gutenberg / 13.5.1
Gutenberg v13.5.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / gallery.php

gallery.php in Gutenberg 13.5.1, at build/block-library/blocks/gallery.php

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