function abc_handle_csv_upload() { if (!isset($_POST['upload_csv']) || !check_admin_referer('abc_csv_upload', 'abc_csv_nonce')) return; global $wpdb; // مسیر دلخواه: wp-content/uploads/abc_csv_uploads/ $upload_base = wp_upload_dir(); $upload_dir = trailingslashit($upload_base['basedir']) . 'abc_csv_uploads/'; $upload_url = trailingslashit($upload_base['baseurl']) . 'abc_csv_uploads/'; // ساخت پوشه در صورت نیاز if (!file_exists($upload_dir)) { wp_mkdir_p($upload_dir); } $uploadedfile = $_FILES['csv_file']; $original_name = sanitize_file_name($uploadedfile['name']); $new_name = uniqid() . '-' . $original_name; $destination = $upload_dir . $new_name; if (move_uploaded_file($uploadedfile['tmp_name'], $destination)) { echo '

فایل با موفقیت آپلود شد.

'; $file_url = $upload_url . $new_name; $wpdb->insert( $wpdb->prefix . 'abc_csv_files', ['file_url' => esc_url($file_url), 'uploaded_at' => current_time('mysql')] ); $file_id = $wpdb->insert_id; // پردازش CSV و درج در جدول abc_csv_rows if (file_exists($destination)) { $csv = array_map('str_getcsv', file($destination)); $headers = array_map('sanitize_text_field', array_shift($csv)); foreach ($csv as $row) { $data = array_combine($headers, array_map('sanitize_text_field', $row)); if (isset($data['comment_post_ID'], $data['comment_content'])) { $wpdb->insert($wpdb->prefix . 'abc_csv_rows', [ 'file_id' => $file_id, 'post_id' => intval($data['comment_post_ID']), 'author' => $data['comment_author'] ?? 'Anonymous', 'email' => $data['comment_author_email'] ?? '', 'content' => $data['comment_content'], 'approved' => isset($data['comment_approved']) ? intval($data['comment_approved']) : 1, 'comment_date' => $data['comment_date'] ?? current_time('mysql'), 'status' => 'pending' ]); $comment_id = $wpdb->insert_id; // درج پاسخ در صورت وجود reply_content if (!empty($data['reply_content'])) { $comment_data = [ 'comment_post_ID' => intval($data['comment_post_ID']), 'comment_content' => $data['reply_content'], 'comment_parent' => $comment_id, 'comment_author' => 'مدیر', 'comment_approved' => 1, 'user_id' => get_current_user_id(), ]; wp_insert_comment($comment_data); } } } } } else { echo '

در آپلود فایل مشکلی پیش آمد.

'; } }