-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclass-sunflowersettingspage.php
324 lines (287 loc) · 8.9 KB
/
class-sunflowersettingspage.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Class for the Sunflower settings page.
*
* @package sunflower
*/
/**
* The class itself.
*/
class SunflowerSettingsPage {
/**
* Holds the values to be used in the fields callbacks
*
* @var $options
*/
private $options;
/**
* Start up
*/
public function __construct() {
add_action( 'admin_menu', $this->sunflower_add_plugin_page( ... ) );
add_action( 'admin_init', $this->sunflower_settings_page_init( ... ) );
}
/**
* Add options page
*/
public function sunflower_add_plugin_page(): void {
add_submenu_page(
'sunflower_admin',
__( 'Settings', 'sunflower' ),
__( 'Settings', 'sunflower' ),
'edit_pages',
'sunflower_settings',
$this->create_sunflower_settings_page( ... )
);
}
/**
* Options page callback
*/
public function create_sunflower_settings_page(): void {
// Set class properties from options.
$this->options = get_option( 'sunflower_options' );
?>
<div class="wrap">
<h1><?php esc_html_e( 'Sunflower Settings', 'sunflower' ); ?></h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields.
settings_fields( 'sunflower_option_group' );
do_settings_sections( 'sunflower-setting-admin' );
submit_button();
?>
</form>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">Permalinks</th>
<td>
<?php
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sunflower_options-flushpermalinks' ) && isset( $_GET['flush_permalinks'] ) ) {
flush_rewrite_rules();
?>
Die Permalinkstruktur wurde neu eingelesen.
<?php } else { ?>
<p>Falls der Link für die Termineseite fehlerhaft ist, kannst Du die Permalinkstruktur neu einlesen. </p><br>
<?php
$sunflower_flushpermalinks_url = wp_nonce_url( 'admin.php?page=sunflower_settings&flush_permalinks=1', 'sunflower_options-flushpermalinks' );
printf( '<a href="%s" class="button button-primary">%s</a>', esc_html( $sunflower_flushpermalinks_url ), esc_attr__( 'Reimport permalink structure', 'sunflower' ) );
}
?>
</td>
</tr>
</tbody>
</table>
</div>
<?php
}
/**
* Register and add settings
*/
public function sunflower_settings_page_init(): void {
register_setting(
'sunflower_option_group',
'sunflower_options',
$this->sanitize( ... )
);
add_settings_section(
'sunflower_layout',
__( 'Layout', 'sunflower' ),
$this->print_section_info( ... ),
'sunflower-setting-admin'
);
add_settings_field(
'excerps_length',
__( 'Excerpt length (words)', 'sunflower' ),
$this->excerpt_length_callback( ... ),
'sunflower-setting-admin',
'sunflower_layout'
);
add_settings_field(
'sunflower_show_related_posts',
__( 'show related posts', 'sunflower' ),
$this->sunflower_checkbox_callback( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_show_related_posts', __( 'show related posts', 'sunflower' ) )
);
add_settings_field(
'sunflower_show_author',
__( 'show author of posts', 'sunflower' ),
$this->sunflower_checkbox_callback( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_show_author', __( 'Show post author on post details and via REST api.', 'sunflower' ) )
);
add_settings_field(
'sunflower_hide_prev_next',
__( 'hide previous and next links', 'sunflower' ),
$this->sunflower_checkbox_callback( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_hide_prev_next', __( 'hide previous and next links', 'sunflower' ) )
);
add_settings_field(
'sunflower_contact_form_to',
__( 'to-field for contact-forms', 'sunflower' ),
$this->sunflower_contact_form_to( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_contact_form_to', __( 'to-field for contact-forms', 'sunflower' ) )
);
add_settings_field(
'sunflower_main_menu_item_is_placeholder',
__( 'items in menu', 'sunflower' ),
$this->sunflower_checkbox_callback( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_main_menu_item_is_placeholder', __( 'items with href=# in the main menu are placeholders for submenu', 'sunflower' ) )
);
add_settings_field(
'sunflower_header_layout',
__( 'Use this header layout', 'sunflower' ),
$this->sunflower_header_layout( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_header_layout', __( 'Use this header layout', 'sunflower' ) )
);
add_settings_field(
'sunflower_header_social_media',
__( 'Show social media icons in header', 'sunflower' ),
$this->sunflower_header_social_media( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_header_social_media', __( 'Show social media icons in header', 'sunflower' ) )
);
add_settings_field(
'sunflower_categories_archive',
__( 'Show list of categories on category archive', 'sunflower' ),
$this->sunflower_categories_archive( ... ),
'sunflower-setting-admin',
'sunflower_layout',
array( 'sunflower_categories_filter', __( 'Show list of categories on category archive', 'sunflower' ) )
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
*/
public function sanitize( $input ) {
$new_input = array();
// Sanitize everything element of the input array.
foreach ( $input as $key => $value ) {
if ( isset( $input[ $key ] ) ) {
$new_input[ $key ] = sanitize_text_field( $value );
}
}
// Sanitize special values.
if ( isset( $input['excerpt_length'] ) ) {
$new_input['excerpt_length'] = absint( $input['excerpt_length'] ) ?? '';
}
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info() {
}
/**
* Checkbox callback
*
* @param array $args The field arguments.
*/
public function sunflower_checkbox_callback( $args ): void {
$field = $args[0];
$label = $args[1];
printf(
'<label>
<input type="checkbox" id="%1$s" name="sunflower_options[%1$s]" value="checked" %2$s />
%3$s
</label>',
esc_attr( $field ),
isset( $this->options[ $field ] ) ? 'checked' : '',
esc_attr( $label )
);
}
/**
* Excerpt length field
*/
public function excerpt_length_callback(): void {
printf(
'<input type="text" id="excerpt_length" name="sunflower_options[excerpt_length]" value="%s" />',
isset( $this->options['excerpt_length'] ) ? esc_attr( $this->options['excerpt_length'] ) : ''
);
}
/**
* Contact form field "to"
*/
public function sunflower_contact_form_to(): void {
printf(
'<input type="email" id="sunflower_contact_form_to" name="sunflower_options[sunflower_contact_form_to]" value="%s" />',
isset( $this->options['sunflower_contact_form_to'] ) ? esc_attr( $this->options['sunflower_contact_form_to'] ) : ''
);
}
/**
* Header layout variant field
*/
public function sunflower_header_layout(): void {
echo '<select id="sunflower_header_layout" name="sunflower_options[sunflower_header_layout]">';
$options = array(
array( 'standard', __( 'Standard', 'sunflower' ) ),
array( 'personal', __( 'Personal', 'sunflower' ) ),
);
foreach ( $options as $option ) {
$selected = ( isset( $this->options['sunflower_header_layout'] ) && $this->options['sunflower_header_layout'] === $option[0] ) ? 'selected' : '';
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $option[0] ),
esc_attr( $selected ),
esc_attr( $option[1] )
);
}
echo '</select>';
}
/**
* Header layout variant field
*/
public function sunflower_categories_archive(): void {
echo '<select id="sunflower_categories_archive" name="sunflower_options[sunflower_categories_archive]">';
$options = array(
array( 'no', __( 'do not show', 'sunflower' ) ),
array( 'main-categories', __( 'main categories', 'sunflower' ) ),
array( 'only-subcategories', __( 'only sub-categories', 'sunflower' ) ),
);
foreach ( $options as $option ) {
$selected = ( isset( $this->options['sunflower_categories_archive'] ) && $this->options['sunflower_categories_archive'] === $option[0] ) ? 'selected' : '';
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $option[0] ),
esc_attr( $selected ),
esc_attr( $option[1] )
);
}
echo '</select>';
}
/**
* Show social media icons in header, too.
*
* @param Array $args The forms argument.
*/
public function sunflower_header_social_media( $args ): void {
$field = $args[0];
$label = $args[1];
printf(
'<label>
<input type="checkbox" id="%1$s" name="sunflower_options[%1$s]" value="checked" %2$s />
%3$s
</label>',
esc_attr( $field ),
isset( $this->options[ $field ] ) ? 'checked' : '',
esc_attr( $label )
);
}
}
if ( is_admin() ) {
$sunflower_settings_page = new SunflowerSettingsPage();
}