Make WordPress Core

Changeset 51816


Ignore:
Timestamp:
09/15/2021 07:05:20 PM (21 months 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.

Location:
trunk
Files:
2 edited

Legend:

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

    r51230 r51816  
    20862086 * @since 3.9.0 The `class` attribute was added.
    20872087 * @since 5.1.0 The `caption_id` attribute was added.
     2088 * @since 5.9.0 The `$content` parameter default value changed from `null` to `''`.
    20882089 *
    20892090 * @param array  $attr {
     
    20982099 *     @type string $class      Additional class name(s) added to the caption container.
    20992100 * }
    2100  * @param string $content Shortcode content.
     2101 * @param string $content Optional. Shortcode content. Default empty string.
    21012102 * @return string HTML content to display the caption.
    21022103 */
    2103 function img_caption_shortcode( $attr, $content = null ) {
     2104function img_caption_shortcode( $attr, $content = '' ) {
    21042105    // New-style shortcode with the caption inside the shortcode with the link and image tags.
    21052106    if ( ! isset( $attr['caption'] ) ) {
  • 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.