-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypo-mods.php
212 lines (184 loc) · 5.57 KB
/
hypo-mods.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
<?php
/*
* Plugin Name: Hypothesis Mods
* Plugin URI: none
* Description: A plugin that allows some modification of the Hypothesis client
* Author: Steel Wagstaff
* Version: 0.1.0
* Author URI: https://steelwagstaff.info
*/
// Exit if called directly.
defined( 'ABSPATH' ) or die( 'Cannot access pages directly.' );
/**
* Create settings page (see https://codex.wordpress.org/Creating_Options_Pages)
*/
class HypothesisModsSettingsPage {
/**
* Holds the values to be used in the fields callbacks
*
* @var array
*/
private $options;
/**
* 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 Mods Settings Admin',
'Hypothesis Mods Settings',
'manage_options',
'hypothesis-mods-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property.
$this->options = get_option( 'hypothesis_mods_options' ); ?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( 'hypothesis_mods_option_group' );
do_settings_sections( 'hypothesis-mods-setting-admin' );
submit_button();
?>
</form>
</div>
<?php }
/**
* Register and add settings
*/
public function page_init() {
register_setting(
'hypothesis_mods_option_group', // Option group.
'hypothesis_mods_options', // Option name.
array( $this, 'sanitize' ) // Sanitize callback.
);
/**
* Hypothesis Mod Settings
*/
add_settings_section(
'hypothesis_mods_settings_section', // ID.
'Hypothesis Mod Settings', // Title.
array( $this, 'print_section_info' ), // Callback.
'hypothesis-mods-setting-admin' // Page.
);
add_settings_field(
'adjust-page-width',
'Adjust the width of the page when annotation pane is expanded',
array( $this, 'adjust_page_width_callback' ),
'hypothesis-mods-setting-admin',
'hypothesis_mods_settings_section'
);
add_settings_field(
'darken-highlights',
'Darken highlights (to make more visible)',
array( $this, 'darken_highlights_callback' ),
'hypothesis-mods-setting-admin',
'hypothesis_mods_settings_section'
);
add_settings_field(
'hide-annotation-header',
__( 'Hide annotation author & date information', 'hypothesis' ),
array( $this, 'hide_annotation_header_callback' ),
'hypothesis-mods-setting-admin',
'hypothesis_mods_settings_section'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['adjust-page-width'] ) ) {
$new_input['adjust-page-width'] = absint( $input['adjust-page-width'] );
}
if ( isset( $input['darken-highlights'] ) ) {
$new_input['darken-highlights'] = absint( $input['darken-highlights'] );
}
if ( isset( $input['hide-annotation-header'] ) ) {
$new_input['hide-annotation-header'] = absint( $input['hide-annotation-header'] );
}
return $new_input;
}
/**
* Print the Display Settings section text
*/
public function print_section_info() {
printf( 'Control additional Hypothesis display settings.');
}
/**
* Callback for 'adjust-page-width'.
*/
public function adjust_page_width_callback ( $args ) {
$val = isset( $this->options['adjust-page-width'] ) ? esc_attr( $this->options['adjust-page-width'] ) : 0;
printf(
'<input type="checkbox" id="adjust-page-width" name="hypothesis_mods_options[adjust-page-width]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'darken-highlights'.
*/
public function darken_highlights_callback ( $args ) {
$val = isset( $this->options['darken-highlights'] ) ? esc_attr( $this->options['darken-highlights'] ) : 0;
printf(
'<input type="checkbox" id="darken-highlights" name="hypothesis_mods_options[darken-highlights]" value="1" %s/>',
checked( $val, 1, false )
);
}
/**
* Callback for 'hide-annotation-header'.
*/
public function hide_annotation_header_callback ( $args ) {
$val = isset( $this->options['hide-annotation-header'] ) ? esc_attr( $this->options['hide-annotation-header'] ) : 0;
printf(
'<input type="checkbox" id="hide-annotation-header" name="hypothesis_mods_options[hide-annotation-header]" value="1" %s/>',
checked( $val, 1, false )
);
}
}
if ( is_admin() ) {
$hypothesis_mods_settings_page = new HypothesisModsSettingsPage();
}
function hypothesis_modifications() {
$options = get_option( 'hypothesis_mods_options' );
if ( empty( $options ) ) :
$defaults = array();
add_option( 'hypothesis_mods_options', $defaults );
endif;
/**
* Calls script to resize page upon expansion of Hypothesis annotation pane
*/
if ( isset ( $options['adjust-page-width'] ) ) {
$src = plugin_dir_url( __FILE__ ) .'js/resize.js';
wp_enqueue_script( 'resize', $src, array('jquery'), false, true );
}
/**
* Loads CSS which hides annotation header
*/
// display settings
if ( isset( $options['hide-annotation-header'] ) ) {
$src = plugin_dir_url( __FILE__ ) .'css/hypo.css';
wp_enqueue_style( 'hypo', $src, 90 );
}
/**
* Loads CSS which darkens annotations to make more visible
*/
if (isset ($options['darken-highlights'] ) ) {
$src = plugin_dir_url( __FILE__ ) .'css/darken.css';
wp_enqueue_style( 'darken', $src, 91 );
}
}
add_action( 'wp_enqueue_scripts', 'hypothesis_modifications' );