Repository navigation
Expand file tree
/
Copy pathrequest_handler.php
More file actions
437 lines (381 loc) · 14.6 KB
/
Copy pathrequest_handler.php
File metadata and controls
437 lines (381 loc) · 14.6 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
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
<?php
function cackle_i($text, $params = null) {
if (!is_array($params)) {
$params = func_get_args();
$params = array_slice($params, 1);
}
return vsprintf(__($text, 'cackle'), $params);
}
function cackle_normalize_comment_status($status) {
switch ((string) $status) {
case '1':
return 'approved';
case '0':
return 'pending';
case 'spam':
return 'spam';
case 'trash':
return 'deleted';
default:
return 'pending';
}
}
function cackle_send_error($message, $code = 400) {
wp_send_json_error(
array(
'message' => $message,
),
$code
);
}
function cackle_decode_request_payload() {
if (!isset($_POST['data'])) {
cackle_send_error(__('Missing request data.', 'cackle'));
}
$raw = wp_unslash($_POST['data']);
if (!is_string($raw)) {
cackle_send_error(__('Invalid request payload.', 'cackle'));
}
$payload = json_decode($raw, true);
if (!is_array($payload)) {
cackle_send_error(__('Unable to decode request payload.', 'cackle'));
}
return $payload;
}
// Main AJAX entry point; validates permissions and CSRF token before delegating heavy work.
function cackle_handle_request() {
if (!current_user_can('manage_options')) {
cackle_send_error(__('You do not have permission to perform this action.', 'cackle'), 403);
}
// Protect long running operations against CSRF.
check_ajax_referer('cackle_request', 'nonce');
$payload = cackle_decode_request_payload();
$action = isset($payload['cackleApi']) ? sanitize_text_field($payload['cackleApi']) : '';
switch ($action) {
case 'export':
cackle_handle_export($payload);
break;
case 'import_prepare':
cackle_handle_import_prepare($payload);
break;
case 'import':
cackle_handle_import($payload);
break;
case 'checkKeys':
cackle_handle_check_keys($payload);
break;
default:
cackle_send_error(__('Unknown action requested.', 'cackle'));
}
}
// Export WordPress comments to Cackle in manageable batches.
function cackle_handle_export(array $payload) {
global $wpdb, $cackle_api;
$offset = isset($payload['offset']) ? max(0, (int) $payload['offset']) : 0;
$timestamp = isset($payload['timestamp']) ? (int) $payload['timestamp'] : time();
$action = isset($payload['action']) ? sanitize_text_field($payload['action']) : '';
$post_id = isset($payload['post_id']) ? max(0, (int) $payload['post_id']) : 0;
$manual_export = get_option('cackle_manual_export', '');
if (!is_object($manual_export)) {
$manual_export = new stdClass();
$manual_export->status = 'export';
}
if ('export_start' === $action && isset($manual_export->status) && 'stop' === $manual_export->status) {
$manual_export->status = 'export';
update_option('cackle_manual_export', $manual_export);
wp_send_json(
array(
'result' => 'fail',
'timestamp' => $timestamp,
'status' => 'stopped',
'post_id' => $post_id,
'msg' => __('Export was previously stopped. Please try again.', 'cackle'),
'eof' => 1,
'response' => null,
)
);
}
$post = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type != %s AND post_status = %s AND comment_count > 0 AND ID > %d ORDER BY ID ASC LIMIT 1",
'revision',
'publish',
$post_id
)
);
if (!$post) {
wp_send_json(
array(
'result' => 'fail',
'timestamp' => $timestamp,
'status' => 'complete',
'post_id' => null,
'eof' => 1,
'response' => null,
'debug' => __('No posts found for export.', 'cackle'),
)
);
}
$post_id = (int) $post->ID;
$max_post_id = (int) $wpdb->get_var("SELECT MAX(ID) FROM {$wpdb->posts} WHERE post_type != 'revision' AND post_status = 'publish' AND comment_count > 0");
$eof = (int) ($post_id === $max_post_id);
$status = $eof ? 'complete' : 'partial';
if (!$eof) {
$manual_export->finish = false;
update_option('cackle_manual_export', $manual_export);
}
$comments = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_agent NOT LIKE %s ORDER BY comment_date ASC LIMIT %d OFFSET %d",
$post_id,
$wpdb->esc_like('Cackle:') . '%',
100,
$offset
)
);
$response = 'success';
$comments_pack_status = 'complete';
$comments_prepared = 0;
$fail_response = null;
$result = 'success';
if ($comments) {
$export_payload = array();
foreach ($comments as $comment) {
try {
$created = new DateTime($comment->comment_date);
} catch (Exception $exception) {
// Fallback to the GMT value if the stored date string is malformed.
$created = new DateTime('@' . strtotime($comment->comment_date_gmt));
}
$export_payload[] = array(
'id' => (int) $comment->comment_ID,
'ip' => $comment->comment_author_IP,
'status' => cackle_normalize_comment_status($comment->comment_approved),
'msg' => $comment->comment_content,
'created' => (int) $created->getTimestamp() * 1000,
'user' => ((int) $comment->user_id > 0) ? array(
'id' => (int) $comment->user_id,
'name' => $comment->comment_author,
'email' => $comment->comment_author_email,
) : null,
'parent' => (int) $comment->comment_parent,
'name' => ((int) $comment->user_id === 0) ? $comment->comment_author : null,
'email' => ((int) $comment->user_id === 0) ? $comment->comment_author_email : null,
);
}
$api_response = $cackle_api->import_wordpress_comments($export_payload, $post, (bool) $eof);
$decoded_response = json_decode($api_response, true);
$fail_response = $decoded_response;
$response = (isset($decoded_response['responseApi']['status']) && 'ok' === $decoded_response['responseApi']['status']) ? 'success' : 'fail';
$result = $response;
$comments_pack_status = 'partial';
$comments_prepared = count($export_payload);
}
if ('success' === $response) {
if ($eof) {
$manual_export->finish = true;
}
$manual_export->last_post_id = $post_id;
$manual_export->last_offset = ($comments_pack_status === 'complete') ? 0 : $offset;
update_option('cackle_manual_export', $manual_export);
}
wp_send_json(
array(
'result' => $result,
'timestamp' => $timestamp,
'status' => $status,
'post_id' => $post_id,
'eof' => $eof,
'response' => $response,
'debug' => null,
'export' => 'export',
'fail_response' => $fail_response,
'comments_pack_status' => $comments_pack_status,
'comments_prepared' => $comments_prepared,
)
);
}
// Prepare the local database before a bulk import from Cackle.
function cackle_handle_import_prepare(array $payload) {
global $wpdb, $cackle_api;
$offset = isset($payload['offset']) ? max(0, (int) $payload['offset']) : 0;
$page = (int) floor($offset / 100);
$manual_sync = get_option('cackle_manual_sync', '');
if (!is_object($manual_sync)) {
$manual_sync = new stdClass();
$manual_sync->status = 'sync';
}
if (0 === $offset) {
$wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE meta_key IN ('cackle_post_id', 'cackle_parent_post_id')");
$wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_agent LIKE 'Cackle:%'");
$wpdb->query("DELETE FROM {$wpdb->prefix}cackle_channel");
delete_option('cackle_monitor');
delete_option('cackle_monitor_short');
delete_option('cackle_modified_trigger');
delete_option('cackle_posts_update');
delete_option('cackle_channel_modified_trigger');
}
if (!get_option('cackle_monitor')) {
$object = new stdClass();
$object->post_id = 0;
$object->time = 0;
$object->mode = 'by_channel';
$object->status = 'finish';
$object->counter = 0;
update_option('cackle_monitor', $object);
}
if (!get_option('cackle_monitor_short')) {
$object = new stdClass();
$object->post_id = 0;
$object->time = 0;
$object->mode = 'by_channel';
$object->status = 'finish';
update_option('cackle_monitor_short', $object);
}
if (!get_option('cackle_modified_trigger')) {
update_option('cackle_modified_trigger', new stdClass());
}
if (!get_option('cackle_posts_update')) {
update_option('cackle_posts_update', new stdClass());
}
$raw_channels = $cackle_api->get_all_channels(100, $page);
if (!$raw_channels) {
cackle_send_error(__('Unable to fetch channel list from Cackle API.', 'cackle'));
}
$response = json_decode($raw_channels, true);
$channels = (isset($response['chans']) && is_array($response['chans'])) ? $response['chans'] : array();
$monitor = get_option('cackle_monitor');
$monitor->post_id = 0;
$monitor->status = 'inprocess';
$monitor->mode = 'all_comments';
$monitor->time = time();
update_option('cackle_monitor', $monitor);
if (!empty($channels)) {
foreach ($channels as $chan) {
$channel_id = isset($chan['channel']) ? (int) $chan['channel'] : 0;
if (0 === $channel_id) {
continue;
}
$post = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type != %s AND post_status = %s AND ID = %d ORDER BY ID ASC LIMIT 1",
'revision',
'publish',
$channel_id
)
);
if (!$post) {
continue;
}
$existing = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}cackle_channel WHERE id = %d ORDER BY ID ASC LIMIT 1",
$post->ID
)
);
if (!$existing) {
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}cackle_channel (id, time) VALUES (%d, %d) ON DUPLICATE KEY UPDATE time = VALUES(time)",
$post->ID,
0
)
);
}
}
wp_send_json(
array(
'status' => 'partial',
'channels_prepared' => count($channels),
)
);
}
wp_send_json(
array(
'status' => 'complete',
'channels_prepared' => 0,
)
);
}
// Synchronise a single comment thread from Cackle back into WordPress.
function cackle_handle_import(array $payload) {
global $wpdb, $cackle_api;
$post_id = isset($payload['post_id']) ? max(0, (int) $payload['post_id']) : 0;
$manual_sync = get_option('cackle_manual_sync', '');
if (!is_object($manual_sync)) {
$manual_sync = new stdClass();
$manual_sync->status = 'sync';
}
if (0 === $post_id) {
update_option('cackle_channel_modified_first', time() * 1000);
}
$post = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}cackle_channel WHERE id > %d ORDER BY ID ASC LIMIT 1",
$post_id
)
);
if (!$post) {
wp_send_json(
array(
'result' => 'success',
'timestamp' => time(),
'status' => 'complete',
'post_id' => $post_id,
'msg' => __('No channels available for synchronization.', 'cackle'),
'eof' => 1,
'response' => null,
)
);
}
$post_id = (int) $post->id;
$max_post_id = (int) $wpdb->get_var("SELECT MAX(id) FROM {$wpdb->prefix}cackle_channel");
$eof = (int) ($post_id === $max_post_id);
$status = $eof ? 'complete' : 'partial';
$msg = $eof ? __('Your comments have been synchronized with Cackle!', 'cackle') : cackle_i('Processed comments on post #%s…', $post_id);
$manual_sync->finish = $eof;
if (!$eof) {
update_option('cackle_manual_sync', $manual_sync);
}
$sync = new Sync();
$response = $sync->init($post_id, 'all_comments');
$result = ('success' === $response) ? 'success' : 'fail';
if ('success' === $result) {
$manual_sync->last_post_id = $post_id;
if ($eof) {
$object = new stdClass();
$object->mode = 'by_channel';
$object->post_id = 0;
$object->status = 'finish';
$object->time = time();
update_option('cackle_monitor', $object);
update_option('cackle_manual_sync', $manual_sync);
} else {
$manual_sync->finish = false;
update_option('cackle_manual_sync', $manual_sync);
}
} else {
$msg = '<p class="status cackle-export-fail">' . cackle_i('Sorry, something happened with the export. Please <a href="#" id="cackle_export_retry">try again</a></p><p>If your API key has changed, you may need to reinstall Cackle (deactivate the plugin and then reactivate it). If you are still having issues, refer to the <a href="%s" onclick="window.open(this.href); return false">WordPress help page</a>.', 'https://cackle.me/help/') . '</p>';
}
wp_send_json(
array(
'result' => $result,
'timestamp' => time(),
'status' => $status,
'post_id' => $post_id,
'msg' => $msg,
'eof' => $eof,
'response' => $response,
'import' => 'import',
'fail_response' => ('success' === $result) ? null : $response,
)
);
}
function cackle_handle_check_keys(array $payload) {
if (!isset($payload['value']) || !is_array($payload['value'])) {
cackle_send_error(__('Missing activation data.', 'cackle'));
}
require_once __DIR__ . '/cackle_activation.php';
$resp = CackleActivation::check($payload['value']);
wp_send_json($resp);
}