forked from DiscipleTools/disciple-tools-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab-sso-login.php
232 lines (207 loc) · 9.21 KB
/
tab-sso-login.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
<?php
if ( !defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Disciple_Tools_SSO_Login
*/
class Disciple_Tools_SSO_Login extends Disciple_Tools_Abstract_Menu_Base
{
private $token = 'sso-login';
private $tab_title = 'SSO Login';
private $is_site_admin;
private static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} // End instance()
/**
* Constructor function.
*
* @access public
* @since 0.1.0
*/
public function __construct() {
if ( !current_user_can( 'manage_dt' ) ){
return;
}
add_action( 'admin_menu', [ $this, 'add_submenu' ], 99 );
add_action( 'dt_settings_tab_menu', [ $this, 'add_tab' ], 50, 1 ); // use the priority setting to control load order
add_action( 'dt_settings_tab_content', [ $this, 'content' ], 99, 1 );
$user_id = get_current_user_id();
$this->is_site_admin = is_super_admin( $user_id );
parent::__construct();
} // End __construct()
public function add_submenu() {
add_submenu_page( 'dt_options', __( 'SSO Login', 'disciple_tools' ), __( 'SSO Login', 'disciple_tools' ), 'manage_dt', 'dt_options&tab='.$this->token, [ 'Disciple_Tools_Settings_Menu', 'content' ] );
}
public function add_tab( $tab ) {
echo '<a href="'. esc_url( admin_url() ).'admin.php?page=dt_options&tab=sso-login" class="nav-tab ';
if ( $tab == 'sso-login' ) {
echo 'nav-tab-active';
}
echo '">'. esc_attr__( 'SSO Login', 'disciple_tools' ) .'</a>';
}
public function content( $settings_tab ) {
if ( 'sso-login' !== $settings_tab ) {
return;
}
if ( !current_user_can( 'manage_dt' ) ) {
var_dump( user_can( get_current_user_id(), 'manage_dt' ) );
wp_die( 'You do not have sufficient permissions to access this page.' );
}
$link = 'admin.php?page=dt_options&tab='.$this->token.'&sub_tab=';
if ( isset( $_GET['sub_tab'] ) ) {
$tab = sanitize_key( wp_unslash( $_GET['sub_tab'] ) );
} else {
$tab = 'general';
}
$vars = $this->process_postback();
$tabs = [];
foreach ( $vars as $val ) {
$tabs[$val['tab']] = ucwords( str_replace( '_', ' ', $val['tab'] ) );
}
?>
<div class="wrap">
<h2><?php echo esc_html( $this->tab_title ) ?></h2>
<?php if ( is_multisite() ) : ?>
<p>
Please configure the SSO Login settings from the
<?php if ( class_exists( 'DT_Multisite' ) ) : ?>
<a href="<?php echo esc_url( network_admin_url( 'admin.php?page=disciple-tools-multisite&tab=sso-login' ) ) ?>">
Disciple.Tools Network Admin Dashboard </a>
<?php else : ?>
Disciple.Tools Network Admin Dashboard
<?php endif; ?>
using the <a href="https://disciple.tools/plugins/multisite/" target="_blank">D.T Multisite plugin.</a>
</p>
<?php endif; ?>
<h2 class="nav-tab-wrapper">
<?php
foreach ( $tabs as $key => $value ) {
?>
<a href="<?php echo esc_attr( $link . $key ) ?>"
class="nav-tab <?php echo esc_html( ( $tab == $key ) ? 'nav-tab-active' : '' ); ?>"><?php echo esc_html( $value ) ?></a>
<?php
}
?>
</h2>
<div class="wrap">
<div id="poststuff">
<div id="post-body" class="metabox-holder">
<div id="post-body-content">
<!-- Box -->
<form method="post">
<?php wp_nonce_field( $this->token.get_current_user_id(), $this->token . '_nonce' ) ?>
<table class="widefat striped">
<tbody>
<?php
$any_multisite_level_args = false;
if ( ! empty( $vars ) ) {
foreach ( $vars as $key => $value ) {
if ( $tab === $value['tab'] ) {
$any_multisite_level_args = is_multisite() && !empty( $value['multisite_level'] );
$this->tab( $value );
}
}
}
?>
<tr>
<td colspan="2">
<button class="button" type="submit">Save</button> <button class="button" <?php echo esc_attr( $any_multisite_level_args ? 'disabled' : '' ) ?> type="submit" style="float:right;" name="delete" value="1">Reset</button>
</td>
</tr>
</tbody>
</table>
</form>
<br>
<!-- End Main Column -->
</div><!-- end post-body-content -->
</div><!-- post-body meta box container -->
</div><!--poststuff end -->
</div><!-- wrap end -->
</div><!-- End wrap -->
<?php
}
public function tab( $args ) {
$must_have_super_admin_rights = is_multisite() && !empty( $args['multisite_level'] );
switch ( $args['type'] ) {
case 'text':
?>
<tr>
<td style="width:10%; white-space:nowrap;">
<strong><?php echo esc_html( $args['label'] ) ?></strong>
</td>
<td>
<input
type="<?php echo esc_attr( $must_have_super_admin_rights ? 'password' : 'text' ) ?>"
name="<?php echo esc_attr( $args['key'] ) ?>"
value="<?php echo esc_attr( $must_have_super_admin_rights ? 'hidden secret value' : $args['value'] ) ?>"
<?php echo $must_have_super_admin_rights ? 'disabled' : '' ?>
/>
<?php echo esc_attr( $args['description'] ) ?>
</td>
</tr>
<?php
break;
case 'select':
?>
<tr>
<td style="width:10%; white-space:nowrap;">
<strong><?php echo esc_html( $args['label'] ) ?></strong>
</td>
<td>
<select
name="<?php echo esc_attr( $args['key'] ) ?>"
<?php echo $must_have_super_admin_rights ? 'disabled' : '' ?>
>
<option></option>
<?php
foreach ( $args['default'] as $item_key => $item_value ) {
?>
<option value="<?php echo esc_attr( $item_key ) ?>" <?php echo ( $item_key === $args['value'] ) ? 'selected' : '' ?>><?php echo esc_html( $item_value ) ?></option>
<?php
}
?>
</select>
<?php echo esc_html( $args['description'] ) ?>
</td>
</tr>
<?php
break;
case 'label':
?>
<tr>
<td style="width:10%; white-space:nowrap;">
<strong><?php echo esc_html( $args['label'] ) ?></strong>
</td>
<td>
<?php echo esc_html( $args['description'] ) ?>
<?php echo ( isset( $args['description_2'] ) && ! empty( $args['description_2'] ) ) ? '<p>' . esc_html( $args['description_2'] ) . '</p>' : '' ?>
</td>
</tr>
<?php
break;
default:
break;
}
}
public function process_postback(){
// process POST
$has_permission = current_user_can( 'manage_dt' );
if ( $has_permission && isset( $_POST[$this->token.'_nonce'] )
&& wp_verify_nonce( sanitize_key( wp_unslash( $_POST[$this->token.'_nonce'] ) ), $this->token . get_current_user_id() ) ) {
$params = dt_recursive_sanitize_array( $_POST );
if ( isset( $params['delete'] ) ) {
DT_Login_Fields::delete();
} else {
DT_Login_Fields::update( $params );
}
}
$vars = DT_Login_Fields::all();
return $vars;
}
}
Disciple_Tools_SSO_Login::instance();