暫無描述

class-wc-settings-page.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * WooCommerce Settings Page/Tab
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 2.1.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit; // Exit if accessed directly.
  10. }
  11. if ( ! class_exists( 'WC_Settings_Page', false ) ) :
  12. /**
  13. * WC_Settings_Page.
  14. */
  15. abstract class WC_Settings_Page {
  16. /**
  17. * Setting page id.
  18. *
  19. * @var string
  20. */
  21. protected $id = '';
  22. /**
  23. * Setting page label.
  24. *
  25. * @var string
  26. */
  27. protected $label = '';
  28. /**
  29. * Constructor.
  30. */
  31. public function __construct() {
  32. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
  33. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
  34. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
  35. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
  36. }
  37. /**
  38. * Get settings page ID.
  39. *
  40. * @since 3.0.0
  41. * @return string
  42. */
  43. public function get_id() {
  44. return $this->id;
  45. }
  46. /**
  47. * Get settings page label.
  48. *
  49. * @since 3.0.0
  50. * @return string
  51. */
  52. public function get_label() {
  53. return $this->label;
  54. }
  55. /**
  56. * Add this page to settings.
  57. *
  58. * @param array $pages The setings array where we'll add ourselves.
  59. *
  60. * @return mixed
  61. */
  62. public function add_settings_page( $pages ) {
  63. $pages[ $this->id ] = $this->label;
  64. return $pages;
  65. }
  66. /**
  67. * Get settings array for the default section.
  68. *
  69. * External settings classes (registered via 'woocommerce_get_settings_pages' filter)
  70. * might have redefined this method as "get_settings($section_id='')", thus we need
  71. * to use this method internally instead of 'get_settings_for_section' to register settings
  72. * and render settings pages.
  73. *
  74. * *But* we can't just redefine the method as "get_settings($section_id='')" here, since this
  75. * will break on PHP 8 if any external setting class have it as 'get_settings()'.
  76. *
  77. * Thus we leave the method signature as is and use 'func_get_arg' to get the setting id
  78. * if it's supplied, and we use this method internally; but it's deprecated and should
  79. * otherwise never be used.
  80. *
  81. * @deprecated 5.4.0 Use 'get_settings_for_section' (passing an empty string for default section)
  82. *
  83. * @return array Settings array, each item being an associative array representing a setting.
  84. */
  85. public function get_settings() {
  86. $section_id = 0 === func_num_args() ? '' : func_get_arg( 0 );
  87. return $this->get_settings_for_section( $section_id );
  88. }
  89. /**
  90. * Get settings array.
  91. *
  92. * The strategy for getting the settings is as follows:
  93. *
  94. * - If a method named 'get_settings_for_{section_id}_section' exists in the class
  95. * it will be invoked (for the default '' section, the method name is 'get_settings_for_default_section').
  96. * Derived classes can implement these methods as required.
  97. *
  98. * - Otherwise, 'get_settings_for_section_core' will be invoked. Derived classes can override it
  99. * as an alternative to implementing 'get_settings_for_{section_id}_section' methods.
  100. *
  101. * @param string $section_id The id of the section to return settings for, an empty string for the default section.
  102. *
  103. * @return array Settings array, each item being an associative array representing a setting.
  104. */
  105. final public function get_settings_for_section( $section_id ) {
  106. if ( '' === $section_id ) {
  107. $method_name = 'get_settings_for_default_section';
  108. } else {
  109. $method_name = "get_settings_for_{$section_id}_section";
  110. }
  111. if ( method_exists( $this, $method_name ) ) {
  112. $settings = $this->$method_name();
  113. } else {
  114. $settings = $this->get_settings_for_section_core( $section_id );
  115. }
  116. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $section_id );
  117. }
  118. /**
  119. * Get the settings for a given section.
  120. * This method is invoked from 'get_settings_for_section' when no 'get_settings_for_{current_section}_section'
  121. * method exists in the class.
  122. *
  123. * When overriding, note that the 'woocommerce_get_settings_' filter must NOT be triggered,
  124. * as this is already done by 'get_settings_for_section'.
  125. *
  126. * @param string $section_id The section name to get the settings for.
  127. *
  128. * @return array Settings array, each item being an associative array representing a setting.
  129. */
  130. protected function get_settings_for_section_core( $section_id ) {
  131. return array();
  132. }
  133. /**
  134. * Get all sections for this page, both the own ones and the ones defined via filters.
  135. *
  136. * @return array
  137. */
  138. public function get_sections() {
  139. $sections = $this->get_own_sections();
  140. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
  141. }
  142. /**
  143. * Get own sections for this page.
  144. * Derived classes should override this method if they define sections.
  145. * There should always be one default section with an empty string as identifier.
  146. *
  147. * Example:
  148. * return array(
  149. * '' => __( 'General', 'woocommerce' ),
  150. * 'foobars' => __( 'Foos & Bars', 'woocommerce' ),
  151. * );
  152. *
  153. * @return array An associative array where keys are section identifiers and the values are translated section names.
  154. */
  155. protected function get_own_sections() {
  156. return array( '' => __( 'General', 'woocommerce' ) );
  157. }
  158. /**
  159. * Output sections.
  160. */
  161. public function output_sections() {
  162. global $current_section;
  163. $sections = $this->get_sections();
  164. if ( empty( $sections ) || 1 === count( $sections ) ) {
  165. return;
  166. }
  167. echo '<ul class="subsubsub">';
  168. $array_keys = array_keys( $sections );
  169. foreach ( $sections as $id => $label ) {
  170. $url = admin_url( 'admin.php?page=wc-settings&tab=' . $this->id . '&section=' . sanitize_title( $id ) );
  171. $class = ( $current_section === $id ? 'current' : '' );
  172. $separator = ( end( $array_keys ) === $id ? '' : '|' );
  173. $text = esc_html( $label );
  174. echo "<li><a href='$url' class='$class'>$text</a> $separator </li>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  175. }
  176. echo '</ul><br class="clear" />';
  177. }
  178. /**
  179. * Output the HTML for the settings.
  180. */
  181. public function output() {
  182. global $current_section;
  183. // We can't use "get_settings_for_section" here
  184. // for compatibility with derived classes overriding "get_settings".
  185. $settings = $this->get_settings( $current_section );
  186. WC_Admin_Settings::output_fields( $settings );
  187. }
  188. /**
  189. * Save settings and trigger the 'woocommerce_update_options_'.id action.
  190. */
  191. public function save() {
  192. $this->save_settings_for_current_section();
  193. $this->do_update_options_action();
  194. }
  195. /**
  196. * Save settings for current section.
  197. */
  198. protected function save_settings_for_current_section() {
  199. global $current_section;
  200. // We can't use "get_settings_for_section" here
  201. // for compatibility with derived classes overriding "get_settings".
  202. $settings = $this->get_settings( $current_section );
  203. WC_Admin_Settings::save_fields( $settings );
  204. }
  205. /**
  206. * Trigger the 'woocommerce_update_options_'.id action.
  207. *
  208. * @param string $section_id Section to trigger the action for, or null for current section.
  209. */
  210. protected function do_update_options_action( $section_id = null ) {
  211. global $current_section;
  212. if ( is_null( $section_id ) ) {
  213. $section_id = $current_section;
  214. }
  215. if ( $section_id ) {
  216. do_action( 'woocommerce_update_options_' . $this->id . '_' . $section_id );
  217. }
  218. }
  219. }
  220. endif;