function adrotate_options_submit()
{
    if (wp_verify_nonce($_POST['adrotate_nonce_settings'], 'adrotate_settings')) {
        $settings_tab = esc_attr($_POST['adrotate_settings_tab']);
        if ($settings_tab == 'general') {
            $config = get_option('adrotate_config');
            $config['textwidget_shortcodes'] = isset($_POST['adrotate_textwidget_shortcodes']) ? 'Y' : 'N';
            $config['live_preview'] = isset($_POST['adrotate_live_preview']) ? 'Y' : 'N';
            $config['mobile_dynamic_mode'] = isset($_POST['adrotate_mobile_dynamic_mode']) ? 'Y' : 'N';
            $config['jquery'] = isset($_POST['adrotate_jquery']) ? 'Y' : 'N';
            $config['jsfooter'] = isset($_POST['adrotate_jsfooter']) ? 'Y' : 'N';
            $adblock_disguise = strtolower(trim($_POST['adrotate_adblock_disguise']));
            $config['adblock_disguise'] = strlen($adblock_disguise) > 0 ? preg_replace('/[^a-z]/', '', strtolower(substr($adblock_disguise, 0, 6))) : "";
            $banner_folder = strtolower(trim($_POST['adrotate_banner_folder']));
            $config['banner_folder'] = strlen($banner_folder) > 0 ? preg_replace('/[^a-zA-Z0-9\\/\\-_]/', '', $banner_folder) : "wp-content/banners/";
            $config['adblock'] = isset($_POST['adrotate_adblock']) ? 'Y' : 'N';
            $config['adblock_loggedin'] = isset($_POST['adrotate_adblock_loggedin']) ? 'Y' : 'N';
            $adblock_timer = trim($_POST['adrotate_adblock_timer']);
            $config['adblock_timer'] = (strlen($adblock_timer) > 0 and (is_numeric($adblock_timer) and $adblock_timer >= 1 and $adblock_timer <= 20)) ? $adblock_timer : 5;
            $adblock_message = trim($_POST['adrotate_adblock_message']);
            $config['adblock_message'] = strlen($adblock_message) > 0 ? strip_tags(htmlspecialchars(trim($adblock_message, "\t\n "), ENT_QUOTES)) : "Ad blocker detected! Please wait %time% seconds or disable your ad blocker!";
            update_option('adrotate_config', $config);
            // Sort out crawlers
            $crawlers = explode(',', trim($_POST['adrotate_crawlers']));
            $new_crawlers = array();
            foreach ($crawlers as $crawler) {
                $crawler = preg_replace('/[^a-zA-Z0-9\\[\\]\\-_:; ]/i', '', trim($crawler));
                if (strlen($crawler) > 0) {
                    $new_crawlers[] = $crawler;
                }
            }
            update_option('adrotate_crawlers', $new_crawlers);
        }
        if ($settings_tab == 'notifications') {
            $notifications = get_option('adrotate_notifications');
            // Notifications
            $notifications['notification_email'] = isset($_POST['adrotate_notification_email']) ? 'Y' : 'N';
            $notifications['notification_push'] = isset($_POST['adrotate_notification_push']) ? 'Y' : 'N';
            $notifications['notification_dashboard'] = isset($_POST['adrotate_notification_dashboard']) ? 'N' : 'Y';
            // Filter and validate notification addresses, if not set, turn option off.
            $notification_emails = $_POST['adrotate_notification_email_publisher'];
            if (strlen($notification_emails) > 0) {
                $notification_emails = explode(',', trim($notification_emails));
                foreach ($notification_emails as $notification_email) {
                    $notification_email = trim($notification_email);
                    if (strlen($notification_email) > 0) {
                        if (is_email($notification_email)) {
                            $clean_notification_email[] = $notification_email;
                        }
                    }
                }
                $notifications['notification_email_publisher'] = array_unique(array_slice($clean_notification_email, 0, 5));
            } else {
                $notifications['notification_email_publisher'] = array();
            }
            // Filter and validate advertiser addresses
            $advertiser_emails = $_POST['adrotate_notification_email_advertiser'];
            if (strlen($advertiser_emails) > 0) {
                $advertiser_emails = explode(',', trim($advertiser_emails));
                foreach ($advertiser_emails as $advertiser_email) {
                    $advertiser_email = trim($advertiser_email);
                    if (strlen($advertiser_email) > 0) {
                        if (is_email($advertiser_email)) {
                            $clean_advertiser_email[] = $advertiser_email;
                        }
                    }
                }
                $notifications['notification_email_advertiser'] = array_unique(array_slice($clean_advertiser_email, 0, 2));
            } else {
                $notifications['notification_email_advertiser'] = array(get_option('admin_email'));
            }
            // Push Notifications
            $notifications['notification_push_geo'] = isset($_POST['adrotate_notification_push_geo']) ? 'Y' : 'N';
            $notifications['notification_push_status'] = isset($_POST['adrotate_notification_push_status']) ? 'Y' : 'N';
            $notifications['notification_push_queue'] = isset($_POST['adrotate_notification_push_queue']) ? 'Y' : 'N';
            $notifications['notification_push_approved'] = isset($_POST['adrotate_notification_push_approved']) ? 'Y' : 'N';
            $notifications['notification_push_rejected'] = isset($_POST['adrotate_notification_push_rejected']) ? 'Y' : 'N';
            $notifications['notification_push_user'] = strlen($_POST['adrotate_notification_push_user']) > 0 ? preg_replace('/[^a-z0-9.]+/i', '', trim(esc_attr($_POST['adrotate_notification_push_user']))) : '';
            $notifications['notification_push_api'] = strlen($_POST['adrotate_notification_push_api']) > 0 ? preg_replace('/[^a-z0-9.]+/i', '', trim(esc_attr($_POST['adrotate_notification_push_api']))) : '';
            update_option('adrotate_notifications', $notifications);
        }
        if ($settings_tab == 'stats') {
            $config = get_option('adrotate_config');
            $stats = trim($_POST['adrotate_stats']);
            $config['stats'] = (is_numeric($stats) and $stats >= 0 and $stats <= 3) ? $stats : 1;
            $config['enable_loggedin_impressions'] = isset($_POST['adrotate_enable_loggedin_impressions']) ? 'Y' : 'N';
            $config['enable_loggedin_clicks'] = isset($_POST['adrotate_enable_loggedin_clicks']) ? 'Y' : 'N';
            $impression_timer = trim($_POST['adrotate_impression_timer']);
            $config['impression_timer'] = (is_numeric($impression_timer) and $impression_timer >= 10 and $impression_timer <= 3600) ? $impression_timer : 60;
            $click_timer = trim($_POST['adrotate_click_timer']);
            $config['click_timer'] = (is_numeric($click_timer) and $click_timer >= 60 and $click_timer <= 86400) ? $click_timer : 86400;
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'geo') {
            $config = get_option('adrotate_config');
            $geo = trim($_POST['adrotate_enable_geo']);
            $config['enable_geo'] = (is_numeric($geo) and $geo >= 0 and $geo <= 5) ? $geo : 0;
            $geo_cookie = trim($_POST['adrotate_geo_cookie_life']);
            $config['geo_cookie_life'] = is_numeric($geo_cookie) ? $geo_cookie : 86400;
            $geo_email = trim($_POST['adrotate_geo_email']);
            $config['geo_email'] = strlen($geo_email) > 0 ? $geo_email : '';
            $geo_pass = trim($_POST['adrotate_geo_pass']);
            $config['geo_pass'] = strlen($geo_pass) > 0 ? $geo_pass : '';
            // Try to update the Geo Cookie for Admin
            if ($config['enable_geo'] > 0) {
                adrotate_geolocation();
            }
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'advertisers') {
            $config = get_option('adrotate_config');
            $config['enable_advertisers'] = isset($_POST['adrotate_enable_advertisers']) ? 'Y' : 'N';
            $config['enable_editing'] = isset($_POST['adrotate_enable_editing']) ? 'Y' : 'N';
            $config['enable_mobile_advertisers'] = isset($_POST['adrotate_enable_mobile_advertisers']) ? 1 : 0;
            $config['enable_geo_advertisers'] = isset($_POST['adrotate_enable_geo_advertisers']) ? 1 : 0;
            if (isset($_POST['adrotate_role'])) {
                adrotate_prepare_roles('add');
            } else {
                adrotate_prepare_roles('remove');
            }
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'roles') {
            $config = get_option('adrotate_config');
            adrotate_set_capability($_POST['adrotate_advertiser'], "adrotate_advertiser");
            adrotate_set_capability($_POST['adrotate_global_report'], "adrotate_global_report");
            adrotate_set_capability($_POST['adrotate_ad_manage'], "adrotate_ad_manage");
            adrotate_set_capability($_POST['adrotate_ad_delete'], "adrotate_ad_delete");
            adrotate_set_capability($_POST['adrotate_group_manage'], "adrotate_group_manage");
            adrotate_set_capability($_POST['adrotate_group_delete'], "adrotate_group_delete");
            adrotate_set_capability($_POST['adrotate_schedule_manage'], "adrotate_schedule_manage");
            adrotate_set_capability($_POST['adrotate_schedule_delete'], "adrotate_schedule_delete");
            adrotate_set_capability($_POST['adrotate_moderate'], "adrotate_moderate");
            adrotate_set_capability($_POST['adrotate_moderate_approve'], "adrotate_moderate_approve");
            $config['advertiser'] = $_POST['adrotate_advertiser'];
            $config['global_report'] = $_POST['adrotate_global_report'];
            $config['ad_manage'] = $_POST['adrotate_ad_manage'];
            $config['ad_delete'] = $_POST['adrotate_ad_delete'];
            $config['group_manage'] = $_POST['adrotate_group_manage'];
            $config['group_delete'] = $_POST['adrotate_group_delete'];
            $config['schedule_manage'] = $_POST['adrotate_schedule_manage'];
            $config['schedule_delete'] = $_POST['adrotate_schedule_delete'];
            $config['moderate'] = $_POST['adrotate_moderate'];
            $config['moderate_approve'] = $_POST['adrotate_moderate_approve'];
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'misc') {
            $config = get_option('adrotate_config');
            $config['widgetalign'] = isset($_POST['adrotate_widgetalign']) ? 'Y' : 'N';
            $config['widgetpadding'] = isset($_POST['adrotate_widgetpadding']) ? 'Y' : 'N';
            $config['adminbar'] = isset($_POST['adrotate_adminbar']) ? 'Y' : 'N';
            $config['hide_schedules'] = isset($_POST['adrotate_hide_schedules']) ? 'Y' : 'N';
            $config['w3caching'] = isset($_POST['adrotate_w3caching']) ? 'Y' : 'N';
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'maintenance') {
            $debug = get_option('adrotate_debug');
            $debug['general'] = isset($_POST['adrotate_debug']) ? true : false;
            $debug['publisher'] = isset($_POST['adrotate_debug_publisher']) ? true : false;
            $debug['advertiser'] = isset($_POST['adrotate_debug_advertiser']) ? true : false;
            $debug['geo'] = isset($_POST['adrotate_debug_geo']) ? true : false;
            $debug['timers'] = isset($_POST['adrotate_debug_timers']) ? true : false;
            $debug['track'] = isset($_POST['adrotate_debug_track']) ? true : false;
            update_option('adrotate_debug', $debug);
        }
        // Return to dashboard
        adrotate_return('adrotate-settings', 400, array('tab' => $settings_tab));
    } else {
        adrotate_nonce_error();
        exit;
    }
}
function adrotate_options_submit()
{
    if (wp_verify_nonce($_POST['adrotate_nonce_settings'], 'adrotate_settings')) {
        $settings_tab = esc_attr($_POST['adrotate_settings_tab']);
        if ($settings_tab == 'general') {
            $config = get_option('adrotate_config');
            $config['jquery'] = isset($_POST['adrotate_jquery']) ? 'Y' : 'N';
            $config['jsfooter'] = isset($_POST['adrotate_jsfooter']) ? 'Y' : 'N';
            // Turn options off. Available in AdRotate Pro only
            $config['banner_folder'] = "wp-content/banners/";
            $config['notification_email'] = array();
            $config['advertiser_email'] = array();
            $config['adblock'] = 'N';
            $config['adblock_timer'] = 0;
            $config['adblock_message'] = '';
            $config['enable_geo'] = 0;
            $config['geo_cookie_life'] = 86400;
            $config['geo_email'] = '';
            $config['geo_pass'] = '';
            $config['enable_advertisers'] = 'N';
            $config['enable_editing'] = 'N';
            $config['enable_geo_advertisers'] = 0;
            $config['adblock'] = 'N';
            $config['adblock_loggedin'] = 'N';
            $config['adblock_timer'] = 5;
            $config['adblock_message'] = "Ad blocker detected! Please wait %time% seconds or disable your ad blocker!";
            update_option('adrotate_config', $config);
            // Sort out crawlers
            $crawlers = explode(',', trim($_POST['adrotate_crawlers']));
            $new_crawlers = array();
            foreach ($crawlers as $crawler) {
                $crawler = preg_replace('/[^a-zA-Z0-9\\[\\]\\-_:; ]/i', '', trim($crawler));
                if (strlen($crawler) > 0) {
                    $new_crawlers[] = $crawler;
                }
            }
            update_option('adrotate_crawlers', $new_crawlers);
            $notifications = get_option('adrotate_notifications');
            // Turn options off. Available in AdRotate Pro only
            $notifications['notification_email'] = 'N';
            $notifications['notification_push'] = 'N';
            $notifications['notification_dashboard'] = 'Y';
            $notifications['notification_email_publisher'] = array();
            $notifications['notification_email_advertiser'] = array(get_option('admin_email'));
            $notifications['notification_push_geo'] = 'N';
            $notifications['notification_push_status'] = 'N';
            $notifications['notification_push_queue'] = 'N';
            $notifications['notification_push_approved'] = 'N';
            $notifications['notification_push_rejected'] = 'N';
            $notifications['notification_push_user'] = '';
            $notifications['notification_push_api'] = '';
            update_option('adrotate_notifications', $notifications);
        }
        if ($settings_tab == 'stats') {
            $config = get_option('adrotate_config');
            $stats = trim($_POST['adrotate_stats']);
            $config['stats'] = (is_numeric($stats) and $stats >= 0 and $stats <= 3) ? $stats : 1;
            $config['enable_loggedin_impressions'] = 'Y';
            $config['enable_loggedin_clicks'] = 'Y';
            $impression_timer = trim($_POST['adrotate_impression_timer']);
            $config['impression_timer'] = (is_numeric($impression_timer) and $impression_timer >= 10 and $impression_timer <= 3600) ? $impression_timer : 60;
            $click_timer = trim($_POST['adrotate_click_timer']);
            $config['click_timer'] = (is_numeric($click_timer) and $click_timer >= 60 and $click_timer <= 86400) ? $click_timer : 86400;
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'roles') {
            $config = get_option('adrotate_config');
            adrotate_set_capability($_POST['adrotate_ad_manage'], "adrotate_ad_manage");
            adrotate_set_capability($_POST['adrotate_ad_delete'], "adrotate_ad_delete");
            adrotate_set_capability($_POST['adrotate_group_manage'], "adrotate_group_manage");
            adrotate_set_capability($_POST['adrotate_group_delete'], "adrotate_group_delete");
            $config['ad_manage'] = $_POST['adrotate_ad_manage'];
            $config['ad_delete'] = $_POST['adrotate_ad_delete'];
            $config['group_manage'] = $_POST['adrotate_group_manage'];
            $config['group_delete'] = $_POST['adrotate_group_delete'];
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'misc') {
            $config = get_option('adrotate_config');
            $config['widgetalign'] = isset($_POST['adrotate_widgetalign']) ? 'Y' : 'N';
            $config['widgetpadding'] = isset($_POST['adrotate_widgetpadding']) ? 'Y' : 'N';
            $config['adminbar'] = isset($_POST['adrotate_adminbar']) ? 'Y' : 'N';
            $config['hide_schedules'] = isset($_POST['adrotate_hide_schedules']) ? 'Y' : 'N';
            $config['w3caching'] = isset($_POST['adrotate_w3caching']) ? 'Y' : 'N';
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'maintenance') {
            $debug = get_option('adrotate_debug');
            $debug['general'] = isset($_POST['adrotate_debug']) ? true : false;
            $debug['publisher'] = isset($_POST['adrotate_debug_publisher']) ? true : false;
            $debug['timers'] = isset($_POST['adrotate_debug_timers']) ? true : false;
            $debug['track'] = isset($_POST['adrotate_debug_track']) ? true : false;
            update_option('adrotate_debug', $debug);
        }
        // Return to dashboard
        adrotate_return('adrotate-settings', 400, array('tab' => $settings_tab));
    } else {
        adrotate_nonce_error();
        exit;
    }
}
function adrotate_options_submit()
{
    if (wp_verify_nonce($_POST['adrotate_nonce_settings'], 'adrotate_settings')) {
        // Set and save user roles
        adrotate_set_capability($_POST['adrotate_ad_manage'], "adrotate_ad_manage");
        adrotate_set_capability($_POST['adrotate_ad_delete'], "adrotate_ad_delete");
        adrotate_set_capability($_POST['adrotate_group_manage'], "adrotate_group_manage");
        adrotate_set_capability($_POST['adrotate_group_delete'], "adrotate_group_delete");
        $config['ad_manage'] = $_POST['adrotate_ad_manage'];
        $config['ad_delete'] = $_POST['adrotate_ad_delete'];
        $config['group_manage'] = $_POST['adrotate_group_manage'];
        $config['group_delete'] = $_POST['adrotate_group_delete'];
        // Enable stats
        $stats = trim($_POST['adrotate_stats']);
        if (is_numeric($stats) and $stats >= 0 and $stats <= 2) {
            $config['stats'] = $stats;
        } else {
            $config['stats'] = 1;
        }
        // Set the banner folder, reset if empty
        $config['banner_folder'] = "wp-content/banners/";
        // Turn option off.
        $config['notification_email_switch'] = 'N';
        $config['notification_email'] = array();
        $config['advertiser_email'] = array();
        // Set up impression tracker timer
        $impression_timer = trim($_POST['adrotate_impression_timer']);
        if (is_numeric($impression_timer) and $impression_timer >= 10 and $impression_timer <= 3600) {
            $config['impression_timer'] = $impression_timer;
        } else {
            $config['impression_timer'] = 60;
        }
        // Set up click timer
        $click_timer = trim($_POST['adrotate_click_timer']);
        if (is_numeric($click_timer) and $click_timer >= 60 and $click_timer <= 86400) {
            $config['click_timer'] = $click_timer;
        } else {
            $config['click_timer'] = 86400;
        }
        // Miscellaneous Options
        if (isset($_POST['adrotate_widgetalign'])) {
            $config['widgetalign'] = 'Y';
        } else {
            $config['widgetalign'] = 'N';
        }
        if (isset($_POST['adrotate_widgetpadding'])) {
            $config['widgetpadding'] = 'Y';
        } else {
            $config['widgetpadding'] = 'N';
        }
        if (isset($_POST['adrotate_w3caching'])) {
            $config['w3caching'] = 'Y';
        } else {
            $config['w3caching'] = 'N';
        }
        if (isset($_POST['adrotate_supercache'])) {
            $config['supercache'] = 'Y';
        } else {
            $config['supercache'] = 'N';
        }
        if (isset($_POST['adrotate_jquery'])) {
            $config['jquery'] = 'Y';
        } else {
            $config['jquery'] = 'N';
        }
        if (isset($_POST['adrotate_jsfooter'])) {
            $config['jsfooter'] = 'Y';
        } else {
            $config['jsfooter'] = 'N';
        }
        $config['adblock'] = 'N';
        // Pro only
        $config['adblock_timer'] = 0;
        // Pro only
        $config['adblock_message'] = '';
        // Pro only
        update_option('adrotate_config', $config);
        // Sort out crawlers
        $crawlers = explode(',', trim($_POST['adrotate_crawlers']));
        $clean_crawler = array();
        foreach ($crawlers as $crawler) {
            $crawler = preg_replace('/[^a-zA-Z0-9\\[\\]\\-_:; ]/i', '', trim($crawler));
            if (strlen($crawler) > 0) {
                $clean_crawler[] = $crawler;
            }
        }
        update_option('adrotate_crawlers', $clean_crawler);
        // Debug option
        if (isset($_POST['adrotate_debug'])) {
            $debug['general'] = true;
        } else {
            $debug['general'] = false;
        }
        if (isset($_POST['adrotate_debug_dashboard'])) {
            $debug['dashboard'] = true;
        } else {
            $debug['dashboard'] = false;
        }
        if (isset($_POST['adrotate_debug_userroles'])) {
            $debug['userroles'] = true;
        } else {
            $debug['userroles'] = false;
        }
        if (isset($_POST['adrotate_debug_userstats'])) {
            $debug['userstats'] = true;
        } else {
            $debug['userstats'] = false;
        }
        if (isset($_POST['adrotate_debug_stats'])) {
            $debug['stats'] = true;
        } else {
            $debug['stats'] = false;
        }
        if (isset($_POST['adrotate_debug_timers'])) {
            $debug['timers'] = true;
        } else {
            $debug['timers'] = false;
        }
        if (isset($_POST['adrotate_debug_track'])) {
            $debug['track'] = true;
        } else {
            $debug['track'] = false;
        }
        update_option('adrotate_debug', $debug);
        // Return to dashboard
        adrotate_return('settings_saved');
    } else {
        adrotate_nonce_error();
        exit;
    }
}