PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Value / Extended / Posts.php
codepress-admin-columns / classes / Value / Extended Last commit date
ExtendedValue.php 2 days ago MediaPreview.php 2 days ago Posts.php 2 days ago Value.php 2 days ago
Posts.php
133 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\Value\Extended;
6
7 use AC\Column;
8 use AC\Helper;
9 use AC\ListScreen;
10 use AC\Value\ExtendedValueLink;
11 use AC\View;
12
13 class Posts implements ExtendedValue
14 {
15 public function can_render(string $view): bool
16 {
17 return $view === 'posts';
18 }
19
20 public function get_link($id, string $label): ExtendedValueLink
21 {
22 return new ExtendedValueLink($label, $id, 'posts', ['class' => '-w-xlarge']);
23 }
24
25 private function get_post_count(int $user_id, array $post_types, array $status): int
26 {
27 return Helper\Post::create()->count_user_posts(
28 $user_id,
29 $post_types,
30 $status
31 );
32 }
33
34 public function render($id, array $params, Column $column, ListScreen $list_screen): string
35 {
36 $post_types = $params['post_type'] ?? get_post_types(['show_ui' => true]);
37 $status = $params['post_stati'] ?? get_post_stati(['internal' => 0]);
38
39 $count = $this->get_post_count($id, $post_types, $status);
40
41 if ($count < 1) {
42 return __('No items', 'codepress-admin-columns');
43 }
44
45 $posts = [];
46
47 $limit = 50;
48
49 foreach ($this->get_recent_posts($id, $post_types, $status, $limit) as $post) {
50 $post_title = strip_tags($post->post_title) ?: $post->ID;
51 $edit_link = get_edit_post_link($post->ID);
52
53 if ($edit_link) {
54 $post_title = sprintf(
55 '<a href="%s">%s</a>',
56 $edit_link,
57 $post_title
58 );
59 }
60
61 $post_type = get_post_type_object($post->post_type);
62
63 if ($post_type) {
64 $post_type = $post_type->labels->singular_name;
65 }
66
67 $post_status_obj = get_post_status_object($post->post_status);
68
69 $posts[] = [
70 'id' => $post->ID,
71 'post_type' => $post_type,
72 'post_title' => $post_title,
73 'post_status' => $post_status_obj ? $post_status_obj->label : '-',
74 'post_date' => wp_date(
75 Helper\WpDateFormat::date(),
76 strtotime($post->post_date)
77 ) ?: '',
78 ];
79 }
80
81 $view = new View([
82 'title' => __('Recent items', 'codepress-admin-columns'),
83 'posts' => $posts,
84 'post_types' => $this->get_post_count_per_post_type($id, $post_types, $status),
85 ]);
86
87 return $view
88 ->set_template('modal-value/posts')
89 ->render();
90 }
91
92 private function get_post_count_per_post_type(int $user_id, array $post_types, array $status): array
93 {
94 $items = [];
95
96 foreach ($post_types as $post_type) {
97 $count = Helper\Post::create()->count_user_posts($user_id, [$post_type], $status);
98
99 if ($count > 0) {
100 $items[] = [
101 'link' => $this->get_post_table_link($user_id, $post_type),
102 'post_type' => ($type_obj = get_post_type_object($post_type)) ? $type_obj->labels->singular_name : $post_type,
103 'count' => number_format_i18n($count),
104 ];
105 }
106 }
107
108 return $items;
109 }
110
111 private function get_post_table_link(int $user_id, string $post_type): string
112 {
113 return add_query_arg(
114 [
115 'post_type' => $post_type,
116 'author' => $user_id,
117 ],
118 admin_url('edit.php')
119 );
120 }
121
122 private function get_recent_posts(int $user_id, array $post_types, array $post_status, ?int $limit = null): array
123 {
124 return get_posts([
125 'author' => $user_id,
126 'post_type' => $post_types,
127 'post_status' => $post_status,
128 'posts_per_page' => $limit ?: -1,
129 ]);
130 }
131
132 }
133