PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.2.0
GiveWP – Donation Plugin and Fundraising Platform v4.2.0
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 3 years ago Endpoint.php 1 year ago ListDonations.php 1 year ago SwitchDonationView.php 4 years ago
ListDonations.php
307 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 Give\Framework\QueryBuilder\Types\Operator;
12 use WP_REST_Request;
13 use WP_REST_Response;
14
15 /**
16 * @since 4.0.0 replace form with campaignId.
17 * @since 3.4.0 The class is extendable
18 */
19 class ListDonations extends Endpoint
20 {
21 /**
22 * @var string
23 */
24 protected $endpoint = 'admin/donations';
25
26 /**
27 * @var WP_REST_Request
28 */
29 protected $request;
30
31 /**
32 * @var DonationsListTable
33 */
34 protected $listTable;
35
36 /**
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 ],
130 ]
131 );
132 }
133
134 /**
135 * @since 2.24.0 Change this to use the new ListTable class
136 * @since 2.20.0
137 *
138 * @param WP_REST_Request $request
139 *
140 * @return WP_REST_Response
141 * @throws ColumnIdCollisionException
142 */
143 public function handleRequest(WP_REST_Request $request): WP_REST_Response
144 {
145 $this->request = $request;
146
147 $donations = $this->getDonations();
148 $donationsCount = $this->getTotalDonationsCount();
149 $totalPages = (int)ceil($donationsCount / $this->request->get_param('perPage'));
150
151 if ('model' === $this->request->get_param('return')) {
152 $items = $donations;
153 } else {
154 $this->listTable->items($donations, $this->request->get_param('locale') ?? '');
155 $items = $this->listTable->getItems();
156 }
157
158 return new WP_REST_Response(
159 [
160 'items' => $items,
161 'totalItems' => $donationsCount,
162 'totalPages' => $totalPages,
163 ]
164 );
165 }
166
167 /**
168 * @since 2.24.0 Replace Query Builder with Donations model
169 * @since 2.21.0
170 *
171 * @return array
172 */
173 public function getDonations(): array
174 {
175 $page = $this->request->get_param('page');
176 $perPage = $this->request->get_param('perPage');
177 $sortColumns = $this->listTable->getSortColumnById($this->request->get_param('sortColumn') ?: 'id');
178 $sortDirection = $this->request->get_param('sortDirection') ?: 'desc';
179
180 $query = give()->donations->prepareQuery();
181 list($query) = $this->getWhereConditions($query);
182
183 foreach ($sortColumns as $sortColumn) {
184 $query->orderBy($sortColumn, $sortDirection);
185 }
186
187 $query->limit($perPage)
188 ->offset(($page - 1) * $perPage);
189
190 $donations = $query->getAll();
191
192 if (!$donations) {
193 return [];
194 }
195
196 return $donations;
197 }
198
199 /**
200 * @since 2.24.0 Replace Query Builder with Donations model
201 * @since 2.21.0
202 *
203 * @return int
204 */
205 public function getTotalDonationsCount(): int
206 {
207 $query = DB::table('posts')
208 ->where('post_type', 'give_payment')
209 ->groupBy('mode');
210
211 list($query, $dependencies) = $this->getWhereConditions($query);
212
213 $query->attachMeta(
214 'give_donationmeta',
215 'ID',
216 'donation_id',
217 ...DonationMetaKeys::getColumnsForAttachMetaQueryFromArray($dependencies)
218 );
219
220 return $query->count();
221 }
222
223 /**
224 * @since 3.4.0 Make this method protected so it can be extended
225 * @since 3.2.0 Updated query to account for possible null and empty values for _give_payment_mode meta
226 * @since 2.24.0 Remove joins as it uses ModelQueryBuilder and change clauses to use attach_meta
227 * @since 2.21.0
228 *
229 * @param QueryBuilder $query
230 *
231 * @return array{0: QueryBuilder, 1: array<DonationMetaKeys>}
232 */
233 protected function getWhereConditions(QueryBuilder $query): array
234 {
235 $search = $this->request->get_param('search');
236 $start = $this->request->get_param('start');
237 $end = $this->request->get_param('end');
238 $donor = $this->request->get_param('donor');
239 $testMode = $this->request->get_param('testMode');
240 $campaignId = $this->request->get_param('campaignId');
241
242 $dependencies = [
243 DonationMetaKeys::MODE(),
244 ];
245
246 $hasWhereConditions = $search || $start || $end || $campaignId || $donor;
247
248 if ($search) {
249 if (ctype_digit($search)) {
250 $query->where('id', $search);
251 } elseif (strpos($search, '@') !== false) {
252 $query
253 ->whereLike('give_donationmeta_attach_meta_email.meta_value', $search);
254 $dependencies[] = DonationMetaKeys::EMAIL();
255 } else {
256 $query
257 ->whereLike('give_donationmeta_attach_meta_firstName.meta_value', $search)
258 ->orWhereLike('give_donationmeta_attach_meta_lastName.meta_value', $search);
259 $dependencies[] = DonationMetaKeys::FIRST_NAME();
260 $dependencies[] = DonationMetaKeys::LAST_NAME();
261 }
262 }
263
264 if ($donor) {
265 if (ctype_digit($donor)) {
266 $query
267 ->where('give_donationmeta_attach_meta_donorId.meta_value', $donor);
268 $dependencies[] = DonationMetaKeys::DONOR_ID();
269 } else {
270 $query
271 ->whereLike('give_donationmeta_attach_meta_firstName.meta_value', $donor)
272 ->orWhereLike('give_donationmeta_attach_meta_lastName.meta_value', $donor);
273 $dependencies[] = DonationMetaKeys::FIRST_NAME();
274 $dependencies[] = DonationMetaKeys::LAST_NAME();
275 }
276 }
277
278 if ($campaignId) {
279 $query
280 ->where('give_donationmeta_attach_meta_campaignId.meta_value', $campaignId);
281 $dependencies[] = DonationMetaKeys::CAMPAIGN_ID();
282 }
283
284 if ($start && $end) {
285 $query->whereBetween('post_date', $start, $end);
286 } elseif ($start) {
287 $query->where('post_date', $start, '>=');
288 } elseif ($end) {
289 $query->where('post_date', $end, '<=');
290 }
291
292 if ($hasWhereConditions) {
293 $query->havingRaw('HAVING COALESCE(give_donationmeta_attach_meta_mode.meta_value, %s) = %s', DonationMode::LIVE, $testMode ? DonationMode::TEST : DonationMode::LIVE);
294 } elseif ($testMode) {
295 $query->where('give_donationmeta_attach_meta_mode.meta_value', DonationMode::TEST);
296 } else {
297 $query->whereIsNull('give_donationmeta_attach_meta_mode.meta_value')
298 ->orWhere('give_donationmeta_attach_meta_mode.meta_value', DonationMode::TEST, '<>');
299 }
300
301 return [
302 $query,
303 $dependencies,
304 ];
305 }
306 }
307