-
Notifications
You must be signed in to change notification settings - Fork 102
/
comment-delete.php
112 lines (96 loc) · 2.64 KB
/
comment-delete.php
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
<?php
/**
* Class used for ajax callback `comment_delete`.
* This class is auto loaded by AnsPress loader on demand.
*
* @author Rahul Aryan <rah12@live.com>
* @package AnsPress
* @subpackage Ajax
* @since 4.1.8
*/
namespace AnsPress\Ajax;
// Die if called directly.
if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
/**
* The `comment_delete` ajax callback.
*
* @since 4.1.8
*/
class Comment_Delete extends \AnsPress\Classes\Ajax {
/**
* Instance of this class.
*
* @var null|Comment_Delete
*/
protected static $instance;
/**
* The class constructor.
*
* Set requests and nonce key.
*/
protected function __construct() {
$comment_id = ap_sanitize_unslash( 'comment_id', 'r' );
$this->nonce_key = 'delete_comment_' . $comment_id;
$this->req( 'comment_id', $comment_id );
// Call parent.
parent::__construct();
}
/**
* Verify user permission.
*
* @return void
*/
protected function verify_permission() {
$comment_id = $this->req( 'comment_id' );
if ( ! empty( $comment_id ) && ! ap_user_can_delete_comment( $comment_id ) ) {
parent::verify_permission();
}
}
/**
* Handle ajax for logged in users.
*
* @return void
*/
public function logged_in() {
$comment_id = $this->req( 'comment_id' );
$_comment = get_comment( $comment_id );
// Check if deleting comment is locked.
if ( ap_comment_delete_locked( $_comment->comment_ID ) && ! is_super_admin() ) {
$this->set_fail();
$this->snackbar(
sprintf(
// Translators: %s contain comment created date. i.e. 10 hours.
__( 'The comment is locked and cannot be deleted. Any comments posted before %s cannot be deleted.', 'anspress-question-answer' ),
human_time_diff( ap_get_current_timestamp() + ap_opt( 'disable_delete_after' ) )
)
);
$this->send();
}
$delete = wp_delete_comment( (int) $_comment->comment_ID, true );
if ( $delete ) {
do_action( 'ap_unpublish_comment', $_comment );
do_action( 'ap_after_deleting_comment', $_comment );
$count = get_comment_count( $_comment->comment_post_ID );
$this->set_success();
$this->snackbar( __( 'Comment successfully deleted', 'anspress-question-answer' ) );
$this->add_res( 'cb', 'commentDeleted' );
$this->add_res( 'post_ID', $_comment->comment_post_ID );
$this->add_res(
'commentsCount',
array(
'text' => sprintf(
// Translators: %d contain comment count.
_n( '%d Comment', '%d Comments', $count['all'], 'anspress-question-answer' ),
$count['all']
),
'number' => $count['all'],
'unapproved' => $count['awaiting_moderation'],
)
);
}
}
}