Refactor in the WC_Settings_Page class for PHP 8 compatibility
- Turn get_settings into a parameterless method, but accept one parameter via func_get_arg; and mark the method as deprecated. - Rename the existing get_settings to get_settings_for_section; and mark the method as final. - Rename the existing get_settings_for_section to get_settings_for_section_core. See the comment added to get_settings for the rationale for the change.
This commit is contained in:
parent
5af12170d7
commit
cf9300bbbc
|
@ -77,12 +77,25 @@ if ( ! class_exists( 'WC_Settings_Page', false ) ) :
|
|||
/**
|
||||
* Get settings array for the default section.
|
||||
*
|
||||
* External settings classes (registered via 'woocommerce_get_settings_pages' filter)
|
||||
* might have redefined this method as "get_settings($section_id='')", thus we need
|
||||
* to use this method internally instead of 'get_settings_for_section' to register settings
|
||||
* and render settings pages.
|
||||
*
|
||||
* *But* we can't just redefine the method as "get_settings($section_id='')" here, since this
|
||||
* will break on PHP 8 if any external setting class have it as 'get_settings()'.
|
||||
*
|
||||
* Thus we leave the method signature as is and use 'func_get_arg' to get the setting id
|
||||
* if it's supplied, and we use this method internally; but it's deprecated and should
|
||||
* otherwise never be used.
|
||||
*
|
||||
* @deprecated 5.4.0 Use 'get_settings_for_section' (passing an empty string for default section)
|
||||
*
|
||||
* @return array Settings array, each item being an associative array representing a setting.
|
||||
*/
|
||||
public function get_settings() {
|
||||
return $this->get_settings_for_section( '' );
|
||||
$section_id = 0 === func_num_args() ? '' : func_get_arg( 0 );
|
||||
return $this->get_settings_for_section( $section_id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -603,8 +603,13 @@ class WC_Install {
|
|||
}
|
||||
$subsections = array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) );
|
||||
|
||||
/**
|
||||
* We are using 'WC_Settings_Page::get_settings' on purpose even thought it's deprecated.
|
||||
* See the method documentation for an explanation.
|
||||
*/
|
||||
|
||||
foreach ( $subsections as $subsection ) {
|
||||
foreach ( $section->get_settings_for_section( $subsection ) as $value ) {
|
||||
foreach ( $section->get_settings( $subsection ) as $value ) {
|
||||
if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
|
||||
$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
|
||||
add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
|
||||
|
|
|
@ -114,11 +114,16 @@ class WC_Register_WP_Admin_Settings {
|
|||
$sections = $this->object->get_sections();
|
||||
if ( empty( $sections ) ) {
|
||||
// Default section is just an empty string, per admin page classes.
|
||||
$sections = array( '' );
|
||||
$sections = array( '' => '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* We are using 'WC_Settings_Page::get_settings' on purpose even thought it's deprecated.
|
||||
* See the method documentation for an explanation.
|
||||
*/
|
||||
|
||||
foreach ( $sections as $section => $section_label ) {
|
||||
$settings_from_section = $this->object->get_settings_for_section( $section );
|
||||
$settings_from_section = $this->object->get_settings( $section );
|
||||
foreach ( $settings_from_section as $setting ) {
|
||||
if ( ! isset( $setting['id'] ) ) {
|
||||
continue;
|
||||
|
|
|
@ -186,7 +186,7 @@ class WC_Tests_Register_WP_Admin_Settings extends WC_Unit_Test_Case {
|
|||
* @since 3.0.0
|
||||
* @covers WC_Register_WP_Admin_Settings::register_page_settings
|
||||
*/
|
||||
public function test_register_settings_one_section() {
|
||||
public function test_register_settings_default_section_no_settings() {
|
||||
$this->page
|
||||
->expects( $this->any() )
|
||||
->method( 'get_sections' )
|
||||
|
@ -194,8 +194,8 @@ class WC_Tests_Register_WP_Admin_Settings extends WC_Unit_Test_Case {
|
|||
|
||||
$this->page
|
||||
->expects( $this->once() )
|
||||
->method( 'get_settings_for_section' )
|
||||
->with( $this->equalTo( 0 ) )
|
||||
->method( 'get_settings' )
|
||||
->with( $this->equalTo( '' ) )
|
||||
->will( $this->returnValue( array() ) );
|
||||
|
||||
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
||||
|
@ -210,7 +210,7 @@ class WC_Tests_Register_WP_Admin_Settings extends WC_Unit_Test_Case {
|
|||
* @since 3.0.0
|
||||
* @covers WC_Register_WP_Admin_Settings::register_page_settings
|
||||
*/
|
||||
public function test_register_settings() {
|
||||
public function test_register_settings_default_section_with_settings() {
|
||||
$this->page
|
||||
->expects( $this->any() )
|
||||
->method( 'get_sections' )
|
||||
|
@ -235,7 +235,7 @@ class WC_Tests_Register_WP_Admin_Settings extends WC_Unit_Test_Case {
|
|||
|
||||
$this->page
|
||||
->expects( $this->any() )
|
||||
->method( 'get_settings_for_section' )
|
||||
->method( 'get_settings' )
|
||||
->will( $this->returnValue( $settings ) );
|
||||
|
||||
$settings = new WC_Register_WP_Admin_Settings( $this->page, 'page' );
|
||||
|
|
|
@ -59,12 +59,36 @@ class WC_Settings_Page_Test extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_settings_for_section (default section).
|
||||
*/
|
||||
public function test_get_settings_for_section__default_section() {
|
||||
$sut = new WC_Settings_Example();
|
||||
|
||||
$actual = $sut->get_settings_for_section( '' );
|
||||
|
||||
$expected = array( 'key' => 'value' );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_settings (named section with its own get_settings_for_X_section method).
|
||||
*/
|
||||
public function test_get_settings__named_section_with_own_method() {
|
||||
$sut = new WC_Settings_Example();
|
||||
|
||||
$actual = $sut->get_settings( 'foobar' );
|
||||
|
||||
$expected = array( 'foo' => 'bar' );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_settings_for_section (named section with its own get_settings_for_X_section method).
|
||||
*/
|
||||
public function test_get_settings_for_section__named_section_with_own_method() {
|
||||
$sut = new WC_Settings_Example();
|
||||
|
||||
$actual = $sut->get_settings_for_section( 'foobar' );
|
||||
|
||||
$expected = array( 'foo' => 'bar' );
|
||||
|
|
Loading…
Reference in New Issue