1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages\AbstractMenuPage; |
8
|
|
|
use GraphQLAPI\GraphQLAPI\Admin\Tables\AbstractItemListTable; |
9
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Table menu page |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractTableMenuPage extends AbstractMenuPage |
15
|
|
|
{ |
16
|
|
|
protected ?AbstractItemListTable $tableObject; |
17
|
|
|
|
18
|
|
|
abstract protected function getHeader(): string; |
19
|
|
|
|
20
|
|
|
protected function hasViews(): bool |
21
|
|
|
{ |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function print(): void |
26
|
|
|
{ |
27
|
|
|
if (is_null($this->tableObject)) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
?> |
31
|
|
|
<div class="wrap"> |
32
|
|
|
<h1><?php echo $this->getHeader() ?></h1> |
33
|
|
|
<?php |
34
|
|
|
if ($this->hasViews()) { |
35
|
|
|
$this->tableObject->views(); |
36
|
|
|
} |
37
|
|
|
?> |
38
|
|
|
<form method="post"> |
39
|
|
|
<?php |
40
|
|
|
$this->tableObject->prepare_items(); |
41
|
|
|
$this->tableObject->display(); ?> |
42
|
|
|
</form> |
43
|
|
|
</div> |
44
|
|
|
<?php |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function showScreenOptions(): bool |
48
|
|
|
{ |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getScreenOptionLabel(): string |
53
|
|
|
{ |
54
|
|
|
return $this->getHeader(); |
55
|
|
|
} |
56
|
|
|
protected function getScreenOptionDefault(): int |
57
|
|
|
{ |
58
|
|
|
return 999; |
59
|
|
|
} |
60
|
|
|
protected function getScreenOptionName(): string |
61
|
|
|
{ |
62
|
|
|
return str_replace(' ', '_', strtolower($this->getScreenOptionLabel())) . '_per_page'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
abstract protected function getTableClass(): string; |
66
|
|
|
|
67
|
|
|
public function initializeTable(): void |
68
|
|
|
{ |
69
|
|
|
/** |
70
|
|
|
* Screen options |
71
|
|
|
*/ |
72
|
|
|
if ($this->showScreenOptions()) { |
73
|
|
|
/** |
74
|
|
|
* Set-up the screen options |
75
|
|
|
*/ |
76
|
|
|
$option = 'per_page'; |
77
|
|
|
$args = [ |
78
|
|
|
'label' => $this->getScreenOptionLabel(), |
79
|
|
|
'default' => $this->getScreenOptionDefault(), |
80
|
|
|
'option' => $this->getScreenOptionName(), |
81
|
|
|
]; |
82
|
|
|
\add_screen_option($option, $args); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Instantiate the table object |
87
|
|
|
*/ |
88
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
89
|
|
|
/** |
90
|
|
|
* @var AbstractItemListTable |
91
|
|
|
*/ |
92
|
|
|
$tableObject = $instanceManager->getInstance($this->getTableClass()); |
93
|
|
|
$this->tableObject = $tableObject; |
94
|
|
|
/** |
95
|
|
|
* Set properties |
96
|
|
|
*/ |
97
|
|
|
$this->tableObject->setItemsPerPageOptionName($this->getScreenOptionName()); |
98
|
|
|
$this->tableObject->setDefaultItemsPerPage($this->getScreenOptionDefault()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function initialize(): void |
102
|
|
|
{ |
103
|
|
|
parent::initialize(); |
104
|
|
|
|
105
|
|
|
if ($this->showScreenOptions()) { |
106
|
|
|
/** |
107
|
|
|
* Save the screen options |
108
|
|
|
*/ |
109
|
|
|
\add_filter( |
110
|
|
|
'set-screen-option', |
111
|
|
|
fn ($status, $option, $value) => $value, |
112
|
|
|
10, |
113
|
|
|
3 |
114
|
|
|
); |
115
|
|
|
} else { |
116
|
|
|
/** |
117
|
|
|
* Remove the Screen Options tab |
118
|
|
|
*/ |
119
|
|
|
\add_filter('screen_options_show_screen', '__return_false'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Priority 30: execute after `addMenuPagesBottom`, so by then we have the hookName |
124
|
|
|
*/ |
125
|
|
|
\add_action( |
126
|
|
|
'admin_menu', |
127
|
|
|
function () { |
128
|
|
|
/** |
129
|
|
|
* Attach to the hook corresponding to this page |
130
|
|
|
*/ |
131
|
|
|
\add_action( |
132
|
|
|
'load-' . $this->getHookName(), |
133
|
|
|
[$this, 'initializeTable'] |
134
|
|
|
); |
135
|
|
|
}, |
136
|
|
|
30 |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|