Make WordPress Core

Ticket #43826: 43826.patch

File 43826.patch, 3.2 KB (added by Takahashi_Fumiki, 6 years ago)

Add gutenberg block parser for get_post_galleries

  • src/wp-includes/media.php

     
    37653765        if ( ! $post = get_post( $post ) )
    37663766                return array();
    37673767
    3768         if ( ! has_shortcode( $post->post_content, 'gallery' ) )
    3769                 return array();
    3770 
    37713768        $galleries = array();
    3772         if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
    3773                 foreach ( $matches as $shortcode ) {
    3774                         if ( 'gallery' === $shortcode[2] ) {
    3775                                 $srcs = array();
    3776 
    3777                                 $shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
    3778                                 if ( ! is_array( $shortcode_attrs ) ) {
    3779                                         $shortcode_attrs = array();
     3769       
     3770        // For backward compatibility, check shortcode.
     3771        if ( has_shortcode( $post->post_content, 'gallery' ) ) {
     3772                if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
     3773                        foreach ( $matches as $shortcode ) {
     3774                                if ( 'gallery' === $shortcode[2] ) {
     3775                                        $srcs = array();
     3776                                       
     3777                                        $shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
     3778                                        if ( ! is_array( $shortcode_attrs ) ) {
     3779                                                $shortcode_attrs = array();
     3780                                        }
     3781                                       
     3782                                        // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
     3783                                        if ( ! isset( $shortcode_attrs['id'] ) ) {
     3784                                                $shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
     3785                                        }
     3786                                       
     3787                                        $gallery = do_shortcode_tag( $shortcode );
     3788                                        if ( $html ) {
     3789                                                $galleries[] = $gallery;
     3790                                        } else {
     3791                                                preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
     3792                                                if ( ! empty( $src ) ) {
     3793                                                        foreach ( $src as $s ) {
     3794                                                                $srcs[] = $s[2];
     3795                                                        }
     3796                                                }
     3797                                               
     3798                                                $galleries[] = array_merge(
     3799                                                        $shortcode_attrs,
     3800                                                        array(
     3801                                                                'src' => array_values( array_unique( $srcs ) )
     3802                                                        )
     3803                                                );
     3804                                        }
    37803805                                }
    3781 
    3782                                 // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
    3783                                 if ( ! isset( $shortcode_attrs['id'] ) ) {
    3784                                         $shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
    3785                                 }
    3786 
    3787                                 $gallery = do_shortcode_tag( $shortcode );
     3806                        }
     3807                }
     3808        }
     3809       
     3810        // Check gutenberg gallery block.
     3811        if ( false !== strpos( $post->post_content, '<!-- wp:gallery' ) ) {
     3812                if ( preg_match_all( '/<!-- wp:gallery([^>]*)-->(.*?)<!-- \/wp:gallery -->/s', $post->post_content, $blocks, PREG_SET_ORDER ) ) {
     3813                        foreach ( $blocks as $block ) {
    37883814                                if ( $html ) {
    3789                                         $galleries[] = $gallery;
     3815                                        $galleries[] = $block[2];
    37903816                                } else {
    3791                                         preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
    3792                                         if ( ! empty( $src ) ) {
    3793                                                 foreach ( $src as $s ) {
    3794                                                         $srcs[] = $s[2];
    3795                                                 }
     3817                                        $attributes = (array) json_decode( trim( $block[1] ), true );
     3818                                        $src = array();
     3819                                        if ( preg_match_all( '/src="([^"]+)"/s', $block[2], $imgs ) ) {
     3820                                                $src = $imgs[1];
     3821                                                $src = array_unique( $src );
    37963822                                        }
    3797 
    3798                                         $galleries[] = array_merge(
    3799                                                 $shortcode_attrs,
    3800                                                 array(
    3801                                                         'src' => array_values( array_unique( $srcs ) )
    3802                                                 )
    3803                                         );
     3823                                        if ( $src ) {
     3824                                                $attributes['src'] = $src;
     3825                                                $galleries[] = $attributes;
     3826                                        }
    38043827                                }
    38053828                        }
    38063829                }