| @@ -32,19 +32,99 @@ | ||
| 32 | 32 | |
| 33 | 33 | add_filter( 'render_block_data', 'gutenberg_block_core_gallery_data_id_backcompatibility' ); |
| 34 | 34 | |
| 35 | 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 | + // Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. | |
| 54 | + $value = is_string( $value ) ? $value : ''; | |
| 55 | + $value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; | |
| 56 | + | |
| 57 | + // Get spacing CSS variable from preset value if provided. | |
| 58 | + if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) { | |
| 59 | + $index_to_splice = strrpos( $value, '|' ) + 1; | |
| 60 | + $slug = _wp_to_kebab_case( substr( $value, $index_to_splice ) ); | |
| 61 | + $value = "var(--wp--preset--spacing--$slug)"; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $gap[ $key ] = $value; | |
| 65 | + } | |
| 66 | + } else { | |
| 67 | + // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. | |
| 68 | + $gap = is_string( $gap ) ? $gap : ''; | |
| 69 | + $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap; | |
| 70 | + | |
| 71 | + // Get spacing CSS variable from preset value if provided. | |
| 72 | + if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) { | |
| 73 | + $index_to_splice = strrpos( $gap, '|' ) + 1; | |
| 74 | + $slug = _wp_to_kebab_case( substr( $gap, $index_to_splice ) ); | |
| 75 | + $gap = "var(--wp--preset--spacing--$slug)"; | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + $unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' ); | |
| 80 | + $processed_content = new WP_HTML_Tag_Processor( $content ); | |
| 81 | + $processed_content->next_tag(); | |
| 82 | + $processed_content->add_class( $unique_gallery_classname ); | |
| 83 | + | |
| 84 | + // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default | |
| 85 | + // gap on the gallery. | |
| 86 | + $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )'; | |
| 87 | + $gap_value = $gap ? $gap : $fallback_gap; | |
| 88 | + $gap_column = $gap_value; | |
| 89 | + | |
| 90 | + if ( is_array( $gap_value ) ) { | |
| 91 | + $gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap; | |
| 92 | + $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap; | |
| 93 | + $gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column; | |
| 94 | + } | |
| 95 | + | |
| 96 | + // The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`. | |
| 97 | + if ( '0' === $gap_column ) { | |
| 98 | + $gap_column = '0px'; | |
| 99 | + } | |
| 100 | + | |
| 101 | + // Set the CSS variable to the column value, and the `gap` property to the combined gap value. | |
| 102 | + $gallery_styles = array(); | |
| 103 | + $gallery_styles[] = array( | |
| 104 | + 'selector' => ".wp-block-gallery.{$unique_gallery_classname}", | |
| 105 | + 'declarations' => array( | |
| 106 | + '--wp--style--unstable-gallery-gap' => $gap_column, | |
| 107 | + 'gap' => $gap_value, | |
| 108 | + ), | |
| 109 | + ); | |
| 110 | + | |
| 111 | + gutenberg_style_engine_get_stylesheet_from_css_rules( | |
| 112 | + $gallery_styles, | |
| 113 | + array( | |
| 114 | + 'context' => 'block-supports', | |
| 115 | + ) | |
| 116 | + ); | |
| 117 | + return (string) $processed_content; | |
| 118 | +} | |
| 119 | +/** | |
| 36 | 120 | * Registers the `core/gallery` block on server. |
| 37 | - * This render callback needs to be here | |
| 38 | - * so that the gallery styles are loaded in block-based themes. | |
| 39 | 121 | */ |
| 40 | 122 | function gutenberg_register_block_core_gallery() { |
| 41 | 123 | register_block_type_from_metadata( |
| 42 | 124 | __DIR__ . '/gallery', |
| 43 | 125 | array( |
| 44 | - 'render_callback' => function ( $attributes, $content ) { | |
| 45 | - return $content; | |
| 46 | - }, | |
| 126 | + 'render_callback' => 'gutenberg_block_core_gallery_render', | |
| 47 | 127 | ) |
| 48 | 128 | ); |
| 49 | 129 | } |
| 50 | 130 | |