Make WordPress Core


Ignore:
Timestamp:
09/15/2021 07:05:20 PM (3 years ago)
Author:
hellofromTonya
Message:

Media: Fix $content parameter default value in img_caption_shortcode().

The shortcode content is expected to be a string, not null. do_shortcode() expects a string for $content.

The img_caption_shortcode() also expects a string for the $content parameter and is expected to return a string for the HTML content to display the caption.

Prior to this commit:
The default value for the $content parameter was set to null. If no $content was passed, the function:

  • could return null when the $atts['width'] < 1 or there was no caption
  • else, it invoked do_shortcode( $content ) passing null which on PHP 8.1+ triggers a deprecation notice:
    strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated
    

This commit:

  • Fixes the default $content value to align to the expected shortcode content of string, not null.
  • Fixes the PHP 8.1 deprecation notice when null was being passed to do_shortcode().
  • Changes the assertion in a couple of tests to check for the empty string instead of `null.

Follow-up to [8196], [8925], [8239], [26915], [31530], [42704].

Props jrf, hellofromTonya, azaozz, joedolson.
See #53635.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r51587 r51816  
    8585    function test_img_caption_shortcode_with_empty_params() {
    8686        $result = img_caption_shortcode( array() );
    87         $this->assertNull( $result );
     87        $this->assertSame( '', $result );
    8888    }
    8989
     
    135135            )
    136136        );
    137         $this->assertNull( $result );
     137        $this->assertSame( '', $result );
    138138    }
    139139
Note: See TracChangeset for help on using the changeset viewer.