Donations
11 months ago
P2P
1 year ago
RevenueTable
1 year ago
Tables
1 year ago
CacheCampaignsData.php
9 months ago
MigrateFormsToCampaignForms.php
1 year ago
CacheCampaignsData.php
182 lines
| 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.8.0 |
| 57 | * |
| 58 | * @throws DatabaseMigrationException |
| 59 | */ |
| 60 | public function runBatch($firstId, $lastId) |
| 61 | { |
| 62 | try { |
| 63 | $query = $this->query(); |
| 64 | |
| 65 | // Migration Runner will pass null for lastId in the last step |
| 66 | if (is_null($lastId)) { |
| 67 | $query->where('id', $firstId, '>'); |
| 68 | } else { |
| 69 | $query->whereBetween('id', $firstId, $lastId); |
| 70 | } |
| 71 | |
| 72 | $campaigns = $query->getAll(); |
| 73 | $campaignIds = array_map(function ($campaign) { |
| 74 | return $campaign->id; |
| 75 | }, $campaigns); |
| 76 | |
| 77 | $donations = CampaignsDataQuery::donations($campaignIds); |
| 78 | |
| 79 | $campaignsData = get_option('give_campaigns_data', []); |
| 80 | |
| 81 | update_option('give_campaigns_data', [ |
| 82 | 'amounts' => array_merge( |
| 83 | $campaignsData['amounts'] ?? [], |
| 84 | $donations->collectIntendedAmounts() |
| 85 | ), |
| 86 | 'donationsCount' => array_merge( |
| 87 | $campaignsData['donationsCount'] ?? [], |
| 88 | $donations->collectDonations() |
| 89 | ), |
| 90 | 'donorsCount' => array_merge( |
| 91 | $campaignsData['donationsCount'] ?? [], |
| 92 | $donations->collectDonors() |
| 93 | ), |
| 94 | ]); |
| 95 | |
| 96 | // Set subscriptions data |
| 97 | if (defined('GIVE_RECURRING_VERSION')) { |
| 98 | $subscriptionsData = get_option('give_campaigns_data', []); |
| 99 | $subscriptions = CampaignsDataQuery::subscriptions($campaignIds); |
| 100 | |
| 101 | update_option('give_campaigns_subscriptions_data', [ |
| 102 | 'amounts' => array_merge( |
| 103 | $subscriptionsData['amounts'] ?? [], |
| 104 | $subscriptions->collectInitialAmounts() |
| 105 | ), |
| 106 | 'donationsCount' => array_merge( |
| 107 | $subscriptionsData['donationsCount'] ?? [], |
| 108 | $subscriptions->collectDonations() |
| 109 | ), |
| 110 | 'donorsCount' => array_merge( |
| 111 | $subscriptionsData['donorsCount'] ?? [], |
| 112 | $subscriptions->collectDonors() |
| 113 | ), |
| 114 | ]); |
| 115 | } |
| 116 | |
| 117 | } catch (DatabaseQueryException $exception) { |
| 118 | throw new DatabaseMigrationException("An error occurred while caching campaign data", 0, $exception); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @inheritDoc |
| 124 | */ |
| 125 | public function getItemsCount(): int |
| 126 | { |
| 127 | return $this->query()->count(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @inheritDoc |
| 132 | */ |
| 133 | public function getBatchItemsAfter($lastId): ?array |
| 134 | { |
| 135 | $item = DB::get_row(sprintf( |
| 136 | 'SELECT MIN(id) AS first_id, MAX(id) AS last_id FROM (SELECT id FROM %1s WHERE id > %d ORDER BY id ASC LIMIT %d) as batch', |
| 137 | DB::prefix('give_campaigns'), |
| 138 | $lastId, |
| 139 | $this->getBatchSize() |
| 140 | )); |
| 141 | |
| 142 | if ( ! $item) { |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | return [ |
| 147 | $item->first_id, |
| 148 | $item->last_id, |
| 149 | ]; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @inheritDoc |
| 154 | */ |
| 155 | public function getBatchSize(): int |
| 156 | { |
| 157 | return 10; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @inheritDoc |
| 162 | */ |
| 163 | public function hasMoreItemsToBatch($lastProcessedId): ?bool |
| 164 | { |
| 165 | return $this->query() |
| 166 | ->where('id', $lastProcessedId, '>') |
| 167 | ->limit(1) |
| 168 | ->count(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @inheritDoc |
| 173 | * |
| 174 | * @since 4.8.0 |
| 175 | */ |
| 176 | public function reverse(): void |
| 177 | { |
| 178 | delete_option('give_campaigns_data'); |
| 179 | delete_option('give_campaigns_subscriptions_data'); |
| 180 | } |
| 181 | } |
| 182 |