Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,16 @@ public function ai_connect_metadata() {
/**
* Set Black Friday data.
*
* @param array $configs The configuration array for the loaded products.
* @param array<string, array<string, mixed>> $configs The configuration array for the loaded products.
*
* @return array
* @return array<string, array<string, mixed>>
*/
public function add_black_friday_data( $configs ) {
// An earlier filter may drop the default entry, or empty the list to opt out of the sale.
if ( empty( $configs['default'] ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The new empty() check also skips a present but empty default, such as array( 'default' => array() ). Before this change, Otter still added its sale entry for that value. Does any supported filter send an empty default and expect the Otter entry? If no, the change is safe. If yes, check ! isset() instead, so an empty array still gets the entry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No supported producer sends an empty default. The SDK always seeds it from get_notice_data(), a non-empty array of title, sale_url, message and time_left (Announcements.php:212-217). Every themeisle_sdk_blackfriday_data callback on the test install reads default and none empties it, so an empty default only arrives when a filter blanks the sale data. Passing it through unchanged is the intended opt-out.

return $configs;
}

$config = $configs['default'];

$message = __( 'Advanced blocks, custom CSS, WooCommerce integration. Everything you need to build better pages, without code. Exclusively for existing Otter users.', 'otter-blocks' );
Expand Down
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,6 @@ parameters:
count: 1
path: inc/class-main.php

-
rawMessage: 'Method ThemeIsle\GutenbergBlocks\Main::add_black_friday_data() has parameter $configs with no value type specified in iterable type array.'
identifier: missingType.iterableValue
count: 1
path: inc/class-main.php

-
rawMessage: 'Method ThemeIsle\GutenbergBlocks\Main::add_black_friday_data() return type has no value type specified in iterable type array.'
identifier: missingType.iterableValue
count: 1
path: inc/class-main.php

-
rawMessage: 'Method ThemeIsle\GutenbergBlocks\Main::add_random_orderby_param() has parameter $params with no value type specified in iterable type array.'
identifier: missingType.iterableValue
Expand Down
2 changes: 1 addition & 1 deletion plugins/blocks-animation/blocks-animation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function () {
add_filter(
'themeisle_sdk_blackfriday_data',
function ( $configs ) {
if ( defined( 'OTTER_BLOCKS_PATH' ) ) {
if ( defined( 'OTTER_BLOCKS_PATH' ) || empty( $configs['default'] ) ) {
return $configs;
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/blocks-css/blocks-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function () {
add_filter(
'themeisle_sdk_blackfriday_data',
function ( $configs ) {
if ( defined( 'OTTER_BLOCKS_PATH' ) ) {
if ( defined( 'OTTER_BLOCKS_PATH' ) || empty( $configs['default'] ) ) {
return $configs;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/test-black-friday-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Tests for Main::add_black_friday_data().
*
* @package gutenberg-blocks
*/

use ThemeIsle\GutenbergBlocks\Main;

/**
* Black Friday SDK data test case.
*/
class TestBlackFridayData extends WP_UnitTestCase {

/**
* Set up each test.
*/
public function set_up(): void {
parent::set_up();

// The test bootstrap loads Composer before WordPress, so the SDK's load.php skips its helpers.
if ( ! function_exists( 'tsdk_translate_link' ) ) {
function tsdk_translate_link( string $link ): string {
return $link;
}
}

if ( ! function_exists( 'tsdk_utmify' ) ) {
function tsdk_utmify( string $link ): string {
return $link;
}
}
}

/**
* An earlier filter returning an empty array opts out of the promotion, so it must stay empty.
*/
public function test_empty_configs_are_returned_unchanged(): void {
$this->assertSame( array(), ( new Main() )->add_black_friday_data( array() ) );
}

/**
* Configs whose default entry was removed upstream are passed through untouched.
*/
public function test_configs_without_default_are_returned_unchanged(): void {
$configs = array(
'other-plugin' => array( 'message' => 'Other plugin sale' ),
);

$this->assertSame( $configs, ( new Main() )->add_black_friday_data( $configs ) );
}

/**
* The SDK default entry is extended into an Otter entry.
*/
public function test_default_config_adds_otter_entry(): void {
$default = array(
'title' => 'Default title',
'message' => 'Default message',
);

$configs = ( new Main() )->add_black_friday_data( array( 'default' => $default ) );

$this->assertSame( $default, $configs['default'] );
$this->assertArrayHasKey( OTTER_PRODUCT_SLUG, $configs );
$this->assertStringContainsString( 'otter-bf', $configs[ OTTER_PRODUCT_SLUG ]['sale_url'] );
$this->assertNotSame( $default['message'], $configs[ OTTER_PRODUCT_SLUG ]['message'] );
}
}
Loading