class-settings.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * Reference : http://codex.wordpress.org/Settings_API
  4. * Reference : http://codex.wordpress.org/Function_Reference/add_settings_field
  5. */
  6. add_filter('booked_admin_booked_screens', array('Booked_WC_Settings_Hooks', 'booked_admin_booked_screens'), 10, 1);
  7. class Booked_WC_Settings {
  8. private function __construct() {
  9. }
  10. protected function _setup() {
  11. add_action('admin_menu', array($this, 'add_admin_menu_page') );
  12. add_action('admin_init', array('Booked_WC_Settings_Fields', 'register') );
  13. }
  14. public static function setup() {
  15. $settings = new self();
  16. $settings->_setup();
  17. }
  18. public function add_admin_menu_page () {
  19. add_submenu_page(
  20. BOOKED_WC_APPOINTMENTS_PAGE,
  21. __('WooCommerce', 'booked-woocommerce-payments'),
  22. __('WooCommerce', 'booked-woocommerce-payments'),
  23. 'manage_booked_options',
  24. BOOKED_WC_PLUGIN_PREFIX . 'payment_options',
  25. array($this, 'render_payment_options_page')
  26. );
  27. }
  28. public function render_payment_options_page() {
  29. Booked_WC_Fragments::load('settings', 'payment-options');
  30. }
  31. public static function get_option($option_name) {
  32. $options = get_option(BOOKED_WC_PLUGIN_PREFIX . 'payment_options');
  33. if ( isset($options[$option_name]) ) {
  34. return $options[$option_name];
  35. }
  36. }
  37. }
  38. class Booked_WC_Settings_Hooks {
  39. public static function booked_admin_booked_screens( $screens ) {
  40. $screens[] = BOOKED_WC_PLUGIN_PREFIX . 'payment_options';
  41. return $screens;
  42. }
  43. }
  44. class Booked_WC_Settings_Fields {
  45. protected $options_name;
  46. private function __construct() {
  47. $this->options_name = BOOKED_WC_PLUGIN_PREFIX . 'payment_options';
  48. $this->register_fields();
  49. }
  50. public static function register() {
  51. return new self();
  52. }
  53. protected function register_fields() {
  54. // Sets the defaults if settings aren't saved yet.
  55. $settings = get_option($this->options_name);
  56. if ( !$settings ) {
  57. $default_settings['email_confirmations'] = 'after_complete';
  58. $default_settings['enable_thumbnails'] = 'enable';
  59. $default_settings['enable_auto_cleanup'] = 'disable';
  60. $default_settings['cleanup_mode'] = 'twicedaily';
  61. update_option($this->options_name, $default_settings);
  62. }
  63. add_settings_section(
  64. $this->options_name,
  65. '',
  66. array($this, 'field_section'),
  67. $this->options_name
  68. );
  69. add_settings_field(
  70. 'email_confirmations',
  71. __('Email Confirmations', 'booked-woocommerce-payments'),
  72. array($this, 'email_confirmations'),
  73. $this->options_name,
  74. $this->options_name
  75. );
  76. add_settings_field(
  77. 'enable_thumbnails',
  78. __('Enable Thumbnails', 'booked-woocommerce-payments'),
  79. array($this, 'enable_thumbnails'),
  80. $this->options_name,
  81. $this->options_name
  82. );
  83. add_settings_field(
  84. 'enable_auto_cleanup',
  85. __('Enable Auto-Cleanup', 'booked-woocommerce-payments'),
  86. array($this, 'enable_auto_cleanup'),
  87. $this->options_name,
  88. $this->options_name
  89. );
  90. add_settings_field(
  91. 'cleanup_mode',
  92. __('Cleanup Schedule', 'booked-woocommerce-payments'),
  93. array($this, 'field_modes'),
  94. $this->options_name,
  95. $this->options_name
  96. );
  97. add_settings_field(
  98. 'redirect_page',
  99. __('Redirect After Booking', 'booked-woocommerce-payments'),
  100. array($this, 'redirect_page'),
  101. $this->options_name,
  102. $this->options_name
  103. );
  104. // Register our setting so that $_POST handling is done for us and
  105. // our callback function just has to echo the <input>
  106. register_setting($this->options_name, $this->options_name);
  107. }
  108. public function field_section() {
  109. //
  110. }
  111. public function email_confirmations() {
  112. $options = get_option($this->options_name);
  113. $field_value = isset($options['email_confirmations']) ? $options['email_confirmations'] : 'after_complete';
  114. $radio_options = array(
  115. 'after_complete' => __('Send Booked confirmation emails ONLY after payment is marked as "complete".', 'booked-woocommerce-payments'),
  116. 'before_complete' => __('Send Booked confirmation emails right away.', 'booked-woocommerce-payments')
  117. );
  118. ?>
  119. <p><?php echo __('When do you want Booked to send out its confirmation emails?', 'booked-woocommerce-payments') ?></p>
  120. <?php foreach ($radio_options as $option_value => $option_label): ?>
  121. <div>
  122. <input name="<?php echo $this->options_name ?>[email_confirmations]" type="radio" value="<?php echo $option_value ?>" <?php echo checked($field_value, $option_value) ?> />
  123. <p for="<?php echo $this->options_name ?>" style="display:inline-block; margin:0; padding:0; position:relative; top:-1px;"><?php echo $option_label ?></p>
  124. </div>
  125. <?php endforeach ?>
  126. <?php
  127. }
  128. public function enable_thumbnails() {
  129. $options = get_option($this->options_name);
  130. $field_value = isset($options['enable_thumbnails']) ? $options['enable_thumbnails'] : 'enable';
  131. $radio_options = array(
  132. 'enable' => __('Enable', 'booked-woocommerce-payments'),
  133. 'disable' => __('Disable', 'booked-woocommerce-payments')
  134. );
  135. ?>
  136. <p><?php echo __('Enable this to show product thumbnails for Booked items (on the cart/checkout screens).', 'booked-woocommerce-payments') ?></p>
  137. <?php foreach ($radio_options as $option_value => $option_label): ?>
  138. <div>
  139. <input name="<?php echo $this->options_name ?>[enable_thumbnails]" type="radio" value="<?php echo $option_value ?>" <?php echo checked($field_value, $option_value) ?> />
  140. <p for="<?php echo $this->options_name ?>" style="display:inline-block; margin:0; padding:0; position:relative; top:-1px;"><?php echo $option_label ?></p>
  141. </div>
  142. <?php endforeach ?>
  143. <?php
  144. }
  145. public function enable_auto_cleanup() {
  146. $options = get_option($this->options_name);
  147. $field_value = isset($options['enable_auto_cleanup']) ? $options['enable_auto_cleanup'] : 'enable';
  148. $radio_options = array(
  149. 'enable' => __('Enable', 'booked-woocommerce-payments'),
  150. 'disable' => __('Disable', 'booked-woocommerce-payments')
  151. );
  152. ?>
  153. <p><?php echo __('Automatically cleans up all non-paid appointments at a specific time interval. For example, when someone chooses a day and product but does not pay for the appointment, then it will continue to be "booked" unless removed manually. This removes that step with an automated option.', 'booked-woocommerce-payments') ?></p>
  154. <?php foreach ($radio_options as $option_value => $option_label): ?>
  155. <div>
  156. <input name="<?php echo $this->options_name ?>[enable_auto_cleanup]" type="radio" value="<?php echo $option_value ?>" <?php echo checked($field_value, $option_value) ?> />
  157. <p for="<?php echo $this->options_name ?>" style="display:inline-block; margin:0; padding:0; position:relative; top:-1px;"><?php echo $option_label ?></p>
  158. </div>
  159. <?php endforeach ?>
  160. <?php
  161. }
  162. public function redirect_page() {
  163. $options = get_option($this->options_name);
  164. $field_value = isset($options['redirect_page']) ? $options['redirect_page'] : 'checkout';
  165. $radio_options = array(
  166. 'checkout' => __('Checkout Page (default)', 'booked-woocommerce-payments'),
  167. 'cart' => __('Cart Page', 'booked-woocommerce-payments')
  168. );
  169. ?>
  170. <p><?php echo __('You can choose to have Booked redirect to the Checkout page (default) or the Cart page instead.', 'booked-woocommerce-payments') ?></p>
  171. <?php foreach ($radio_options as $option_value => $option_label): ?>
  172. <div>
  173. <input name="<?php echo $this->options_name ?>[redirect_page]" type="radio" value="<?php echo $option_value ?>" <?php echo ($option_value == 'checkout' && !$field_value ? 'checked="checked"' : checked($field_value, $option_value)); ?> />
  174. <p for="<?php echo $this->options_name ?>" style="display:inline-block; margin:0; padding:0; position:relative; top:-1px;"><?php echo $option_label ?></p>
  175. </div>
  176. <?php endforeach ?>
  177. <?php
  178. }
  179. public function field_modes() {
  180. $options = get_option($this->options_name);
  181. $field_value = isset($options['cleanup_mode']) ? $options['cleanup_mode'] : false;
  182. $available_schedules = wp_get_schedules();
  183. $schedules = array();
  184. foreach ($available_schedules as $name => $data) {
  185. $interval = (int)$data['interval'];
  186. if ( !isset($schedules[$interval]) ){
  187. $schedules[$interval] = array_merge(
  188. array('name' => $name),
  189. $data
  190. );
  191. }
  192. }
  193. usort($schedules, array($this, 'booked_schedule_sort'));
  194. ?>
  195. <p><?php echo __('More schedule intervals can be added with a plugin like <a href="https://wordpress.org/plugins/wp-crontrol/" target="_blank">WP Crontrol</a>.', 'booked-woocommerce-payments') ?> <?php echo __('The currently available schedules are listed below.', 'booked-woocommerce-payments') ?></p>
  196. <?php foreach ($schedules as $index => $schedule_data): ?>
  197. <div>
  198. <input id="<?php echo $schedule_data['name'] ?>" name="<?php echo $this->options_name ?>[cleanup_mode]" type="radio" value="<?php echo $schedule_data['name'] ?>" <?php echo checked($field_value, $schedule_data['name']) ?> />
  199. <p for="<?php echo $schedule_data['name'] ?>" style="display:inline-block; margin:0; padding:0; position:relative; top:-1px;"><?php echo $schedule_data['display'] ?></p>
  200. </div>
  201. <?php endforeach ?>
  202. <?php
  203. }
  204. public function booked_schedule_sort($a, $b) {
  205. return $a['interval'] - $b['interval'];
  206. }
  207. }