Make WordPress Core


Ignore:
Timestamp:
11/16/2021 09:23:12 PM (2 months ago)
Author:
hellofromTonya
Message:

Media: Add support for v1 and v2 gallery block in get_post_galleries().

The get_post_galleries() function only handled galleries from the [gallery] shortcode. It did not process gallery blocks.

Introducing v1 and v2 gallery block support in get_post_galleries() including support for innerblock nesting.

There are no changes to how the function is called. It detects if the post content has one or more gallery blocks. If detected, it parses the blocks and then processes to add each gallery block's HTML to the array of galleries before being passed through the filter and returned.

Includes integration tests.

Follow-up to [24682], [43309], [48262], [52042].

Props glendaviesnz, costdev, antpb, audrasjb, birgire, celloexpressions, desrosj, hellofromTonya, jeffpaul, lynk, pento, ramonopoly, russhylov, takahashi_fumiki, tellyworth.
Fixes #43826.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r52065 r52190  
    47264726    }
    47274727
    4728     if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
     4728    if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
    47294729        return array();
    47304730    }
     
    47654765                }
    47664766            }
     4767        }
     4768    }
     4769
     4770    if ( has_block( 'gallery', $post->post_content ) ) {
     4771        $post_blocks = parse_blocks( $post->post_content );
     4772
     4773        while ( $block = array_shift( $post_blocks ) ) {
     4774            $has_inner_blocks = ! empty( $block['innerBlocks'] );
     4775
     4776            // Skip blocks with no blockName and no innerHTML.
     4777            if ( ! $block['blockName'] ) {
     4778                continue;
     4779            }
     4780
     4781            // All blocks nested inside non-Gallery blocks should be in the root array.
     4782            if ( $has_inner_blocks && 'core/gallery' !== $block['blockName'] ) {
     4783                array_push( $post_blocks, ...$block['innerBlocks'] );
     4784                continue;
     4785            }
     4786
     4787            // New Gallery block format as HTML.
     4788            if ( $has_inner_blocks && $html ) {
     4789                $block_html  = wp_list_pluck( $block['innerBlocks'], 'innerHTML' );
     4790                $galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>';
     4791                continue;
     4792            }
     4793
     4794            $srcs = array();
     4795
     4796            // New Gallery block format as an array.
     4797            if ( $has_inner_blocks ) {
     4798                $attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' );
     4799                $ids   = wp_list_pluck( $attrs, 'id' );
     4800
     4801                foreach ( $ids as $id ) {
     4802                    $url = wp_get_attachment_url( $id );
     4803
     4804                    if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
     4805                        $srcs[] = $url;
     4806                    }
     4807                }
     4808
     4809                $galleries[] = array(
     4810                    'ids' => implode( ',', $ids ),
     4811                    'src' => $srcs,
     4812                );
     4813
     4814                continue;
     4815            }
     4816
     4817            // Old Gallery block format as HTML.
     4818            if ( $html ) {
     4819                $galleries[] = $block['innerHTML'];
     4820                continue;
     4821            }
     4822
     4823            // Old Gallery block format as an array.
     4824            $ids = ! empty( $block['attrs']['ids'] ) ? $block['attrs']['ids'] : array();
     4825
     4826            // If present, use the image IDs from the JSON blob as canonical.
     4827            if ( ! empty( $ids ) ) {
     4828                foreach ( $ids as $id ) {
     4829                    $url = wp_get_attachment_url( $id );
     4830
     4831                    if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
     4832                        $srcs[] = $url;
     4833                    }
     4834                }
     4835
     4836                $galleries[] = array(
     4837                    'ids' => implode( ',', $ids ),
     4838                    'src' => $srcs,
     4839                );
     4840
     4841                continue;
     4842            }
     4843
     4844            // Otherwise, extract srcs from the innerHTML.
     4845            preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER );
     4846
     4847            if ( ! empty( $found_srcs[0] ) ) {
     4848                foreach ( $found_srcs as $src ) {
     4849                    if ( isset( $src[2] ) && ! in_array( $src[2], $srcs, true ) ) {
     4850                        $srcs[] = $src[2];
     4851                    }
     4852                }
     4853            }
     4854
     4855            $galleries[] = array( 'src' => $srcs );
    47674856        }
    47684857    }
Note: See TracChangeset for help on using the changeset viewer.