-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodebb.php
156 lines (133 loc) · 5.22 KB
/
nodebb.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
<?php
if (!defined('ABSPATH') || !defined('CASTORS_THEME_VERSION')) exit;
define('NODEBB_API_USER_ID', 1);
class Castor_NodeBB {
public static function formatEndpoint($endpoint) {
return sprintf('%s?ts=%d&_uid=%d', $endpoint, current_time('timestamp'), NODEBB_API_USER_ID);
}
public static function parseArgs($args) {
$token = get_option('castors_nbb_api_token');
$defaults = [
'headers' => [
'Authorization' => "Bearer {$token}",
'Accept' => 'application/json',
],
];
return wp_parse_args($args, $defaults);
}
public static function read($endpoint, $args = []) {
$response = wp_remote_get(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
if ($response['response']['code'] === 404) {
return null;
}
return json_decode($response['body']);
}
public static function create($endpoint, $body, $args = []) {
if ($body) {
$args['body'] = $body;
$args['Content-Type'] = 'application/json';
}
$response = wp_remote_post(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return json_decode($response['body']);
}
public static function update($endpoint, $body, $args = []) {
$args['method'] = 'PUT';
if ($body) {
$args['body'] = $body;
$args['Content-Type'] = 'application/json';
}
$response = wp_remote_request(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return json_decode($response['body']);
}
public static function delete($endpoint, $args = []) {
$args['method'] = 'DELETE';
$response = wp_remote_request(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return null;
}
public static function getAccountById($id) {
return static::read(get_site_url() . "/forum/api/v3/users/{$id}");
}
public static function getAccountByUsername($username) {
return static::read(get_site_url() . "/forum/api/user/{$username}");
}
public static function updateEmail($user, $nodeBBUser) {
if ($user->user_email !== $nodeBBUser->email) {
$result = static::create(get_site_url() . "/forum/api/v3/users/{$nodeBBUser->uid}/emails", ["email" => $user->user_email, "skipConfirmation" => 1]);
return $result;
}
return null;
}
public static function createAccount($user) {
$data = [
'username' => $user->user_login,
'fullname' => $user->display_name,
];
return static::create(get_site_url() . "/forum/api/v3/users", $data);
}
public static function updateAccount($user, $location) {
$nodeBBUser = static::getAccountByUsername($user->user_login);
$data = [
'fullname' => $user->display_name,
'website' => $user->user_url ?: '',
'location' => $location ? $location->value : '',
'aboutme' => $user->description ?: '',
];
if ($location) {
$data['fullname'] .= " ({$location->department})";
}
if (!$nodeBBUser) {
$nodeBBUser = static::createAccount($user);
}
if (!$nodeBBUser) {
return null;
}
static::updateEmail($user, $nodeBBUser);
return static::update(get_site_url() . "/forum/api/v3/users/{$nodeBBUser->uid}", $data);
}
public static function getGroups() {
return static::read(get_site_url() . "/forum/api/v3/groups");
}
public static function updateGroups($user) {
if (!class_exists('Groups_User')) {
// Groups extension is not active
return null;
}
$nodeBBUser = static::getAccountByUsername($user->user_login);
$groupsUser = new Groups_User($user->ID);
$nodeBBgroups = static::getGroups()->response->groups;
$nodeBBgroupNames = array_column($nodeBBgroups, 'name');
$groupNames = [];
foreach ($groupsUser->groups as $group) {
$groupNames[] = $group->group->name;
if (in_array($group->group->name, $nodeBBUser->groupTitleArray)) {
// Already in NodeBB group
continue;
}
$index = array_search($group->group->name, $nodeBBgroupNames);
if ($index === false) {
// Not a NodeBB group
}
// Add user to NodeBB group
$slug = $nodeBBgroups[$index]->slug;
static::update(get_site_url() . "/forum/api/v3/groups/{$slug}/membership/{$nodeBBUser->uid}", []);
}
foreach ($nodeBBUser->groups as $group) {
if (!in_array($group->name, $groupNames)) {
// User no longer in group
static::delete(get_site_url() . "/forum/api/v3/groups/{$group->slug}/membership/{$nodeBBUser->uid}");
}
}
}
}