array( $this, 'sanitize_settings' ), 'default' => array( 'ai_provider' => 'openai', 'openai_api_key' => '', 'deepseek_api_key' => '', 'prediction_cache_hours' => '24', 'enable_daily_email' => 'yes', 'email_recipients' => get_option( 'admin_email' ), ), ) ); register_setting( 'aistpr_prediction_settings', 'aistpr_prediction_settings', array( 'sanitize_callback' => array( $this, 'sanitize_prediction_settings' ), 'default' => array( 'model' => 'linear', 'confidence_threshold' => 80, 'historical_data' => '90', ), ) ); register_setting( 'aistpr_general_settings', 'aistpr_general_settings', array( 'sanitize_callback' => array( $this, 'sanitize_general_settings' ), 'default' => array( 'enable' => 0, 'update_frequency' => 'daily', 'threshold' => 5, ), ) ); register_setting( 'aistpr_notification_settings', 'aistpr_notification_settings', array( 'sanitize_callback' => array( $this, 'sanitize_notification_settings' ), 'default' => array( 'enable' => 0, 'email' => get_option( 'admin_email' ), 'types' => array( 'low_stock', 'out_of_stock' ), 'frequency' => 'daily', ), ) ); } /** * Handle API settings form submission. * * @since 1.0.0 * @return void */ public function handle_api_settings_save() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aistpr_api_settings' ) ) { wp_die( esc_html__( 'Invalid nonce. Please try again.', 'ai-stock-predictor' ) ); } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'ai-stock-predictor' ) ); } $input = isset( $_POST['aistpr_api_settings'] ) ? wp_unslash( $_POST['aistpr_api_settings'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->sanitize_settings( $input ); update_option( 'aistpr_api_settings', $settings ); wp_safe_redirect( add_query_arg( array( 'page' => 'ai-stock-predictor-settings', 'tab' => 'api', 'updated' => 'true', ), admin_url( 'admin.php' ) ) ); exit; } /** * Handle notification settings form submission. * * @since 1.0.0 * @return void */ public function handle_notification_settings_save() { // Verify nonce. if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aistpr_notification_settings' ) ) { wp_die( esc_html__( 'Invalid nonce. Please try again.', 'ai-stock-predictor' ) ); } // Check permissions. if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'ai-stock-predictor' ) ); } // Sanitize and save settings. $input = isset( $_POST['aistpr_notification_settings'] ) ? wp_unslash( $_POST['aistpr_notification_settings'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->sanitize_notification_settings( $input ); update_option( 'aistpr_notification_settings', $settings ); // Redirect back to settings page with success message. wp_safe_redirect( add_query_arg( array( 'page' => 'ai-stock-predictor-settings', 'tab' => 'notification', 'updated' => 'true', ), admin_url( 'admin.php' ) ) ); exit; } /** * Handle prediction settings form submission. * * @since 1.0.0 * @return void */ public function handle_prediction_settings_save() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aistpr_prediction_settings' ) ) { wp_die( esc_html__( 'Invalid nonce. Please try again.', 'ai-stock-predictor' ) ); } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'ai-stock-predictor' ) ); } $input = isset( $_POST['aistpr_prediction_settings'] ) ? wp_unslash( $_POST['aistpr_prediction_settings'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->sanitize_prediction_settings( $input ); update_option( 'aistpr_prediction_settings', $settings ); wp_safe_redirect( add_query_arg( array( 'page' => 'ai-stock-predictor-settings', 'tab' => 'prediction', 'updated' => 'true', ), admin_url( 'admin.php' ) ) ); exit; } /** * Handle general settings form submission. * * @since 1.0.0 * @return void */ public function handle_general_settings_save() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aistpr_general_settings' ) ) { wp_die( esc_html__( 'Invalid nonce. Please try again.', 'ai-stock-predictor' ) ); } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'ai-stock-predictor' ) ); } $input = isset( $_POST['aistpr_general_settings'] ) ? wp_unslash( $_POST['aistpr_general_settings'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $settings = $this->sanitize_general_settings( $input ); update_option( 'aistpr_general_settings', $settings ); wp_safe_redirect( add_query_arg( array( 'page' => 'ai-stock-predictor-settings', 'tab' => 'general', 'updated' => 'true', ), admin_url( 'admin.php' ) ) ); exit; } /** * Display admin notices for settings updates. * * @since 1.0.0 * @return void */ public function display_settings_notices() { $screen = get_current_screen(); if ( 'woocommerce_page_ai-stock-predictor-settings' !== $screen->id ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking GET parameter for display purposes, no action taken if ( isset( $_GET['updated'] ) && 'true' === sanitize_text_field( wp_unslash( $_GET['updated'] ) ) ) { ?>