woocommerce/includes/admin/settings/class-wc-settings-page.php

235 lines
6.2 KiB
PHP
Raw Normal View History

2013-07-26 14:36:28 +00:00
<?php
/**
* WooCommerce Settings Page/Tab
*
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Admin
2013-07-26 14:36:28 +00:00
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
2013-07-26 14:36:28 +00:00
2017-02-16 11:46:01 +00:00
if ( ! class_exists( 'WC_Settings_Page', false ) ) :
2013-07-26 14:36:28 +00:00
/**
* WC_Settings_Page.
*/
abstract class WC_Settings_Page {
/**
* Setting page id.
*
* @var string
*/
protected $id = '';
/**
* Setting page label.
*
* @var string
*/
protected $label = '';
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
}
/**
* Get settings page ID.
*
* @since 3.0.0
* @return string
*/
public function get_id() {
return $this->id;
}
2016-03-28 19:04:26 +00:00
/**
* Get settings page label.
*
* @since 3.0.0
* @return string
*/
public function get_label() {
return $this->label;
}
2016-03-28 19:04:26 +00:00
/**
* Add this page to settings.
*
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* @param array $pages The setings array where we'll add ourselves.
*
* @return mixed
*/
public function add_settings_page( $pages ) {
$pages[ $this->id ] = $this->label;
return $pages;
}
2013-07-26 14:36:28 +00:00
/**
* Get settings array for the default section.
*
* @deprecated 5.4.0 'get_settings_for_section' (passing an empty string for default section) should be used instead.
*
* @return array Settings array, each item being an associative array representing a setting.
*/
public function get_settings() {
return $this->get_settings_for_section( '' );
}
/**
* Get settings array.
*
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* The strategy for getting the settings is as follows:
*
* - If a method named 'get_settings_for_{section_id}_section' exists in the class
* it will be invoked (for the default '' section, the method name is 'get_settings_for_default_section').
* Derived classes can implement these methods as required.
*
* - Otherwise, 'get_settings_for_section_core' will be invoked. Derived classes can override it
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* as an alternative to implementing 'get_settings_for_{section_id}_section' methods.
*
* @param string $section_id The id of the section to return settings for, an empty string for the default section.
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
*
* @return array Settings array, each item being an associative array representing a setting.
*/
public function get_settings_for_section( $section_id ) {
if ( '' === $section_id ) {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
$method_name = 'get_settings_for_default_section';
} else {
$method_name = "get_settings_for_{$section_id}_section";
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
}
if ( method_exists( $this, $method_name ) ) {
$settings = $this->$method_name();
} else {
$settings = $this->get_settings_for_section_core( $section_id );
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
}
return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $section_id );
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
}
/**
* Get the settings for a given section.
* This method is invoked from 'get_settings_for_section' when no 'get_settings_for_{current_section}_section'
* method exists in the class.
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
*
* @param string $section_id The section name to get the settings for.
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
*
* @return array Settings array, each item being an associative array representing a setting.
*/
protected function get_settings_for_section_core( $section_id ) {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
return array();
}
2013-07-26 14:36:28 +00:00
/**
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* Get all sections for this page, both the own ones and the ones defined via filters.
*
* @return array
*/
public function get_sections() {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
$sections = $this->get_own_sections();
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
}
/**
* Get own sections for this page.
* Derived classes should override this method if they define sections.
* There should always be one default section with an empty string as identifier.
*
* Example:
* return array(
* '' => __( 'General', 'woocommerce' ),
* 'foobars' => __( 'Foos & Bars', 'woocommerce' ),
* );
*
* @return array An associative array where keys are section identifiers and the values are translated section names.
*/
protected function get_own_sections() {
return array( '' => __( 'General', 'woocommerce' ) );
}
2013-07-26 14:36:28 +00:00
/**
* Output sections.
*/
public function output_sections() {
global $current_section;
2013-07-26 14:36:28 +00:00
$sections = $this->get_sections();
2013-07-26 14:36:28 +00:00
if ( empty( $sections ) || 1 === count( $sections ) ) {
return;
}
2013-07-26 14:36:28 +00:00
echo '<ul class="subsubsub">';
2013-07-26 14:36:28 +00:00
$array_keys = array_keys( $sections );
2013-07-26 14:36:28 +00:00
foreach ( $sections as $id => $label ) {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
$url = admin_url( 'admin.php?page=wc-settings&tab=' . $this->id . '&section=' . sanitize_title( $id ) );
$class = ( $current_section === $id ? 'current' : '' );
$separator = ( end( $array_keys ) === $id ? '' : '|' );
$text = esc_html( $label );
echo "<li><a href='$url' class='$class'>$text</a> $separator </li>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '</ul><br class="clear" />';
}
2013-07-26 14:36:28 +00:00
/**
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* Output the HTML for the settings.
*/
public function output() {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
global $current_section;
$settings = $this->get_settings_for_section( $current_section );
2013-07-26 14:36:28 +00:00
WC_Admin_Settings::output_fields( $settings );
}
2013-07-26 14:36:28 +00:00
/**
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
* Save settings and trigger the 'woocommerce_update_options_'.id action.
*/
public function save() {
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
$this->save_settings_for_current_section();
$this->do_update_options_action();
}
/**
* Save settings for current section.
*/
protected function save_settings_for_current_section() {
global $current_section;
2013-07-26 14:36:28 +00:00
$settings = $this->get_settings_for_section( $current_section );
WC_Admin_Settings::save_fields( $settings );
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
}
/**
* Trigger the 'woocommerce_update_options_'.id action.
*
* @param string $section_id Section to trigger the action for, or null for current section.
*/
protected function do_update_options_action( $section_id = null ) {
global $current_section;
if ( is_null( $section_id ) ) {
$section_id = $current_section;
}
2013-07-26 14:36:28 +00:00
Refactor the settings pages, and add unit tests for them. This commit fixes some inconsistencies in the settings pages, and makes all the existing pages extensible by adding new sections (that was possible in some pages, but not in others). Main changes: 1. Modify the 'get_sections' method so that it invokes a new protected 'get_own_sections' method and then triggers the 'woocommerce_get_sections_' . id filter. This way the filter is triggered only in the base class and not in each of the derived classes too. 2. Change the get_settings() method so that it has its signature changed to get_settings( $current_section = '' ) in the base class and in all the derived class. Some derived classes were already using this signature, but others (those not having multiple sections natively) weren't, making then effectively impossible to define multiple sections for these pages via filters. With this change all the section pages act consistently and allow both adding new settings to the default "General" section and creating new sections via filters. 3. Change the implementation of 'get_settings' in the base class so that it searches for a 'get_settings_for_{section_id}_section' method in the class and executes it, otherwise it executes the new protected method get_settings_for_section( $current_section ); then it triggers the 'woocommerce_get_settings_' . id filter. This makes it easier to separate the code that returns the list of filters in multiple methods, one per section, instead of using one big if-else-else... block. So now instead of overriding get_settings($current_section='') derived classes need to implement get_settings_for_{$current_section}_section for each section, or override get_settings_for_section($current_section) or both. 'get_settings_for_section' returns an empty array by default. Also, 'woocommerce_get_settings_' . id is triggered in one single place too. Other improvements: * Remove duplicated code from 'output' in 'WC_Settings_Page' children. Some classes inherited from 'WC_Settings_Page' override the 'output' method with custom code, which in all cases ended up repeating the code of the original method as a fallback. These repetitions have been replaced with 'parent::output()'. * Fix inconsistencies for 'save' and 'output' in WC_Settings_Tax/Emails The 'WC_Settings_Tax' and 'WC_Settings_Emails' classes had some inconsistencies in their 'save' and 'output' methods that prevented the proper creation new sections and the addition of new settings via the 'woocommerce_get_sections_' and 'woocommerce_get_settings_' filters. Now they work as expected. * Deduplicate parts of 'save' in 'WC_Settings_Page' and children. Two methods have been added to 'WC_Settings_Page' class: 'save_settings_for_current_section' and 'do_update_options_action'. These are intended to be invoked by derived classes in their 'save' methods, in order to remove code repetition. * Add some helper methods to WC_Unit_Test_Case. Methods added: - assertOutputsHTML - assertEqualsHTML - normalize_html - capture_output_from
2020-09-16 10:27:05 +00:00
if ( $section_id ) {
do_action( 'woocommerce_update_options_' . $this->id . '_' . $section_id );
}
}
2013-07-26 14:36:28 +00:00
}
endif;