<?php
/**
* Plugin Name: Laduli Naukri Search Portal – Pro V11.5 (Advanced Alerts)
* Plugin URI: https://laduli.in/
* Description: Smart Content Detection, Post Status Alerts, and Enhanced Security.
* Version: 11.5
* Author: Laduli Naukri Search Portal
*/

if ( ! defined( ‘ABSPATH’ ) ) exit;

// — 1. SETTINGS & HELPERS —
add_action(‘admin_menu’, ‘lad_v11_menu’);
function lad_v11_menu() {
add_menu_page(‘Laduli Settings’, ‘Laduli Settings’, ‘manage_options’, ‘lad-settings’, ‘lad_v11_settings_page’, ‘dashicons-shield’);
}

function lad_v11_settings_page() {
if (isset($_POST[‘save_v11’])) {
update_option(‘lad_admin_mail’, sanitize_email($_POST[‘l_email’]));
echo ‘<div class=”updated”><p>✅ Settings Saved!</p></div>’;
}
$email = get_option(‘lad_admin_mail’, get_option(‘admin_email’));
echo ‘<div class=”wrap”><h1>Laduli Pro Settings</h1><form method=”post”>
<table class=”form-table”><tr><th>Admin Email:</th><td><input type=”email” name=”l_email” value=”‘.esc_attr($email).'” class=”regular-text”></td></tr></table>
<input type=”submit” name=”save_v11″ class=”button-primary” value=”Save Settings”></form></div>’;
}

function lad_v11_send_mail($to, $subject, $body) {
$headers = array(‘Content-Type: text/html; charset=UTF-8’, ‘From: Laduli Portal <‘ . get_option(‘admin_email’) . ‘>’);
wp_mail($to, $subject, $body, $headers);
}

