_wp_ajax_delete_comment_response

Sends back current comment total and new page links if they need to be updated.

Description

_wp_ajax_delete_comment_response( (int) $comment_id, (int) $delta = -1 ); 

Contrary to normal success Ajax response ("1"), die with time() on success.

Parameters (2)

0. $comment_id (int)
The comment id.
1. $delta — Optional. (int) => -1
The delta.

Usage

  1. if ( !function_exists( '_wp_ajax_delete_comment_response' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4. // The comment id. 
  5. $comment_id = -1; 
  6.  
  7. // The delta. 
  8. $delta = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _wp_ajax_delete_comment_response($comment_id, $delta); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { 
  2. $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0; 
  3. $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0; 
  4. $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0; 
  5. $url = isset( $_POST['_url'] ) ? esc_url_raw( $_POST['_url'] ) : ''; 
  6.  
  7. // JS didn't send us everything we need to know. Just die with success message 
  8. if ( ! $total || ! $per_page || ! $page || ! $url ) { 
  9. $time = time(); 
  10. $comment = get_comment( $comment_id ); 
  11. $comment_status = ''; 
  12. $comment_link = ''; 
  13.  
  14. if ( $comment ) { 
  15. $comment_status = $comment->comment_approved; 
  16.  
  17. if ( 1 === (int) $comment_status ) { 
  18. $comment_link = get_comment_link( $comment ); 
  19.  
  20. $counts = wp_count_comments(); 
  21.  
  22. $x = new WP_Ajax_Response( array( 
  23. 'what' => 'comment',  
  24. // Here for completeness - not used. 
  25. 'id' => $comment_id,  
  26. 'supplemental' => array( 
  27. 'status' => $comment_status,  
  28. 'postId' => $comment ? $comment->comment_post_ID : '',  
  29. 'time' => $time,  
  30. 'in_moderation' => $counts->moderated,  
  31. 'i18n_comments_text' => sprintf( 
  32. _n( '%s Comment', '%s Comments', $counts->approved ),  
  33. number_format_i18n( $counts->approved ) 
  34. ),  
  35. 'i18n_moderation_text' => sprintf( 
  36. _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ),  
  37. number_format_i18n( $counts->moderated ) 
  38. ),  
  39. 'comment_link' => $comment_link,  
  40. ) ); 
  41. $x->send(); 
  42.  
  43. $total += $delta; 
  44. if ( $total < 0 ) 
  45. $total = 0; 
  46.  
  47. // Only do the expensive stuff on a page-break, and about 1 other time per page 
  48. if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) { 
  49. $post_id = 0; 
  50. // What type of comment count are we looking for? 
  51. $status = 'all'; 
  52. $parsed = parse_url( $url ); 
  53. if ( isset( $parsed['query'] ) ) { 
  54. parse_str( $parsed['query'], $query_vars ); 
  55. if ( !empty( $query_vars['comment_status'] ) ) 
  56. $status = $query_vars['comment_status']; 
  57. if ( !empty( $query_vars['p'] ) ) 
  58. $post_id = (int) $query_vars['p']; 
  59. if ( ! empty( $query_vars['comment_type'] ) ) 
  60. $type = $query_vars['comment_type']; 
  61.  
  62. if ( empty( $type ) ) { 
  63. // Only use the comment count if not filtering by a comment_type. 
  64. $comment_count = wp_count_comments($post_id); 
  65.  
  66. // We're looking for a known type of comment count. 
  67. if ( isset( $comment_count->$status ) ) { 
  68. $total = $comment_count->$status; 
  69. // Else use the decremented value from above. 
  70.  
  71. // The time since the last comment count. 
  72. $time = time(); 
  73. $comment = get_comment( $comment_id ); 
  74.  
  75. $x = new WP_Ajax_Response( array( 
  76. 'what' => 'comment',  
  77. // Here for completeness - not used. 
  78. 'id' => $comment_id,  
  79. 'supplemental' => array( 
  80. 'status' => $comment ? $comment->comment_approved : '',  
  81. 'postId' => $comment ? $comment->comment_post_ID : '',  
  82. 'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),  
  83. 'total_pages' => ceil( $total / $per_page ),  
  84. 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),  
  85. 'total' => $total,  
  86. 'time' => $time 
  87. ) ); 
  88. $x->send();