-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetttings.php
More file actions
266 lines (217 loc) · 7.12 KB
/
setttings.php
File metadata and controls
266 lines (217 loc) · 7.12 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
<?php
class AddressixOAuthSettings
{
private static $instance = false;
private $options;
static public function init()
{
if (self::$instance===false) {
self::$instance = new AddressixOAuthSettings();
}
}
function __construct()
{
add_action('admin_menu', array($this, 'admin_add_page'));
add_action('admin_init', array($this, 'register_settings'));
}
function admin_add_page()
{
// remove_menu_page( 'users.php' );
add_menu_page('Benutzer', 'Benutzer', 'list_users', 'addressix_users', array($this, 'draw_plugin_admin'), 20);
add_options_page(
'Addressix OAuth2 Options',
'Addressix OAuth2',
'manage_options',
'addressixoauth_admin',
array($this, 'create_admin_page')
);
}
function create_admin_page()
{
$this->options = get_option('addressixoauth');
?>
<div class="wrap">
<h2>Addressix OAuth2</h2>
<form method="post" action="options.php">
<?php
settings_fields('addressixoauth_group');
do_settings_sections('addressixoauth_admin');
submit_button();
?>
</form>
</div><?php
}
function register_settings()
{
register_setting('addressixoauth_group', 'addressixoauth');
add_settings_section(
'addressixoauth_main',
'Main Settings',
function() {
echo '<p>Client credentials for Addressix</p>';
},
'addressixoauth_admin');
add_settings_field(
'clientid',
'Client ID',
array($this, 'clientid_form'),
'addressixoauth_admin',
'addressixoauth_main'
);
add_settings_field(
'secret',
'Secret Key',
array($this, 'secret_form'),
'addressixoauth_admin',
'addressixoauth_main'
);
add_action('admin_post_users_edit', array($this, 'edit_users'));
add_action('admin_post_users_create', array($this, 'create_user'));
// plugin_update
require(plugin_dir_path( dirname( __FILE__ ) ) . 'addressixoauth/aixupdater.php');
$this->updater = new AixUpdater();
add_filter('pre_site_transient_update_plugins', 'display_transient_update_plugins');
}
function clientid_form()
{
printf('<input type="text" id="clientid" name="addressixoauth[clientid]" value="%s">',
isset($this->options['clientid']) ? esc_attr($this->options['clientid']) : '');
}
function secret_form()
{
printf('<input type="text" id="secret" name="addressixoauth[secret]" value="%s">',
isset($this->options['secret']) ? esc_attr($this->options['secret']) : '');
}
// user admin pages
function draw_plugin_admin()
{
if (isset($_GET['user_id']) && ((int)$_GET['user_id'])) {
$url = '/events/v1/events/' . (int)$_GET['user_id'];
$response = $this->api->fetch($url);
if ($response->code==200) {
$this->event = $response->body;
require(plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/edit-user.php');
}
else {
error_log('could not open event ' . $_GET['event_id'] . ' Code(' . $response->code .')');
}
$this->event = $response->body;
} else if (isset($_GET['new'])) {
require(plugin_dir_path( dirname( __FILE__ ) ) . 'addressixoauth/partials/new-user.php');
}
else {
// nothing other matched - draw user list overview
require(plugin_dir_path( dirname( __FILE__ ) ) . 'addressixoauth/usertable.php');
require(plugin_dir_path( dirname( __FILE__ ) ) . 'addressixoauth/partials/users.php');
}
}
function create_user() {
// Check nonce field
check_admin_referer('create-user', '_wpnonce_create-user');
$m = 'nop';
$this->api = AddressixAPI::init();
// check if we find addressix user
$url = '/contact/v1/people?email=' . $_POST['email'];
$response = $this->api->fetch($url, $params, 'GET');
if ($response->code==200) {
$m='';
$people = $response->body;
if (!count($people)) {
$m='notfound';
} else {
foreach($people as $person) {
// register user
$username = $person->primaryemail;
$user_login = $username;
$user_login = preg_replace('#[<>"\'%;()&\s\\\\]|\\.\\./#', "", $user_login);
$user_login = trim (trim($user_login), '.');
$pw = wp_generate_password();
$user_id = wp_insert_user(
array(
'user_login' => $username,
'user_nicename' => trim($person->firstname . ' ' . $person->surname),
'user_email' => $person->primaryemail,
'display_name' => $person->firstname,
'nickname' => $person->firstname,
'first_name' => $person->firstname,
'last_name' => $person->surname,
'password' => $pw
)
);
if (is_wp_error($user_id)) {
error_log($user_id->get_error_message());
$m='create_failed';
}
add_user_meta($user_id, 'wpoa_identity', 'Addressix|' . $person->addressixid . '|' . time());
}
}
}
else {
$m = $response->code;
error_log('could not get person for email ' . $_POST['email'] . ' Code(' . $response->code .')');
}
if ($m=='') {
wp_redirect(admin_url('admin.php?page=addressix_users'));
} else {
$email = $_POST['email'];
wp_redirect(admin_url('admin.php?page=addressix_users&new=1&email'));
}
}
function edit_users() {
error_log('edit users called');
// Check nonce field
// check_admin_referer('edit-users-verify');
require(plugin_dir_path( dirname( __FILE__ ) ) . 'addressixoauth/usertable.php');
$wp_list_table = new AIXUser_List_Table();
$redirect = admin_url('admin.php?page=addressix_users');
switch($wp_list_table->current_action()) {
case promote:
error_log('promote user');
if ( ! current_user_can( 'promote_users' ) )
wp_die( __( 'You can’t edit that user.' ) );
if ( empty($_REQUEST['users']) ) {
wp_redirect($redirect);
exit();
}
$editable_roles = get_editable_roles();
$role = false;
if ( ! empty( $_REQUEST['new_role2'] ) ) {
$role = $_REQUEST['new_role2'];
} elseif ( ! empty( $_REQUEST['new_role'] ) ) {
$role = $_REQUEST['new_role'];
}
if ( ! $role || empty( $editable_roles[ $role ] ) ) {
wp_die( __( 'You can’t give users that role.' ) );
}
$userids = $_REQUEST['users'];
$update = 'promote';
foreach ( $userids as $id ) {
$id = (int) $id;
if ( ! current_user_can('promote_user', $id) )
wp_die(__('You can’t edit that user.'));
// The new role of the current user must also have the promote_users cap or be a multisite super admin
if ( $id == $current_user->ID && ! $wp_roles->role_objects[ $role ]->has_cap('promote_users')
&& ! ( is_multisite() && is_super_admin() ) ) {
$update = 'err_admin_role';
continue;
}
// If the user doesn't already belong to the blog, bail.
if ( is_multisite() && !is_user_member_of_blog( $id ) ) {
wp_die(
'<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' .
'<p>' . __( 'One of the selected users is not a member of this site.' ) . '</p>',
403
);
}
$user = get_userdata( $id );
$user->set_role( $role );
}
$m = 'rolechanged';
break;
default:
$m = '';
}
wp_redirect(admin_url('admin.php?page=addressix_users&m='.$m));
exit;
}
}