PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.16.5
UpdraftPlus: WP Backup & Migration Plugin v1.16.5
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / central / modules / comments.php

comments.php in UpdraftPlus: WP Backup & Migration Plugin 1.16.5, at central/modules/comments.php

835 lines 26.5 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
384 // Here, we're getting the current blog id. If blog id
385 // is passed along with the parameters then we override
386 // that current (default) value with the parameter blog id value.
387
388 $blog_id = get_current_blog_id();
389 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
390
391
392 // If user does not have sufficient privileges to manage and edit
393 // WP options then we return with error.
394
395 if (!current_user_can_for_blog($blog_id, 'manage_options')) {
396 $result = array('error' => true, 'message' => 'insufficient_permission');
397 return $this->_response($result);
398 }
399
400 // Pull sites options if available.
401 $sites = $this->get_blog_sites();
402
403 // Wrap current discussion settings values into an array item
404 // named settings.
405
406 $result = array(
407 'settings' => array(
408 'default_pingback_flag' => $this->_get_wp_option($blog_id, 'default_pingback_flag'),
409 'default_ping_status' => $this->_get_wp_option($blog_id, 'default_ping_status'),
410 'default_comment_status' => $this->_get_wp_option($blog_id, 'default_comment_status'),
411 'require_name_email' => $this->_get_wp_option($blog_id, 'require_name_email'),
412 'comment_registration' => $this->_get_wp_option($blog_id, 'comment_registration'),
413 'close_comments_for_old_posts' => $this->_get_wp_option($blog_id, 'close_comments_for_old_posts'),
414 'close_comments_days_old' => $this->_get_wp_option($blog_id, 'close_comments_days_old'),
415 'thread_comments' => $this->_get_wp_option($blog_id, 'thread_comments'),
416 'thread_comments_depth' => $this->_get_wp_option($blog_id, 'thread_comments_depth'),
417 'page_comments' => $this->_get_wp_option($blog_id, 'page_comments'),
418 'comments_per_page' => $this->_get_wp_option($blog_id, 'comments_per_page'),
419 'default_comments_page' => $this->_get_wp_option($blog_id, 'default_comments_page'),
420 'comment_order' => $this->_get_wp_option($blog_id, 'comment_order'),
421 'comments_notify' => $this->_get_wp_option($blog_id, 'comments_notify'),
422 'moderation_notify' => $this->_get_wp_option($blog_id, 'moderation_notify'),
423 'comment_moderation' => $this->_get_wp_option($blog_id, 'comment_moderation'),
424 'comment_whitelist' => $this->_get_wp_option($blog_id, 'comment_whitelist'),
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 'blacklist_keys' => $this->_get_wp_option($blog_id, 'blacklist_keys'),
428 ),
429 'sites' => $sites,
430 );
431
432 return $this->_response($result);
433 }
434
435 /**
436 * The update_settings function updates the discussion settings
437 * basing on the user generated content/option from the frontend
438 * form.
439 *
440 * @param array $params Specific params to update settings based on discussion
441 * @return array
442 */
443 public function update_settings($params) {
444
445 // Extract settings values from passed parameters.
446 $settings = $params['settings'];
447
448 // Here, we're getting the current blog id. If blog id
449 // is passed along with the parameters then we override
450 // that current (default) value with the parameter blog id value.
451
452 $blog_id = get_current_blog_id();
453 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
454
455
456 // If user does not have sufficient privileges to manage and edit
457 // WP options then we return with error.
458
459 if (!current_user_can_for_blog($blog_id, 'manage_options')) {
460 $result = array('error' => true, 'message' => 'insufficient_permission');
461 return $this->_response($result);
462 }
463
464 // Here, we're sanitizing the input fields before we save them to the database
465 // for safety and security reason. The "explode" and "implode" functions are meant
466 // to maintain the line breaks associated with a textarea input/value.
467
468 foreach ($settings as $key => $value) {
469
470 // We're using update_blog_option and update_option altogether to update the current
471 // discussion settings.
472
473 if (is_multisite()) {
474 update_blog_option($blog_id, $key, implode("\n", array_map('sanitize_text_field', explode("\n", $value))));
475 } else {
476 update_option($key, implode("\n", array_map('sanitize_text_field', explode("\n", $value))));
477 }
478 }
479
480 // We're not checking for errors here, but instead we're directly returning a success (error = false)
481 // status always, because WP's update_option will return fail if values were not changed, meaning
482 // previous values were not changed by the user's current request, not an actual exception thrown.
483 // Thus, giving a false positive message or report to the frontend.
484
485 $result = array('error' => false, 'message' => 'settings_updated', 'values' => array());
486 return $this->_response($result);
487 }
488
489 /**
490 * The get_comment function pulls a single comment based
491 * on a comment ID.
492 *
493 * @param array $params Specific params for getting a single comment
494 * @return array
495 */
496 public function get_comment($params) {
497
498 // Here, we're getting the current blog id. If blog id
499 // is passed along with the parameters then we override
500 // that current (default) value with the parameter blog id value.
501
502 $blog_id = get_current_blog_id();
503 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
504
505
506 // If user does not have sufficient privileges to moderate or edit
507 // a comment then we return with error.
508
509 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
510 $result = array('error' => true, 'message' => 'insufficient_permission');
511 return $this->_response($result);
512 }
513
514 // Here, we're switching to the actual blog that we need
515 // to pull comments from.
516
517 $switched = false;
518 if (function_exists('switch_to_blog')) {
519 $switched = switch_to_blog($blog_id);
520 }
521
522 // Get comment by comment_ID parameter and return result as an array.
523 $result = array(
524 'comment' => get_comment($params['comment_id'], ARRAY_A)
525 );
526
527
528 // Here, we're restoring to the current (default) blog before we
529 // do the switched.
530
531 if (function_exists('restore_current_blog') && $switched) {
532 restore_current_blog();
533 }
534
535 return $this->_response($result);
536 }
537
538 /**
539 * The reply_comment function creates a new comment as a reply
540 * to a certain/selected comment.
541 *
542 * @param array $params Specific params to create a new comment reply
543 * @return array
544 */
545 public function reply_comment($params) {
546
547 // Extract reply info from the passed parameters
548 $reply = $params['comment'];
549
550 // Here, we're getting the current blog id. If blog id
551 // is passed along with the parameters then we override
552 // that current (default) value with the parameter blog id value.
553
554 $blog_id = get_current_blog_id();
555 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
556
557
558 // If user does not have sufficient privileges to moderate or edit
559 // a comment then we return with error.
560
561 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
562 $result = array('error' => true, 'message' => 'comment_reply_no_permission');
563 return $this->_response($result);
564 }
565
566 // Here, we're switching to the actual blog that we need
567 // to apply our changes.
568
569 $switched = false;
570 if (function_exists('switch_to_blog')) {
571 $switched = switch_to_blog($blog_id);
572 }
573
574
575 // Get comment by comment_ID parameter.
576 $comment = get_comment($reply['comment_id']);
577 if ($comment) {
578
579 // Get the currently logged in user
580 $user = wp_get_current_user();
581
582 // If the current comment was not approved yet then
583 // we need to approve it before we create a reply to
584 // to the comment, mimicking exactly the WP behaviour
585 // in terms of creating a reply to a comment.
586
587 if (empty($comment->comment_approved)) {
588 $update_data = array(
589 'comment_ID' => $reply['comment_id'],
590 'comment_approved' => 1
591 );
592 wp_update_comment($update_data);
593 }
594
595 // Build new comment parameters based on current user info and
596 // the target comment for the reply.
597 $data = array(
598 'comment_post_ID' => $comment->comment_post_ID,
599 'comment_author' => $user->display_name,
600 'comment_author_email' => $user->user_email,
601 'comment_author_url' => $user->user_url,
602 'comment_content' => $reply['message'],
603 'comment_parent' => $reply['comment_id'],
604 'user_id' => $user->ID,
605 'comment_date' => current_time('mysql'),
606 'comment_approved' => 1
607 );
608
609 // Create new comment based on the parameters above, and return
610 // the status accordingly.
611
612 if (wp_insert_comment($data)) {
613 $result = array('error' => false, 'message' => 'comment_replied_with_comment_author', 'values' => array($comment->comment_author));
614 } else {
615 $result = array('error' => true, 'message' => 'comment_reply_failed_with_error', 'values' => array($comment->comment_ID));
616 }
617 } else {
618 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($reply['comment_id']));
619 }
620
621
622 // Here, we're restoring to the current (default) blog before we
623 // do the switched.
624
625 if (function_exists('restore_current_blog') && $switched) {
626 restore_current_blog();
627 }
628
629 return $this->_response($result);
630 }
631
632 /**
633 * The edit_comment function saves new information for the
634 * currently selected comment.
635 *
636 * @param array $params Specific params for editing a coment
637 * @return array
638 */
639 public function edit_comment($params) {
640
641 // Extract new comment info from the passed parameters
642 $comment = $params['comment'];
643
644 // Here, we're getting the current blog id. If blog id
645 // is passed along with the parameters then we override
646 // that current (default) value with the parameter blog id value.
647
648 $blog_id = get_current_blog_id();
649 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
650
651
652 // If user does not have sufficient privileges to moderate or edit
653 // a comment then we return with error.
654
655 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
656 $result = array('error' => true, 'message' => 'comment_edit_no_permission');
657 return $this->_response($result);
658 }
659
660 // Here, we're switching to the actual blog that we need
661 // to apply our changes.
662
663 $switched = false;
664 if (function_exists('switch_to_blog')) {
665 $switched = switch_to_blog($blog_id);
666 }
667
668
669 // Get current comment details
670 $original_comment = get_comment($comment['comment_id']);
671 if ($original_comment) {
672 $data = array();
673
674 // Replace "comment_id" with "comment_ID" since WP does not recognize
675 // the small case "id".
676 $comment['comment_ID'] = $original_comment->comment_ID;
677 unset($comment['comment_id']);
678
679 // Here, we're sanitizing the input fields before we save them to the database
680 // for safety and security reason. The "explode" and "implode" functions are meant
681 // to maintain the line breaks associated with a textarea input/value.
682
683 foreach ($comment as $key => $value) {
684 $data[$key] = implode("\n", array_map('sanitize_text_field', explode("\n", $value)));
685 }
686
687 // Update existing comment based on the passed parameter fields and
688 // return the status accordingly.
689
690 if (wp_update_comment($data)) {
691 $result = array('error' => false, 'message' => 'comment_edited_with_comment_author', 'values' => array($original_comment->comment_author));
692 } else {
693 $result = array('error' => true, 'message' => 'comment_edit_failed_with_error', 'values' => array($original_comment->comment_ID));
694 }
695 } else {
696 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($comment['comment_id']));
697 }
698
699 // Here, we're restoring to the current (default) blog before we
700 // do the switched.
701
702 if (function_exists('restore_current_blog') && $switched) {
703 restore_current_blog();
704 }
705
706 return $this->_response($result);
707 }
708
709 /**
710 * The update_comment_status function is a generic handler for the following
711 * comment actions:
712 *
713 * - approve comment
714 * - unapprove comment
715 * - set comment as spam
716 * - move commment to trash
717 * - delete comment permanently
718 * - unset comment as spam
719 * - restore comment
720 *
721 * @param array $params Specific params to update comment status
722 * @return array
723 */
724 public function update_comment_status($params) {
725
726 // Here, we're getting the current blog id. If blog id
727 // is passed along with the parameters then we override
728 // that current (default) value with the parameter blog id value.
729
730 $blog_id = get_current_blog_id();
731 if (isset($params['blog_id'])) $blog_id = $params['blog_id'];
732
733
734 // If user does not have sufficient privileges to moderate or edit
735 // a comment then we return with error.
736
737 if (!current_user_can_for_blog($blog_id, 'moderate_comments')) {
738 $result = array('error' => true, 'message' => 'comment_change_status_no_permission');
739 return $this->_response($result);
740 }
741
742 // Here, we're switching to the actual blog that we need
743 // to apply our changes.
744
745 $switched = false;
746 if (function_exists('switch_to_blog')) {
747 $switched = switch_to_blog($blog_id);
748 }
749
750
751 // We make sure that we still have a valid comment from the server
752 // before we apply the currently selected action.
753
754 $comment = get_comment($params['comment_id']);
755 if ($comment) {
756 $post = get_post($comment->comment_post_ID);
757
758 if ($post) $comment->in_response_to = $post->post_title;
759 if (!empty($comment->comment_parent)) {
760 $parent_comment = get_comment($comment->comment_parent);
761 if ($parent_comment) $comment->in_reply_to = $parent_comment->comment_author;
762 }
763
764 // We're formatting the comment_date to be exactly the same
765 // with that of WP Comments table (e.g. 2016/12/21 at 10:30 PM)
766
767 $comment->comment_date = date('Y/m/d \a\t g:i a', strtotime($comment->comment_date));
768
769 $status = wp_get_comment_status($comment->comment_ID);
770 if ($status) {
771 $comment->comment_status = $status;
772 }
773
774 $succeeded = false;
775 $message = '';
776
777 // Here, we're using WP's wp_set_comment_status function to change the state
778 // of the selected comment based on the current action, except for the "delete" action
779 // where we use the wp_delete_comment to delete the comment permanently by passing
780 // "true" to the second argument.
781
782 switch ($params['action']) {
783 case 'approve':
784 $succeeded = wp_set_comment_status($params['comment_id'], 'approve');
785 $message = 'comment_approve_with_comment_author';
786 break;
787 case 'unapprove':
788 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
789 $message = 'comment_unapprove_with_comment_author';
790 break;
791 case 'spam':
792 $succeeded = wp_set_comment_status($params['comment_id'], 'spam');
793 $message = 'comment_spam_with_comment_author';
794 break;
795 case 'trash':
796 $succeeded = wp_set_comment_status($params['comment_id'], 'trash');
797 $message = 'comment_trash_with_comment_author';
798 break;
799 case 'delete':
800 $succeeded = wp_delete_comment($params['comment_id'], true);
801 $message = 'comment_delete_with_comment_author';
802 break;
803 case 'notspam':
804 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
805 $message = 'comment_not_spam_with_comment_author';
806 break;
807 case 'restore':
808 $succeeded = wp_set_comment_status($params['comment_id'], 'hold');
809 $message = 'comment_restore_with_comment_author';
810 break;
811 }
812
813 // If the current action succeeded, then we return a success message, otherwise,
814 // we return an error message to the user issuing the request.
815
816 if ($succeeded) {
817 $result = array('error' => false, 'message' => $message, 'values' => array($comment->comment_author), 'status' => $comment->comment_status, 'approved' => $comment->comment_approved);
818 } else {
819 $result = array('error' => true, 'message' => 'comment_change_status_failed_with_error', 'values' => array($comment->comment_ID));
820 }
821 } else {
822 $result = array('error' => true, 'message' => 'comment_does_not_exists_error', 'values' => array($params['comment_id']));
823 }
824
825 // Here, we're restoring to the current (default) blog before we
826 // do the switched.
827
828 if (function_exists('restore_current_blog') && $switched) {
829 restore_current_blog();
830 }
831
832 return $this->_response($result);
833 }
834 }
835