| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Campaigns\Migrations; |
| 4 |
|
| 5 |
use Give\Campaigns\CampaignsDataQuery; |
| 6 |
use Give\Campaigns\ValueObjects\CampaignType; |
| 7 |
use Give\Framework\Database\DB; |
| 8 |
use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 9 |
use Give\Framework\Migrations\Contracts\BatchMigration; |
| 10 |
use Give\Framework\Migrations\Contracts\ReversibleMigration; |
| 11 |
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 12 |
use Give\Framework\QueryBuilder\QueryBuilder; |
| 13 |
|
| 14 |
/** |
| 15 |
* @since 4.8.0 |
| 16 |
*/ |
| 17 |
class CacheCampaignsData extends BatchMigration implements ReversibleMigration |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @inheritDoc |
| 21 |
*/ |
| 22 |
public static function id(): string |
| 23 |
{ |
| 24 |
return 'cache_campaign_data'; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @inheritDoc |
| 29 |
*/ |
| 30 |
public static function title(): string |
| 31 |
{ |
| 32 |
return 'Cache campaign data'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @inheritdoc |
| 37 |
*/ |
| 38 |
public static function timestamp(): string |
| 39 |
{ |
| 40 |
return strtotime('2025-07-25 00:00:00'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Base query |
| 45 |
* |
| 46 |
* @since 4.8.0 |
| 47 |
*/ |
| 48 |
protected function query(): QueryBuilder |
| 49 |
{ |
| 50 |
return DB::table('give_campaigns')->where('campaign_type', CampaignType::CORE); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @inheritDoc |
| 55 |
* |
| 56 |
* @since 4.12.0 add early return if no campaigns found |
| 57 |
* @since 4.8.0 |
| 58 |
* |
| 59 |
* @throws DatabaseMigrationException |
| 60 |
*/ |
| 61 |
public function runBatch($firstId, $lastId) |
| 62 |
{ |
| 63 |
try { |
| 64 |
$query = $this->query(); |
| 65 |
|
| 66 |
// Migration Runner will pass null for lastId in the last step |
| 67 |
if (is_null($lastId)) { |
| 68 |
$query->where('id', $firstId, '>'); |
| 69 |
} else { |
| 70 |
$query->whereBetween('id', $firstId, $lastId); |
| 71 |
} |
| 72 |
|
| 73 |
$campaigns = $query->getAll(); |
| 74 |
$campaignIds = array_map(function ($campaign) { |
| 75 |
return $campaign->id; |
| 76 |
}, $campaigns); |
| 77 |
|
| 78 |
if (empty($campaignIds)) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$donations = CampaignsDataQuery::donations($campaignIds); |
| 83 |
|
| 84 |
$campaignsData = get_option('give_campaigns_data', []); |
| 85 |
|
| 86 |
update_option('give_campaigns_data', [ |
| 87 |
'amounts' => array_merge( |
| 88 |
$campaignsData['amounts'] ?? [], |
| 89 |
$donations->collectIntendedAmounts() |
| 90 |
), |
| 91 |
'donationsCount' => array_merge( |
| 92 |
$campaignsData['donationsCount'] ?? [], |
| 93 |
$donations->collectDonations() |
| 94 |
), |
| 95 |
'donorsCount' => array_merge( |
| 96 |
$campaignsData['donationsCount'] ?? [], |
| 97 |
$donations->collectDonors() |
| 98 |
), |
| 99 |
]); |
| 100 |
|
| 101 |
// Set subscriptions data |
| 102 |
if (defined('GIVE_RECURRING_VERSION')) { |
| 103 |
$subscriptionsData = get_option('give_campaigns_data', []); |
| 104 |
$subscriptions = CampaignsDataQuery::subscriptions($campaignIds); |
| 105 |
|
| 106 |
update_option('give_campaigns_subscriptions_data', [ |
| 107 |
'amounts' => array_merge( |
| 108 |
$subscriptionsData['amounts'] ?? [], |
| 109 |
$subscriptions->collectInitialAmounts() |
| 110 |
), |
| 111 |
'donationsCount' => array_merge( |
| 112 |
$subscriptionsData['donationsCount'] ?? [], |
| 113 |
$subscriptions->collectDonations() |
| 114 |
), |
| 115 |
'donorsCount' => array_merge( |
| 116 |
$subscriptionsData['donorsCount'] ?? [], |
| 117 |
$subscriptions->collectDonors() |
| 118 |
), |
| 119 |
]); |
| 120 |
} |
| 121 |
} catch (DatabaseQueryException $exception) { |
| 122 |
throw new DatabaseMigrationException("An error occurred while caching campaign data", 0, $exception); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @inheritDoc |
| 128 |
*/ |
| 129 |
public function getItemsCount(): int |
| 130 |
{ |
| 131 |
return $this->query()->count(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @inheritDoc |
| 136 |
* |
| 137 |
* @since 4.12.0 Add campaign type filter |
| 138 |
*/ |
| 139 |
public function getBatchItemsAfter($lastId): ?array |
| 140 |
{ |
| 141 |
$item = DB::get_row(sprintf( |
| 142 |
'SELECT MIN(id) AS first_id, MAX(id) AS last_id FROM (SELECT id FROM %1s WHERE campaign_type = %2s AND id > %d ORDER BY id ASC LIMIT %d) as batch', |
| 143 |
DB::prefix('give_campaigns'), |
| 144 |
DB::prepare('%s', CampaignType::CORE), |
| 145 |
$lastId, |
| 146 |
$this->getBatchSize() |
| 147 |
)); |
| 148 |
|
| 149 |
if (!$item) { |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
return [ |
| 154 |
$item->first_id, |
| 155 |
$item->last_id, |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @inheritDoc |
| 161 |
*/ |
| 162 |
public function getBatchSize(): int |
| 163 |
{ |
| 164 |
return 10; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @inheritDoc |
| 169 |
*/ |
| 170 |
public function hasMoreItemsToBatch($lastProcessedId): ?bool |
| 171 |
{ |
| 172 |
return $this->query() |
| 173 |
->where('id', $lastProcessedId, '>') |
| 174 |
->limit(1) |
| 175 |
->count(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @inheritDoc |
| 180 |
* |
| 181 |
* @since 4.8.0 |
| 182 |
*/ |
| 183 |
public function reverse(): void |
| 184 |
{ |
| 185 |
delete_option('give_campaigns_data'); |
| 186 |
delete_option('give_campaigns_subscriptions_data'); |
| 187 |
} |
| 188 |
} |
| 189 |
|