19
|
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 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', '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 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 |
// Ideally styles should be loaded in the head, but blocks may be parsed |
|
83 |
// after that, so loading in the footer for now. |
|
84 |
// See https://core.trac.wordpress.org/ticket/53494. |
|
85 |
add_action( |
|
86 |
'wp_footer', |
|
87 |
function () use ( $style ) { |
|
88 |
echo '<style> ' . $style . '</style>'; |
|
89 |
}, |
|
90 |
11 |
|
91 |
); |
|
92 |
return $content; |
|
93 |
} |
|
94 |
/** |
|
95 |
* Registers the `core/gallery` block on server. |
|
96 |
*/ |
|
97 |
function register_block_core_gallery() { |
|
98 |
register_block_type_from_metadata( |
|
99 |
__DIR__ . '/gallery', |
|
100 |
array( |
|
101 |
'render_callback' => 'block_core_gallery_render', |
|
102 |
) |
|
103 |
); |
|
104 |
} |
|
105 |
|
|
106 |
add_action( 'init', 'register_block_core_gallery' ); |