function lad_get_location($ip) {
$response = wp_remote_get(“https://ip-api.com/json/{$ip}?fields=status,country,regionName,city”, array(‘timeout’ => 5));
if (is_wp_error($response)) return “Unknown Location”;
$body = json_decode(wp_remote_retrieve_body($response));
return ($body && $body->status == ‘success’) ? “{$body->city}, {$body->regionName}, {$body->country}” : “Unknown Location”;
}

// — 2. ENHANCED POST PUBLISH LOGIC —
add_action(‘publish_post’, ‘lad_v11_post_publish_trigger’, 10, 2);
function lad_v11_post_publish_trigger($ID, $post) {
if (get_post_meta($ID, ‘_lad_mail_sent’, true)) return;

$admin_email = get_option(‘lad_admin_mail’, get_option(‘admin_email’));
$author = get_userdata($post->post_author);

// Marathi & English Word Count
$content = strip_tags($post->post_content);
preg_match_all(‘/[\x{0900}-\x{097F}a-zA-Z0-9]+/u’, $content, $matches);
$word_count = count($matches[0]);

$is_low_content = ($word_count < 1000);
$accent_color = $is_low_content ? “#e67e22” : “#27ae60”; // Orange for low, Green for high
$status_title = $is_low_content ? “⚠️ Low Content Alert!” : “✅ Your Post is Live!”;
$button_text = $is_low_content ? “Review & Edit Post” : “View Live Post”;
$button_link = $is_low_content ? get_edit_post_link($ID) : get_permalink($ID);

$marathi_msg = $is_low_content
? “तुमची पोस्ट यशस्वीरित्या जतन झाली आहे, परंतु त्यामध्ये <b>१००० पेक्षा कमी शब्द</b> आहेत. कृपया अधिक माहिती जोडून पोस्ट अपडेट करा.”
: “अभिनंदन! तुमची पोस्ट यशस्वीरित्या पब्लिश झाली आहे. तुमच्या पोस्टमध्ये उत्तम माहिती (१०००+ शब्द) असल्यामुळे ती आता वाचकांसाठी उपलब्ध आहे.”;

// — AUTHOR EMAIL DESIGN —
$author_body = ”
<div style=’font-family: \”Segoe UI\”, Tahoma, sans-serif; max-width: 600px; border: 1px solid #e2e8f0; border-radius: 15px; overflow: hidden; background: #ffffff;’>
<div style=’background: $accent_color; padding: 25px; text-align: center; color: white;’>
<h1 style=’margin: 0; font-size: 22px;’>$status_title</h1>
</div>
<div style=’padding: 30px;’>
<p style=’font-size: 16px; color: #2d3748;’>नमस्कार <b>{$author->display_name}</b>,</p>
<p style=’font-size: 15px; color: #4a5568; line-height: 1.6;’>$marathi_msg</p>

<div style=’background: #f7fafc; border-radius: 10px; padding: 20px; margin: 25px 0; border-left: 4px solid $accent_color;’>
<table style=’width: 100%; border-collapse: collapse;’>
<tr><td style=’padding: 5px; color: #718096;’>पोस्टचे नाव:</td><td style=’font-weight: bold; color: #2d3748;’>{$post->post_title}</td></tr>
<tr><td style=’padding: 5px; color: #718096;’>शब्द संख्या:</td><td style=’font-weight: bold; color: $accent_color;’>$word_count Words</td></tr>
</table>
</div>

<p style=’text-align: center;’>
<a href=’$button_link’ style=’background: $accent_color; color: #ffffff; padding: 14px 30px; text-decoration: none; border-radius: 8px; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px rgba(0,0,0,0.1);’>$button_text</a>
</p>
</div>
<div style=’background: #f8fafc; padding: 15px; text-align: center; font-size: 12px; color: #a0aec0; border-top: 1px solid #edf2f7;’>
© “.date(‘Y’).” Laduli Naukri Search Portal | laduli.in
</div>
</div>”;

// — ADMIN EMAIL (Quick Notification) —
$admin_body = “<div style=’font-family: Arial; padding: 20px; background: #f1f5f9;’>
<div style=’background: #fff; padding: 20px; border-radius: 8px;’>
<h3>📝 New Content Report</h3>
<p><b>Author:</b> {$author->display_name}<br><b>Words:</b> $word_count<br><b>Status:</b> “.($is_low_content ? ‘Needs Review’ : ‘Good Content’).”</p>
<a href='”.get_edit_post_link($ID).”‘>Open Admin Dashboard</a>
</div>
</div>”;

lad_v11_send_mail($author->user_email, “$status_title : ” . $post->post_title, $author_body);
lad_v11_send_mail($admin_email, “📝 Post Report: ” . $author->display_name . ” ($word_count Words)”, $admin_body);

update_post_meta($ID, ‘_lad_mail_sent’, time());
}

// — 3. LOGIN SECURITY ALERT —
add_action(‘wp_login’, ‘lad_v11_login_alert’, 10, 2);
function lad_v11_login_alert($user_login, $user) {
$admin_email = get_option(‘lad_admin_mail’, get_option(‘admin_email’));
$ip = $_SERVER[‘REMOTE_ADDR’];
$location = lad_get_location($ip);
$time = current_time(‘mysql’);
$device = wp_is_mobile() ? “Mobile” : “Desktop/PC”;
$reset_url = wp_lostpassword_url();

$auth_body = ”
<div style=’font-family: Arial, sans-serif; max-width: 600px; border: 1px solid #eee; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 10px rgba(0,0,0,0.1);’>
<div style=’background: #d93025; padding: 20px; text-align: center; color: #fff;’>
<h2 style=’margin:0;’>⚠️ Security Alert</h2>
</div>
<div style=’padding: 30px; line-height: 1.6;’>
<p style=’font-size: 16px;’>नमस्कार <b>{$user->display_name}</b>,</p>
<p style=’font-size: 16px;’>तुमच्या <b>Laduli Naukri Search Portal</b> खात्यात नुकतेच लॉगिन झाले आहे.</p>

<div style=’background: #fff5f5; border: 1px solid #feb2b2; padding: 15px; border-radius: 5px; color: #c53030; margin: 20px 0;’>
<strong>सूचना:</strong> जर हे लॉगिन तुम्ही केले नसेल, तर कृपया सुरक्षा कारणास्तव त्वरित तुमचा पासवर्ड रिसेट करा किंवा ॲडमिनशी संपर्क साधा.
</div>

<p style=’text-align: center; margin-top: 30px;’>
<a href=’$reset_url’ style=’background: #d93025; color: #ffffff; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block;’>Reset Password Now</a>
</p>
<p style=’font-size: 12px; color: #777; text-align:center;’>वेळ: $time | डिव्हाइस: $device</p>
</div>
</div>”;

lad_v11_send_mail($user->user_email, “⚠️ Security Alert: New Login Detected”, $auth_body);

// Admin log includes location
$adm_body = “<h3>Login Info</h3><p>User: {$user->display_name}<br>IP: $ip<br>Loc: $location</p>”;
lad_v11_send_mail($admin_email, “🔑 Admin Login: ” . $user->display_name, $adm_body);
}