File: /home/tsacademy/domains/tsacademy.ir/private_html/bale-latest-ticker.php
<?php
require_once("wp-load.php");
global $wpdb;
header('Content-Type: application/json; charset=utf-8');
/**
* تنظیمات
*/
$limit_per_table = 6; // از هر جدول چند پیام
$max_total = 12; // حداکثر کل خروجی
$feeds = [
[
'table' => $wpdb->prefix . 'bale_messages',
'title' => 'سهام',
'url' => 'https://tsacademy.ir/stock-tahlil/',
],
[
'table' => $wpdb->prefix . 'bale_messages_dollar',
'title' => 'دلار',
'url' => 'https://tsacademy.ir/dollar-tahlil/',
],
[
'table' => $wpdb->prefix . 'bale_messages_gold',
'title' => 'طلا',
'url' => 'https://tsacademy.ir/gold-tahlil/',
],
[
'table' => $wpdb->prefix . 'bale_messages_crypto',
'title' => 'کریپتو',
'url' => 'https://tsacademy.ir/crypto-tahlil/',
],
];
$all = [];
foreach ($feeds as $feed) {
$table = $feed['table'];
$rows = $wpdb->get_results("
SELECT id, msg_type, msg_text, created_at
FROM {$table}
WHERE msg_text IS NOT NULL
AND TRIM(msg_text) <> ''
AND msg_type IN ('text','photo','voice','video','document')
ORDER BY id DESC
LIMIT " . intval($limit_per_table) . "
");
if (!$rows) continue;
foreach ($rows as $row) {
$text = trim((string)$row->msg_text);
// حذف پیامهای سیستمی و دستوری
if ($text === '' || strpos($text, '/') === 0) {
continue;
}
$all[] = [
'id' => (int)$row->id,
'msg_type' => $row->msg_type,
'msg_text' => wp_strip_all_tags($text),
'created_at' => $row->created_at,
'title' => $feed['title'],
'url' => $feed['url'],
];
}
}
/**
* مرتبسازی بر اساس جدیدترین
*/
usort($all, function($a, $b){
return strtotime($b['created_at']) - strtotime($a['created_at']);
});
/**
* حذف موارد تکراری
*/
$seen = [];
$unique = [];
foreach ($all as $item) {
$key = md5($item['title'] . '|' . $item['msg_text']);
if (isset($seen[$key])) {
continue;
}
$seen[$key] = true;
$unique[] = $item;
}
/**
* محدودیت نهایی
*/
$unique = array_slice($unique, 0, $max_total);
echo json_encode($unique, JSON_UNESCAPED_UNICODE);
exit;