← All changes
|
src/Subscriptions/Endpoints/ListSubscriptions.php
+61
-18
2.30.0
→
4.16.9
View file →
| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\Subscriptions\Endpoints; |
| 4 | 4 | |
| 5 | +use Give\Donations\ValueObjects\DonationMetaKeys; | |
| 6 | +use Give\Donors\ValueObjects\DonorMetaKeys; | |
| 5 | 7 | use Give\Framework\Database\DB; |
| 6 | 8 | use Give\Framework\QueryBuilder\QueryBuilder; |
| 7 | 9 | use Give\Subscriptions\ListTable\SubscriptionsListTable; |
| 8 | 10 | use Give\Subscriptions\ValueObjects\SubscriptionMode; |
| 11 | +use Give\Subscriptions\ValueObjects\SubscriptionStatus; | |
| 9 | 12 | use WP_REST_Request; |
| 10 | 13 | use WP_REST_Response; |
| 11 | 14 | |
| 12 | 15 | class ListSubscriptions extends Endpoint |
| @@ -27,8 +30,10 @@ | ||
| 27 | 30 | protected $listTable; |
| 28 | 31 | |
| 29 | 32 | /** |
| 30 | 33 | * @inheritDoc |
| 34 | + * | |
| 35 | + * @since 4.12.0 Add format parameter to start and end dates, replacing custom validation callback | |
| 31 | 36 | */ |
| 32 | 37 | public function registerRoute() |
| 33 | 38 | { |
| 34 | 39 | register_rest_route( |
| @@ -64,11 +69,11 @@ | ||
| 64 | 69 | ], |
| 65 | 70 | 'start' => [ |
| 66 | 71 | 'type' => 'string', |
| 67 | 72 | 'required' => false, |
| 68 | - 'validate_callback' => [$this, 'validateDate'] | |
| 73 | + 'format' => 'date-time' | |
| 69 | 74 | ], |
| 70 | - 'form' => [ | |
| 75 | + 'campaignId' => [ | |
| 71 | 76 | 'type' => 'integer', |
| 72 | 77 | 'required' => false, |
| 73 | 78 | 'default' => 0 |
| 74 | 79 | ], |
| @@ -74,9 +79,9 @@ | ||
| 74 | 79 | ], |
| 75 | 80 | 'end' => [ |
| 76 | 81 | 'type' => 'string', |
| 77 | 82 | 'required' => false, |
| 78 | - 'validate_callback' => [$this, 'validateDate'] | |
| 83 | + 'format' => 'date-time' | |
| 79 | 84 | ], |
| 80 | 85 | 'sortColumn' => [ |
| 81 | 86 | 'type' => 'string', |
| 82 | 87 | 'required' => false, |
| @@ -108,8 +113,17 @@ | ||
| 108 | 113 | 'model', |
| 109 | 114 | 'columns', |
| 110 | 115 | ], |
| 111 | 116 | ], |
| 117 | + 'status' => [ | |
| 118 | + 'type' => 'array', | |
| 119 | + 'required' => false, | |
| 120 | + 'items' => [ | |
| 121 | + 'type' => 'string', | |
| 122 | + 'enum' => array_values(SubscriptionStatus::toArray()), | |
| 123 | + ], | |
| 124 | + 'description' => 'Filter subscriptions by status. Accepts comma-separated list of SubscriptionStatus values (e.g., "active,expired,pending"). If not provided, excludes trash subscriptions by default.' | |
| 125 | + ], | |
| 112 | 126 | ], |
| 113 | 127 | ] |
| 114 | 128 | ); |
| 115 | 129 | } |
| @@ -146,8 +160,9 @@ | ||
| 146 | 160 | ); |
| 147 | 161 | } |
| 148 | 162 | |
| 149 | 163 | /** |
| 164 | + * @since 4.12.0 add sort by donor name | |
| 150 | 165 | * @since 2.24.0 |
| 151 | 166 | * |
| 152 | 167 | * @return array |
| 153 | 168 | */ |
| @@ -158,8 +173,20 @@ | ||
| 158 | 173 | $sortColumns = $this->listTable->getSortColumnById($this->request->get_param('sortColumn') ?: 'id'); |
| 159 | 174 | $sortDirection = $this->request->get_param('sortDirection') ?: 'desc'; |
| 160 | 175 | |
| 161 | 176 | $query = give()->subscriptions->prepareQuery(); |
| 177 | + | |
| 178 | + if ('donorName' === $sortColumns[0]) { | |
| 179 | + $query->attachMeta( | |
| 180 | + 'give_donormeta', | |
| 181 | + 'customer_id', | |
| 182 | + 'donor_id', | |
| 183 | + [DonorMetaKeys::FIRST_NAME, 'firstName'], | |
| 184 | + [DonorMetaKeys::LAST_NAME, 'lastName'] | |
| 185 | + ); | |
| 186 | + $sortColumns = ['firstName', 'lastName']; | |
| 187 | + } | |
| 188 | + | |
| 162 | 189 | $query = $this->getWhereConditions($query); |
| 163 | 190 | |
| 164 | 191 | foreach ($sortColumns as $sortColumn) { |
| 165 | 192 | $query->orderBy($sortColumn, $sortDirection); |
| @@ -190,8 +217,10 @@ | ||
| 190 | 217 | return $query->count(); |
| 191 | 218 | } |
| 192 | 219 | |
| 193 | 220 | /** |
| 221 | + * @since 4.12.0 Add "status" where condition | |
| 222 | + * @since 4.11.0 fix search by donor name or email | |
| 194 | 223 | * @since 2.24.0 Replace Query Builder with Subscriptions model |
| 195 | 224 | * @since 2.21.0 |
| 196 | 225 | * |
| 197 | 226 | * @param QueryBuilder $query |
| @@ -202,44 +231,58 @@ | ||
| 202 | 231 | { |
| 203 | 232 | $search = $this->request->get_param('search'); |
| 204 | 233 | $start = $this->request->get_param('start'); |
| 205 | 234 | $end = $this->request->get_param('end'); |
| 206 | - $form = $this->request->get_param('form'); | |
| 235 | + $campaignId = $this->request->get_param('campaignId'); | |
| 207 | 236 | $testMode = $this->request->get_param('testMode'); |
| 237 | + $status = $this->request->get_param('status'); | |
| 208 | 238 | |
| 209 | - $hasWhereConditions = $search || $start || $end || $form; | |
| 239 | + $hasWhereConditions = $search || $start || $end || $campaignId || $status; | |
| 210 | 240 | |
| 241 | + if (!empty($status)) { | |
| 242 | + $query->whereIn('status', $status); | |
| 243 | + } else { | |
| 244 | + // Default behavior: exclude trashed subscriptions | |
| 245 | + $query->where('status', SubscriptionStatus::TRASHED, '<>'); | |
| 246 | + } | |
| 247 | + | |
| 211 | 248 | if ($search) { |
| 212 | 249 | if (ctype_digit($search)) { |
| 213 | 250 | $query->where('id', $search); |
| 214 | 251 | } else { |
| 215 | - $query->whereLike('name', $search); | |
| 216 | - $query->orWhereLike('email', $search); | |
| 252 | + $query->whereIn('customer_id', static function (QueryBuilder $builder) use ($search) { | |
| 253 | + $builder | |
| 254 | + ->from('give_donors') | |
| 255 | + ->distinct() | |
| 256 | + ->select('id') | |
| 257 | + ->whereLike('name', $search) | |
| 258 | + ->orWhereLike('email', $search); | |
| 259 | + }); | |
| 217 | 260 | } |
| 218 | 261 | } |
| 219 | 262 | |
| 220 | 263 | if ($start && $end) { |
| 221 | - $query->whereBetween('date_created', $start, $end); | |
| 222 | - } else if ($start) { | |
| 223 | - $query->where('date_created', $start, '>='); | |
| 224 | - } else if ($end) { | |
| 225 | - $query->where('date_created', $end, '<='); | |
| 264 | + $query->whereBetween('created', $start, $end); | |
| 265 | + } elseif ($start) { | |
| 266 | + $query->where('created', $start, '>='); | |
| 267 | + } elseif ($end) { | |
| 268 | + $query->where('created', $end, '<='); | |
| 226 | 269 | } |
| 227 | 270 | |
| 228 | - if ($form) { | |
| 271 | + if ($campaignId) { | |
| 229 | 272 | $query |
| 230 | - ->whereIn('id', static function (QueryBuilder $builder) use ($form) { | |
| 273 | + ->whereIn('id', static function (QueryBuilder $builder) use ($campaignId) { | |
| 231 | 274 | $builder |
| 232 | 275 | ->from('give_donationmeta') |
| 233 | 276 | ->distinct() |
| 234 | 277 | ->select('meta_value') |
| 235 | - ->where('meta_key', '_give_payment_subscription_id') | |
| 236 | - ->whereIn('donation_id', static function (QueryBuilder $builder) use ($form) { | |
| 278 | + ->where('meta_key', DonationMetaKeys::SUBSCRIPTION_ID) | |
| 279 | + ->whereIn('donation_id', static function (QueryBuilder $builder) use ($campaignId) { | |
| 237 | 280 | $builder |
| 238 | 281 | ->from('give_donationmeta') |
| 239 | 282 | ->select('donation_id') |
| 240 | - ->where('meta_key', '_give_payment_form_id') | |
| 241 | - ->where('meta_value', $form); | |
| 283 | + ->where('meta_key', DonationMetaKeys::CAMPAIGN_ID) | |
| 284 | + ->where('meta_value', $campaignId); | |
| 242 | 285 | }); |
| 243 | 286 | }); |
| 244 | 287 | } |
| 245 | 288 | |