forked from hypothesis/wp-hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypothesis.php
466 lines (407 loc) · 13.4 KB
/
hypothesis.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/**
* @package Hypothesis
* @version 0.4.8
*/
/*
Plugin Name: Hypothesis
Plugin URI: http://hypothes.is/
Description: Hypothesis is an open platform for the collaborative evaluation of knowledge. This plugin embeds the necessary scripts in your Wordpress site to enable any user to use Hypothesis without installing any extensions.
Author: The Hypothesis Project and contributors
Version: 0.4.8
Author URI: http://hypothes.is/
*/
// Exit if called directly.
defined( 'ABSPATH' ) or die( 'Cannot access pages directly.' );
/**
* 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',
'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' => 'posts',
'page' => 'pages',
) );
}
/**
* 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', // Title.
array( $this, 'settings_section_info' ), // Callback.
'hypothesis-setting-admin' // Page.
);
add_settings_field(
'highlights-on-by-default',
'Highlights on by default',
array( $this, 'highlights_on_by_default_callback' ),
'hypothesis-setting-admin',
'hypothesis_settings_section'
);
add_settings_field(
'sidebar-open-by-default',
'Sidebar open by default',
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',
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', // Title.
array( $this, 'content_section_info' ), // Callback.
'hypothesis-setting-admin' // Page.
);
add_settings_field(
'allow-on-front-page',
'Allow on front page',
array( $this, 'allow_on_front_page_callback' ),
'hypothesis-setting-admin',
'hypothesis_content_section'
);
add_settings_field(
'allow-on-blog-page',
'Allow on blog page',
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",
"Allow on $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.
"Allow on specific $name (list of comma-separated $slug IDs, no spaces)", // 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.
"Disallow on specific $name (list of comma-separated $slug IDs, no spaces)", // 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' ] ) ) {
$new_input[ $slug . '_ids_show_h' ] = explode( ',', esc_attr( $input[ $slug . '_ids_show_h' ] ) );
}
if ( isset( $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() {
print 'Customize Hypothesis defaults and behavior.';
}
/**
* Print the Content Settings section text
*/
public function content_section_info() {
print 'Control where Hypothesis is loaded.';
}
/**
* 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 )
);
}
}
if ( is_admin() ) {
$hypothesis_settings_page = new HypothesisSettingsPage();
}
/**
* Add Hypothesis based on conditions set in the plugin settings.
*/
add_action( 'wp', 'add_hypothesis' );
/**
* Wrapper for the primary Hypothesis wp_enqueue call.
*/
function enqueue_hypothesis() {
wp_enqueue_script( 'hypothesis', 'https://hypothes.is/embed.js', array(), false, true );
}
/**
* Add Hypothesis script(s) to front end.
*/
function add_hypothesis() {
$options = get_option( 'wp_hypothesis_options' );
$posttypes = HypothesisSettingsPage::get_posttypes();
// Set defaults if we $options is not set yet.
if ( empty( $options ) ) :
$defaults = array(
'highlights-on-by-default' => 1,
);
add_option( 'wp_hypothesis_options', $defaults );
endif;
// Otherwise highlighting is on by default.
wp_enqueue_script( 'nohighlights', '/wp-content/plugins/hypothesis/js/nohighlights.js', '', false, true );
// Embed options.
if ( isset( $options['highlights-on-by-default'] ) ) :
wp_enqueue_script( 'showhighlights', '/wp-content/plugins/hypothesis/js/showhighlights.js', '', false, true );
endif;
if ( isset( $options['sidebar-open-by-default'] ) ) :
wp_enqueue_script( 'sidebaropen', '/wp-content/plugins/hypothesis/js/sidebaropen.js', '', false, true );
endif;
if ( isset( $options['serve-pdfs-with-via'] ) ) :
wp_enqueue_script( 'pdfs-with-via', '/wp-content/plugins/hypothesis/js/via-pdf.js', '', false, true );
endif;
// Content settings.
$enqueue = false;
if ( is_front_page() && isset( $options['allow-on-front-page'] ) ) {
enqueue_hypothesis();
} elseif ( is_home() && isset( $options['allow-on-blog-page'] ) ) {
enqueue_hypothesis();
}
foreach ( $posttypes as $slug => $name ) {
if ( 'page' !== $slug ) {
$posttype = $slug;
if ( 'post' === $slug ) {
$slug = 'posts'; // Backwards compatibility.
}
if ( isset( $options[ "allow-on-$slug" ] ) && is_singular( $posttype ) ) { // Check if Hypothesis is allowed on this post type.
if ( isset( $options[ $posttype . '_ids_override' ] ) && ! is_single( $options[ $posttype . '_ids_override' ] ) ) { // Make sure this post isn't in the override list if it exists.
enqueue_hypothesis();
}
} elseif ( ! isset( $options[ "allow-on-$slug" ] ) && isset( $options[ $posttype . '_ids_show_h' ] ) && is_single( $options[ $posttype . '_ids_show_h' ] ) ) { // Check if Hypothesis is allowed on this specific post.
enqueue_hypothesis();
}
} elseif ( 'page' === $slug ) {
if ( isset( $options['allow-on-pages'] ) && is_page() && ! is_front_page() && ! is_home() ) { // Check if Hypothesis is allowed on pages (and that we aren't on a special page).
if ( isset( $options['page_ids_override'] ) && ! is_page( $options['page_ids_override'] ) ) { // Make sure this page isn't in the override list if it exists.
enqueue_hypothesis();
}
} elseif ( ! isset( $options['allow-on-pages'] ) && isset( $options['page_ids_show_h'] ) && is_page( $options['page_ids_show_h'] ) ) { // Check if Hypothesis is allowed on this specific page.
enqueue_hypothesis();
}
}
}
}