| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- /**
- * Settings Manager Class - Handles plugin settings
- *
- * @package StudiouWCCustomReports
- */
- if (!defined('ABSPATH')) {
- exit;
- }
- class StudiouWC_Settings_Manager {
-
- private const OPTION_NAME = 'studiou_wc_custom_reports_settings';
-
- public function activate() {
- $default_settings = array(
- 'default_interval' => 'last_month',
- 'default_property' => '',
- 'default_statuses' => array('wc-completed', 'wc-processing', 'wc-on-hold')
- );
- add_option(self::OPTION_NAME, $default_settings);
- }
-
- public function get_settings() {
- return get_option(self::OPTION_NAME, array());
- }
-
- public function update_settings($settings) {
- return update_option(self::OPTION_NAME, $settings);
- }
-
- public function get_default_date_from() {
- $settings = $this->get_settings();
- $interval = $settings['default_interval'] ?? 'last_month';
-
- switch ($interval) {
- case 'last_day':
- return date('Y-m-d', strtotime('-1 day'));
- case 'last_week':
- return date('Y-m-d', strtotime('-1 week'));
- case 'last_month':
- default:
- return date('Y-m-d', strtotime('-1 month'));
- }
- }
-
- public function save_settings() {
- if (!current_user_can('manage_woocommerce')) {
- wp_send_json_error(__('You do not have sufficient permissions.', StudiouWC_Main::get_text_domain()));
- }
-
- $settings = array(
- 'default_interval' => isset($_POST['default_interval']) ? sanitize_text_field($_POST['default_interval']) : 'last_month',
- 'default_property' => isset($_POST['default_property']) ? sanitize_text_field($_POST['default_property']) : '',
- 'default_statuses' => isset($_POST['default_statuses']) ? array_map('sanitize_text_field', $_POST['default_statuses']) : array()
- );
-
- $this->update_settings($settings);
-
- wp_send_json_success(__('Settings saved successfully!', StudiouWC_Main::get_text_domain()));
- }
- }
|