function generate_annual_license($user_id) {
global $wpdb;
$user = get_userdata($user_id);
$first_name = get_user_meta($user_id, 'first_name', true);
$last_name = get_user_meta($user_id, 'last_name', true);
if (empty($first_name)) {
$parts = explode('.', $user->user_login);
$first_name = $parts[0] ?? 'User';
$last_name = $parts[1] ?? '';
}
$first_name = capitalize_name($first_name);
$last_name = capitalize_name($last_name);
$email = $user->user_email;
$table = $wpdb->prefix . 'user_licenses';
// Check if license already exists
$existing = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE user_id = %d", $user_id));
$created_at = current_time('mysql');
$expires_at = date('Y-m-d H:i:s', strtotime('+365 days'));
$license_key = generate_random_license_key();
if ($existing) {
// Update old license
$wpdb->update($table, [
'license_key' => $license_key,
'created_at' => $created_at,
'expires_at' => $expires_at,
'is_active' => 1,
'license_type' => 'annual',
], ['user_id' => $user_id]);
} else {
// Insert new license
$wpdb->insert($table, [
'user_id' => $user_id,
'license_key' => $license_key,
'created_at' => $created_at,
'expires_at' => $expires_at,
'is_active' => 1,
'license_type' => 'annual',
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email
]);
}
$welcome_body = "Thank you for creating an account!
We’re thrilled to have you on board!
As a new member of our growing community, you now have access to powerful tools designed to enhance your digital experience.
You’ll receive your free annual license in a separate email.
Would you consider supporting Emboldened with a donation? -
Support Emboldened";
send_html_email($email, 'Welcome to Emboldened', $welcome_body, $first_name);
$license_body = "Here's your free annual license
(valid until $expires_at):
$license_key";
send_html_email($email, 'Your Emboldened License', $license_body, $first_name);
}