forked from bootscore/bootscore-child
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengage-functions.php
145 lines (124 loc) · 4.73 KB
/
engage-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
<?php
/**
* Engage API for Displaying Events
*
* API Documentation: https://engage-api.campuslabs.com/
*/
// Returns a UTC timestamp
function utcTimestamp()
{
$time = new DateTime('now', new DateTimeZone('UTC'));
$timestamp = $time->format('c');
return $timestamp;
}
// Engage Request
// Using the Engage API, make an HTTP request using the provided parameters.
function engage_request($endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get the Engage API Key
$engage_api_key = get_option('engage_api_key');
// Throw an error if the API Key is unset
if ($engage_api_key == NULL || $engage_api_key == false) {
throw new WP_Error('engage_api_key_unset', 'You must set the Engage API Key under Settings -> Engage/Event Settings to make a request to the Engage API.');
}
// Merge given arguments with default arguments
$allArgs = array_merge(array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => '0'
), $args);
// Merge given headers with default arguments
$allHeaders = array_merge(
array(
'Accept' => 'application/json',
'X-Engage-Api-Key' => $engage_api_key
),
$headers
);
// Build the full endpoint URL
$full_url = ENGAGE_BASE_URL . $endpoint . '?' . http_build_query($allArgs);
// Make the request
$request = wp_remote_request(
$full_url,
array(
'method' => $method,
'httpversion' => '1.1',
'headers' => $allHeaders,
'body' => $body
)
);
// Retrieve the response body
$response_body = wp_remote_retrieve_body($request);
// Return the JSON decoded response body
$decoded_body = json_decode($response_body, true);
return $decoded_body;
}
// Cached Engage Request
// Make a request to the Engage API, or return a previously cached response.
function engage_request_cached($cacheName, $cacheExpires = 60, $endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get any existing copy of our cached engage request
if (false === ($request = get_transient($cacheName))) {
// If a cached value does not exist, return the value of a new request and save that value as a new transient
$request = engage_request($endpoint, $args, $method, $body, $headers);
set_transient($cacheName, $request, $cacheExpires);
}
// Return the cached or new request value
return $request;
}
// Engage Request Concat
// Concat a paged response from the Engage API into a single array.
function engage_request_concat($endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Initialize variables
$allItems = array();
$saved = 0;
// Submit request to find total number of values
$baseReq = engage_request($endpoint, array_merge(
$args,
array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => strval($saved)
)
), $method, $body, $headers);
// Add the first batch of items
$baseReqItems = $baseReq['items'];
foreach ($baseReqItems as $baseReqItem) {
$allItems[] = $baseReqItem;
$saved++;
}
// Save the total number of items
$totalItems = intval($baseReq['totalItems']);
$remaining = $totalItems - $saved;
// Iterate through additional pages
while ($remaining > 0) {
$request = engage_request($endpoint, array_merge($args, array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => strval($saved)
)), $method, $body, $headers);
$items = $request['items'];
foreach ($items as $item) {
$allItems[] = $item;
$saved++;
}
$remaining = $totalItems - $saved;
}
// Put response into array
$response = array(
'totalItems' => $baseReq['totalItems'],
'items' => $allItems
);
return $response;
}
// Cached Concatenated Engage Request
// Make a request to the Engage API and concatenate paged values, or return a previously cached response.
function engage_request_concat_cached($cacheName, $cacheExpires = 60, $endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get any existing copy of our cached engage request
if (false === ($request = get_transient($cacheName))) {
// If a cached value does not exist, return the value of a new request and save that value as a new transient
$request = engage_request_concat($endpoint, $args, $method, $body, $headers);
set_transient($cacheName, $request, $cacheExpires);
}
// Return the cached or new request value
return $request;
}