Admin.php
6.89 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
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
<?php
/**
* Implements the Cassava plugin's administration interface components.
*
* @version 1.0.1
* @since 1.0.0
*/
namespace Cassava;
/**
* Plugin administration class.
*
* @since 1.0.0
*/
class Admin {
/**
* Instantiates the admin panel object.
*
* @uses add_action()
*/
public function __construct () {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
/**
* Initializes the admin panel and registers settings fields.
*
* Triggered by the `admin_init` action.
*
* @uses add_action()
*/
public function admin_init() {
$this->savePermalinks();
$this->addSettings();
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
/**
* Register the menu entry for the plugin's settings page.
*
* @since 1.1.0
*/
public function admin_menu() {
add_options_page(
__( 'Cassava CAS Server', 'wp-cas-server' ),
__( 'Cassava CAS Server', 'wp-cas-server' ),
'manage_options',
Plugin::SLUG,
array( $this, 'pageSettings' )
);
}
/**
* Presents admin notices.
*
* Triggered by the `admin_notices` action.
*
* @uses current_user_can()
* @uses is_ssl()
*
* @uses ::adminNoticeNoSSL()
*
* @SuppressWarnings(CamelCaseMethodName)
*/
public function admin_notices() {
/*
if ( ! is_ssl() && current_user_can( 'install_plugins' ) ) {
$this->adminNoticeNoSSL();
}
*/
}
/**
* Nags the user with an administration notice explaining that the plugin will only
* work if HTTP
*/
protected function adminNoticeNoSSL() {
?>
<div class="update-nag">
<?php _e( 'Cassava CAS Server requires that this site be configured for HTTPS. For more information, contact your system administrator or hosting provider.', 'wp-cas-server' ); ?>
</div>
<?php
}
/**
* Updates the CAS server endpoint when saving permalinks.
*
* @uses is_admin()
* @uses sanitize_text_field()
*/
protected function savePermalinks() {
if ( ! is_admin() ) {
return;
}
$option = Options::KEY . '_endpoint_slug';
if ( false
|| isset( $_POST['permalink_structure'] )
|| isset( $_POST['category_base'] )
|| isset( $_POST[ $option ] )
) {
Options::set( 'endpoint_slug', trim( sanitize_text_field( $_POST[ $option ] ) ) );
}
}
/**
* Register plugin settings.
*
* @uses add_settings_field()
* @uses add_settings_section()
* @uses register_setting()
*
* @since 1.0.0
*/
protected function addSettings() {
register_setting(
Plugin::SLUG,
Options::KEY,
array( $this, 'validateSettings' )
);
// Default plugin settings:
add_settings_section( 'default', '', false, Plugin::SLUG );
add_settings_field(
'attributes',
__( 'User Attributes To Return', 'wp-cas-server' ),
array( $this, 'fieldUserAttributes' ),
Plugin::SLUG
);
// Permalink settings:
add_settings_field(
Options::KEY . '_endpoint_slug',
__( 'CAS server base', 'wp-cas-server' ),
array( $this, 'fieldPermalinksEndpointSlug' ),
'permalink',
'optional'
);
}
/**
* Validates and updates CAS server plugin settings.
*
* @param array $input Unvalidated input arguments when settings are updated.
*
* @return array Validated plugin settings to be saved in the database.
*
* @since 1.1.0
*/
public function validateSettings( $input ) {
$options = Options::getAll();
$options['attributes'] = (array) $input['attributes'];
return $options;
}
/**
* Display the configuration field for the CAS endpoint.
*
* @uses esc_attr()
*
* @since 1.0.0
*/
public function fieldPermalinksEndpointSlug() {
$option = Options::KEY . '_endpoint_slug';
$endpoint = Options::get( 'endpoint_slug' );
?>
<input id="<?php echo $option; ?>" name="<?php echo $option; ?>"
type="text" class="regular-text code"
value="<?php if ( isset( $endpoint ) ) echo esc_attr( $endpoint ); ?>"
placeholder="<?php echo Plugin::ENDPOINT_SLUG; ?>" />
<?php
}
/**
* Displays the CAS server settings page in the dashboard.
*
* @uses _e()
* @uses do_settings_sections()
* @uses settings_fields()
* @uses submit_button()
*
* @since 1.1.0
*/
public function pageSettings() {
?>
<div class="wrap">
<h2><?php _e( 'Cassava CAS Server Settings', 'wp-cas-server' ); ?></h2>
<p><?php _e( 'Configuration panel for the Central Authentication Service provided by this site.', 'wp-cas-server' ); ?></p>
<form action="options.php" method="POST">
<?php do_settings_sections( Plugin::SLUG ); ?>
<?php settings_fields( Plugin::SLUG ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Display the configuration fieldset for the user attributs to return on successful
* requests.
*
* Checked attributes for the authenticated user will be returned on successful
* `/validateService` request responses inside an optional `<cas:attributes></cas:attributes>`
* tag.
*
* @uses _e()
* @uses apply_filters()
*
* @since 1.1.0
*/
public function fieldUserAttributes() {
$user = wp_get_current_user();
$attributes = Options::get( 'attributes' );
$attributeOptions = array(
'first_name' => __( 'First Name', 'wp-cas-server' ),
'last_name' => __( 'Last Name', 'wp-cas-server' ),
'display_name' => __( 'Public Name', 'wp-cas-server' ),
'user_email' => __( 'Email', 'wp-cas-server' ),
'user_url' => __( 'Website', 'wp-cas-server' ),
);
/**
* Allows developers to change the list of user attributes that appear in the dashboard for
* an administrator to set to return on successful validation requests.
*
* Options are stored in an associative array, with user attribute slugs as array keys and
* option labels as array values.
*
* These settings are valid only for CAS 2.0 validation requests.
*
* @param array $attributeOptions Attribute options an administrator can set on the dashboard.
*
* @return array Attribute options to display.
*
* @since 1.1.0
*/
$attributeOptions = apply_filters( 'cas_server_settings_user_attribute_options', $attributeOptions );
?>
<fieldset>
<legend class="screen-reader-text"><?php _e( 'User Attributes', 'wp-cas-server' ) ?></legend>
<?php foreach ($attributeOptions as $value => $label) : ?>
<label>
<input id="<?php echo Options::KEY . '-attribute-' . $value ?>"
name="<?php echo Options::KEY ?>[attributes][]"
type="checkbox" <?php if (in_array( $value, $attributes )) echo "checked" ?>
value="<?php echo $value ?>">
<span><?php echo $label ?></span>
<?php if ($user->get( $value )) : ?>
<span class="description"><?php
printf( __( '(e.g. %s)', 'wp-cas-server' ), implode( ',', (array) $user->get( $value ) ) );
?></span>
<?php endif; ?>
</label><br>
<?php endforeach; ?>
<p class="description"><?php _e( 'Checked attributes are disclosed on successful validation requests (CAS 2.0 only).', 'wp-cas-server' ) ?></p>
</fieldset>
<?php
}
}