<?php
// این فایل بعد از اجرای کدهای 1-5 فراخوانی می‌شود
if (!isset($_POST['bumpDate'])) {
    return; // فقط اگر ON باشد اجرا شود
}

$folder = trim($_POST['folderPath'] ?? '');
if (!$folder) return;

// === تابع مستقل استخراج عنوان و سال ===
function mhp_extract_title_year($folder_name) {
    $name = preg_replace('/[._-]+/', ' ', $folder_name);
    if (preg_match('/\b(19|20)\d{2}\b/', $name, $ym)) {
        $year = $ym[0];
        $title = trim(preg_replace('/\b(19|20)\d{2}\b.*/', '', $name));
        return [$title, $year];
    }
    return [null, null];
}
// ======================================

$folder_name = basename(rtrim($folder, '/'));
list($guess_title, $guess_year) = mhp_extract_title_year($folder_name);

if (!$guess_title || !$guess_year) return;

global $wpdb;
$post_id = $wpdb->get_var($wpdb->prepare(
    "SELECT pm.post_id 
     FROM $wpdb->postmeta pm 
     JOIN $wpdb->posts p ON pm.post_id = p.ID 
     WHERE pm.meta_key = 'title_movie' AND pm.meta_value = %s 
     AND p.post_status = 'publish' 
     LIMIT 1",
    $guess_title
));

if ($post_id) {
    wp_update_post([
        'ID' => $post_id,
        'post_date' => current_time('mysql'),
        'post_date_gmt' => get_gmt_from_date(current_time('mysql'))
    ]);
    error_log("Media Hub: تاریخ پست فیلم '$guess_title' بروزرسانی شد.");
}