-
Notifications
You must be signed in to change notification settings - Fork 10
/
ClassyContent.php
120 lines (100 loc) · 3.31 KB
/
ClassyContent.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
<?php
/**
* Class ClassyContent
*
* Manage fetching/caching of API content.
*/
class ClassyContent
{
const EXPIRATION_IN_MINUTES = 10;
private $apiClient;
public function __construct()
{
$this->apiClient = ClassyAPIClient::getInstance(get_option('client_id'), get_option('client_secret'));
}
/**
* Fetch campaign overview from API
* @param $campaignId
* @return array|bool|mixed
*/
public function campaignOverview($campaignId)
{
$cacheKey = ClassyOrg::CACHE_KEY_PREFIX . '_CAMPAIGN_OVERVIEW_' . $campaignId;
$result = get_transient($cacheKey);
if ($result === false)
{
$campaign = $this->apiClient->request('/campaigns/' . $campaignId);
$overview = $this->apiClient->request('/campaigns/' . $campaignId . '/overview');
$result = json_decode($campaign, true);
$result['overview'] = json_decode($overview, true);
set_transient($cacheKey, $result, $this->getExpiration());
}
return $result;
}
/**
* Fetch campaign fundraisers from API
*
* @param integer $campaignId ID of campaign to pull
* @param integer $count Number of records to return
* @return array|bool|mixed
*/
public function campaignFundraisers($campaignId, $count = 5)
{
$cacheKey = ClassyOrg::CACHE_KEY_PREFIX . '_CAMPAIGN_FUNDRAISERS_' . $campaignId;
$result = get_transient($cacheKey);
if ($result === false)
{
$params = array(
'aggregates' => 'true',
'sort' => 'total_raised:desc',
'per_page' => $count,
'with' => 'supporter',
'filter' => 'status=active'
);
$fundraisers = $this->apiClient->request(
'/campaigns/' . $campaignId . '/fundraising-pages',
'GET',
$params
);
$result = json_decode($fundraisers, true);
// Pluck off relevant bits
$result = $result['data'];
set_transient($cacheKey, $result, $this->getExpiration());
}
return $result;
}
/**
* Fetch campaign fundraising teams from API.
*
* @param $campaignId
* @param int $count
* @return array|mixed
*/
public function campaignFundraisingTeams($campaignId, $count = 5)
{
$cacheKey = ClassyOrg::CACHE_KEY_PREFIX . '_CAMPAIGN_FUNDRAISING_TEAMS_' . $campaignId;
$result = get_transient($cacheKey);
if ($result === false)
{
$params = array(
'aggregates' => 'true',
'sort' => 'total_raised:desc',
'per_page' => $count,
'filter' => 'status=active'
);
$fundraisingPages = $this->apiClient->request('/campaigns/' . $campaignId . '/fundraising-teams', 'GET', $params);
$result = json_decode($fundraisingPages, true);
$result = $result['data'];
set_transient($cacheKey, $result, $this->getExpiration());
}
return $result;
}
/**
* Simple helper for expiration policy.
* @return int
*/
private function getExpiration()
{
return (self::EXPIRATION_IN_MINUTES * 60);
}
}