@@ -5257,7 +5257,7 @@ function get_media_embedded_in_content( $content, $types = null ) {
52575257function get_post_galleries ( $ post , $ html = true , $ max_galleries = PHP_INT_MAX ) {
52585258 $ post = get_post ( $ post );
52595259
5260- if ( ! $ post ) {
5260+ if ( ! ( $ post instanceof WP_Post ) ) {
52615261 return array ();
52625262 }
52635263
@@ -5269,7 +5269,7 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
52695269 if ( preg_match_all ( '/ ' . get_shortcode_regex () . '/s ' , $ post ->post_content , $ matches , PREG_SET_ORDER ) ) {
52705270 foreach ( $ matches as $ shortcode ) {
52715271 if ( 'gallery ' === $ shortcode [2 ] ) {
5272- $ srcs = array ();
5272+ $ sources = array ();
52735273
52745274 $ shortcode_attrs = shortcode_parse_atts ( $ shortcode [3 ] );
52755275
@@ -5285,14 +5285,14 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
52855285 preg_match_all ( '#src=([ \'"])(.+?)\1#is ' , $ gallery , $ src , PREG_SET_ORDER );
52865286 if ( ! empty ( $ src ) ) {
52875287 foreach ( $ src as $ s ) {
5288- $ srcs [] = $ s [2 ];
5288+ $ sources [] = $ s [2 ];
52895289 }
52905290 }
52915291
52925292 $ galleries [] = array_merge (
52935293 $ shortcode_attrs ,
52945294 array (
5295- 'src ' => array_values ( array_unique ( $ srcs ) ),
5295+ 'src ' => array_values ( array_unique ( $ sources ) ),
52965296 )
52975297 );
52985298 }
@@ -5311,32 +5311,29 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
53115311 $ has_inner_blocks = false ;
53125312 $ gallery_ids = $ processor ->allocate_and_return_parsed_attributes ()['ids ' ] ?? array ();
53135313 $ ids = array ();
5314- $ srcs = array ();
5314+ $ sources = array ();
5315+ $ inner_html = array ();
53155316
5316- $ html_chunks = array ();
5317- $ top_chunks = array ();
53185317 while ( $ processor ->next_token () && $ processor ->get_depth () > $ gallery_depth ) {
5319- if ( $ processor ->is_html () ) {
5320- $ chunk_span = $ processor ->get_span ();
5321- $ chunk = substr ( $ post ->post_content , $ chunk_span ->start , $ chunk_span ->length );
5322- $ html_chunks [] = $ chunk ;
5323- if ( $ gallery_depth === $ processor ->get_depth () - 1 ) {
5324- $ top_chunks [] = $ chunk ;
5325- }
5326- } else {
5318+ if ( ! $ processor ->is_html () ) {
53275319 $ has_inner_blocks = true ;
53285320 break ;
53295321 }
5322+
5323+ if ( $ processor ->get_depth () === $ gallery_depth + 1 ) {
5324+ $ inner_html [] = $ processor ->get_html_content ();
5325+ }
53305326 }
53315327
53325328 // New Gallery block format as HTML.
53335329 if ( $ has_inner_blocks && $ html ) {
5330+ // Reset this, because the new format stores everything inside the inner blocks.
5331+ $ html_chunks = array ();
53345332
53355333 // Get the rest of the innerHTML of the Gallery’s direct children.
53365334 while ( $ processor ->next_token () && $ processor ->get_depth () > $ gallery_depth ) {
53375335 if ( $ processor ->is_html () && $ processor ->get_depth () === $ gallery_depth + 2 ) {
5338- $ chunk_span = $ processor ->get_span ();
5339- $ html_chunks [] = substr ( $ post ->post_content , $ chunk_span ->start , $ chunk_span ->length );
5336+ $ html_chunks [] = $ processor ->get_html_content ();
53405337 }
53415338 }
53425339
@@ -5363,24 +5360,24 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
53635360 $ ids [] = $ id ;
53645361 $ url = wp_get_attachment_url ( $ id );
53655362
5366- if ( is_string ( $ url ) && ! in_array ( $ url , $ srcs , true ) ) {
5367- $ srcs [] = $ url ;
5363+ if ( is_string ( $ url ) && ! in_array ( $ url , $ sources , true ) ) {
5364+ $ sources [] = $ url ;
53685365 }
53695366 }
53705367 }
53715368 } while ( $ processor ->next_block () && $ processor ->get_depth () > $ gallery_depth );
53725369
53735370 $ galleries [] = array (
53745371 'ids ' => implode ( ', ' , $ ids ),
5375- 'src ' => $ srcs ,
5372+ 'src ' => $ sources ,
53765373 );
53775374
53785375 continue ;
53795376 }
53805377
53815378 // Old Gallery block format as HTML.
53825379 if ( $ html ) {
5383- $ galleries [] = implode ( '' , $ top_chunks );
5380+ $ galleries [] = implode ( '' , $ inner_html );
53845381 continue ;
53855382 }
53865383
@@ -5397,30 +5394,30 @@ function get_post_galleries( $post, $html = true, $max_galleries = PHP_INT_MAX )
53975394 foreach ( $ gallery_ids as $ id ) {
53985395 $ url = wp_get_attachment_url ( $ id );
53995396
5400- if ( is_string ( $ url ) && ! in_array ( $ url , $ srcs , true ) ) {
5401- $ srcs [] = $ url ;
5397+ if ( is_string ( $ url ) && ! in_array ( $ url , $ sources , true ) ) {
5398+ $ sources [] = $ url ;
54025399 }
54035400 }
54045401
54055402 $ galleries [] = array (
5406- 'ids ' => implode ( ', ' , $ ids ),
5407- 'src ' => $ srcs ,
5403+ 'ids ' => implode ( ', ' , $ gallery_ids ),
5404+ 'src ' => $ sources ,
54085405 );
54095406
54105407 continue ;
54115408 }
54125409
54135410 // Otherwise, extract srcs from the innerHTML.
5414- $ inner_html = implode ( '' , $ top_chunks );
5411+ $ inner_html = implode ( '' , $ inner_html );
54155412 $ src_finder = new WP_HTML_Tag_Processor ( $ inner_html );
54165413 while ( $ src_finder ->next_tag () ) {
54175414 $ src = $ src_finder ->get_attribute ( 'src ' );
5418- if ( is_string ( $ src ) && ! in_array ( $ src , $ srcs , true ) ) {
5419- $ srcs [] = $ src ;
5415+ if ( is_string ( $ src ) && ! in_array ( $ src , $ sources , true ) ) {
5416+ $ sources [] = $ src ;
54205417 }
54215418 }
54225419
5423- $ galleries [] = array ( 'src ' => $ srcs );
5420+ $ galleries [] = array ( 'src ' => $ sources );
54245421 }
54255422
54265423 /**
0 commit comments