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