PostCount.php
1.62 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
<?php
namespace AC\Column\User;
use AC\Column;
use AC\Settings;
/**
* @since 2.0
*/
class PostCount extends Column {
public function __construct() {
$this->set_type( 'column-user_postcount' )
->set_label( __( 'Post Count', 'codepress-admin-columns' ) );
}
public function get_value( $user_id ) {
$ids = $this->get_raw_value( $user_id );
if ( empty( $ids ) ) {
return $this->get_empty_char();
}
$value = number_format_i18n( count( $ids ) );
if ( post_type_exists( $this->get_selected_post_type() ) ) {
$link = add_query_arg( [
'post_type' => $this->get_selected_post_type(),
'author' => $user_id,
], admin_url( 'edit.php' ) );
$value = sprintf( '<a href="%s">%s</a>', $link, $value );
}
return $value;
}
protected function get_selected_post_type() {
return $this->get_setting( 'post_type' )->get_post_type();
}
public function get_raw_value( $user_id ) {
$post_type = $this->get_selected_post_type();
if ( 'any' === $post_type ) {
// All post types, including the ones that are marked "exclude from search"
$post_type = get_post_types();
}
return get_posts( [
'fields' => 'ids',
'author' => $user_id,
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status' => $this->get_selected_post_status(),
] );
}
/**
* @return array
*/
public function get_selected_post_status() {
return $this->get_setting( 'post_status' )->get_value();
}
protected function register_settings() {
$this->add_setting( new Settings\Column\PostType( $this, true ) );
$this->add_setting( new Settings\Column\PostStatus( $this ) );
}
}