add_action('admin_post_set_unbounce_domains', function () {
    $domains_list = UBUtil::array_fetch($_POST, 'domains', '');
    $domains = array_filter(explode(',', $domains_list), function ($domain) {
        return $domain == UBConfig::domain();
    });
    if ($domains && is_array($domains)) {
        $authorization = 'success';
        $has_authorized = get_option(UBConfig::UB_HAS_AUTHORIZED_KEY, false);
        $data = array('domain_name' => UBConfig::domain(), 'first_authorization' => !$has_authorized, 'user_id' => UBUtil::array_fetch($_POST, 'user_id', ''), 'client_id' => UBUtil::array_fetch($_POST, 'client_id', ''), 'domain_id' => UBUtil::array_fetch($_POST, 'domain_id', ''));
        UBConfig::update_authorization_options($domains, $data);
        if (UBConfig::is_authorized_domain(UBConfig::domain())) {
            $event = UBEvents::successful_authorization_event($data);
        } else {
            $event = UBEvents::failed_authorization_event($data);
        }
        UBHTTP::send_event_to_events_gateway(UBConfig::remote_events_url(), $event);
    } else {
        $authorization = 'failure';
    }
    UBUtil::set_flash('authorization', $authorization);
    status_header(301);
    $location = admin_url('admin.php?page=unbounce-pages');
    header("Location: {$location}");
});
add_action('admin_post_flush_unbounce_pages', function () {
    $domain = UBConfig::domain();
    // Expire cache and redirect
    $_domain_info = UBConfig::read_unbounce_domain_info($domain, true);
    status_header(301);
    $location = admin_url('admin.php?page=unbounce-pages');
    header("Location: {$location}");