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

Prevent potential fatal in multi-currency widget markup getter #10291

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions changelog/fix-10220-multi-currency-widget-markup-getter
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Ensure multi-currency widget markup getter don't throw errors.
9 changes: 8 additions & 1 deletion includes/multi-currency/MultiCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,15 @@ public function get_switcher_widget_markup( array $instance = [], array $args =
* call the_widget, you need to have the name of the widget, so we get the instance and hash to use.
*/
ob_start();

$currency_switcher_widget = $this->get_currency_switcher_widget();

if ( ! is_object( $currency_switcher_widget ) ) {
return ob_get_clean();
}

the_widget(
spl_object_hash( $this->get_currency_switcher_widget() ),
spl_object_hash( $currency_switcher_widget ),
apply_filters( self::FILTER_PREFIX . 'theme_widget_instance', $instance ),
apply_filters( self::FILTER_PREFIX . 'theme_widget_args', $args )
);
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/multi-currency/test-class-multi-currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,18 @@ public function test_get_switcher_widget_markup() {
$this->assertEquals( $expected, $this->multi_currency->get_switcher_widget_markup() );
}

public function test_get_switcher_widget_markup_when_widget_instance_is_null() {
$mock_multi_currency = $this
->getMockBuilder( WCPay\MultiCurrency\MultiCurrency::class )
->disableOriginalConstructor()
->onlyMethods( [ 'get_currency_switcher_widget' ] )
->getMock();

$mock_multi_currency->method( 'get_currency_switcher_widget' )->willReturn( null );

$this->assertEquals( '', $mock_multi_currency->get_switcher_widget_markup() );
}

public function test_validate_currency_code_returns_existing_currency_code() {
$this->assertEquals( 'CAD', $this->multi_currency->validate_currency_code( 'CAD' ) );
$this->assertEquals( 'CAD', $this->multi_currency->validate_currency_code( 'cAd' ) );
Expand Down
Loading