Skip to content

Commit

Permalink
Fix bug with Jetpack and improper usage of current_screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Dec 15, 2022
1 parent ff8e7d4 commit 524ea51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/php/Settings/Abstracts/SettingsBase.php
Expand Up @@ -378,6 +378,10 @@ public function base_admin_enqueue_scripts() {
* Setup settings sections.
*/
public function setup_sections() {
if ( ! $this->is_options_screen() ) {
return;
}

$tab = $this->get_active_tab();

foreach ( $this->form_fields as $form_field ) {
Expand All @@ -395,12 +399,7 @@ public function setup_sections() {
* Setup tabs section.
*/
public function setup_tabs_section() {
/**
* Protection from the bug in \Automattic\Jetpack\Sync\Sender::get_items_to_send(),
* which sets screen without loading of wp-admin/includes/template.php,
* where add_settings_section() is defined.
*/
if ( ! function_exists( 'add_settings_section' ) ) {
if ( ! $this->is_options_screen() ) {
return;
}

Expand Down
26 changes: 17 additions & 9 deletions tests/phpunit/Settings/Abstracts/SettingsBaseTest.php
Expand Up @@ -472,6 +472,7 @@ public function test_setup_sections( $tabs ) {
$tab->shouldReceive( 'option_page' )->andReturn( $tab_option_page );

$subject = Mockery::mock( SettingsBase::class )->makePartial()->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'is_options_screen' )->andReturn( true );
$subject->shouldReceive( 'get_active_tab' )->once()->andReturn( $tab );

$form_fields = $this->get_test_form_fields();
Expand Down Expand Up @@ -509,17 +510,22 @@ public function dp_test_setup_sections() {
}

/**
* Test setup_tabs_section() without add_settings_section().
* Test setup_sections() not on options screen.
*/
public function test_setup_tabs_section_without_add_settings_section() {
FunctionMocker::replace(
'function_exists',
static function ( $function ) {
return 'add_settings_section' !== $function;
}
);
public function test_setup_sections_not_on_options_screen() {
$subject = Mockery::mock( SettingsBase::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'is_options_screen' )->andReturn( false );
$subject->setup_sections();
}

/**
* Test setup_tabs_section() not on options screen.
*/
public function test_setup_tabs_section_not_on_options_screen() {
$subject = Mockery::mock( SettingsBase::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'is_options_screen' )->andReturn( false );

$subject->setup_tabs_section();
}
Expand All @@ -533,7 +539,9 @@ public function test_setup_tabs_section() {
$tab = Mockery::mock( SettingsBase::class )->makePartial()->shouldAllowMockingProtectedMethods();
$tab->shouldReceive( 'option_page' )->andReturn( $tab_option_page );

$subject = Mockery::mock( SettingsBase::class )->makePartial()->shouldAllowMockingProtectedMethods();
$subject = Mockery::mock( SettingsBase::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'is_options_screen' )->once()->andReturn( true );
$subject->shouldReceive( 'get_active_tab' )->once()->andReturn( $tab );

WP_Mock::userFunction( 'add_settings_section' )
Expand Down

0 comments on commit 524ea51

Please sign in to comment.