CommentCount.php
2.63 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
<?php
namespace AC\Settings\Column;
use AC\Settings;
use AC\View;
class CommentCount extends Settings\Column
implements Settings\FormatValue {
const NAME = 'comment_count';
const STATUS_ALL = 'all';
const STATUS_APPROVED = 'approved';
const STATUS_PENDING = 'moderated';
const STATUS_SPAM = 'spam';
const STATUS_TRASH = 'trash';
const STATUS_TOTAL_COMMENTS = 'total_comments';
/**
* @var string
*/
private $comment_status;
protected function set_name() {
$this->name = self::NAME;
}
protected function define_options() {
return [
'comment_status' => self::STATUS_ALL,
];
}
/**
* @return View
*/
public function create_view() {
$setting = $this->create_element( 'select' )
->set_options( $this->get_comment_statuses() );
$view = new View( [
'label' => __( 'Comment status', 'codepress-admin-columns' ),
'tooltip' => __( 'Select which comment status you like to display.', 'codepress-admin-columns' ),
'setting' => $setting,
] );
return $view;
}
/**
* @return array
*/
protected function get_comment_statuses() {
$options = [
self::STATUS_APPROVED => __( 'Approved', 'codepress-admin-columns' ),
self::STATUS_PENDING => __( 'Pending', 'codepress-admin-columns' ),
self::STATUS_SPAM => __( 'Spam', 'codepress-admin-columns' ),
self::STATUS_TRASH => __( 'Trash', 'codepress-admin-columns' ),
];
natcasesort( $options );
// First
$options = [ self::STATUS_ALL => __( 'Total', 'codepress-admin-columns' ) ] + $options;
return $options;
}
/**
* @return int
*/
public function get_comment_status() {
return $this->comment_status;
}
/**
* @param string $comment_status
*
* @return bool
*/
public function set_comment_status( $comment_status ) {
if ( self::STATUS_TOTAL_COMMENTS === $comment_status ) {
$comment_status = self::STATUS_ALL;
}
$this->comment_status = $comment_status;
return true;
}
/**
* @param int $post_id
*
* @return int|null
*/
public function get_comment_count( $post_id ) {
$status = $this->get_comment_status();
$count = wp_count_comments( $post_id );
if ( empty( $count->{$status} ) ) {
return null;
}
return (int) $count->{$status};
}
/**
* @param int $post_id
* @param int $original_value
*
* @return false|string
*/
public function format( $post_id, $original_value ) {
$count = $this->get_comment_count( $post_id );
if ( ! $count ) {
return $this->column->get_empty_char();
}
return ac_helper()->html->link( add_query_arg( [
'p' => $post_id,
'comment_status' => $this->get_comment_status(),
], admin_url( 'edit-comments.php' ) ), $count );
}
}