Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery: update the get_post_galleries method to account for gallery blocks #1788

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4711,7 +4711,7 @@ function get_post_galleries( $post, $html = true ) {
return array();
}

if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
return array();
}

Expand Down Expand Up @@ -4753,6 +4753,76 @@ function get_post_galleries( $post, $html = true ) {
}
}

if ( has_block( 'gallery', $post->post_content ) ) {
$post_blocks = parse_blocks( $post->post_content );
// Use while/array_shift instead of foreach so we can modify the array from within the loop
while ( $block = array_shift( $post_blocks ) ) {
if ( 'core/gallery' === $block['blockName'] ) {
// If a Gallery block has innerBlocks it is the new format and needs to be handled separately to the old
// format with nested <img> tags.
if ( ! empty( $block['innerBlocks'] ) ) {
$gallery_srcs = array();
$ids = array();
$block_html = array();
foreach ( $block['innerBlocks'] as $image ) {
if ( $html ) {
$block_html[] = $image['innerHTML'];
} else {
$gallery_srcs[] = wp_get_attachment_url( $image ['attrs']['id'] );
$ids[] = $image['attrs']['id'];
}
}
if ( $html ) {
$galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>';
} else {
$galleries[] = array(
// array_filter will eliminate any empty entries that came from unknown or invalid IDs
'src' => array_values( array_filter( array_unique( $gallery_srcs ) ) ),
// Only explicitly include the ids attribute. In future this could be changed to include all attributes, similar to $shortcode_attrs above.
'ids' => implode( ',', $ids ),
);
}
} else {
// Now handle the v1 gallery block format.
if ( $html ) {
$galleries[] = $block['innerHTML'];
} elseif ( ! empty( $block['attrs']['ids'] ) ) {
// Use the image IDs from the json blob as canonical if present
$gallery_srcs = array();
foreach ( $block['attrs']['ids'] as $gallery_img_id ) {
$gallery_srcs[] = wp_get_attachment_url( $gallery_img_id );
}
$galleries[] = array(
// array_filter will eliminate any empty entries that came from unknown or invalid IDs
'src' => array_values( array_filter( array_unique( $gallery_srcs ) ) ),
// Only explicitly include the ids attribute. In future this could be changed to include all attributes, similar to $shortcode_attrs above.
'ids' => implode( ',', $block['attrs']['ids'] ),
);
} else {
// Otherwise extract srcs from the innerHTML
$srcs = array();
preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $src, PREG_SET_ORDER );
if ( ! empty( $src ) ) {
foreach ( $src as $s ) {
$srcs[] = $s[2];
}
}

$galleries[] = array(
// Note that unlike shortcodes, all we are returning here is the src list
'src' => array_values( array_unique( $srcs ) ),
);
}
}
} elseif ( ! empty( $block['innerBlocks'] ) ) {
// If we have nested blocks then gradually flatten it by moving those onto the end of the root array for traversal
while ( $inner = array_pop( $block['innerBlocks'] ) ) {
array_push( $post_blocks, $inner );
}
}
}
}

/**
* Filters the list of all found galleries in the given post.
*
Expand Down
Loading