ScreenColumns.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\TableScreen\ManageHeading; |
| 6 | |
| 7 | use AC\Registerable; |
| 8 | |
| 9 | class ScreenColumns implements Registerable |
| 10 | { |
| 11 | private string $screen_id; |
| 12 | |
| 13 | /** |
| 14 | * @var array [ $column_id => $label, ... ] |
| 15 | */ |
| 16 | private array $headings; |
| 17 | |
| 18 | private int $priority; |
| 19 | |
| 20 | public function __construct(string $screen_id, array $headings, int $priority = 200) |
| 21 | { |
| 22 | $this->screen_id = $screen_id; |
| 23 | $this->headings = $headings; |
| 24 | $this->priority = $priority; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @see get_column_headers() |
| 29 | */ |
| 30 | public function register(): void |
| 31 | { |
| 32 | add_filter(sprintf('manage_%s_columns', $this->screen_id), [$this, 'handle'], $this->priority); |
| 33 | } |
| 34 | |
| 35 | public function handle($current_headings): array |
| 36 | { |
| 37 | $headings = $this->headings; |
| 38 | $checkbox = $current_headings['cb'] ?? null; |
| 39 | |
| 40 | if ($checkbox) { |
| 41 | $headings = ['cb' => $checkbox] + $headings; |
| 42 | } |
| 43 | |
| 44 | return $headings; |
| 45 | } |
| 46 | |
| 47 | } |
| 48 |