-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_api.php
108 lines (94 loc) · 2.33 KB
/
user_api.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
<?php
/**
* Use this functions to retrieve user info.
*
* @package WP REMP Connector
* @author Peter PayteR Gašparík
* @license MIT
* @copyright Copyright (c) 2020
*/
if (!defined('ABSPATH')) {
exit;
}
if(!defined('REMP_USER_KEY_INFO')) define( 'REMP_USER_KEY_INFO', 'user_info' );
if(!defined('REMP_USER_KEY_SUBSCRIPTION')) define( 'REMP_USER_KEY_SUBSCRIPTION', 'user_subscription' );
if(!defined('REMP_USER_KEY_PREMIUM')) define( 'REMP_USER_KEY_PREMIUM', 'user_premium' );
/**
* Check, if user is logged in REMP
*
* @return bool
*/
function remp_user_logged_in()
{
return (bool)remp_user_get_info();
}
/**
* Retrieve user info from API
*
* @return stdClass|null
*/
function remp_user_get_info()
{
if($data = remp_cache_get(REMP_USER_KEY_INFO)) {
return $data;
}
$data = remp_api_data(remp_get_url(REMP_URL_API_USERINFO));
if (!$data || !isset($data->user)) {
return null;
}
remp_cache_set(REMP_USER_KEY_INFO, $data->user);
return $data->user;
}
/**
* Retrieve all subscriptions from API for logged user
*
* @return stdClass|null
*/
function remp_user_get_subscriptions()
{
if($data = remp_cache_get(REMP_USER_KEY_SUBSCRIPTION)) {
return $data;
}
$data = remp_api_data(remp_get_url(REMP_URL_API_SUBSCRIPTION));
if (!$data || !isset($data->subscriptions)) {
return null;
}
remp_cache_set(REMP_USER_KEY_SUBSCRIPTION, $data->subscriptions);
return $data->subscriptions;
}
/**
* Check by subscriptions, if user have currently active subscription
*
* @return bool
*/
function remp_user_is_premium()
{
if($data = remp_cache_get(REMP_USER_KEY_PREMIUM)) {
return $data;
}
$subscriptions = remp_user_get_subscriptions();
if (!$subscriptions || !count($subscriptions)) {
return false;
}
try {
$now = new \DateTime();
$end = new \DateTime($subscriptions[0]->end_at);
} catch (\Exception $e) {
return false;
}
$is_premium = $now < $end;
remp_cache_set(REMP_USER_KEY_PREMIUM, $is_premium);
return $is_premium;
}
/**
* Get last subscription code
*
* @return bool|string
*/
function remp_user_subscription_code() {
if(!remp_user_is_premium()) {
return false;
}
$subscriptions = remp_user_get_subscriptions();
return $subscriptions[0]->code;
}