-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-menu.php
More file actions
210 lines (185 loc) · 4.27 KB
/
class-menu.php
File metadata and controls
210 lines (185 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Class Menu
*
* @package wpct-plugin
*/
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
namespace WPCT_PLUGIN;
use ArgumentCountError;
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
/**
* Plugin menu abstract class.
*/
class Menu extends Singleton {
/**
* Handle menu name.
*
* @var string
*/
private $name;
/**
* Handle menu slug.
*
* @var string
*/
private $slug;
/**
* Handle plugin settings store instance.
*
* @var Settings_Store
*/
private $store;
/**
* Class constructor. Set attributes and hooks to wp admin hooks.
*
* @param array{0: string, 1:string, 2:Settings_Store} ...$args Constructor arguments:
* 1. Menu name.
* 2. Plugin slug.
* 3. Plugin store.
*
* @throws ArgumentCountError If parameters are not passed to the constructor.
*/
protected function construct( ...$args ) {
if ( count( $args ) < 3 ) {
throw new ArgumentCountError( 'Too few arguments to Menu constructor' );
}
list( $name, $slug, $store ) = $args;
$this->name = $name;
$this->slug = $slug;
$this->store = $store;
add_action(
'admin_menu',
function () {
static::add_menu();
do_action( 'wpct_plugin_register_menu', $this->name, $this );
}
);
}
/**
* Register plugin options page.
*/
private static function add_menu() {
add_options_page(
static::name(),
static::name(),
'manage_options',
static::slug(),
static function () {
static::render_page();
}
);
}
/**
* Render menu page HTML.
*
* @param bool $echo Control if the HTML is outputed as a return value or
* echoed to the output buffer.
*
* @return string|null Page content.
*/
protected static function render_page( $echo = true ) {
$store_settings = static::store()->settings();
$tabs = array();
foreach ( $store_settings as $setting ) {
$setting_name = $setting->option();
$tabs[ $setting_name ] = esc_html(
static::tab_title( $setting_name )
);
}
$current_tab = isset( $_GET['tab'] )
? sanitize_text_field( wp_unslash( $_GET['tab'] ) )
: array_key_first( $tabs );
ob_start();
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<nav class="nav-tab-wrapper">
<?php
foreach ( $tabs as $tab => $name ) {
$current = $tab === $current_tab ? 'nav-tab-active' : '';
$url = add_query_arg(
array(
'page' => static::slug(),
'tab' => $tab,
),
''
);
printf(
'<a class="nav-tab %s" href="%s">%s</a>',
esc_attr( $current ),
esc_url( $url ),
esc_html( $name )
);
}
?>
</nav>
<?php
settings_fields( $current_tab );
do_settings_sections( $current_tab );
submit_button();
?>
</form>
</div>
<?php
$output = ob_get_clean();
if ( $echo ) {
// phpcs:disable WordPress.Security.EscapeOutput
echo $output;
// phpcs:enable WordPress.Security.EscapeOutput
}
return $output;
}
/**
* Menu name getter.
*
* @return string $name menu name
*/
final public static function name() {
return static::get_instance()->name;
}
/**
* Menu slug getter.
*
* @return string $slug menu slug
*/
final public static function slug() {
return static::get_instance()->slug;
}
/**
* Menu settings store getter.
*
* @return Settings_Store plugin settings store instance
*/
final public static function store() {
return static::get_instance()->store;
}
/**
* To be overwriten by the child class.
*
* @param string $setting_name Setting name.
*
* @return string
*/
protected static function tab_title( $setting_name ) {
return $setting_name;
}
/**
* Check if the current page is the plugin admin page.
*
* @return boolean
*/
public static function is_admin_current_page() {
if ( is_admin() ) {
$page = isset( $_GET['page'] )
? sanitize_text_field( wp_unslash( $_GET['page'] ) )
: null;
$slug = static::slug();
return $page && $page === $slug;
}
return false;
}
}