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 |