cron = new Aardwolf_Cron(); $this->export = new Aardwolf_Export(); if ( is_admin() ) { $this->admin = new Aardwolf_Admin(); add_action( 'admin_init', array( $this, 'register_settings' ) ); // Re-sync the WP-Cron schedule whenever the settings are saved. add_action( 'update_option_' . self::SETTINGS_OPTION, array( $this->cron, 'sync_schedule' ), 10, 0 ); } } /** * Return the settings array with defaults filled in. * * @return array */ public static function get_settings() { $defaults = array( // Scheduled scanning + email alerts. 'schedule_frequency' => '', // '', 'daily' or 'weekly'. 'schedule_time' => '03:00', // HH:MM in the site timezone. 'schedule_day' => 'monday', // Day of week for weekly scans. 'notify_email' => get_option( 'admin_email' ), 'notify_when' => 'worse', // 'worse' or 'always'. ); $saved = get_option( self::SETTINGS_OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( $defaults, $saved ); } /** * Allowed scheduling frequencies mapped to WP-Cron recurrence names. * * @return array */ public static function schedule_options() { return array( '' => __( 'Disabled', 'aardwolf-security-scanner' ), 'daily' => __( 'Daily', 'aardwolf-security-scanner' ), 'weekly' => __( 'Weekly', 'aardwolf-security-scanner' ), ); } /** * Days of the week for weekly scheduling, keyed by the value stored in * settings and mapped to the ISO-8601 day number (1 = Monday … 7 = Sunday) * used when computing the next run. * * @return array */ public static function schedule_days() { return array( 'monday' => array( 'label' => __( 'Monday', 'aardwolf-security-scanner' ), 'iso' => 1 ), 'tuesday' => array( 'label' => __( 'Tuesday', 'aardwolf-security-scanner' ), 'iso' => 2 ), 'wednesday' => array( 'label' => __( 'Wednesday', 'aardwolf-security-scanner' ), 'iso' => 3 ), 'thursday' => array( 'label' => __( 'Thursday', 'aardwolf-security-scanner' ), 'iso' => 4 ), 'friday' => array( 'label' => __( 'Friday', 'aardwolf-security-scanner' ), 'iso' => 5 ), 'saturday' => array( 'label' => __( 'Saturday', 'aardwolf-security-scanner' ), 'iso' => 6 ), 'sunday' => array( 'label' => __( 'Sunday', 'aardwolf-security-scanner' ), 'iso' => 7 ), ); } /** * Register the settings section and fields. */ public function register_settings() { register_setting( 'aardwolf_sc_settings_group', self::SETTINGS_OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => array(), ) ); } /** * Sanitize submitted settings. * * @param mixed $input Raw submitted value. * @return array */ public function sanitize_settings( $input ) { $clean = array(); // Scheduling frequency — only accept known recurrences. $frequency = isset( $input['schedule_frequency'] ) ? (string) $input['schedule_frequency'] : ''; $clean['schedule_frequency'] = array_key_exists( $frequency, self::schedule_options() ) ? $frequency : ''; // Time of day (HH:MM, 24-hour) in the site timezone. $time = isset( $input['schedule_time'] ) ? trim( (string) $input['schedule_time'] ) : ''; $clean['schedule_time'] = preg_match( '/^([01][0-9]|2[0-3]):[0-5][0-9]$/', $time ) ? $time : '03:00'; // Day of week for weekly scans. $day = isset( $input['schedule_day'] ) ? (string) $input['schedule_day'] : ''; $clean['schedule_day'] = array_key_exists( $day, self::schedule_days() ) ? $day : 'monday'; // Notification email — fall back to the admin address if invalid. $email = isset( $input['notify_email'] ) ? sanitize_email( $input['notify_email'] ) : ''; $clean['notify_email'] = is_email( $email ) ? $email : get_option( 'admin_email' ); // When to notify. $when = isset( $input['notify_when'] ) ? (string) $input['notify_when'] : 'worse'; $clean['notify_when'] = in_array( $when, array( 'worse', 'always' ), true ) ? $when : 'worse'; return $clean; } }