Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcode: Fix issue when no attributes are passed #13264

Merged
Merged
9 changes: 7 additions & 2 deletions includes/Shortcode/Stories_Shortcode.php
Expand Up @@ -61,10 +61,10 @@ public function register(): void {
*
* @since 1.5.0
*
* @param array<string,string|int> $attrs Shortcode attributes.
* @param array<string,string|int>|string $attrs Shortcode attributes.
* @return string Story markup.
*/
public function render_stories( array $attrs ): string {
public function render_stories( $attrs ): string {
$default_pairs = [
'view' => 'circles',
'number_of_columns' => 1,
Expand All @@ -89,6 +89,11 @@ public function render_stories( array $attrs ): string {
$default_pairs[ $taxonomy ] = '';
}

// Initialize '$attrs' when not an array OR is an empty string.
if ( empty( $attrs ) || ! \is_array( $attrs ) ) {
$attrs = [];
}

$attributes = shortcode_atts(
$default_pairs,
$attrs,
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/integration/tests/Shortcode/Stories_Shortcode.php
Expand Up @@ -87,6 +87,20 @@ public function test_render_circles_view_in_shortcode(): void {
$this->assertNotSame( false, strpos( $actual, 'is-view-type-circles' ) );
}

/**
* Should not fail on empty/blank string attribute.
*
* @covers ::render_stories
* @covers ::prepare_story_attrs
* @covers ::prepare_story_args
*/
public function test_empty_attribute_in_shortcode(): void {
$stories_shortcode = new Testee();
$actual = $stories_shortcode->render_stories( '' );

$this->assertTrue( strpos( $actual, 'web-stories-list' ) > -1 );
}

/**
* Stories should not be greater than 100.
*
Expand Down