Edit File by line
/home/zeestwma/richards.../wp-inclu.../blocks
File: gallery.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/gallery` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Handles backwards compatibility for Gallery Blocks,
[8] Fix | Delete
* whose images feature a `data-id` attribute.
[9] Fix | Delete
*
[10] Fix | Delete
* Now that the Gallery Block contains inner Image Blocks,
[11] Fix | Delete
* we add a custom `data-id` attribute before rendering the gallery
[12] Fix | Delete
* so that the Image Block can pick it up in its render_callback.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 5.9.0
[15] Fix | Delete
*
[16] Fix | Delete
* @param array $parsed_block The block being rendered.
[17] Fix | Delete
* @return array The migrated block object.
[18] Fix | Delete
*/
[19] Fix | Delete
function block_core_gallery_data_id_backcompatibility( $parsed_block ) {
[20] Fix | Delete
if ( 'core/gallery' === $parsed_block['blockName'] ) {
[21] Fix | Delete
foreach ( $parsed_block['innerBlocks'] as $key => $inner_block ) {
[22] Fix | Delete
if ( 'core/image' === $inner_block['blockName'] ) {
[23] Fix | Delete
if ( ! isset( $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] ) && isset( $inner_block['attrs']['id'] ) ) {
[24] Fix | Delete
$parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] = esc_attr( $inner_block['attrs']['id'] );
[25] Fix | Delete
}
[26] Fix | Delete
}
[27] Fix | Delete
}
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
return $parsed_block;
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Renders the `core/gallery` block on the server.
[37] Fix | Delete
*
[38] Fix | Delete
* @since 6.0.0
[39] Fix | Delete
*
[40] Fix | Delete
* @param array $attributes Attributes of the block being rendered.
[41] Fix | Delete
* @param string $content Content of the block being rendered.
[42] Fix | Delete
* @return string The content of the block being rendered.
[43] Fix | Delete
*/
[44] Fix | Delete
function block_core_gallery_render( $attributes, $content ) {
[45] Fix | Delete
// Adds a style tag for the --wp--style--unstable-gallery-gap var.
[46] Fix | Delete
// The Gallery block needs to recalculate Image block width based on
[47] Fix | Delete
// the current gap setting in order to maintain the number of flex columns
[48] Fix | Delete
// so a css var is added to allow this.
[49] Fix | Delete
[50] Fix | Delete
$gap = $attributes['style']['spacing']['blockGap'] ?? null;
[51] Fix | Delete
// Skip if gap value contains unsupported characters.
[52] Fix | Delete
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here
[53] Fix | Delete
// because we only want to match against the value, not the CSS attribute.
[54] Fix | Delete
if ( is_array( $gap ) ) {
[55] Fix | Delete
foreach ( $gap as $key => $value ) {
[56] Fix | Delete
// Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
[57] Fix | Delete
$value = is_string( $value ) ? $value : '';
[58] Fix | Delete
$value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
[59] Fix | Delete
[60] Fix | Delete
// Get spacing CSS variable from preset value if provided.
[61] Fix | Delete
if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) {
[62] Fix | Delete
$index_to_splice = strrpos( $value, '|' ) + 1;
[63] Fix | Delete
$slug = _wp_to_kebab_case( substr( $value, $index_to_splice ) );
[64] Fix | Delete
$value = "var(--wp--preset--spacing--$slug)";
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
$gap[ $key ] = $value;
[68] Fix | Delete
}
[69] Fix | Delete
} else {
[70] Fix | Delete
// Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
[71] Fix | Delete
$gap = is_string( $gap ) ? $gap : '';
[72] Fix | Delete
$gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
[73] Fix | Delete
[74] Fix | Delete
// Get spacing CSS variable from preset value if provided.
[75] Fix | Delete
if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
[76] Fix | Delete
$index_to_splice = strrpos( $gap, '|' ) + 1;
[77] Fix | Delete
$slug = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );
[78] Fix | Delete
$gap = "var(--wp--preset--spacing--$slug)";
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
$unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );
[83] Fix | Delete
$processed_content = new WP_HTML_Tag_Processor( $content );
[84] Fix | Delete
$processed_content->next_tag();
[85] Fix | Delete
$processed_content->add_class( $unique_gallery_classname );
[86] Fix | Delete
[87] Fix | Delete
// --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
[88] Fix | Delete
// gap on the gallery.
[89] Fix | Delete
$fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
[90] Fix | Delete
$gap_value = $gap ? $gap : $fallback_gap;
[91] Fix | Delete
$gap_column = $gap_value;
[92] Fix | Delete
[93] Fix | Delete
if ( is_array( $gap_value ) ) {
[94] Fix | Delete
$gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;
[95] Fix | Delete
$gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap;
[96] Fix | Delete
$gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
// The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.
[100] Fix | Delete
if ( '0' === $gap_column ) {
[101] Fix | Delete
$gap_column = '0px';
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
// Set the CSS variable to the column value, and the `gap` property to the combined gap value.
[105] Fix | Delete
$gallery_styles = array(
[106] Fix | Delete
array(
[107] Fix | Delete
'selector' => ".wp-block-gallery.{$unique_gallery_classname}",
[108] Fix | Delete
'declarations' => array(
[109] Fix | Delete
'--wp--style--unstable-gallery-gap' => $gap_column,
[110] Fix | Delete
'gap' => $gap_value,
[111] Fix | Delete
),
[112] Fix | Delete
),
[113] Fix | Delete
);
[114] Fix | Delete
[115] Fix | Delete
wp_style_engine_get_stylesheet_from_css_rules(
[116] Fix | Delete
$gallery_styles,
[117] Fix | Delete
array(
[118] Fix | Delete
'context' => 'block-supports',
[119] Fix | Delete
)
[120] Fix | Delete
);
[121] Fix | Delete
[122] Fix | Delete
// The WP_HTML_Tag_Processor class calls get_updated_html() internally
[123] Fix | Delete
// when the instance is treated as a string, but here we explicitly
[124] Fix | Delete
// convert it to a string.
[125] Fix | Delete
$updated_content = $processed_content->get_updated_html();
[126] Fix | Delete
[127] Fix | Delete
/*
[128] Fix | Delete
* Randomize the order of image blocks. Ideally we should shuffle
[129] Fix | Delete
* the `$parsed_block['innerBlocks']` via the `render_block_data` hook.
[130] Fix | Delete
* However, this hook doesn't apply inner block updates when blocks are
[131] Fix | Delete
* nested.
[132] Fix | Delete
* @todo In the future, if this hook supports updating innerBlocks in
[133] Fix | Delete
* nested blocks, it should be refactored.
[134] Fix | Delete
*
[135] Fix | Delete
* @see: https://github.com/WordPress/gutenberg/pull/58733
[136] Fix | Delete
*/
[137] Fix | Delete
if ( empty( $attributes['randomOrder'] ) ) {
[138] Fix | Delete
return $updated_content;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
// This pattern matches figure elements with the `wp-block-image` class to
[142] Fix | Delete
// avoid the gallery's wrapping `figure` element and extract images only.
[143] Fix | Delete
$pattern = '/<figure[^>]*\bwp-block-image\b[^>]*>.*?<\/figure>/';
[144] Fix | Delete
[145] Fix | Delete
// Find all Image blocks.
[146] Fix | Delete
preg_match_all( $pattern, $updated_content, $matches );
[147] Fix | Delete
if ( ! $matches ) {
[148] Fix | Delete
return $updated_content;
[149] Fix | Delete
}
[150] Fix | Delete
$image_blocks = $matches[0];
[151] Fix | Delete
[152] Fix | Delete
// Randomize the order of Image blocks.
[153] Fix | Delete
shuffle( $image_blocks );
[154] Fix | Delete
$i = 0;
[155] Fix | Delete
$content = preg_replace_callback(
[156] Fix | Delete
$pattern,
[157] Fix | Delete
static function () use ( $image_blocks, &$i ) {
[158] Fix | Delete
$new_image_block = $image_blocks[ $i ];
[159] Fix | Delete
++$i;
[160] Fix | Delete
return $new_image_block;
[161] Fix | Delete
},
[162] Fix | Delete
$updated_content
[163] Fix | Delete
);
[164] Fix | Delete
[165] Fix | Delete
return $content;
[166] Fix | Delete
}
[167] Fix | Delete
/**
[168] Fix | Delete
* Registers the `core/gallery` block on server.
[169] Fix | Delete
*
[170] Fix | Delete
* @since 5.9.0
[171] Fix | Delete
*/
[172] Fix | Delete
function register_block_core_gallery() {
[173] Fix | Delete
register_block_type_from_metadata(
[174] Fix | Delete
__DIR__ . '/gallery',
[175] Fix | Delete
array(
[176] Fix | Delete
'render_callback' => 'block_core_gallery_render',
[177] Fix | Delete
)
[178] Fix | Delete
);
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
add_action( 'init', 'register_block_core_gallery' );
[182] Fix | Delete
[183] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function