PostCount.php
5.74 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace ACP\Settings\Column\NetworkSite;
use AC\Settings;
use AC\View;
use WP_Site;
class PostCount extends Settings\Column
implements Settings\FormatValue {
private $post_type;
private $post_status;
protected function define_options() {
return [ 'post_type', 'post_status' ];
}
public function create_view() {
$options = $this->get_post_types();
if ( ! $options ) {
return false;
}
$setting = $this
->create_element( 'select', 'post_type' )
->set_options( $options );
$view_post_type = new View( [
'label' => __( 'Post Type', 'codepress-admin-columns' ),
'setting' => $setting,
'for' => $setting->get_id(),
] );
$setting = $this
->create_element( 'select', 'post_status' )
->set_attribute( 'data-refresh', 'column' )
->set_options( $this->get_post_statuses() );
if ( $excluded = $this->get_exludeded_post_statuses() ) {
$setting->set_description( sprintf( __( 'Does not include %s', 'codepress-admin-columns' ), ac_helper()->string->enumeration_list( $excluded ) ) );
}
$view_post_status = new View( [
'label' => __( 'Post Status', 'codepress-admin-columns' ),
'setting' => $setting,
'for' => $setting->get_id(),
] );
$view = new View( [
'label' => __( 'Display Options', 'codepress-admin-columns' ),
'sections' => [ $view_post_type, $view_post_status ],
] );
return $view;
}
/**
* @param string $field
* @param int $expire
*
* @return array|bool|mixed
*/
private function get_cached_distinct_db_values( $field, $expire = 15 ) {
$values = wp_cache_get( $this->column->get_list_screen()->get_storage_key(), 'ac-network-postcount-' . $field );
if ( ! $values ) {
$values = $this->get_distinct_db_values( $field );
wp_cache_add( $this->column->get_list_screen()->get_storage_key(), $values, 'ac-network-postcount-' . $field, $expire );
}
return $values;
}
/**
* @param $field
*
* @return array
*/
private function get_distinct_db_values( $field ) {
global $wpdb;
$queries = [];
foreach ( get_sites() as $site ) {
/* @var WP_Site $site */
$table = $wpdb->get_blog_prefix( $site->id ) . 'posts';
$field = '`' . sanitize_key( $field ) . '`';
$sql = "SELECT DISTINCT {$field} FROM {$table}";
$queries[] = $sql;
}
return $wpdb->get_col( implode( " UNION ", $queries ) );
}
/**
* @return array
*/
private function get_post_types() {
$post_types = $this->get_cached_distinct_db_values( 'post_type' );
if ( ! $post_types ) {
return [];
}
natcasesort( $post_types );
$post_types = array_combine( $post_types, $post_types );
return [ '' => __( 'All post types', 'codepress-admin-columns' ) ] + $post_types;
}
/**
* @return array
*/
private function get_post_statuses() {
$post_statuses = $this->get_cached_distinct_db_values( 'post_status' );
if ( ! $post_statuses ) {
return [];
}
$post_statuses[] = 'trash';
$post_statuses = array_unique( array_merge( $post_statuses, array_keys( get_post_statuses() ) ) );
$post_statuses = array_combine( $post_statuses, $post_statuses );
// Exclude 'auto-draft', 'inherit'
$excluded = (array) get_post_stati( [ 'show_in_admin_status_list' => false ] );
foreach ( $excluded as $k => $status ) {
if ( isset( $post_statuses[ $status ] ) ) {
unset( $post_statuses[ $status ] );
}
}
natcasesort( $post_statuses );
$options = [
'' => __( 'Any post status', 'codepress-admin-columns' ),
'without_trash' => __( 'Any post status without Trash', 'codepress-admin-columns' ),
] + $post_statuses;
return $options;
}
/**
* Excludes 'auto-draft' and 'inherit'
* Or use 'show_in_admin_all_list' to also exclude 'trash'
* @return array Post statuses
*/
private function get_exludeded_post_statuses() {
if ( 'without_trash' === $this->get_post_status() ) {
return (array) get_post_stati( [ 'show_in_admin_all_list' => false ] );
}
if ( ! $this->get_post_status() ) {
return (array) get_post_stati( [ 'show_in_admin_status_list' => false ] );
}
return [];
}
/**
* @return string
*/
public function get_post_type() {
return $this->post_type;
}
/**
* @param string $post_type
*
* @return bool
*/
public function set_post_type( $post_type ) {
$this->post_type = $post_type;
return true;
}
/**
* @return string
*/
public function get_post_status() {
return $this->post_status;
}
/**
* @param string $post_status
*
* @return bool
*/
public function set_post_status( $post_status ) {
$this->post_status = $post_status;
return true;
}
public function format( $value, $original_value ) {
global $wpdb;
$blog_id = $original_value;
$table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
$post_status = $this->get_post_status();
$sql = "SELECT count(*) FROM {$table}";
$conditional = [];
// Exclude internal post status, like 'auto-draft' and 'inherit' or 'trash'
if ( $excluded = $this->get_exludeded_post_statuses() ) {
$conditional[] = "{$table}.post_status NOT IN ( '" . implode( "','", $excluded ) . "' )";
$post_status = '';
}
if ( $this->get_post_type() ) {
$conditional[] = $wpdb->prepare( "{$table}.post_type = %s", $this->get_post_type() );
}
if ( $post_status ) {
$conditional[] = $wpdb->prepare( "{$table}.post_status = %s", $post_status );
}
if ( $conditional ) {
$sql .= " WHERE " . implode( " AND ", $conditional );
}
$new_value = $wpdb->get_var( $sql );
if ( $this->get_post_type() ) {
$url = add_query_arg( [ 'post_type' => $this->get_post_type() ], get_admin_url( $blog_id, 'edit.php' ) );
if ( $post_status ) {
$url = add_query_arg( [ 'post_status' => $post_status ], $url );
}
$new_value = ac_helper()->html->link( $url, $new_value );
}
return $new_value;
}
}