| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?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' => 'format', // Set format as default since it exists
- 'default_statuses' => array('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-to-print')
- );
- add_option(self::OPTION_NAME, $default_settings);
- }
-
- public function get_settings() {
- $settings = get_option(self::OPTION_NAME, array());
-
- // Ensure we have default values
- $defaults = array(
- 'default_interval' => 'last_month',
- 'default_property' => 'format',
- 'default_statuses' => array('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-to-print')
- );
-
- return array_merge($defaults, $settings);
- }
-
- 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() {
- // Verify nonce for security
- if (!isset($_POST['studiou_settings_nonce']) || !wp_verify_nonce($_POST['studiou_settings_nonce'], 'studiou_save_settings')) {
- wp_send_json_error(__('Security check failed.', StudiouWC_Main::get_text_domain()));
- }
-
- 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()
- );
-
- // Validate interval
- $valid_intervals = array('last_day', 'last_week', 'last_month');
- if (!in_array($settings['default_interval'], $valid_intervals)) {
- $settings['default_interval'] = 'last_month';
- }
-
- if ($this->update_settings($settings)) {
- wp_send_json_success(__('Settings saved successfully!', StudiouWC_Main::get_text_domain()));
- } else {
- wp_send_json_error(__('Error saving settings.', StudiouWC_Main::get_text_domain()));
- }
- }
- }
|