class-settings-manager.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Settings Manager Class - Handles plugin settings
  4. *
  5. * @package StudiouWCCustomReports
  6. */
  7. if (!defined('ABSPATH')) {
  8. exit;
  9. }
  10. class StudiouWC_Settings_Manager {
  11. private const OPTION_NAME = 'studiou_wc_custom_reports_settings';
  12. public function activate() {
  13. $default_settings = array(
  14. 'default_interval' => 'last_month',
  15. 'default_property' => '',
  16. 'default_statuses' => array('wc-completed', 'wc-processing', 'wc-on-hold')
  17. );
  18. add_option(self::OPTION_NAME, $default_settings);
  19. }
  20. public function get_settings() {
  21. return get_option(self::OPTION_NAME, array());
  22. }
  23. public function update_settings($settings) {
  24. return update_option(self::OPTION_NAME, $settings);
  25. }
  26. public function get_default_date_from() {
  27. $settings = $this->get_settings();
  28. $interval = $settings['default_interval'] ?? 'last_month';
  29. switch ($interval) {
  30. case 'last_day':
  31. return date('Y-m-d', strtotime('-1 day'));
  32. case 'last_week':
  33. return date('Y-m-d', strtotime('-1 week'));
  34. case 'last_month':
  35. default:
  36. return date('Y-m-d', strtotime('-1 month'));
  37. }
  38. }
  39. public function save_settings() {
  40. if (!current_user_can('manage_woocommerce')) {
  41. wp_send_json_error(__('You do not have sufficient permissions.', StudiouWC_Main::get_text_domain()));
  42. }
  43. $settings = array(
  44. 'default_interval' => isset($_POST['default_interval']) ? sanitize_text_field($_POST['default_interval']) : 'last_month',
  45. 'default_property' => isset($_POST['default_property']) ? sanitize_text_field($_POST['default_property']) : '',
  46. 'default_statuses' => isset($_POST['default_statuses']) ? array_map('sanitize_text_field', $_POST['default_statuses']) : array()
  47. );
  48. $this->update_settings($settings);
  49. wp_send_json_success(__('Settings saved successfully!', StudiouWC_Main::get_text_domain()));
  50. }
  51. }