This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
class-b2c-settings-page.php
231 lines (202 loc) · 7.61 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
<?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_aad_tenant', // ID
'Tenant Name / Tenant ID', // Title
array( $this, 'b2c_aad_tenant_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_verify_tokens', // ID
'Verify ID Tokens', // Title
array( $this, 'b2c_verify_tokens_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_aad_tenant'] ) )
$new_input['b2c_aad_tenant'] = sanitize_text_field(strtolower( $input['b2c_aad_tenant'] ));
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'] ));
$new_input['b2c_verify_tokens'] = $input['b2c_verify_tokens'];
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter the settings your created for your blog in the <a href="https://portal.azure.com" target="_blank">Azure Portal</a>';
}
/**
* 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_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" />',
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" />',
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_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 ) . ' />';
}
}