mirror of
git://develop.git.wordpress.org/
synced 2025-03-24 22:10:02 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51816 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f5aaafd1fe
commit
c0b328a74d
@ -2085,6 +2085,7 @@ add_shortcode( 'caption', 'img_caption_shortcode' );
|
||||
* @since 2.6.0
|
||||
* @since 3.9.0 The `class` attribute was added.
|
||||
* @since 5.1.0 The `caption_id` attribute was added.
|
||||
* @since 5.9.0 The `$content` parameter default value changed from `null` to `''`.
|
||||
*
|
||||
* @param array $attr {
|
||||
* Attributes of the caption shortcode.
|
||||
@ -2097,10 +2098,10 @@ add_shortcode( 'caption', 'img_caption_shortcode' );
|
||||
* @type string $caption The caption text.
|
||||
* @type string $class Additional class name(s) added to the caption container.
|
||||
* }
|
||||
* @param string $content Shortcode content.
|
||||
* @param string $content Optional. Shortcode content. Default empty string.
|
||||
* @return string HTML content to display the caption.
|
||||
*/
|
||||
function img_caption_shortcode( $attr, $content = null ) {
|
||||
function img_caption_shortcode( $attr, $content = '' ) {
|
||||
// New-style shortcode with the caption inside the shortcode with the link and image tags.
|
||||
if ( ! isset( $attr['caption'] ) ) {
|
||||
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
|
||||
|
@ -84,7 +84,7 @@ CAP;
|
||||
|
||||
function test_img_caption_shortcode_with_empty_params() {
|
||||
$result = img_caption_shortcode( array() );
|
||||
$this->assertNull( $result );
|
||||
$this->assertSame( '', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +134,7 @@ CAP;
|
||||
'caption' => '',
|
||||
)
|
||||
);
|
||||
$this->assertNull( $result );
|
||||
$this->assertSame( '', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user