*/ private array $filters; /** @param array $filters */ public function __construct(Submissions $repo, array $filters) { $this->repo = $repo; $this->filters = $filters; parent::__construct(['singular' => 'submission', 'plural' => 'submissions', 'ajax' => false]); } public function get_columns(): array { return [ 'cb' => '', 'date' => __('Date', 'zadex'), 'form' => __('Form', 'zadex'), 'summary' => __('Submission', 'zadex'), 'page' => __('Page', 'zadex'), 'status' => __('Status', 'zadex'), ]; } protected function get_bulk_actions(): array { return [ 'read' => __('Mark as read', 'zadex'), 'unread' => __('Mark as unread', 'zadex'), 'delete' => __('Delete', 'zadex'), ]; } public function no_items(): void { esc_html_e('No submissions yet.', 'zadex'); } public function prepare_items(): void { $per_page = 20; $result = $this->repo->query(array_merge($this->filters, ['per_page' => $per_page, 'page' => $this->get_pagenum()])); $this->items = $result['items']; $this->_column_headers = [$this->get_columns(), [], []]; $this->set_pagination_args(['total_items' => $result['total'], 'per_page' => $per_page]); } protected function column_cb($item): string { return ''; } protected function column_date(array $item): string { $view = SubmissionsPage::url(['view' => $item['id']]); $out = '' . esc_html($item['created_at']) . ''; if ($item['status'] === 'new') { $out = '' . $out . ''; } $page = new SubmissionsPage($this->repo); $actions = [ 'view' => '' . esc_html__('View', 'zadex') . '', 'toggle' => $item['status'] === 'new' ? '' . esc_html__('Mark as read', 'zadex') . '' : '' . esc_html__('Mark as unread', 'zadex') . '', 'delete' => '' . esc_html__('Delete', 'zadex') . '', ]; return $out . $this->row_actions($actions); } protected function column_form(array $item): string { return esc_html($item['form_name'] !== '' ? $item['form_name'] : $item['form_id']); } protected function column_summary(array $item): string { $parts = []; foreach ($item['data'] as $field) { if (!is_array($field) || in_array($field['type'] ?? '', ['password', 'upload'], true)) { continue; } $v = $field['value'] ?? ''; $v = is_array($v) ? implode(', ', array_map('strval', $v)) : (string) $v; if ($v === '') { continue; } $parts[] = '' . esc_html((string) ($field['label'] ?? '')) . ': ' . esc_html(mb_strimwidth($v, 0, 60, '…')) . ''; if (count($parts) >= 3) { break; } } return implode('
', $parts); } protected function column_page(array $item): string { $url = (string) ($item['meta']['page'] ?? ''); $title = function_exists('get_the_title') ? (string) get_the_title($item['post_id']) : ''; $label = $title !== '' ? $title : '#' . $item['post_id']; return $url !== '' ? '' . esc_html($label) . '' : esc_html($label); } protected function column_status(array $item): string { return $item['status'] === 'new' ? '' . esc_html__('Unread', 'zadex') . '' : esc_html__('Read', 'zadex'); } }