PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.10.1
GiveWP – Donation Plugin and Fundraising Platform v4.10.1
4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Donations / Endpoints / ListDonations.php
give / src / Donations / Endpoints Last commit date
DonationActions.php 11 months ago Endpoint.php 1 year ago ListDonations.php 11 months ago ListDonationsStats.php 11 months ago SwitchDonationView.php 4 years ago
ListDonations.php
334 lines
1 <?php
2
3 namespace Give\Donations\Endpoints;
4
5 use Give\Donations\ListTable\DonationsListTable;
6 use Give\Donations\ValueObjects\DonationMetaKeys;
7 use Give\Donations\ValueObjects\DonationMode;
8 use Give\Framework\Database\DB;
9 use Give\Framework\ListTable\Exceptions\ColumnIdCollisionException;
10 use Give\Framework\QueryBuilder\QueryBuilder;
11 use WP_REST_Request;
12 use WP_REST_Response;
13
14 /**
15 * @since 4.0.0 replace form with campaignId.
16 * @since 3.4.0 The class is extendable
17 */
18 class ListDonations extends Endpoint
19 {
20 /**
21 * @var string
22 */
23 protected $endpoint = 'admin/donations';
24
25 /**
26 * @var WP_REST_Request
27 */
28 protected $request;
29
30 /**
31 * @var DonationsListTable
32 */
33 protected $listTable;
34
35 /**
36 * @since 4.6.0 add status parameter to filter donations by status
37 * @since 3.4.0
38 * @access public
39 */
40 public function __construct(DonationsListTable $listTable)
41 {
42 $this->listTable = $listTable;
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function registerRoute()
49 {
50 register_rest_route(
51 'give-api/v2',
52 $this->endpoint,
53 [
54 [
55 'methods' => 'GET',
56 'callback' => [$this, 'handleRequest'],
57 'permission_callback' => [$this, 'permissionsCheck'],
58 ],
59 'args' => [
60 'page' => [
61 'type' => 'integer',
62 'required' => false,
63 'default' => 1,
64 'minimum' => 1
65 ],
66 'perPage' => [
67 'type' => 'integer',
68 'required' => false,
69 'default' => 30,
70 'minimum' => 1
71 ],
72 'campaignId' => [
73 'type' => 'integer',
74 'required' => false,
75 'default' => 0
76 ],
77 'search' => [
78 'type' => 'string',
79 'required' => false,
80 'sanitize_callback' => 'sanitize_text_field',
81 ],
82 'start' => [
83 'type' => 'string',
84 'required' => false,
85 'validate_callback' => [$this, 'validateDate']
86 ],
87 'end' => [
88 'type' => 'string',
89 'required' => false,
90 'validate_callback' => [$this, 'validateDate']
91 ],
92 'donor' => [
93 'type' => 'string',
94 'required' => false,
95 'sanitize_callback' => 'sanitize_text_field',
96 ],
97 'sortColumn' => [
98 'type' => 'string',
99 'required' => false,
100 'sanitize_callback' => 'sanitize_text_field',
101 ],
102 'sortDirection' => [
103 'type' => 'string',
104 'required' => false,
105 'enum' => [
106 'asc',
107 'desc',
108 ],
109 ],
110 'locale' => [
111 'type' => 'string',
112 'required' => false,
113 'default' => get_locale(),
114 ],
115 'testMode' => [
116 'type' => 'boolean',
117 'required' => false,
118 'default' => give_is_test_mode(),
119 ],
120 'return' => [
121 'type' => 'string',
122 'required' => false,
123 'default' => 'columns',
124 'enum' => [
125 'model',
126 'columns',
127 ],
128 ],
129 'status' => [
130 'type' => 'string',
131 'required' => false,
132 'default' => 'active',
133 'enum' => [
134 'active',
135 'trash',
136 ],
137 'description' => 'Filter donations by status: active (all except trash), or trash.'
138 ],
139 ],
140 ]
141 );
142 }
143
144 /**
145 * @since 2.24.0 Change this to use the new ListTable class
146 * @since 2.20.0
147 *
148 * @param WP_REST_Request $request
149 *
150 * @return WP_REST_Response
151 * @throws ColumnIdCollisionException
152 */
153 public function handleRequest(WP_REST_Request $request): WP_REST_Response
154 {
155 $this->request = $request;
156
157 $donations = $this->getDonations();
158 $donationsCount = $this->getTotalDonationsCount();
159 $totalPages = (int)ceil($donationsCount / $this->request->get_param('perPage'));
160
161 if ('model' === $this->request->get_param('return')) {
162 $items = $donations;
163 } else {
164 $this->listTable->items($donations, $this->request->get_param('locale') ?? '');
165 $items = $this->listTable->getItems();
166 }
167
168 return new WP_REST_Response(
169 [
170 'items' => $items,
171 'totalItems' => $donationsCount,
172 'totalPages' => $totalPages,
173 ]
174 );
175 }
176
177 /**
178 * @since 2.24.0 Replace Query Builder with Donations model
179 * @since 2.21.0
180 *
181 * @return array
182 */
183 public function getDonations(): array
184 {
185 $page = $this->request->get_param('page');
186 $perPage = $this->request->get_param('perPage');
187 $sortColumns = $this->listTable->getSortColumnById($this->request->get_param('sortColumn') ?: 'id');
188 $sortDirection = $this->request->get_param('sortDirection') ?: 'desc';
189
190 $query = give()->donations->prepareQuery();
191 list($query) = $this->getWhereConditions($query);
192
193 foreach ($sortColumns as $sortColumn) {
194 $query->orderBy($sortColumn, $sortDirection);
195 }
196
197 $query->limit($perPage)
198 ->offset(($page - 1) * $perPage);
199
200 $donations = $query->getAll();
201
202 if (!$donations) {
203 return [];
204 }
205
206 return $donations;
207 }
208
209 /**
210 * @since 2.24.0 Replace Query Builder with Donations model
211 * @since 2.21.0
212 *
213 * @return int
214 */
215 public function getTotalDonationsCount(): int
216 {
217 $query = DB::table('posts')
218 ->where('post_type', 'give_payment')
219 ->groupBy('mode');
220
221 list($query, $dependencies) = $this->getWhereConditions($query);
222
223 $query->attachMeta(
224 'give_donationmeta',
225 'ID',
226 'donation_id',
227 ...DonationMetaKeys::getColumnsForAttachMetaQueryFromArray($dependencies)
228 );
229
230 return $query->count();
231 }
232
233 /**
234 * @since 4.8.0 Added support for subscriptionId parameter to filter donations
235 * @since 4.6.0 add status status condition to filter donations
236 * @since 3.4.0 Make this method protected so it can be extended
237 * @since 3.2.0 Updated query to account for possible null and empty values for _give_payment_mode meta
238 * @since 2.24.0 Remove joins as it uses ModelQueryBuilder and change clauses to use attach_meta
239 * @since 2.21.0
240 *
241 * @param QueryBuilder $query
242 *
243 * @return array{0: QueryBuilder, 1: array<DonationMetaKeys>}
244 */
245 protected function getWhereConditions(QueryBuilder $query): array
246 {
247 $search = $this->request->get_param('search');
248 $start = $this->request->get_param('start');
249 $end = $this->request->get_param('end');
250 $donor = $this->request->get_param('donor');
251 $testMode = $this->request->get_param('testMode');
252 $campaignId = $this->request->get_param('campaignId');
253 $subscriptionId = $this->request->get_param('subscriptionId');
254 $status = $this->request->get_param('status');
255 $dependencies = [
256 DonationMetaKeys::MODE(),
257 ];
258
259 $hasWhereConditions = $search || $start || $end || $campaignId || $subscriptionId || $donor;
260
261 $query->where('post_type', 'give_payment');
262
263 if ($status === 'trash') {
264 $query->where('post_status', 'trash');
265 } elseif ($status === 'active') {
266 $query->where('post_status', 'trash', '<>');
267 }
268
269 if ($search) {
270 if (ctype_digit($search)) {
271 $query->where('id', $search);
272 } elseif (strpos($search, '@') !== false) {
273 $query
274 ->whereLike('give_donationmeta_attach_meta_email.meta_value', $search);
275 $dependencies[] = DonationMetaKeys::EMAIL();
276 } else {
277 $query
278 ->whereLike('give_donationmeta_attach_meta_firstName.meta_value', $search)
279 ->orWhereLike('give_donationmeta_attach_meta_lastName.meta_value', $search);
280 $dependencies[] = DonationMetaKeys::FIRST_NAME();
281 $dependencies[] = DonationMetaKeys::LAST_NAME();
282 }
283 }
284
285 if ($donor) {
286 if (ctype_digit($donor)) {
287 $query
288 ->where('give_donationmeta_attach_meta_donorId.meta_value', $donor);
289 $dependencies[] = DonationMetaKeys::DONOR_ID();
290 } else {
291 $query
292 ->whereLike('give_donationmeta_attach_meta_firstName.meta_value', $donor)
293 ->orWhereLike('give_donationmeta_attach_meta_lastName.meta_value', $donor);
294 $dependencies[] = DonationMetaKeys::FIRST_NAME();
295 $dependencies[] = DonationMetaKeys::LAST_NAME();
296 }
297 }
298
299 if ($campaignId) {
300 $query
301 ->where('give_donationmeta_attach_meta_campaignId.meta_value', $campaignId);
302 $dependencies[] = DonationMetaKeys::CAMPAIGN_ID();
303 }
304
305 if ($subscriptionId) {
306 $query
307 ->where('give_donationmeta_attach_meta_subscriptionId.meta_value', $subscriptionId);
308 $dependencies[] = DonationMetaKeys::SUBSCRIPTION_ID();
309 }
310
311 if ($start && $end) {
312 $query->whereBetween('post_date', $start, $end);
313 } elseif ($start) {
314 $query->where('post_date', $start, '>=');
315 } elseif ($end) {
316 $query->where('post_date', $end, '<=');
317 }
318
319 if ($hasWhereConditions) {
320 $query->havingRaw('HAVING COALESCE(give_donationmeta_attach_meta_mode.meta_value, %s) = %s', DonationMode::LIVE, $testMode ? DonationMode::TEST : DonationMode::LIVE);
321 } elseif ($testMode) {
322 $query->where('give_donationmeta_attach_meta_mode.meta_value', DonationMode::TEST);
323 } else {
324 $query->whereIsNull('give_donationmeta_attach_meta_mode.meta_value')
325 ->orWhere('give_donationmeta_attach_meta_mode.meta_value', DonationMode::TEST, '<>');
326 }
327
328 return [
329 $query,
330 $dependencies,
331 ];
332 }
333 }
334