<?php
defined('ABSPATH') or die('No script kiddies please!!');
$std_settings = get_option('std_settings');
$client_id = $std_settings['constant_contact_v3']['client_id'];
$client_secret = $std_settings['constant_contact_v3']['client_secret'];
$redirect_uri = urlencode(site_url() . '?std_constant_contact_authorize');
$state = bin2hex(random_bytes(16)); // Generate a random state parameter

// Set a secure cookie with the state value
setcookie('oauth_state', $state, time() + 300, "/", "", false, true); // Expires in 5 minutes, HTTP-only

$auth_url = "https://authz.constantcontact.com/oauth2/default/v1/authorize?"
    . "client_id=$client_id"
    . "&redirect_uri=$redirect_uri"
    . "&response_type=code"
    . "&scope=contact_data%20account_read%20offline_access" // Add offline_access
    . "&state=$state";
    
wp_redirect($auth_url);
exit;
