-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-b2c-settings-page.php
357 lines (310 loc) · 13.1 KB
/
class-b2c-settings-page.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
<?php
/**
* A class to create and manage the admin's B2C settings page.
*/
class B2C_Settings_Page
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Adds a B2C options page under "Settings"
*/
public function add_plugin_page()
{
add_options_page(
'Settings Admin',
'B2C Authentication Settings',
'manage_options',
'b2c-settings-page',
array( $this, 'create_B2C_page' )
);
}
/**
* B2C Options page callback
*/
public function create_B2C_page()
{
// Set class property
$this->options = get_option( 'b2c_config_elements' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Azure AD B2C Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'b2c_option_group' );
do_settings_sections( 'b2c-settings-page' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register the B2C options page and add the B2C settings boxes
*/
public function page_init()
{
register_setting(
'b2c_option_group', // Option group
'b2c_config_elements', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'service_config_section', // ID
'Settings', // Title
array( $this, 'print_section_info' ), // Callback
'b2c-settings-page' // Page
);
add_settings_field(
'b2c_login_tenant', // ID
'Tenant Subdomain', // Title
array( $this, 'b2c_login_tenant_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_aad_tenant', // ID
'Tenant Name', // Title
array( $this, 'b2c_aad_tenant_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_scope', // ID
'B2C Scope', // Title
array( $this, 'b2c_scope_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_client_id', // ID
'Client ID (Application ID)', // Title
array( $this, 'b2c_client_id_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_subscriber_policy_id', // ID
'Sign-in Policy for Users', // Title
array( $this, 'b2c_subscriber_policy_id_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_admin_policy_id', // ID
'Sign-in Policy for Admins', // Title
array( $this, 'b2c_admin_policy_id_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_edit_profile_policy_id', // ID
'Edit Profile Policy', // Title
array( $this, 'b2c_edit_profile_policy_id_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_password_reset_policy_id', // ID
'Password Reset Policy', // Title
array( $this, 'b2c_password_reset_policy_id_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_verify_tokens', // ID
'Verify ID Tokens', // Title
array( $this, 'b2c_verify_tokens_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_default_role', // ID
'Default Wordpress User Role', // Title
array( $this, 'b2c_default_role_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // Section
);
add_settings_field(
'b2c_show_admin_bar', // ID
'Show WP Admin Bar For New User', // Title
array( $this, 'b2c_show_admin_bar_callback' ), // Callback
'b2c-settings-page', // Page
'service_config_section' // 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['b2c_login_tenant'] ) )
$new_input['b2c_login_tenant'] = sanitize_text_field(strtolower( $input['b2c_login_tenant'] ));
if( isset( $input['b2c_aad_tenant'] ) )
$new_input['b2c_aad_tenant'] = sanitize_text_field(strtolower( $input['b2c_aad_tenant'] ));
if( isset( $input['b2c_scope'] ) )
$new_input['b2c_scope'] = sanitize_text_field(strtolower( $input['b2c_scope'] ));
if( isset( $input['b2c_client_id'] ) )
$new_input['b2c_client_id'] = sanitize_text_field( $input['b2c_client_id'] );
if( isset( $input['b2c_subscriber_policy_id'] ) )
$new_input['b2c_subscriber_policy_id'] = sanitize_text_field(strtolower( $input['b2c_subscriber_policy_id'] ));
if( isset( $input['b2c_admin_policy_id'] ) )
$new_input['b2c_admin_policy_id'] = sanitize_text_field(strtolower( $input['b2c_admin_policy_id'] ));
if( isset( $input['b2c_edit_profile_policy_id'] ) )
$new_input['b2c_edit_profile_policy_id'] = sanitize_text_field(strtolower( $input['b2c_edit_profile_policy_id'] ));
if( isset( $input['b2c_password_reset_policy_id'] ) )
$new_input['b2c_password_reset_policy_id'] = sanitize_text_field(strtolower( $input['b2c_password_reset_policy_id'] ));
if( isset( $input['b2c_verify_tokens'] ) )
$new_input['b2c_verify_tokens'] = $input['b2c_verify_tokens'];
if( isset( $input['b2c_default_role'] ) )
$new_input['b2c_default_role'] = sanitize_text_field(strtolower( $input['b2c_default_role'] ));
if( isset( $input['b2c_show_admin_bar'] ) )
$new_input['b2c_show_admin_bar'] = $input['b2c_show_admin_bar'];
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter the B2C settings you created for your blog in the <a href="https://portal.azure.com/" target="_blank">Azure Portal</a>.<br>'.
'Make sure that the redirect url <strong>'.home_url().'/</strong> is registered in your B2C configuration.';
print '<style type="text/css">input[type=text]{width: 400px;}</style>';
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_login_tenant_callback()
{
printf(
'<input type="text" id="b2c_login_tenant" name="b2c_config_elements[b2c_login_tenant]" value="%s" />'
. '<br/><i><subdomain>.b2clogin.com</i>',
isset( $this->options['b2c_login_tenant'] ) ? esc_attr( $this->options['b2c_login_tenant']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_aad_tenant_callback()
{
printf(
'<input type="text" id="b2c_aad_tenant" name="b2c_config_elements[b2c_aad_tenant]" value="%s" />'
. '<br/><i>i.e. contoso.onmicrosoft.com</i>',
isset( $this->options['b2c_aad_tenant'] ) ? esc_attr( $this->options['b2c_aad_tenant']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_scope_callback()
{
printf(
'<input type="text" id="b2c_scope" name="b2c_config_elements[b2c_scope]" value="%s" />'
. '<br/><i>i.e. openid profile</i>',
isset( $this->options['b2c_scope'] ) ? esc_attr( urldecode($this->options['b2c_scope'])) : 'openid profile'
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_client_id_callback()
{
printf(
'<input type="text" id="b2c_client_id" name="b2c_config_elements[b2c_client_id]" value="%s" />',
isset( $this->options['b2c_client_id'] ) ? esc_attr( $this->options['b2c_client_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_admin_policy_id_callback()
{
printf(
'<input type="text" id="b2c_admin_policy_id" name="b2c_config_elements[b2c_admin_policy_id]" value="%s" />'
. '<br/><i>Can be the same as Sign-in Policy for Users but typically includes multi-factor authentication for extra protection of Wordpress administration mode.</i>',
isset( $this->options['b2c_admin_policy_id'] ) ? esc_attr( $this->options['b2c_admin_policy_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_subscriber_policy_id_callback()
{
printf(
'<input type="text" id="b2c_subscriber_policy_id" name="b2c_config_elements[b2c_subscriber_policy_id]" value="%s" />'
. '<br/><i>Specify a Sign-in Policy if you manage creation of Wordpress subscriber accounts yourself.</i>'
. '<br/><i>Specify a Sign-in/Sign-up policy to allow Wordpress users to create their own subscriber accounts.</i>',
isset( $this->options['b2c_subscriber_policy_id'] ) ? esc_attr( $this->options['b2c_subscriber_policy_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_edit_profile_policy_id_callback()
{
printf(
'<input type="text" id="b2c_edit_profile_policy_id" name="b2c_config_elements[b2c_edit_profile_policy_id]" value="%s" />',
isset( $this->options['b2c_edit_profile_policy_id'] ) ? esc_attr( $this->options['b2c_edit_profile_policy_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_password_reset_policy_id_callback()
{
printf(
'<input type="text" id="b2c_password_reset_policy_id" name="b2c_config_elements[b2c_password_reset_policy_id]" value="%s" />'
. '<br/><i>Used if your Sign-in Policy for Users is using a sign-in/sign-up policy and the user clicks the forgotten password link.</i>',
isset( $this->options['b2c_password_reset_policy_id'] ) ? esc_attr( $this->options['b2c_password_reset_policy_id']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_verify_tokens_callback()
{
if (empty($this->options['b2c_verify_tokens']))
$this->options['b2c_verify_tokens'] = 0;
$current_value = $this->options['b2c_verify_tokens'];
echo '<input type="checkbox" id="b2c_verify_tokens" name="b2c_config_elements[b2c_verify_tokens]" value="1" class="code" ' . checked( 1, $current_value, false ) . ' />';
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_default_role_callback()
{
global $wp_roles;
$all_roles = $wp_roles->roles;
$current_value = isset( $this->options['b2c_default_role'] ) ? esc_attr( $this->options['b2c_default_role']) : get_option('default_role');
echo '<select id="b2c_default_role" name="b2c_config_elements[b2c_default_role]">';
foreach ($all_roles as $key => $role){
echo '<option value="'.$key.'"'.($current_value == strtolower($key)?' selected="selected"':'').'>'.$role['name'].'</option>';
}
echo '</select>';
}
/**
* Get the settings option array and print one of its values
*/
public function b2c_show_admin_bar_callback()
{
if (empty($this->options['b2c_show_admin_bar']))
$this->options['b2c_show_admin_bar'] = 0;
$current_value = $this->options['b2c_show_admin_bar'];
echo '<input type="checkbox" id="b2c_show_admin_bar" name="b2c_config_elements[b2c_show_admin_bar]" value="1" class="code" ' . checked( 1, $current_value, false ) . ' />';
}
}