[wp-trac] [WordPress Trac] #60928: Interactivity API: Fatal error with empty image

WordPress Trac noreply at wordpress.org
Tue Jun 11 22:56:24 UTC 2024


#60928: Interactivity API: Fatal error with empty image
-------------------------------------+----------------------
 Reporter:  donohoe                  |       Owner:  (none)
     Type:  defect (bug)             |      Status:  closed
 Priority:  normal                   |   Milestone:
Component:  Editor                   |     Version:  6.5.4
 Severity:  normal                   |  Resolution:  invalid
 Keywords:  reporter-feedback close  |     Focuses:
-------------------------------------+----------------------

Comment (by fnorte):

 I Discovery the problem is in this function bellow, commenting the hook
 and resolve the error, my I'm losting my resource:

 What WordPress change to cause this error?
 How can I implement this better?

 {{{

   //Adicionado crédito as imagens inseridas no Gutenberg
   function change_image_block( $block_content, $block ) {
     if(!is_admin()){
       if ($block['blockName'] === 'core/image') {
         if( str_contains( $block['innerHTML'], 'figure')){
           if( str_contains( $block['innerHTML'], 'figcaption')){
             $block_caption = true;
           }else{
             $block_caption = false;
           }
           $imagem_id = $block['attrs']['id'];
           $imagem = get_post( $imagem_id );
           $imagem_legenda = isset($imagem->post_excerpt) ?
 $imagem->post_excerpt : null;
           $imagem_credito = isset($imagem->post_content) ?
 $imagem->post_content : null;
           $imagem_full = isset(wp_get_attachment_image_src($imagem_id,
 'normal-full')[0]) ? wp_get_attachment_image_src($imagem_id, 'normal-
 full')[0] : null;
           $imagem_desktop = isset(wp_get_attachment_image_src($imagem_id,
 'normal-desktop')[0]) ? wp_get_attachment_image_src($imagem_id, 'normal-
 desktop')[0] : null;
           $imagem_mobile = isset(wp_get_attachment_image_src($imagem_id,
 'normal-mobile')[0]) ? wp_get_attachment_image_src($imagem_id, 'normal-
 mobile')[0] : null;
           $imagem_alt = get_post_meta($imagem_id,
 '_wp_attachment_image_alt', true );
           if($block_caption){
             $search =
 "/(<figure(.*?)>)(.*?)(<figcaption(.*?)>)(.*?)(<\/figcaption>)(<\/figure>)/";
             if($imagem_credito){
               $figcaption = '<figcaption $5><small class="image-
 subtitle">$6</small><small class="image-credit">(foto:
 '.$imagem_credito.')</small></figcaption>';
               $alt = $imagem_alt.' - (foto: '.$imagem_credito.')';
             }else{
               $figcaption = '<figcaption $5><small class="image-
 subtitle">$6</small></figcaption>';
               $alt = $imagem_alt;
             }
           }else{
             $search = "/(<figure(.*?)>)(.*?)(<\/figure>)/";
             if($imagem_credito){
               $figcaption = '<figcaption class="wp-element-caption"><small
 class="image-credit">(foto: '.$imagem_credito.')</small></figcaption>';
               $alt = '(foto: '.$imagem_credito.')';
             }else{
               $figcaption = '';
               $alt = '';
             }
           }
           if(isset($block['attrs']['width'])){
             $width = $block['attrs']['width'];
           }else{
             $width = '100%';
           }
           if(isset($block['attrs']['height'])){
             $height = $block['attrs']['height'];
           }else{
             $height = '100%';
           }
           if( str_contains( $block['innerHTML'], 'is-resized')){
             $replace = '<div class="wp-block-image"><figure $2>
             <picture>
               <source srcset="'.$imagem_mobile.'" media="(max-width:
 1023px)">
               <source srcset="'.$imagem_desktop.'" media="(min-width:
 1024px) and (max-width: 1399px)">
               <source srcset="'.$imagem_full.'" media="(min-width:
 1400px)">
               <img loading="eager" width="'.$width.'" height="'.$height.'"
 src="'.$imagem_full.'"  title="'.$alt.'" alt="'.$alt.'">
             </picture>'.$figcaption.'</figure></div>';
           }else{
             $replace = '<figure $2>
             <picture>
               <source srcset="'.$imagem_mobile.'" media="(max-width:
 1023px)">
               <source srcset="'.$imagem_desktop.'" media="(min-width:
 1024px) and (max-width: 1399px)">
               <source srcset="'.$imagem_full.'" media="(min-width:
 1400px)">
               <img loading="eager" width="'.$width.'" height="'.$height.'"
 src="'.$imagem_full.'"  title="'.$alt.'" alt="'.$alt.'">
             </picture>'.$figcaption.'</figure>';
           }
           $new_content = preg_replace($search, $replace, $block_content);
           return $new_content;
         }else{
           return $block_content;
         }
       }else{
         return $block_content;
       }
     }
   }
   // add_filter( 'render_block', 'change_image_block', 10, 2 );
 }}}


 Replying to [comment:15 fnorte]:
 > Thsi closed issue happening with me too, after upgrade from 6.4.3 to
 6.5.4, but in all post with the image block on content. like this...
 >
 > {{{
 > <!-- wp:image {"id":226159,"sizeSlug":"full","linkDestination":"none"}
 -->
 > <figure class="wp-block-image size-full"><img
 src="https://noataque.local/wp-
 content/uploads/2024/06/53782904335_2d2f19b211_c.jpg" alt="Gustavo Scarpa
 deve atuar novamente pelo lado esquerdo no Atlético" class="wp-
 image-226159"/><figcaption class="wp-element-caption">Gustavo Scarpa deve
 atuar novamente pelo lado esquerdo no Atlético</figcaption></figure>
 > <!-- /wp:image -->
 > }}}
 >
 > I made some tests removing the block and work withouth problemas.
 > I got back to old version (6.4.3) and works withouth problem.
 >
 > Is a legacy code, and have several issues on them, the most relevant is
 full of custom post types and taxonomies.
 >
 > I trying to figured out how to identify what is causing that in my own
 theme code.
 >
 >
 > {{{
 > [11-Jun-2024 22:39:52 UTC] PHP Fatal error:  Uncaught TypeError:
 {closure}(): Argument #1 ($content) must be of type string, null given,
 called in /var/www/noataque/public_html/wp-includes/class-wp-hook.php on
 line 326 and defined in /var/www/noataque/public_html/wp-includes
 /interactivity-api/interactivity-api.php:46
 > Stack trace:
 > #0 /var/www/noataque/public_html/wp-includes/class-wp-hook.php(326):
 {closure}()
 > #1 /var/www/noataque/public_html/wp-includes/plugin.php(205):
 WP_Hook->apply_filters()
 > #2 /var/www/noataque/public_html/wp-includes/class-wp-block.php(525):
 apply_filters()
 > #3 /var/www/noataque/public_html/wp-includes/blocks.php(1705):
 WP_Block->render()
 > #4 /var/www/noataque/public_html/wp-includes/blocks.php(1743):
 render_block()
 > #5 /var/www/noataque/public_html/wp-includes/class-wp-hook.php(324):
 do_blocks()
 > #6 /var/www/noataque/public_html/wp-includes/plugin.php(205):
 WP_Hook->apply_filters()
 > #7 /var/www/noataque/public_html/wp-includes/rest-api/endpoints/class-
 wp-rest-posts-controller.php(1865): apply_filters()
 > #8 /var/www/noataque/public_html/wp-includes/rest-api/endpoints/class-
 wp-rest-posts-controller.php(569):
 WP_REST_Posts_Controller->prepare_item_for_response()
 > #9 /var/www/noataque/public_html/wp-includes/rest-api/class-wp-rest-
 server.php(1230): WP_REST_Posts_Controller->get_item()
 > #10 /var/www/noataque/public_html/wp-includes/rest-api/class-wp-rest-
 server.php(1063): WP_REST_Server->respond_to_request()
 > #11 /var/www/noataque/public_html/wp-includes/rest-api.php(555):
 WP_REST_Server->dispatch()
 > #12 /var/www/noataque/public_html/wp-includes/rest-api.php(2922):
 rest_do_request()
 > #13 [internal function]: rest_preload_api_request()
 > #14 /var/www/noataque/public_html/wp-includes/block-editor.php(753):
 array_reduce()
 > #15 /var/www/noataque/public_html/wp-admin/edit-form-blocks.php(77):
 block_editor_rest_api_preload()
 > #16 /var/www/noataque/public_html/wp-admin/post.php(187): require('...')
 > #17 {main}
 >   thrown in /var/www/noataque/public_html/wp-includes/interactivity-api
 /interactivity-api.php on line 46
 >
 > }}}
 >

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60928#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list