-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.php
More file actions
131 lines (107 loc) · 5.09 KB
/
scheduler.php
File metadata and controls
131 lines (107 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// نظام جدولة المنشورات - Post scheduling system
require_once 'config.php';
require_once 'functions.php';
require_once 'content_generator.php';
/**
* معالجة المنشورات المجدولة - Process scheduled posts
*/
function processScheduledPosts() {
$scheduledPosts = getScheduledPosts();
$currentTime = time();
$processed = false;
foreach ($scheduledPosts as $post) {
// نشر المنشورات المستحقة فقط - Only post due posts
if ($post['status'] === 'pending' && $post['scheduled_time'] <= $currentTime) {
$result = postToChannel($post['content']);
if (isset($result['ok']) && $result['ok']) {
updatePostStatus($post['id'], 'published');
} else {
updatePostStatus($post['id'], 'failed');
logError('فشل نشر المحتوى المجدول', json_encode($result));
}
$processed = true;
}
}
// تنظيف المنشورات القديمة - Clean up old posts
cleanupOldPosts();
return $processed;
}
/**
* تنظيف المنشورات القديمة - Clean up old posts
*/
function cleanupOldPosts() {
$scheduledPosts = getScheduledPosts();
$currentTime = time();
$oneWeekAgo = $currentTime - (7 * 24 * 60 * 60); // أسبوع واحد - One week
$updatedPosts = array_filter($scheduledPosts, function($post) use ($oneWeekAgo) {
// الاحتفاظ بالمنشورات المعلقة وتلك التي تم نشرها في الأسبوع الماضي
// Keep pending posts and those published in the last week
return $post['status'] === 'pending' ||
($post['status'] === 'published' && $post['scheduled_time'] >= $oneWeekAgo);
});
file_put_contents(SCHEDULED_POSTS_FILE, json_encode(array_values($updatedPosts)));
}
/**
* جدولة منشورات لليوم - Schedule posts for the day
*/
function schedulePostsForToday() {
$settings = getSettings();
$scheduledPosts = getScheduledPosts();
// التحقق مما إذا كان النشر نشطًا - Check if posting is active
if (!$settings['active']) {
return false;
}
// احسب عدد المنشورات المجدولة لليوم - Count scheduled posts for today
$today = strtotime('today');
$tomorrow = strtotime('tomorrow');
$todayPosts = array_filter($scheduledPosts, function($post) use ($today, $tomorrow) {
return $post['scheduled_time'] >= $today && $post['scheduled_time'] < $tomorrow;
});
// عدد المنشورات المراد إنشاؤها - Number of posts to create
$postsToCreate = $settings['daily_posts'] - count($todayPosts);
if ($postsToCreate <= 0) {
return false; // لا حاجة لإنشاء منشورات جديدة - No need to create new posts
}
// إنشاء وجدولة المنشورات الجديدة - Create and schedule new posts
$dayHours = 24; // ساعات اليوم - Hours in a day
$postInterval = $dayHours / $settings['daily_posts']; // الفاصل الزمني بين المنشورات بالساعات - Hours between posts
// تحديد أوقات النشر المتبقية - Determine remaining posting times
$postTimes = [];
$startTime = $today + (9 * 3600); // البداية من الساعة 9 صباحًا - Start at 9 AM
for ($i = 0; $i < $settings['daily_posts']; $i++) {
$postTime = $startTime + ($i * $postInterval * 3600);
// إضافة بعض العشوائية (±30 دقيقة) - Add some randomness (±30 minutes)
$postTime += mt_rand(-1800, 1800);
// التأكد من أن وقت النشر ليس في الماضي - Ensure posting time is not in the past
if ($postTime > time()) {
$postTimes[] = $postTime;
}
}
// إزالة أوقات النشر التي تم جدولتها بالفعل - Remove times that already have scheduled posts
foreach ($todayPosts as $post) {
foreach ($postTimes as $key => $time) {
// إذا كان هناك منشور مجدول ضمن ساعة من الوقت المقترح - If a post is scheduled within an hour of the suggested time
if (abs($post['scheduled_time'] - $time) < 3600) {
unset($postTimes[$key]);
break;
}
}
}
// إعادة ترتيب المصفوفة - Reindex array
$postTimes = array_values($postTimes);
// إنشاء المنشورات وجدولتها - Create posts and schedule them
$created = 0;
foreach ($postTimes as $time) {
if ($created >= $postsToCreate) {
break;
}
$content = createNewPost();
if ($content !== false) {
schedulePost($content, $time);
$created++;
}
}
return $created;
}
?>