|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The abstract class used to create Page Groups within WP-Admin |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Glynn Quelch <[email protected]> |
|
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
22
|
|
|
* @package PinkCrab\Perique_Admin_Menu |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace PinkCrab\Perique_Admin_Menu\Page; |
|
26
|
|
|
|
|
27
|
|
|
use PinkCrab\Perique_Admin_Menu\Page\Page; |
|
28
|
|
|
use PinkCrab\Perique\Services\View\View; |
|
29
|
|
|
use PinkCrab\Perique_Admin_Menu\Exception\Page_Exception; |
|
30
|
|
|
|
|
31
|
|
|
abstract class Menu_Page implements Page { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The pages menu slug. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $parent_slug; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The pages menu slug. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $page_slug; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The menu title |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $menu_title; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The pages title |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $page_title; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The pages position, in relation to other pages in group. |
|
63
|
|
|
* |
|
64
|
|
|
* @var int|null |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $position = null; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The min capabilities required to access page. |
|
70
|
|
|
* |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $capability = 'manage_options'; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The template to be rendered. |
|
77
|
|
|
* |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $view_template; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Data to be used to render the page. |
|
84
|
|
|
* |
|
85
|
|
|
* @var array<string, mixed> |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $view_data = array(); |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Holds the page hook. |
|
91
|
|
|
* |
|
92
|
|
|
* @var ?string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $page_hook; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* View |
|
98
|
|
|
* |
|
99
|
|
|
* @var View |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $view; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set view |
|
105
|
|
|
* |
|
106
|
|
|
* @param View $view View |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
|
|
public function set_view( View $view ): self { |
|
110
|
|
|
$this->view = $view; |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string|null |
|
116
|
|
|
*/ |
|
117
|
|
|
public function parent_slug(): ?string { |
|
118
|
|
|
return $this->parent_slug; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function slug(): string { |
|
125
|
|
|
if ( $this->page_slug === null ) { |
|
126
|
|
|
throw Page_Exception::undefined_property( 'page_slug', $this ); |
|
127
|
|
|
} |
|
128
|
|
|
return $this->page_slug; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function menu_title(): string { |
|
135
|
|
|
if ( $this->menu_title === null ) { |
|
136
|
|
|
throw Page_Exception::undefined_property( 'menu_title', $this ); |
|
137
|
|
|
} |
|
138
|
|
|
return $this->menu_title; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string|null |
|
143
|
|
|
*/ |
|
144
|
|
|
public function page_title(): ?string { |
|
145
|
|
|
return $this->page_title; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return int|null |
|
150
|
|
|
*/ |
|
151
|
|
|
public function position(): ?int { |
|
152
|
|
|
return $this->position; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function capability(): string { |
|
159
|
|
|
return $this->capability; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Renders the page view. |
|
164
|
|
|
* |
|
165
|
|
|
* @return callable |
|
166
|
|
|
* @throws Page_Exception code 200 if view not defined. |
|
167
|
|
|
* @throws Page_Exception code 201 if template not defined. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function render_view(): callable { |
|
170
|
|
|
if ( null === $this->view ) { |
|
171
|
|
|
throw Page_Exception::view_not_set( $this ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ( null === $this->view_template ) { |
|
175
|
|
|
throw Page_Exception::undefined_property( 'view_template', $this ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return function() { |
|
|
|
|
|
|
179
|
|
|
$this->view->render( $this->view_template, $this->view_data ); |
|
180
|
|
|
}; |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Callback for enqueuing scripts and styles at a page level. |
|
186
|
|
|
* |
|
187
|
|
|
* @param Page $page |
|
188
|
|
|
* @return void |
|
189
|
|
|
* @codeCoverageIgnore This can be tested as it does nothing and is extended only |
|
190
|
|
|
*/ |
|
191
|
|
|
public function enqueue( Page $page ): void { |
|
192
|
|
|
// Do nothing. |
|
193
|
|
|
// Can be extended in any child class that extends. |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Callback for the pre-load of the page |
|
198
|
|
|
* |
|
199
|
|
|
* @param Page $page |
|
200
|
|
|
* @return void |
|
201
|
|
|
* @codeCoverageIgnore This can be tested as it does nothing and is extended only |
|
202
|
|
|
*/ |
|
203
|
|
|
public function load( Page $page ): void { |
|
204
|
|
|
// Do nothing. |
|
205
|
|
|
// Can be extended in any child class that extends. |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Sets the page hook |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $page_hook |
|
212
|
|
|
* @return void |
|
213
|
|
|
*/ |
|
214
|
|
|
final public function set_page_hook( string $page_hook ): void { |
|
215
|
|
|
$this->page_hook = $page_hook; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Gets the page hook |
|
220
|
|
|
* |
|
221
|
|
|
* @return string|null |
|
222
|
|
|
*/ |
|
223
|
|
|
final public function page_hook(): ?string { |
|
224
|
|
|
return $this->page_hook; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|