-
Notifications
You must be signed in to change notification settings - Fork 14
/
class-hypothesissettingspage.php
390 lines (344 loc) · 10.6 KB
/
class-hypothesissettingspage.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
namespace Hypothesis;
/**
* Create settings page (see https://codex.wordpress.org/Creating_Options_Pages)
*/
class HypothesisSettingsPage {
/**
* Holds the values to be used in the fields callbacks
*
* @var array
*/
private $options;
/**
* Holds the posttypes to be used in the fields callbacks
*
* @var array
*/
private $posttypes;
/**
* Start up
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page() {
add_options_page(
__( 'Hypothesis Settings', 'hypothesis' ),
__( 'Hypothesis', 'hypothesis' ),
'manage_options',
'hypothesis-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Return an array of post type slugs and corresponding plural display names for options page.
*
* @returns array
*/
public static function get_posttypes() {
return apply_filters(
'hypothesis_supported_posttypes',
array(
'post' => _x( 'posts', 'plural post type', 'hypothesis' ),
'page' => _x( 'pages', 'plural post type', 'hypothesis' ),
)
);
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property.
$this->options = get_option( 'wp_hypothesis_options' ); ?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( 'hypothesis_option_group' );
do_settings_sections( 'hypothesis-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init() {
$posttypes = $this->get_posttypes();
register_setting(
'hypothesis_option_group', // Option group.
'wp_hypothesis_options', // Option name.
array( $this, 'sanitize' ) // Sanitize callback.
);
/**
* Hypothesis Settings
*/
add_settings_section(
'hypothesis_settings_section', // ID.
__( 'Hypothesis Settings', 'hypothesis' ), // Title.
array( $this, 'settings_section_info' ), // Callback.
'hypothesis-setting-admin' // Page.
);
add_settings_field(
'highlights-on-by-default',
__( 'Highlights on by default', 'hypothesis' ),
array( $this, 'highlights_on_by_default_callback' ),
'hypothesis-setting-admin',
'hypothesis_settings_section'
);
add_settings_field(
'sidebar-open-by-default',
__( 'Sidebar open by default', 'hypothesis' ),
array( $this, 'sidebar_open_by_default_callback' ),
'hypothesis-setting-admin',
'hypothesis_settings_section'
);
add_settings_field(
'serve-pdfs-with-via',
__( 'Enable annotation for PDFs in Media Library', 'hypothesis' ),
array( $this, 'serve_pdfs_with_via_default_callback' ),
'hypothesis-setting-admin',
'hypothesis_settings_section'
);
/**
* Content Settings
* Control which pages / posts / custom post types Hypothesis is loaded on.
*/
add_settings_section(
'hypothesis_content_section', // ID.
__( 'Content Settings', 'hypothesis' ), // Title.
array( $this, 'content_section_info' ), // Callback.
'hypothesis-setting-admin' // Page.
);
add_settings_field(
'allow-on-front-page',
__( 'Allow on front page', 'hypothesis' ),
array( $this, 'allow_on_front_page_callback' ),
'hypothesis-setting-admin',
'hypothesis_content_section'
);
add_settings_field(
'allow-on-blog-page',
__( 'Allow on blog page', 'hypothesis' ),
array( $this, 'allow_on_blog_page_callback' ),
'hypothesis-setting-admin',
'hypothesis_content_section'
);
foreach ( $posttypes as $slug => $name ) {
if ( 'post' === $slug ) {
$slug = 'posts';
} elseif ( 'page' === $slug ) {
$slug = 'pages';
}
add_settings_field(
"allow-on-$slug",
/* Translators: name of post type */
sprintf( __( 'Allow on %s', 'hypothesis' ), $name ),
array( $this, 'allow_on_posttype_callback' ),
'hypothesis-setting-admin',
'hypothesis_content_section',
array(
$slug,
$name,
)
);
}
foreach ( $posttypes as $slug => $name ) {
add_settings_field(
$slug . '_ids_show_h', // ID.
sprintf(
/* Translators: plural name of post type */
__( 'Allow on specific %1$s (list of comma-separated %1$s IDs, no spaces)', 'hypothesis' ),
$name,
$slug
), // Title.
array( $this, 'posttype_ids_show_h_callback' ), // Callback.
'hypothesis-setting-admin', // Page.
'hypothesis_content_section', // Section.
array(
$slug,
$name,
)
);
}
foreach ( $posttypes as $slug => $name ) {
add_settings_field(
$slug . '_ids_override', // ID.
sprintf(
/* Translators: plural name of post type */
__( 'Disallow on specific %1$s (list of comma-separated %1$s IDs, no spaces)', 'hypothesis' ),
$name,
$slug
), // Title.
array( $this, 'posttype_ids_override_callback' ), // Callback.
'hypothesis-setting-admin', // Page.
'hypothesis_content_section', // Section.
array(
$slug,
$name,
)
);
}
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
*/
public function sanitize( $input ) {
$posttypes = $this->get_posttypes();
$new_input = array();
if ( isset( $input['highlights-on-by-default'] ) ) {
$new_input['highlights-on-by-default'] = absint( $input['highlights-on-by-default'] );
}
if ( isset( $input['sidebar-open-by-default'] ) ) {
$new_input['sidebar-open-by-default'] = absint( $input['sidebar-open-by-default'] );
}
if ( isset( $input['serve-pdfs-with-via'] ) ) {
$new_input['serve-pdfs-with-via'] = absint( $input['serve-pdfs-with-via'] );
}
if ( isset( $input['allow-on-blog-page'] ) ) {
$new_input['allow-on-blog-page'] = absint( $input['allow-on-blog-page'] );
}
if ( isset( $input['allow-on-front-page'] ) ) {
$new_input['allow-on-front-page'] = absint( $input['allow-on-front-page'] );
}
foreach ( $posttypes as $slug => $name ) {
if ( 'post' === $slug ) { // Adjust for backwards compatibility.
$slug = 'posts';
} elseif ( 'page' === $slug ) {
$slug = 'pages';
}
if ( isset( $input[ "allow-on-$slug" ] ) ) {
$new_input[ "allow-on-$slug" ] = absint( $input[ "allow-on-$slug" ] );
}
if ( 'posts' === $slug ) { // Adjust for backwards compatibility.
$slug = 'post';
} elseif ( 'pages' === $slug ) {
$slug = 'page';
}
if ( isset( $input[ $slug . '_ids_show_h' ] ) && '' != $input[ $slug . '_ids_show_h' ] ) {
$new_input[ $slug . '_ids_show_h' ] = explode( ',', esc_attr( $input[ $slug . '_ids_show_h' ] ) );
}
if ( isset( $input[ $slug . '_ids_override' ] ) && '' != $input[ $slug . '_ids_override' ] ) {
$new_input[ $slug . '_ids_override' ] = explode( ',', esc_attr( $input[ $slug . '_ids_override' ] ) );
}
}
return $new_input;
}
/**
* Print the Hypothesis Settings section text
*/
public function settings_section_info() {
?>
<p><?php esc_attr_e( 'Customize Hypothesis defaults and behavior.', 'hypothesis' ); ?></p>
<?php
}
/**
* Print the Content Settings section text
*/
public function content_section_info() {
?>
<p><?php esc_attr_e( 'Control where Hypothesis is loaded.', 'hypothesis' ); ?></p>
<?php
}
/**
* Callback for 'highlights-on-by-default'.
*/
public function highlights_on_by_default_callback() {
$val = isset( $this->options['highlights-on-by-default'] ) ? esc_attr( $this->options['highlights-on-by-default'] ) : 0;
printf(
'<input type="checkbox" id="highlights-on-by-default" name="wp_hypothesis_options[highlights-on-by-default]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'sidebar-open-by-default'.
*/
public function sidebar_open_by_default_callback() {
$val = isset( $this->options['sidebar-open-by-default'] ) ? esc_attr( $this->options['sidebar-open-by-default'] ) : 0;
printf(
'<input type="checkbox" id="sidebar-open-by-default" name="wp_hypothesis_options[sidebar-open-by-default]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'serve-pdfs-with-via'.
*/
public function serve_pdfs_with_via_default_callback() {
$val = isset( $this->options['serve-pdfs-with-via'] ) ? esc_attr( $this->options['serve-pdfs-with-via'] ) : 0;
printf(
'<input type="checkbox" id="serve-pdfs-with-via" name="wp_hypothesis_options[serve-pdfs-with-via]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'allow_on_blog_page'.
*/
public function allow_on_blog_page_callback() {
$val = isset( $this->options['allow-on-blog-page'] ) ? esc_attr( $this->options['allow-on-blog-page'] ) : 0;
printf(
'<input type="checkbox" id="allow-on-blog-page" name="wp_hypothesis_options[allow-on-blog-page]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'allow-on-front-page'.
*/
public function allow_on_front_page_callback() {
$val = isset( $this->options['allow-on-front-page'] ) ? esc_attr( $this->options['allow-on-front-page'] ) : 0;
printf(
'<input type="checkbox" id="allow-on-front-page" name="wp_hypothesis_options[allow-on-front-page]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'allow-on-<posttype>'.
*/
public function allow_on_posttype_callback( $args ) {
$slug = $args[0];
$val = isset( $this->options[ "allow-on-$slug" ] ) ? esc_attr( $this->options[ "allow-on-$slug" ] ) : 0;
printf(
'<input type="checkbox" id="allow-on-%s" name="wp_hypothesis_options[allow-on-%s]" value="1" %s/>',
esc_attr( $slug ),
esc_attr( $slug ),
checked( $val, 1, false )
);
}
/**
* Callback for '<posttype>_ids_show_h'.
*
* @param array $args An arry containing the post type slug and the post type name (plural).
*/
public function posttype_ids_show_h_callback( $args ) {
$slug = $args[0];
$val = isset( $this->options[ $slug . '_ids_show_h' ] ) ? esc_attr( implode( ',', $this->options[ $slug . '_ids_show_h' ] ) ) : '';
printf(
'<input type="text" id="%s_ids_show_h" name="wp_hypothesis_options[%s_ids_show_h]" value="%s" />',
esc_attr( $slug ),
esc_attr( $slug ),
esc_attr( $val )
);
}
/**
* Callback for '<posttype>_ids_override'.
*
* @param array $args An arry containing the post type slug and the post type name (plural).
*/
public function posttype_ids_override_callback( $args ) {
$slug = $args[0];
$val = isset( $this->options[ $slug . '_ids_override' ] ) ? esc_attr( implode( ',', $this->options[ $slug . '_ids_override' ] ) ) : '';
printf(
'<input type="text" id="%s_ids_override" name="wp_hypothesis_options[%s_ids_override]" value="%s" />',
esc_attr( $slug ),
esc_attr( $slug ),
esc_attr( $val )
);
}
}