-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv1
461 lines (393 loc) · 19.4 KB
/
v1
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/*
Plugin Name: Comment Scheduler with Replies
Description: Schedule comments and admin replies to be posted on specific dates and times.
Version: 1.7
Author: Your Name
*/
// === Create the database table on plugin activation ===
function create_scheduled_comments_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id INT NOT NULL AUTO_INCREMENT,
post_id INT NOT NULL,
parent_comment_id INT DEFAULT NULL,
commenter_name VARCHAR(255) NOT NULL,
commenter_email VARCHAR(255) NOT NULL,
comment TEXT NOT NULL,
comment_date DATETIME NOT NULL,
status ENUM('pending', 'posted', 'reply_pending', 'replied') DEFAULT 'pending',
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
// Register cron jobs
if (!wp_next_scheduled('post_scheduled_comments')) {
wp_schedule_event(time(), 'hourly', 'post_scheduled_comments');
}
if (!wp_next_scheduled('post_scheduled_replies')) {
wp_schedule_event(time(), 'hourly', 'post_scheduled_replies');
}
}
register_activation_hook(__FILE__, 'create_scheduled_comments_table');
// === Add admin menu ===
function comment_scheduler_menu() {
add_menu_page(
'زمانبندی کامنتها',
'زمانبندی کامنتها',
'manage_options',
'scheduled-comments',
'comment_scheduler_page',
'dashicons-schedule',
26
);
add_submenu_page(
'scheduled-comments',
'پاسخ به کامنتها',
'پاسخ به کامنتها',
'manage_options',
'reply-to-comments',
'reply_to_comments_page'
);
}
add_action('admin_menu', 'comment_scheduler_menu');
// === Admin Page: Comment Scheduler ===
function comment_scheduler_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['schedule_comment'])) {
check_admin_referer('schedule_comment_nonce'); // Check nonce
$post_id = intval($_POST['post_id']);
$commenter_name = sanitize_text_field($_POST['commenter_name']);
$commenter_email = sanitize_email($_POST['commenter_email']);
$comment = sanitize_textarea_field($_POST['comment']);
$comment_date = sanitize_text_field($_POST['comment_date']);
$comment_time = sanitize_text_field($_POST['comment_time']);
// Combine date and time into one datetime field
$comment_datetime = $comment_date . ' ' . $comment_time;
// Insert the comment into scheduled_comments table with status 'pending'
$wpdb->insert(
$table_name,
[
'post_id' => $post_id,
'commenter_name' => $commenter_name,
'commenter_email' => $commenter_email,
'comment' => $comment,
'comment_date' => $comment_datetime,
'status' => 'pending'
]
);
echo '<div class="updated"><p>کامنت با موفقیت زمانبندی شد.</p></div>';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
check_admin_referer('upload_csv_nonce'); // Check nonce
$csv_file = $_FILES['csv_file']['tmp_name'];
if (($handle = fopen($csv_file, 'r')) !== FALSE) {
$row = 0;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($row++ == 0) continue; // Skip header row
$post_id = intval($data[0]);
$parent_comment_id = intval($data[1]);
$commenter_name = sanitize_text_field($data[2]);
$commenter_email = sanitize_email($data[3]);
$comment = sanitize_textarea_field($data[4]);
$comment_date = sanitize_text_field($data[5]);
// Insert the comment into scheduled_comments table with status 'pending'
$wpdb->insert(
$table_name,
[
'post_id' => $post_id,
'parent_comment_id' => $parent_comment_id,
'commenter_name' => $commenter_name,
'commenter_email' => $commenter_email,
'comment' => $comment,
'comment_date' => $comment_date,
'status' => 'pending'
]
);
}
fclose($handle);
echo '<div class="updated"><p>کامنتها با موفقیت از فایل CSV وارد شدند.</p></div>';
} else {
echo '<div class="error"><p>خطا در باز کردن فایل CSV.</p></div>';
}
}
// Form for scheduling a comment
echo '<h1>زمانبندی کامنتها</h1>';
echo '<form method="post">';
wp_nonce_field('schedule_comment_nonce'); // Add nonce field
echo '<label for="post_id">شناسه پست:</label><br>';
echo '<input type="number" name="post_id" required><br><br>';
echo '<label for="commenter_name">نام نویسنده کامنت:</label><br>';
echo '<input type="text" name="commenter_name" required><br><br>';
echo '<label for="commenter_email">ایمیل نویسنده کامنت:</label><br>';
echo '<input type="email" name="commenter_email" required><br><br>';
echo '<label for="comment">متن کامنت:</label><br>';
echo '<textarea name="comment" required></textarea><br><br>';
echo '<label for="comment_date">تاریخ درج:</label><br>';
echo '<input type="date" name="comment_date" required><br><br>';
echo '<label for="comment_time">ساعت درج:</label><br>';
echo '<input type="time" name="comment_time" required><br><br>';
echo '<input type="submit" name="schedule_comment" value="زمانبندی کامنت">';
echo '</form>';
// Form for uploading CSV
echo '<h2>آپلود فایل CSV</h2>';
echo '<form method="post" enctype="multipart/form-data">';
wp_nonce_field('upload_csv_nonce'); // Add nonce field
echo '<input type="file" name="csv_file" accept=".csv" required><br><br>';
echo '<input type="submit" value="آپلود CSV">';
echo '</form>';
display_scheduled_comments_list();
}
// === Display Scheduled Comments List ===
function display_scheduled_comments_list() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
$results_per_page = isset($_GET['items_per_page']) ? intval($_GET['items_per_page']) : 50;
$current_page = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
$offset = ($current_page - 1) * $results_per_page;
$results = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name ORDER BY comment_date DESC LIMIT %d OFFSET %d",
$results_per_page,
$offset
));
$total_comments = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
echo '<h2>لیست کامنتهای زمانبندی شده</h2>';
if (!empty($results)) {
echo '<table style="width:100%; border-collapse: collapse; border: 1px solid #ccc; text-align: left;">';
echo '<thead>
<tr style="background-color: #f9f9f9;">
<th style="padding: 8px; border: 1px solid #ddd;">ردیف</th>
<th style="padding: 8px; border: 1px solid #ddd;">نام نویسنده</th>
<th style="padding: 8px; border: 1px solid #ddd;">ایمیل</th>
<th style="padding: 8px; border: 1px solid #ddd;">متن کامنت</th>
<th style="padding: 8px; border: 1px solid #ddd;">تاریخ و زمان درج</th>
<th style="padding: 8px; border: 1px solid #ddd;">شناسه پست</th>
<th style="padding: 8px; border: 1px solid #ddd;">کد کامنت</th>
<th style="padding: 8px; border: 1px solid #ddd;">عملیات</th>
</tr>
</thead><tbody>';
$row_number = $offset + 1;
foreach ($results as $row) {
$comment_id_wp = $wpdb->get_var($wpdb->prepare(
"SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_content = %s",
$row->post_id,
$row->comment
));
$comment_id_display = $comment_id_wp ? $comment_id_wp : 'نامشخص';
$comment_link = $comment_id_wp ? get_permalink($row->post_id) . '#comment-' . $comment_id_wp : '#';
$post_link = get_permalink($row->post_id);
echo '<tr>
<td style="padding: 8px; border: 1px solid #ddd;">' . $row_number++ . '</td>
<td style="padding: 8px; border: 1px solid #ddd;">' . esc_html($row->commenter_name) . '</td>
<td style="padding: 8px; border: 1px solid #ddd;">' . esc_html($row->commenter_email) . '</td>
<td style="padding: 8px; border: 1px solid #ddd;">' . esc_html($row->comment) . '</td>
<td style="padding: 8px; border: 1px solid #ddd;">' . esc_html($row->comment_date) . '</td>
<td style="padding: 8px; border: 1px solid #ddd;"><a href="' . esc_url($post_link) . '" target="_blank">' . esc_html($row->post_id) . '</a></td>
<td style="padding: 8px; border: 1px solid #ddd;"><a href="' . esc_url($comment_link) . '" target="_blank">' . esc_html($comment_id_display) . '</a></td>
<td style="padding: 8px; border: 1px solid #ddd;">
<form method="post" onsubmit="return confirm(\'آیا مطمئن هستید میخواهید این کامنت را حذف کنید؟\');">
<input type="hidden" name="delete_comment_id" value="' . esc_attr($row->id) . '">
' . wp_nonce_field('delete_comment_nonce', '_wpnonce', true, false) . '
<input type="submit" value="حذف">
</form>
</td>
</tr>';
}
echo '</tbody></table>';
// Pagination
$total_pages = ceil($total_comments / $results_per_page);
echo '<div style="margin-top: 20px;">';
for ($i = 1; $i <= $total_pages; $i++) {
$active = $i === $current_page ? 'font-weight: bold;' : '';
echo '<a href="?page=scheduled-comments&paged=' . $i . '&items_per_page=' . $results_per_page . '" style="margin-right: 10px; ' . $active . '">' . $i . '</a>';
}
echo '</div>';
// Items per page dropdown
echo '<form method="get" action="" style="margin-top: 20px;">';
echo '<input type="hidden" name="page" value="scheduled-comments">';
echo '<label for="items_per_page">تعداد موارد در هر صفحه:</label> ';
echo '<select name="items_per_page" onchange="this.form.submit()">';
echo '<option value="50"' . selected($results_per_page, 50, false) . '>50</option>';
echo '<option value="100"' . selected($results_per_page, 100, false) . '>100</option>';
echo '<option value="500"' . selected($results_per_page, 500, false) . '>500</option>';
echo '</select>';
echo '</form>';
// Copy table button
echo '<button onclick="copyTableToClipboard()" style="margin-top: 20px;">کپی جدول</button>';
echo '<script>
function copyTableToClipboard() {
const table = document.querySelector("table");
const range = document.createRange();
range.selectNode(table);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
selection.removeAllRanges();
alert("جدول کپی شد!");
}
</script>';
} else {
echo '<p>هیچ کامنت زمانبندی شدهای وجود ندارد.</p>';
}
// Handle comment deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_comment_id'])) {
if (check_admin_referer('delete_comment_nonce')) {
$delete_comment_id = intval($_POST['delete_comment_id']);
$wpdb->delete($table_name, ['id' => $delete_comment_id]);
echo '<div class="updated"><p>کامنت با موفقیت حذف شد.</p></div>';
}
}
}
// === Admin Page: Replies to Comments ===
function reply_to_comments_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_reply'])) {
check_admin_referer('reply_comment_nonce'); // Check nonce
$post_id = intval($_POST['post_id']);
$parent_comment_id = intval($_POST['parent_comment_id']);
$admin_name = sanitize_text_field($_POST['admin_name']);
$admin_email = sanitize_email($_POST['admin_email']);
$reply_content = sanitize_textarea_field($_POST['reply_content']);
$reply_date = sanitize_text_field($_POST['reply_date']);
$reply_time = sanitize_text_field($_POST['reply_time']);
$reply_datetime = $reply_date . ' ' . $reply_time;
$wpdb->insert(
$table_name,
[
'post_id' => $post_id,
'commenter_name' => $admin_name,
'commenter_email' => $admin_email,
'comment' => $reply_content,
'comment_date' => $reply_datetime,
'status' => 'reply_pending',
'parent_comment_id' => $parent_comment_id
]
);
echo '<div class="updated"><p>پاسخ زمانبندی شد و در صف ارسال قرار گرفت.</p></div>';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
check_admin_referer('upload_csv_nonce'); // Check nonce
$csv_file = $_FILES['csv_file']['tmp_name'];
if (($handle = fopen($csv_file, 'r')) !== FALSE) {
$row = 0;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($row++ == 0) continue; // Skip header row
$post_id = intval($data[0]);
$parent_comment_id = intval($data[1]);
$commenter_name = sanitize_text_field($data[2]);
$commenter_email = sanitize_email($data[3]);
$comment = sanitize_textarea_field($data[4]);
$comment_date = sanitize_text_field($data[5]);
// Insert the comment into scheduled_comments table with status 'reply_pending'
$wpdb->insert(
$table_name,
[
'post_id' => $post_id,
'parent_comment_id' => $parent_comment_id,
'commenter_name' => $commenter_name,
'commenter_email' => $commenter_email,
'comment' => $comment,
'comment_date' => $comment_date,
'status' => 'reply_pending'
]
);
}
fclose($handle);
echo '<div class="updated"><p>پاسخها با موفقیت از فایل CSV وارد شدند.</p></div>';
} else {
echo '<div class="error"><p>خطا در باز کردن فایل CSV.</p></div>';
}
}
echo '<h1>پاسخ به کامنتها</h1>';
echo '<form method="post" enctype="multipart/form-data">';
wp_nonce_field('upload_csv_nonce'); // Add nonce field
echo '<label for="csv_file">آپلود فایل CSV:</label><br>';
echo '<input type="file" name="csv_file" accept=".csv" required><br><br>';
echo '<input type="submit" value="آپلود CSV">';
echo '</form>';
echo '<form method="post" style="max-width: 600px; margin-top: 20px;">';
wp_nonce_field('reply_comment_nonce'); // Add nonce field
echo '<label for="post_id">شناسه پست:</label><br>';
echo '<input type="number" name="post_id" required><br><br>';
echo '<label for="parent_comment_id">شناسه کامنت اصلی:</label><br>';
echo '<input type="number" name="parent_comment_id" required><br><br>';
echo '<label for="admin_name">نام نویسنده پاسخ:</label><br>';
echo '<input type="text" name="admin_name" required value="Admin"><br><br>';
echo '<label for="admin_email">ایمیل نویسنده پاسخ:</label><br>';
echo '<input type="email" name="admin_email" required value="[email protected]"><br><br>';
echo '<label for="reply_content">متن پاسخ:</label><br>';
echo '<textarea name="reply_content" required></textarea><br><br>';
echo '<label for="reply_date">تاریخ پاسخ:</label><br>';
echo '<input type="date" name="reply_date" required><br><br>';
echo '<label for="reply_time">ساعت پاسخ:</label><br>';
echo '<input type="time" name="reply_time" required><br><br>';
echo '<input type="submit" name="submit_reply" value="زمانبندی پاسخ">';
echo '</form>';
}
// === Cron Job: Post Scheduled Comments ===
function post_scheduled_comments() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
$comments_table = $wpdb->prefix . 'comments';
$current_time = current_time('mysql');
$comments = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE status = 'pending' AND comment_date <= %s",
$current_time
));
foreach ($comments as $comment) {
$comment_id = wp_insert_comment([
'comment_post_ID' => $comment->post_id,
'comment_author' => $comment->commenter_name,
'comment_author_email' => $comment->commenter_email,
'comment_content' => $comment->comment,
'comment_date' => $comment->comment_date,
'comment_date_gmt' => get_gmt_from_date($comment->comment_date),
'comment_approved' => 1
]);
if ($comment_id) {
$wpdb->update(
$table_name,
['status' => 'posted'],
['id' => $comment->id]
);
}
}
}
add_action('post_scheduled_comments', 'post_scheduled_comments');
// === Cron Job: Post Scheduled Replies ===
function post_scheduled_replies() {
global $wpdb;
$table_name = $wpdb->prefix . 'scheduled_comments';
$comments_table = $wpdb->prefix . 'comments';
$current_time = current_time('mysql');
$replies = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE status = 'reply_pending' AND comment_date <= %s",
$current_time
));
foreach ($replies as $reply) {
$comment_id = wp_insert_comment([
'comment_post_ID' => $reply->post_id,
'comment_author' => $reply->commenter_name,
'comment_author_email' => $reply->commenter_email,
'comment_content' => $reply->comment,
'comment_date' => $reply->comment_date,
'comment_date_gmt' => get_gmt_from_date($reply->comment_date),
'comment_approved' => 1,
'comment_parent' => $reply->parent_comment_id
]);
if ($comment_id) {
$wpdb->update(
$table_name,
['status' => 'replied'],
['id' => $reply->id]
);
}
}
}
add_action('post_scheduled_replies', 'post_scheduled_replies');
?>