This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.php
276 lines (225 loc) · 5.5 KB
/
functions.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
<?php
use PEBO\UserSync;
use Firebase\JWT\JWT;
/**
* Add notice to show
*
* @param string $notice
* @param integer $type
* @return void
*/
function peerboard_add_notice($notice, $function_name, $type = "success", $args = [])
{
$notices = is_array(get_transient('peerboard_notices')) ? get_transient('peerboard_notices') : [];
$new_notice = sprintf(
'PeerBoard: (Only administrator see this message) <br>%s (%s) - %s%s<br> %s',
$notice,
$function_name,
$args['file'] ?? '',
isset($args['line']) ? ':' . $args['line'] : '',
__('please contact us at [email protected]', 'peerboard')
);
$notice_exist = false;
// Check if notice already exist
foreach ($notices as $notice) {
if ($notice['notice'] === $new_notice) {
$notice_exist = true;
}
}
if (!$notice_exist) {
$notices[] = [
'notice' => $new_notice,
'type' => $type
];
$extra = [
'args' => $args
];
// temporary solution
if ($notice !== 'provide auth token') {
PEBO\API::add_sentry_error($notice, $function_name, $extra);
}
}
set_transient('peerboard_notices', $notices, 60);
}
/**
* Get environment
*
* @return void
*/
function peerboard_get_environment()
{
$environment = 'prod';
if (defined('PEERBOARD_ENV')) {
$environment = PEERBOARD_ENV;
}
return $environment;
}
/**
* Is wp installed in sub directory
*
* @return string
*/
function peerboard_get_wp_installed_sub_dir()
{
$parsed_url = parse_url(home_url('/'));
if (!empty($parsed_url['path'])) {
return $parsed_url['path'];
}
return false;
}
/**
* Returning community full slug (with parent page slug, and sub directory slug)
*
* @return string
*/
function peerboard_get_comm_full_slug()
{
$post_id = intval(get_option('peerboard_post'));
$post = get_post($post_id);
if (!$post) {
return '';
}
$slug = $post->post_name;
$comm_slug = substr(get_permalink($post_id), strlen(home_url('/')));
if (peerboard_get_wp_installed_sub_dir()) {
$comm_slug = peerboard_get_wp_installed_sub_dir() . $comm_slug;
}
return untrailingslashit($comm_slug);
}
function peerboard_get_path_from_url($full_slug){
$path = parse_url($full_slug)['path'];
return untrailingslashit($path);
}
/**
* Array to JWT
*
* @param [type] $payload
* @return void
*/
function pebo_get_jwt_token($payload)
{
$peerboard_options = get_option('peerboard_options');
$auth_token = $peerboard_options['auth_token'];
JWT::$leeway = 60 * 2; // $leeway in seconds
$jwt = JWT::encode($payload, $auth_token, 'HS256');
return $jwt;
}
function peerboard_get_domain()
{
$info = peerboard_bloginfo_array();
return $info['hosting']['domain'];
}
function peerboard_bloginfo_array()
{
$fields = array('name', 'wpurl', 'admin_email');
$data = array();
foreach ($fields as $field) {
$field_data = get_bloginfo($field);
if ($field === 'wpurl') {
$field_data = explode('://', $field_data);
$field_data = explode('/', $field_data[1]);
$field_data = $field_data[0];
}
$data[$field] = $field_data;
}
$data['type'] = 'wordpress';
return array(
'name' => $data['name'],
'admins' => array(
array(
'email' => $data['admin_email'],
)
),
'hosting' => array(
'domain' => $data['wpurl'],
'type' => 'wordpress',
'version' => PEERBOARD_PLUGIN_VERSION
)
);
}
function peerboard_base64url_encode($data)
{
// First of all you should encode $data to Base64 string
$b64 = base64_encode($data);
// Make sure you get a valid result, otherwise, return FALSE, as the base64_encode() function do
if ($b64 === false) {
return false;
}
// Convert Base64 to Base64URL by replacing “+” with “-” and “/” with “_”
$url = strtr($b64, '+/', '-_');
// Remove padding character from the end of line and return the Base64URL result
return rtrim($url, '=');
}
/**
* Check if comm page slug exist is url
*
* @return bool
*/
function peerboard_is_embed_page($prefix)
{
$current_url = home_url(add_query_arg(null, null));
$url_with_path = home_url($prefix);
$is_embed_page = 0 === strpos($current_url, $url_with_path);
return $is_embed_page;
}
function peerboard_get_auth_hash($params, $secret)
{
$strings = array();
foreach ($params as $key => $value) {
$strings[] = $key . '=' . $value;
}
sort($strings);
return hash_hmac('sha256', implode("\n", $strings), hash('sha256', $secret, true));
}
function peerboard_get_options($data)
{
if(empty($data)){
return false;
}
if (is_wp_error($data)) {
return array("error" => $data);
}
$integration_type = $data['hosting']['type'];
$mode = 'proxy';
if ($integration_type === 'sdk') {
$mode = 'sdk';
}
return array(
'community_id' => $data['id'],
'auth_token' => $data['auth_token'],
'prefix' => $data['hosting']['path'] ?? '',
'redirect' => $data['url'],
'mode' => $mode,
);
}
/**
* Update post slug
*
* @param [type] $slug
* @return void
*/
function peerboard_update_post_slug($slug)
{
$sanitized_slug = sanitize_title($slug);
wp_update_post(array(
"ID" => intval(get_option('peerboard_post')),
"post_name" => $sanitized_slug,
), false, false);
}
/**
* Is community page set as home page
*
* @return boolean
*/
function peerboard_is_comm_set_static_home_page()
{
$page_id = intval(get_option('peerboard_post'));
$home_id = intval(get_option('page_on_front'));
if (!$home_id) {
return false;
}
if ($page_id === $home_id) {
return true;
}
return false;
}