'Please enter your first and last name.']);
if (!$email || !is_email($email)) wp_send_json_error(['message' => 'Please enter a valid email address.']);
if (!$penname) wp_send_json_error(['message' => 'Please enter your pen name.']);
if (!$novel_title) wp_send_json_error(['message' => 'Please enter your novel title.']);
if (!$synopsis) wp_send_json_error(['message' => 'Please write a synopsis.']);
if (!$sample) wp_send_json_error(['message' => 'Please include a writing sample.']);
$apps = get_option('mnc_author_applications', []);
$token = wp_generate_password(32, false);
$app_id = 'app_' . time() . '_' . substr(md5($email), 0, 6);
$apps[$app_id] = [
'id' => $app_id,
'name' => $name,
'email' => $email,
'penname' => $penname,
'country' => $country,
'novel_title' => $novel_title,
'genres' => $genres,
'synopsis' => $synopsis,
'sample' => $sample,
'experience' => $experience,
'source' => $source,
'extra' => $extra,
'status' => 'pending',
'submitted' => current_time('mysql'),
'token' => $token,
];
update_option('mnc_author_applications', $apps);
$approve_url = add_query_arg(['mnc_app_action' => 'approve', 'app_id' => $app_id, 'token' => $token], home_url('/'));
$reject_url = add_query_arg(['mnc_app_action' => 'reject', 'app_id' => $app_id, 'token' => $token], home_url('/'));
$admin_email = get_option('admin_email');
$admin_subject = 'New Author Application — ' . $penname;
$admin_body = "New author application received.\n\n"
. "Name: $name\n"
. "Email: $email\n"
. "Pen Name: $penname\n"
. ($country ? "Country: $country\n" : '')
. "Novel Title: $novel_title\n"
. "Genres: $genres\n"
. ($experience ? "Experience: $experience\n" : '')
. ($source ? "Source: $source\n" : '')
. "\nSynopsis:\n$synopsis\n\n"
. "Writing Sample:\n$sample\n"
. ($extra ? "\nAdditional Notes:\n$extra\n" : '')
. "\n" . str_repeat('-', 40) . "\n"
. "APPROVE this author (click link):\n$approve_url\n\n"
. "REJECT this application (click link):\n$reject_url\n\n"
. "Click a link above — no email reply needed.\n"
. "My Novels Cafe Admin";
wp_mail($admin_email, $admin_subject, $admin_body);
$applicant_subject = 'We received your application — My Novels Cafe';
$applicant_body = "Hi $first,\n\n"
. "Thank you for applying to write on My Novels Cafe! We've received your application for \"$novel_title\" and will review it within 3-5 business days.\n\n"
. "We'll be in touch at this email address with our decision.\n\n"
. "In the meantime, feel free to explore the platform as a reader at mynovelscafe.com\n\n"
. "Warm regards,\nThe My Novels Cafe Team\nhello@mynovelscafe.com";
wp_mail($email, $applicant_subject, $applicant_body);
wp_send_json_success(['message' => 'Application submitted! Check your email for confirmation.']);
}
// ── PROCESS APPROVE/REJECT + WELCOME TOKEN (single init hook) ──
add_action('init', function() {
// ── WELCOME TOKEN ──
$welcome = sanitize_text_field($_GET['mnc_welcome'] ?? '');
if ($welcome) {
if (!is_user_logged_in()) {
setcookie('mnc_pending_welcome', $welcome, time() + 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
wp_redirect(wp_login_url(add_query_arg('mnc_welcome', $welcome, home_url('/author-studio/'))));
exit;
}
$user_id = get_current_user_id();
$stored = get_user_meta($user_id, 'mnc_welcome_token', true);
$expiry = (int) get_user_meta($user_id, 'mnc_welcome_token_expiry', true);
if (!$stored) {
wp_redirect(home_url('/author-studio/'));
exit;
}
if (!hash_equals($stored, $welcome)) {
wp_die(
'
'
. '
',
'Wrong Account — My Novels Cafe'
);
}
if (time() > $expiry) {
delete_user_meta($user_id, 'mnc_welcome_token');
delete_user_meta($user_id, 'mnc_welcome_token_expiry');
wp_die(
'🔒
'
. 'Wrong Account
' . 'This link belongs to a different account. Please log in with the account that received the approval email.
' . 'Switch Account' . ''
. '
',
'Link Expired — My Novels Cafe'
);
}
delete_user_meta($user_id, 'mnc_welcome_token');
delete_user_meta($user_id, 'mnc_welcome_token_expiry');
wp_redirect(home_url('/author-studio/?mnc_first_visit=1'));
exit;
}
// ── APPROVE / REJECT ──
$action = sanitize_text_field($_GET['mnc_app_action'] ?? '');
$app_id = sanitize_text_field($_GET['app_id'] ?? '');
$token = sanitize_text_field($_GET['token'] ?? '');
if (!$action || !$app_id || !$token) return;
if (!in_array($action, ['approve', 'reject'])) return;
$apps = get_option('mnc_author_applications', []);
if (!isset($apps[$app_id])) {
wp_die(
'⏰
'
. 'Link Expired
' . 'This welcome link expired after 7 days. You can still access your Author Studio directly at any time.
' . 'Go to Author Studio' . ''
. '
',
'My Novels Cafe'
);
}
$app = $apps[$app_id];
if (!hash_equals($app['token'], $token)) {
wp_die('Application not found or already processed.
' . 'Back to Admin' . 'Invalid or expired link.
', 'My Novels Cafe', ['response' => 403]); } if ($app['status'] !== 'pending') { wp_die( ''
. '
',
'My Novels Cafe'
);
}
$name = $app['name'];
$email = $app['email'];
$penname = $app['penname'];
$first = explode(' ', $name)[0];
if ($action === 'approve') {
$user = get_user_by('email', $email);
if (!$user) {
$username = sanitize_user(strtolower(str_replace(' ', '', $penname)), true);
$base_u = $username; $i = 1;
while (username_exists($username)) { $username = $base_u . $i; $i++; }
$password = wp_generate_password(12, false);
$user_id = wp_create_user($username, $password, $email);
if (is_wp_error($user_id)) {
wp_die('Error creating user: ' . $user_id->get_error_message());
}
wp_update_user(['ID' => $user_id, 'display_name' => $penname]);
update_user_meta($user_id, 'mnc_pen_name', $penname);
$new_account = true;
} else {
$user_id = $user->ID;
$password = null;
$new_account = false;
}
$user_obj = new WP_User($user_id);
$user_obj->set_role('author');
$welcome_token = wp_generate_password(40, false);
update_user_meta($user_id, 'mnc_welcome_token', $welcome_token);
update_user_meta($user_id, 'mnc_welcome_token_expiry', time() + (7 * DAY_IN_SECONDS));
$studio_url = add_query_arg('mnc_welcome', $welcome_token, home_url('/author-studio/'));
$apps[$app_id]['status'] = 'approved';
$apps[$app_id]['user_id'] = $user_id;
update_option('mnc_author_applications', $apps);
$subject = 'You\'ve been approved as an author — My Novels Cafe!';
$body = "Hi $first!\n\n"
. "Great news — your application to write on My Novels Cafe has been approved!\n\n"
. "Click the link below to access your Author Studio:\n\n"
. "$studio_url\n\n"
. "This link is personal to your account and can only be used once. It expires in 7 days.\n"
. "After your first visit, return to your studio anytime at:\n"
. home_url('/author-studio/') . "\n\n"
. ($new_account
? "Your account details:\nEmail: $email\nTemporary Password: $password\nPlease change your password after first login.\n\n"
: "Log in with your existing account.\n\n")
. "In your Author Studio you can add novels, write chapters, and track earnings.\n\n"
. "Questions? Email authors@mynovelscafe.com\n\n"
. "Welcome to the Cafe!\nThe My Novels Cafe Team";
wp_mail($email, $subject, $body);
wp_die(
'Already Processed
' . 'This application has already been ' . esc_html($app['status']) . '.
' . 'Back to Admin' . ''
. '
',
'Application Approved — My Novels Cafe'
);
} else {
$apps[$app_id]['status'] = 'rejected';
update_option('mnc_author_applications', $apps);
if (!empty($_POST['mnc_reject_reason'])) {
$reason = sanitize_textarea_field($_POST['mnc_reject_reason']);
$subject = 'Your My Novels Cafe author application';
$body = "Hi $first,\n\n"
. "Thank you for your interest in writing on My Novels Cafe.\n\n"
. "After reviewing your application, we're unable to approve it at this time.\n\n"
. ($reason ? "Feedback from our team:\n$reason\n\n" : '')
. "You're welcome to apply again in the future. Keep writing!\n\n"
. "The My Novels Cafe Team\nhello@mynovelscafe.com";
wp_mail($email, $subject, $body);
wp_die(
'',
'Rejection Sent — My Novels Cafe'
);
}
wp_die(
'✅
'
. 'Application Approved!
' . '' . esc_html($penname) . ' has been approved and sent their studio link.
' . 'Email sent to: ' . esc_html($email) . '
' . 'Back to Admin' . ''
. '
',
'Reject Application — My Novels Cafe'
);
}
}, 5);
// ── FIRST VISIT WELCOME BANNER ──
add_action('wp_footer', function() {
if (!isset($_GET['mnc_first_visit'])) return;
if (!is_user_logged_in()) return;
$pen_name = get_user_meta(get_current_user_id(), 'mnc_pen_name', true) ?: wp_get_current_user()->display_name;
$name_esc = esc_html($pen_name);
$studio = esc_url(home_url('/author-studio/'));
echo '📝
'
. 'Reject Application
' . 'Applicant: ' . esc_html($name) . ' <' . esc_html($email) . '>
Novel: ' . esc_html($app['novel_title'] ?? '') . '
';
echo '
';
echo '';
}); ';
echo '
';
echo '';
echo '☕
';
echo '';
echo '
Welcome to the Cafe, ' . $name_esc . '!
';
echo 'Your Author Studio is ready. You can always return by visiting /author-studio after logging in.
';
echo '