diff --git a/src/php/Admin/Bootstrap_Admin.php b/src/php/Admin/Bootstrap_Admin.php index b8875ca4e..c43f0a616 100644 --- a/src/php/Admin/Bootstrap_Admin.php +++ b/src/php/Admin/Bootstrap_Admin.php @@ -353,15 +353,17 @@ public function get_active_notice( ?DateTimeImmutable $now = null ): ?array { $now = $now ?? new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); $dismissed = get_user_meta( get_current_user_id(), self::DISMISSED_NOTICES_META_KEY, false ); $welcome = $this->welcome_client->get_banner(); + $welcome_text = '' === ( $welcome['headline_free'] ?? '' ) ? ( $welcome['text_free'] ?? '' ) : $welcome['headline_free']; if ( ! empty( $welcome['key'] ) && ! in_array( $welcome['key'], $dismissed, true ) && + '' !== trim( wp_strip_all_tags( (string) $welcome_text ) ) && ( empty( $welcome['start_datetime'] ) || $now >= $welcome['start_datetime'] ) && ( empty( $welcome['end_datetime'] ) || $now <= $welcome['end_datetime'] ) ) { return [ 'key' => $welcome['key'], 'style' => self::PROMO_BANNER_STYLE, 'label' => $welcome['label'] ?? '', - 'text' => '' === ( $welcome['headline_free'] ?? '' ) ? ( $welcome['text_free'] ?? '' ) : $welcome['headline_free'], + 'text' => $welcome_text, 'secondary' => $welcome['secondary_free'] ?? '', 'action_url' => $welcome['action_url_free'] ?? '', 'action_label' => $welcome['action_label_free'] ?? '', diff --git a/src/php/Client/Welcome_Client.php b/src/php/Client/Welcome_Client.php index 5d7ffa464..b5dd8ff2c 100644 --- a/src/php/Client/Welcome_Client.php +++ b/src/php/Client/Welcome_Client.php @@ -384,7 +384,8 @@ function ( $section ) { * } */ public function get_banner(): array { - return $this->welcome_data['banner'] ?? []; + $banner = $this->welcome_data['banner'] ?? []; + return is_array( $banner ) ? $banner : []; } /** diff --git a/tests/unit/Admin/Bootstrap_Admin_Test.php b/tests/unit/Admin/Bootstrap_Admin_Test.php index cce5ded15..a102427a1 100644 --- a/tests/unit/Admin/Bootstrap_Admin_Test.php +++ b/tests/unit/Admin/Bootstrap_Admin_Test.php @@ -376,4 +376,23 @@ public function test_dismissal_with_invalid_nonce_is_ignored(): void { $this->assertEmpty( get_user_meta( self::get_user_id(), self::DISMISSED_META_KEY, false ) ); $this->assertIsArray( $admin->get_promo_banner( self::moment( self::DURING_BANNER ) ) ); } + + /** + * A feed banner without any text is not shown, and the survey notice is offered instead. + * + * @return void + */ + public function test_feed_banner_without_text_is_not_shown(): void { + foreach ( [ '', ' ', ' ' ] as $text ) { + $admin = $this->make_admin( + [ + 'text_free' => $text, + 'headline_free' => '', + ] + ); + + $this->assertNull( $admin->get_promo_banner( self::moment( self::DURING_BANNER ) ), "Text: '$text'" ); + $this->assertSame( 'survey', $admin->get_active_notice( self::moment( self::DURING_BANNER ) )['key'], "Text: '$text'" ); + } + } } diff --git a/tests/unit/Client/Welcome_Client_Test.php b/tests/unit/Client/Welcome_Client_Test.php index 39e440c4c..81fdd2caf 100644 --- a/tests/unit/Client/Welcome_Client_Test.php +++ b/tests/unit/Client/Welcome_Client_Test.php @@ -82,4 +82,18 @@ public function test_banner_reads_promotion_fields_and_missing_dates(): void { $this->assertSame( [ 'Only feature' ], array_column( $client->get_features(), 'title' ) ); $this->assertSame( [], $client->get_partners() ); } + + /** + * Stored welcome data with a banner that is not a list is read as having no banner. + * + * @return void + */ + public function test_damaged_stored_banner_is_read_as_no_banner(): void { + set_transient( 'code_snippets_welcome_data', [ 'banner' => 'damaged' ] ); + + $banner = ( new Welcome_Client() )->get_banner(); + delete_transient( 'code_snippets_welcome_data' ); + + $this->assertSame( [], $banner ); + } }