PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 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 All 257 releases
give / src / Campaigns / Migrations / CacheCampaignsData.php

CacheCampaignsData.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Campaigns/Migrations/CacheCampaignsData.php

194 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.18.0 Merge donors into the donors list and subscriptions into the subscriptions option; always compute live stats, the cache holds live stats only.
57 * @since 4.12.0 add early return if no campaigns found
58 * @since 4.8.0
59 *
60 * @throws DatabaseMigrationException
61 */
62 public function runBatch($firstId, $lastId)
63 {
64 add_filter('give_is_test_mode', '__return_false', PHP_INT_MAX);
65
66 try {
67 $query = $this->query();
68
69 // Migration Runner will pass null for lastId in the last step
70 if (is_null($lastId)) {
71 $query->where('id', $firstId, '>');
72 } else {
73 $query->whereBetween('id', $firstId, $lastId);
74 }
75
76 $campaigns = $query->getAll();
77 $campaignIds = array_map(function ($campaign) {
78 return $campaign->id;
79 }, $campaigns);
80
81 if (empty($campaignIds)) {
82 return;
83 }
84
85 $donations = CampaignsDataQuery::donations($campaignIds);
86
87 $campaignsData = get_option('give_campaigns_data', []);
88
89 update_option('give_campaigns_data', [
90 'amounts' => array_merge(
91 $campaignsData['amounts'] ?? [],
92 $donations->collectIntendedAmounts()
93 ),
94 'donationsCount' => array_merge(
95 $campaignsData['donationsCount'] ?? [],
96 $donations->collectDonations()
97 ),
98 'donorsCount' => array_merge(
99 $campaignsData['donorsCount'] ?? [],
100 $donations->collectDonors()
101 ),
102 ]);
103
104 // Set subscriptions data
105 if (defined('GIVE_RECURRING_VERSION')) {
106 $subscriptionsData = get_option('give_campaigns_subscriptions_data', []);
107 $subscriptions = CampaignsDataQuery::subscriptions($campaignIds);
108
109 update_option('give_campaigns_subscriptions_data', [
110 'amounts' => array_merge(
111 $subscriptionsData['amounts'] ?? [],
112 $subscriptions->collectInitialAmounts()
113 ),
114 'donationsCount' => array_merge(
115 $subscriptionsData['donationsCount'] ?? [],
116 $subscriptions->collectDonations()
117 ),
118 'donorsCount' => array_merge(
119 $subscriptionsData['donorsCount'] ?? [],
120 $subscriptions->collectDonors()
121 ),
122 ]);
123 }
124 } catch (DatabaseQueryException $exception) {
125 throw new DatabaseMigrationException("An error occurred while caching campaign data", 0, $exception);
126 } finally {
127 remove_filter('give_is_test_mode', '__return_false', PHP_INT_MAX);
128 }
129 }
130
131 /**
132 * @inheritDoc
133 */
134 public function getItemsCount(): int
135 {
136 return $this->query()->count();
137 }
138
139 /**
140 * @inheritDoc
141 *
142 * @since 4.12.0 Add campaign type filter
143 */
144 public function getBatchItemsAfter($lastId): ?array
145 {
146 $item = DB::get_row(sprintf(
147 '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',
148 DB::prefix('give_campaigns'),
149 DB::prepare('%s', CampaignType::CORE),
150 $lastId,
151 $this->getBatchSize()
152 ));
153
154 if (!$item) {
155 return null;
156 }
157
158 return [
159 $item->first_id,
160 $item->last_id,
161 ];
162 }
163
164 /**
165 * @inheritDoc
166 */
167 public function getBatchSize(): int
168 {
169 return 10;
170 }
171
172 /**
173 * @inheritDoc
174 */
175 public function hasMoreItemsToBatch($lastProcessedId): ?bool
176 {
177 return $this->query()
178 ->where('id', $lastProcessedId, '>')
179 ->limit(1)
180 ->count();
181 }
182
183 /**
184 * @inheritDoc
185 *
186 * @since 4.8.0
187 */
188 public function reverse(): void
189 {
190 delete_option('give_campaigns_data');
191 delete_option('give_campaigns_subscriptions_data');
192 }
193 }
194