PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / central / modules / comments.php

comments.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at central/modules/comments.php

843 lines 26.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.');
4
5 class UpdraftCentral_Comments_Commands extends UpdraftCentral_Commands {
6
7 /**
8 * The _search_comments function searches all available comments based
9 * on the following query parameters (type, status, search)
10 *
11 * Search Parameters/Filters:
12 * type - comment types can be 'comment', 'trackback' and 'pingback', defaults to 'comment'
13 * status - comment status can be 'hold' or unapprove, 'approve', 'spam', 'trash'
14 * search - user generated content or keyword
15 *
16 * @param array $query The query to search comments
17 * @return array
18 */
19 private function _search_comments($query) {
20
21 // Basic parameters to the query and should display
22 // the results in descending order (latest comments) first
23 // based on their generated IDs
24
25 $args = array(
26 'orderby' => 'ID',
27 'order' => 'DESC',
28 'type' => $query['type'],
29 'status' => $query['status'],
30 'search' => esc_attr($query['search']),
31 );
32
33 $query = new WP_Comment_Query;
34 $found_comments = $query->query($args);
35
36 $comments = array();
37 foreach ($found_comments as $comment) {
38
39 // We're returning a collection of comment in an array,
40 // in sync with the originator of the request on the ui side
41 // so, we're pulling it one by one into the array before
42 // returning it.
43
44 if (!in_array($comment, $comments)) {
45 array_push($comments, $comment);
46 }
47 }
48
49 return $comments;
50 }
51
52 /**
53 * The _calculate_pages function generates and builds the pagination links
54 * based on the current search parameters/filters. Please see _search_comments
55 * for the breakdown of these parameters.
56 *
57 * @param array $query Query to generate pagination links
58 * @return array
59 */
60 private function _calculate_pages($query) {
61 $per_page_options = array(10, 20, 30, 40, 50);
62
63 if (!empty($query)) {
64 if (!empty($query['search'])) {
65 return array(
66 'page_count' => 1,
67 'page_no' => 1
68 );
69 }
70
71 $pages = array();
72 $page_query = new WP_Comment_Query;
73
74 // Here, we're pulling the comments based on the
75 // two parameters namely type and status.
76 //
77 // The number of results/comments found will then
78 // be use to compute for the number of pages to be
79 // displayed as navigation links when browsing all
80 // comments from the frontend.
81
82 $comments = $page_query->query(array(
83 'type' => $query['type'],
84 'status' => $query['status']
85 ));
86
87 $total_comments = count($comments);
88 $page_count = ceil($total_comments / $query['per_page']);
89
90 if ($page_count > 1) {
91 for ($i = 0; $i < $page_count; $i++) {
92 if ($i + 1 == $query['page_no']) {
93 $paginator_item = array(
94 'value' => $i+1,
95 'setting' => 'disabled'
96 );
97 } else {
98 $paginator_item = array(
99 'value' => $i+1
100 );
101 }
102 array_push($pages, $paginator_item);
103 }
104
105 if ($query['page_no'] >= $page_count) {
106 $page_next = array(
107 'value' => $page_count,
108 'setting' => 'disabled'
109 );
110 } else {
111 $page_next = array(
112 'value' => $query['page_no'] + 1
113 );
114 }
115
116 if (1 === $query['page_no']) {
117 $page_prev = array(
118 'value' => 1,
119 'setting' => 'disabled'
120 );
121 } else {
122 $page_prev = array(
123 'value' => $query['page_no'] - 1
124 );
125 }
126
127 return array(
128 'page_no' => $query['page_no'],
129 'per_page' => $query['per_page'],
130 'page_count' => $page_count,
131 'pages' => $pages,
132 'page_next' => $page_next,
133 'page_prev' => $page_prev,
134 'total_results' => $total_comments,
135 'per_page_options' => $per_page_options
136 );
137
138 } else {
139 return array(
140 'page_no' => $query['page_no'],
141 'per_page' => $query['per_page'],
142 'page_count' => $page_count,
143 'total_results' => $total_comments,
144 'per_page_options' => $per_page_options
145 );
146 }
147 } else {
148 return array(
149 'per_page_options' => $per_page_options
150 );
151 }
152 }
153
154 /**
155 * The get_blog_sites function pulls blog sites available for the current WP instance.
156 * If Multisite is enabled on the server, then sites under the network will be pulled, otherwise, it will return an empty array.
157 *
158 * @return array
159 */
160 private function get_blog_sites() {
161
162 if (!is_multisite()) return array();
163
164 // Initialize array container
165 $sites = $network_sites = array();
166
167 // Check to see if latest get_sites (available on WP version >= 4.6) function is
168 // available to pull any available sites from the current WP instance. If not, then
169 // we're going to use the fallback function wp_get_sites (for older version).
170
171 if (function_exists('get_sites') && class_exists('WP_Site_Query')) {
172 $network_sites = get_sites();
173 } else {
174 if (function_exists('wp_get_sites')) {
175 $network_sites = wp_get_sites();
176 }
177 }
178
179 // We only process if sites array is not empty, otherwise, bypass
180 // the next block.
181
182 if (!empty($network_sites)) {
183 foreach ($network_sites as $site) {
184
185 // Here we're checking if the site type is an array, because
186 // we're pulling the blog_id property based on the type of
187 // site returned.
188 // get_sites returns an array of object, whereas the wp_get_sites
189 // function returns an array of array.
190
191 $blog_id = (is_array($site)) ? $site['blog_id'] : $site->blog_id;
192
193
194 // We're saving the blog_id and blog name as an associative item
195 // into the sites array, that will be used as "Sites" option in
196 // the frontend.
197
198 $sites[$blog_id] = get_blog_details($blog_id)->blogname;
199 }
200 }
201
202 return $sites;
203 }
204
205 /**
206 * The get_wp_option function pulls current blog options
207 * from the database using either following functions:
208 * - get_blog_option (for multisite)
209 * - get_option (for ordinary blog)
210 *
211 * @param array $blog_id This is the specific blog ID
212 * @param array $setting specifies settings
213 * @return array
214 */
215 private function _get_wp_option($blog_id, $setting) {
216 return is_multisite() ? get_blog_option($blog_id, $setting) : get_option($setting);
217 }
218
219 /**
220 * The get_comments function pull all the comments from the database
221 * based on the current search parameters/filters. Please see _search_comments
222 * for the breakdown of these parameters.
223 *
224 * @param array $query Specific query to pull comments
225 * @return array
226 */
227 public function get_comments($query) {
228
229 // Here, we're getting the current blog id. If blog id
230 // is passed along with the parameters then we override
231 // that current (default) value with the parameter blog id value.
232
233 $blog_id = get_current_blog_id();
234 if (isset($query['blog_id'])) $blog_id = $query['blog_id'];
235
236
237 // Here, we're switching to the actual blog that we need
238 // to pull comments from.
239
240 $switched = false;
241 if (function_exists('switch_to_blog')) {
242 $switched = switch_to_blog($blog_id);
243 }
244
245 if (!empty($query['search'])) {
246 // If a search keyword is present, then we'll call the _search_comments
247 // function to process the query.
248
249 $comments = $this->_search_comments($query);
250 } else {
251 // Set default parameter values if the designated
252 // parameters are empty.
253
254 if (empty($query['per_page'])) {
255 $query['per_page'] = 10;
256 }
257 if (empty($query['page_no'])) {
258 $query['page_no'] = 1;
259 }
260 if (empty($query['type'])) {
261 $query['type'] = '';
262 }
263 if (empty($query['status'])) {
264 $query['status'] = '';
265 }
266
267 // Since WP_Comment_Query parameters doesn't have a "page" attribute, we
268 // need to compute for the offset to get the exact content based on the
269 // current page and the number of items per page.
270
271 $offset = ((int) $query['page_no'] - 1) * (int) $query['per_page'];
272 $args = array(
273 'orderby' => 'ID',
274 'order' => 'DESC',
275 'number' => $query['per_page'],
276 'offset' => $offset,
277 'type' => $query['type'],
278 'status' => $query['status']
279 );
280
281 $comments_query = new WP_Comment_Query;
282 $comments = $comments_query->query($args);
283 }
284
285 // If no comments are found based on the current query then
286 // we return with error.
287
288 if (empty($comments)) {
289 $result = array('message' => 'comments_not_found');
290 return $this->_response($result);
291 }
292
293 // Otherwise, we're going to process each comment
294 // before we return it to the one issuing the request.
295 //
296 // Process in the sense that we add additional related info
297 // such as the post tile where the comment belongs to, the
298 // comment status, a formatted date field, and to which parent comment
299 // does the comment was intended to be as a reply.
300
301 foreach ($comments as &$comment) {
302 $comment = get_comment($comment->comment_ID, ARRAY_A);
303 if ($comment) {
304 $post = get_post($comment['comment_post_ID']);
305
306 if ($post) $comment['in_response_to'] = $post->post_title;
307 if (!empty($comment['comment_parent'])) {
308 $parent_comment = get_comment($comment['comment_parent'], ARRAY_A);
309 if ($parent_comment) $comment['in_reply_to'] = $parent_comment['comment_author'];
310 }
311
312 // We're formatting the comment_date to be exactly the same
313 // with that of WP Comments table (e.g. 2016/12/21 at 10:30 PM)
314
315 $comment['comment_date'] = date('Y/m/d \a\t g:i a', strtotime($comment['comment_date']));
316
317 $status = wp_get_comment_status($comment['comment_ID']);
318 if ($status) {
319 $comment['comment_status'] = $status;
320 }
321 }
322 }
323
324 // We return the following to the one issuing
325 // the request.
326
327 $result = array(
328 'comments' => $comments,
329 'paging' => $this->_calculate_pages($query)
330 );
331
332
333 // Here, we're restoring to the current (default) blog before we
334 // do the switched.
335
336 if (function_exists('restore_current_blog') && $switched) {
337 restore_current_blog();
338 }
339
340 return $this->_response($result);
341 }
342
343 /**
344 * The get_comment_filters function builds a array of options
345 * to be use as filters for the search function on the frontend.
346 */
347 public function get_comment_filters() {
348 // Options for comment_types field
349 $comment_types = apply_filters('admin_comment_types_dropdown', array(
350 'comment' => __('Comments'),
351 'pings' => __('Pings'),
352 ));
353
354 // Options for comment_status field
355 $comment_statuses = array(
356 'approve' => __('Approve'),
357 'hold' => __('Hold or Unapprove'),
358 'trash' => __('Trash'),
359 'spam' => __('Spam'),
360 );
361
362 // Pull sites options if available.
363 $sites = $this->get_blog_sites();
364
365 $result = array(
366 'sites' => $sites,
367 'types' => $comment_types,
368 'statuses' => $comment_statuses,
369 'paging' => $this->_calculate_pages(null),
370 );
371
372 return $this->_response($result);
373 }
374
375 /**
376 * The get_settings function pulls the current discussion settings
377 * option values.
378 *
379 * @param array $params Passing specific params for getting current discussion settings
380 * @return array
381 */
382 public function get_settings($params) {
383 global $updraftcentral_main;
384
385 // Here, we're getting the current blog id. If blog id
386 // is passed along with the parameters then we override
387 // that current (default) value with the parameter blog id value.
388
389 $blog_id = get_current_blog_id();
390 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
391
392
393 // If user does not have sufficient privileges to manage and edit
394 // WP options then we return with error.
395
396 if (!current_user_can_for_blog($blog_id, 'manage_options')) {
397 $result = array('error' => true, 'message' => 'insufficient_permission');
398 return $this->_response($result);
399 }
400
401 // Pull sites options if available.
402 $sites = $this->get_blog_sites();
403
404 // Wrap current discussion settings values into an array item
405 // named settings.
406
407 $result = array(
408 'settings' => array(
409 'default_pingback_flag' => $this->_get_wp_option($blog_id, 'default_pingback_flag'),
410 'default_ping_status' => $this->_get_wp_option($blog_id, 'default_ping_status'),
411 'default_comment_status' => $this->_get_wp_option($blog_id, 'default_comment_status'),
412 'require_name_email' => $this->_get_wp_option($blog_id, 'require_name_email'),
413 'comment_registration' => $this->_get_wp_option($blog_id, 'comment_registration'),
414 'close_comments_for_old_posts' => $this->_get_wp_option($blog_id, 'close_comments_for_old_posts'),
415 'close_comments_days_old' => $this->_get_wp_option($blog_id, 'close_comments_days_old'),
416 'thread_comments' => $this->_get_wp_option($blog_id, 'thread_comments'),
417 'thread_comments_depth' => $this->_get_wp_option($blog_id, 'thread_comments_depth'),
418 'page_comments' => $this->_get_wp_option($blog_id, 'page_comments'),
419 'comments_per_page' => $this->_get_wp_option($blog_id, 'comments_per_page'),
420 'default_comments_page' => $this->_get_wp_option($blog_id, 'default_comments_page'),
421 'comment_order' => $this->_get_wp_option($blog_id, 'comment_order'),
422 'comments_notify' => $this->_get_wp_option($blog_id, 'comments_notify'),
423 'moderation_notify' => $this->_get_wp_option($blog_id, 'moderation_notify'),
424 'comment_moderation' => $this->_get_wp_option($blog_id, 'comment_moderation'),
425 'comment_max_links' => $this->_get_wp_option($blog_id, 'comment_max_links'),
426 'moderation_keys' => $this->_get_wp_option($blog_id, 'moderation_keys'),
427 ),
428 'sites' => $sites,
429 );
430
431 $wp_version = $updraftcentral_main->get_wordpress_version();
432 if (version_compare($wp_version, '5.5.0', '<')) {
433 $result['settings']['comment_whitelist'] = $this->_get_wp_option($blog_id, 'comment_whitelist');
434 $result['settings']['blacklist_keys'] = $this->_get_wp_option($blog_id, 'blacklist_keys');
435 } else {
436 $result['settings']['comment_previously_approved'] = $this->_get_wp_option($blog_id, 'comment_previously_approved');
437 $result['settings']['disallowed_keys'] = $this->_get_wp_option($blog_id, 'disallowed_keys');
438 }
439
440 return $this->_response($result);
441 }
442
443 /**
444 * The update_settings function updates the discussion settings
445 * basing on the user generated content/option from the frontend
446 * form.
447 *
448 * @param array $params Specific params to update settings based on discussion
449 * @return array
450 */
451 public function update_settings($params) {
452
453 // Extract settings values from passed parameters.
454 $settings = $params['settings'];
455
456 // Here, we're getting the current blog id. If blog id
457 // is passed along with the parameters then we override
458 // that current (default) value with the parameter blog id value.
459
460 $blog_id = get_current_blog_id();
461 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
462
463
464 // If user does not have sufficient privileges to manage and edit
465 // WP options then we return with error.
466
467 if (!current_user_can_for_blog($blog_id, 'manage_options')) {
468 $result = array('error' => true, 'message' => 'insufficient_permission');
469 return $this->_response($result);
470 }
471
472 // Here, we're sanitizing the input fields before we save them to the database
473 // for safety and security reason. The "explode" and "implode" functions are meant
474 // to maintain the line breaks associated with a textarea input/value.
475
476 foreach ($settings as $key => $value) {
477
478 // We're using update_blog_option and update_option altogether to update the current
479 // discussion settings.
480
481 if (is_multisite()) {
482 update_blog_option($blog_id, $key, implode("\n", array_map('sanitize_text_field', explode("\n", $value))));
483 } else {
484 update_option($key, implode("\n", array_map('sanitize_text_field', explode("\n", $value))));
485 }
486 }
487
488 // We're not checking for errors here, but instead we're directly returning a success (error = false)
489 // status always, because WP's update_option will return fail if values were not changed, meaning
490 // previous values were not changed by the user's current request, not an actual exception thrown.
491 // Thus, giving a false positive message or report to the frontend.
492
493 $result = array('error' => false, 'message' => 'settings_updated', 'values' => array());
494 return $this->_response($result);
495 }
496
497 /**
498 * The get_comment function pulls a single comment based
499 * on a comment ID.
500 *
501 * @param array $params Specific params for getting a single comment
502 * @return array
503 */
504 public function get_comment($params) {
505
506 // Here, we're getting the current blog id. If blog id
507 // is passed along with the parameters then we override
508 // that current (default) value with the parameter blog id value.
509
510 $blog_id = get_current_blog_id();
511 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
512
513
514 // If user does not have sufficient privileges to moderate or edit
515 // a comment then we return with error.
516
517 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
518 $result = array('error' => true, 'message' => 'insufficient_permission');
519 return $this->_response($result);
520 }
521
522 // Here, we're switching to the actual blog that we need
523 // to pull comments from.
524
525 $switched = false;
526 if (function_exists('switch_to_blog')) {
527 $switched = switch_to_blog($blog_id);
528 }
529
530 // Get comment by comment_ID parameter and return result as an array.
531 $result = array(
532 'comment' => get_comment($params['comment_id'], ARRAY_A)
533 );
534
535
536 // Here, we're restoring to the current (default) blog before we
537 // do the switched.
538
539 if (function_exists('restore_current_blog') && $switched) {
540 restore_current_blog();
541 }
542
543 return $this->_response($result);
544 }
545
546 /**
547 * The reply_comment function creates a new comment as a reply
548 * to a certain/selected comment.
549 *
550 * @param array $params Specific params to create a new comment reply
551 * @return array
552 */
553 public function reply_comment($params) {
554
555 // Extract reply info from the passed parameters
556 $reply = $params['comment'];
557
558 // Here, we're getting the current blog id. If blog id
559 // is passed along with the parameters then we override
560 // that current (default) value with the parameter blog id value.
561
562 $blog_id = get_current_blog_id();
563 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
564
565
566 // If user does not have sufficient privileges to moderate or edit
567 // a comment then we return with error.
568
569 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
570 $result = array('error' => true, 'message' => 'comment_reply_no_permission');
571 return $this->_response($result);
572 }
573
574 // Here, we're switching to the actual blog that we need
575 // to apply our changes.
576
577 $switched = false;
578 if (function_exists('switch_to_blog')) {
579 $switched = switch_to_blog($blog_id);
580 }
581
582
583 // Get comment by comment_ID parameter.
584 $comment = get_comment($reply['comment_id']);
585 if ($comment) {
586
587 // Get the currently logged in user
588 $user = wp_get_current_user();
589
590 // If the current comment was not approved yet then
591 // we need to approve it before we create a reply to
592 // to the comment, mimicking exactly the WP behaviour
593 // in terms of creating a reply to a comment.
594
595 if (empty($comment->comment_approved)) {
596 $update_data = array(
597 'comment_ID' => $reply['comment_id'],
598 'comment_approved' => 1
599 );
600 wp_update_comment($update_data);
601 }
602
603 // Build new comment parameters based on current user info and
604 // the target comment for the reply.
605 $data = array(
606 'comment_post_ID' => $comment->comment_post_ID,
607 'comment_author' => $user->display_name,
608 'comment_author_email' => $user->user_email,
609 'comment_author_url' => $user->user_url,
610 'comment_content' => $reply['message'],
611 'comment_parent' => $reply['comment_id'],
612 'user_id' => $user->ID,
613 'comment_date' => current_time('mysql'),
614 'comment_approved' => 1
615 );
616
617 // Create new comment based on the parameters above, and return
618 // the status accordingly.
619
620 if (wp_insert_comment($data)) {
621 $result = array('error' => false, 'message' => 'comment_replied_with_comment_author', 'values' => array($comment->comment_author));
622 } else {
623 $result = array('error' => true, 'message' => 'comment_reply_failed_with_error', 'values' => array($comment->comment_ID));
624 }
625 } else {
626 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($reply['comment_id']));
627 }
628
629
630 // Here, we're restoring to the current (default) blog before we
631 // do the switched.
632
633 if (function_exists('restore_current_blog') && $switched) {
634 restore_current_blog();
635 }
636
637 return $this->_response($result);
638 }
639
640 /**
641 * The edit_comment function saves new information for the
642 * currently selected comment.
643 *
644 * @param array $params Specific params for editing a comment
645 * @return array
646 */
647 public function edit_comment($params) {
648
649 // Extract new comment info from the passed parameters
650 $comment = $params['comment'];
651
652 // Here, we're getting the current blog id. If blog id
653 // is passed along with the parameters then we override
654 // that current (default) value with the parameter blog id value.
655
656 $blog_id = get_current_blog_id();
657 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
658
659
660 // If user does not have sufficient privileges to moderate or edit
661 // a comment then we return with error.
662
663 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
664 $result = array('error' => true, 'message' => 'comment_edit_no_permission');
665 return $this->_response($result);
666 }
667
668 // Here, we're switching to the actual blog that we need
669 // to apply our changes.
670
671 $switched = false;
672 if (function_exists('switch_to_blog')) {
673 $switched = switch_to_blog($blog_id);
674 }
675
676
677 // Get current comment details
678 $original_comment = get_comment($comment['comment_id']);
679 if ($original_comment) {
680 $data = array();
681
682 // Replace "comment_id" with "comment_ID" since WP does not recognize
683 // the small case "id".
684 $comment['comment_ID'] = $original_comment->comment_ID;
685 unset($comment['comment_id']);
686
687 // Here, we're sanitizing the input fields before we save them to the database
688 // for safety and security reason. The "explode" and "implode" functions are meant
689 // to maintain the line breaks associated with a textarea input/value.
690
691 foreach ($comment as $key => $value) {
692 $data[$key] = implode("\n", array_map('sanitize_text_field', explode("\n", $value)));
693 }
694
695 // Update existing comment based on the passed parameter fields and
696 // return the status accordingly.
697
698 if (wp_update_comment($data)) {
699 $result = array('error' => false, 'message' => 'comment_edited_with_comment_author', 'values' => array($original_comment->comment_author));
700 } else {
701 $result = array('error' => true, 'message' => 'comment_edit_failed_with_error', 'values' => array($original_comment->comment_ID));
702 }
703 } else {
704 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($comment['comment_id']));
705 }
706
707 // Here, we're restoring to the current (default) blog before we
708 // do the switched.
709
710 if (function_exists('restore_current_blog') && $switched) {
711 restore_current_blog();
712 }
713
714 return $this->_response($result);
715 }
716
717 /**
718 * The update_comment_status function is a generic handler for the following
719 * comment actions:
720 *
721 * - approve comment
722 * - unapprove comment
723 * - set comment as spam
724 * - move comment to trash
725 * - delete comment permanently
726 * - unset comment as spam
727 * - restore comment
728 *
729 * @param array $params Specific params to update comment status
730 * @return array
731 */
732 public function update_comment_status($params) {
733
734 // Here, we're getting the current blog id. If blog id
735 // is passed along with the parameters then we override
736 // that current (default) value with the parameter blog id value.
737
738 $blog_id = get_current_blog_id();
739 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
740
741
742 // If user does not have sufficient privileges to moderate or edit
743 // a comment then we return with error.
744
745 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
746 $result = array('error' => true, 'message' => 'comment_change_status_no_permission');
747 return $this->_response($result);
748 }
749
750 // Here, we're switching to the actual blog that we need
751 // to apply our changes.
752
753 $switched = false;
754 if (function_exists('switch_to_blog')) {
755 $switched = switch_to_blog($blog_id);
756 }
757
758
759 // We make sure that we still have a valid comment from the server
760 // before we apply the currently selected action.
761
762 $comment = get_comment($params['comment_id']);
763 if ($comment) {
764 $post = get_post($comment->comment_post_ID);
765
766 if ($post) $comment->in_response_to = $post->post_title;
767 if (!empty($comment->comment_parent)) {
768 $parent_comment = get_comment($comment->comment_parent);
769 if ($parent_comment) $comment->in_reply_to = $parent_comment->comment_author;
770 }
771
772 // We're formatting the comment_date to be exactly the same
773 // with that of WP Comments table (e.g. 2016/12/21 at 10:30 PM)
774
775 $comment->comment_date = date('Y/m/d \a\t g:i a', strtotime($comment->comment_date));
776
777 $status = wp_get_comment_status($comment->comment_ID);
778 if ($status) {
779 $comment->comment_status = $status;
780 }
781
782 $succeeded = false;
783 $message = '';
784
785 // Here, we're using WP's wp_set_comment_status function to change the state
786 // of the selected comment based on the current action, except for the "delete" action
787 // where we use the wp_delete_comment to delete the comment permanently by passing
788 // "true" to the second argument.
789
790 switch ($params['action']) {
791 case 'approve':
792 $succeeded = wp_set_comment_status($params['comment_id'], 'approve');
793 $message = 'comment_approve_with_comment_author';
794 break;
795 case 'unapprove':
796 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
797 $message = 'comment_unapprove_with_comment_author';
798 break;
799 case 'spam':
800 $succeeded = wp_set_comment_status($params['comment_id'], 'spam');
801 $message = 'comment_spam_with_comment_author';
802 break;
803 case 'trash':
804 $succeeded = wp_set_comment_status($params['comment_id'], 'trash');
805 $message = 'comment_trash_with_comment_author';
806 break;
807 case 'delete':
808 $succeeded = wp_delete_comment($params['comment_id'], true);
809 $message = 'comment_delete_with_comment_author';
810 break;
811 case 'notspam':
812 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
813 $message = 'comment_not_spam_with_comment_author';
814 break;
815 case 'restore':
816 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
817 $message = 'comment_restore_with_comment_author';
818 break;
819 }
820
821 // If the current action succeeded, then we return a success message, otherwise,
822 // we return an error message to the user issuing the request.
823
824 if ($succeeded) {
825 $result = array('error' => false, 'message' => $message, 'values' => array($comment->comment_author), 'status' => $comment->comment_status, 'approved' => $comment->comment_approved);
826 } else {
827 $result = array('error' => true, 'message' => 'comment_change_status_failed_with_error', 'values' => array($comment->comment_ID));
828 }
829 } else {
830 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($params['comment_id']));
831 }
832
833 // Here, we're restoring to the current (default) blog before we
834 // do the switched.
835
836 if (function_exists('restore_current_blog') && $switched) {
837 restore_current_blog();
838 }
839
840 return $this->_response($result);
841 }
842 }
843