class-wc-settings-page.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * WooCommerce Settings Page/Tab
  4. *
  5. * @author WooThemes
  6. * @category Admin
  7. * @package WooCommerce/Admin
  8. * @version 2.1.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13. if ( ! class_exists( 'WC_Settings_Page', false ) ) :
  14. /**
  15. * WC_Settings_Page.
  16. */
  17. abstract class WC_Settings_Page {
  18. /**
  19. * Setting page id.
  20. *
  21. * @var string
  22. */
  23. protected $id = '';
  24. /**
  25. * Setting page label.
  26. *
  27. * @var string
  28. */
  29. protected $label = '';
  30. /**
  31. * Constructor.
  32. */
  33. public function __construct() {
  34. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
  35. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
  36. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
  37. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
  38. }
  39. /**
  40. * Get settings page ID.
  41. *
  42. * @since 3.0.0
  43. * @return string
  44. */
  45. public function get_id() {
  46. return $this->id;
  47. }
  48. /**
  49. * Get settings page label.
  50. *
  51. * @since 3.0.0
  52. * @return string
  53. */
  54. public function get_label() {
  55. return $this->label;
  56. }
  57. /**
  58. * Add this page to settings.
  59. *
  60. * @param array $pages
  61. *
  62. * @return mixed
  63. */
  64. public function add_settings_page( $pages ) {
  65. $pages[ $this->id ] = $this->label;
  66. return $pages;
  67. }
  68. /**
  69. * Get settings array.
  70. *
  71. * @return array
  72. */
  73. public function get_settings() {
  74. return apply_filters( 'woocommerce_get_settings_' . $this->id, array() );
  75. }
  76. /**
  77. * Get sections.
  78. *
  79. * @return array
  80. */
  81. public function get_sections() {
  82. return apply_filters( 'woocommerce_get_sections_' . $this->id, array() );
  83. }
  84. /**
  85. * Output sections.
  86. */
  87. public function output_sections() {
  88. global $current_section;
  89. $sections = $this->get_sections();
  90. if ( empty( $sections ) || 1 === sizeof( $sections ) ) {
  91. return;
  92. }
  93. echo '<ul class="subsubsub">';
  94. $array_keys = array_keys( $sections );
  95. foreach ( $sections as $id => $label ) {
  96. echo '<li><a href="' . admin_url( 'admin.php?page=wc-settings&tab=' . $this->id . '&section=' . sanitize_title( $id ) ) . '" class="' . ( $current_section == $id ? 'current' : '' ) . '">' . $label . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
  97. }
  98. echo '</ul><br class="clear" />';
  99. }
  100. /**
  101. * Output the settings.
  102. */
  103. public function output() {
  104. $settings = $this->get_settings();
  105. WC_Admin_Settings::output_fields( $settings );
  106. }
  107. /**
  108. * Save settings.
  109. */
  110. public function save() {
  111. global $current_section;
  112. $settings = $this->get_settings();
  113. WC_Admin_Settings::save_fields( $settings );
  114. if ( $current_section ) {
  115. do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
  116. }
  117. }
  118. }
  119. endif;