PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.11.3
GiveWP – Donation Plugin and Fundraising Platform v2.11.3
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 / includes / admin / upgrades / upgrade-functions.php
give / includes / admin / upgrades Last commit date
views 5 years ago class-give-updates.php 6 years ago upgrade-functions.php 5 years ago
upgrade-functions.php
3564 lines
1 <?php
2 /**
3 * Upgrade Functions
4 *
5 * @package Give
6 * @since 1.0
7 *
8 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
9 * /includes/install.php @ Appox Line 156
10 * @copyright Copyright (c) 2016, GiveWP
11 * @license https://opensource.org/licenses/gpl-license GNU Public License
12 * @subpackage Admin/Upgrades
13 */
14
15 // Exit if accessed directly.
16 use Give\Helpers\Gateways\Stripe;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Perform automatic database upgrades when necessary.
24 *
25 * @since 1.6
26 * @return void
27 */
28 function give_do_automatic_upgrades() {
29 $did_upgrade = false;
30 $give_version = preg_replace( '/[^0-9.].*/', '', Give_Cache_Setting::get_option( 'give_version' ) );
31
32 if ( ! $give_version ) {
33 // 1.0 is the first version to use this option so we must add it.
34 $give_version = '1.0';
35 }
36
37 switch ( true ) {
38
39 case version_compare( $give_version, '1.6', '<' ):
40 give_v16_upgrades();
41 $did_upgrade = true;
42
43 case version_compare( $give_version, '1.7', '<' ):
44 give_v17_upgrades();
45 $did_upgrade = true;
46
47 case version_compare( $give_version, '1.8', '<' ):
48 give_v18_upgrades();
49 $did_upgrade = true;
50
51 case version_compare( $give_version, '1.8.7', '<' ):
52 give_v187_upgrades();
53 $did_upgrade = true;
54
55 case version_compare( $give_version, '1.8.8', '<' ):
56 give_v188_upgrades();
57 $did_upgrade = true;
58
59 case version_compare( $give_version, '1.8.9', '<' ):
60 give_v189_upgrades();
61 $did_upgrade = true;
62
63 case version_compare( $give_version, '1.8.12', '<' ):
64 give_v1812_upgrades();
65 $did_upgrade = true;
66
67 case version_compare( $give_version, '1.8.13', '<' ):
68 give_v1813_upgrades();
69 $did_upgrade = true;
70
71 case version_compare( $give_version, '1.8.17', '<' ):
72 give_v1817_upgrades();
73 $did_upgrade = true;
74
75 case version_compare( $give_version, '1.8.18', '<' ):
76 give_v1818_upgrades();
77 $did_upgrade = true;
78
79 case version_compare( $give_version, '2.0', '<' ):
80 give_v20_upgrades();
81 $did_upgrade = true;
82
83 case version_compare( $give_version, '2.0.1', '<' ):
84 // Do nothing on fresh install.
85 if ( ! doing_action( 'give_upgrades' ) ) {
86 give_v201_create_tables();
87 Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
88 Give_Updates::$background_updater->dispatch();
89 }
90
91 $did_upgrade = true;
92
93 case version_compare( $give_version, '2.0.2', '<' ):
94 // Remove 2.0.1 update to rerun on 2.0.2
95 $completed_upgrades = give_get_completed_upgrades();
96 $v201_updates = [
97 'v201_upgrades_payment_metadata',
98 'v201_add_missing_donors',
99 'v201_move_metadata_into_new_table',
100 'v201_logs_upgrades',
101 ];
102
103 foreach ( $v201_updates as $v201_update ) {
104 if ( in_array( $v201_update, $completed_upgrades ) ) {
105 unset( $completed_upgrades[ array_search( $v201_update, $completed_upgrades ) ] );
106 }
107 }
108
109 update_option( 'give_completed_upgrades', $completed_upgrades, false );
110
111 // Do nothing on fresh install.
112 if ( ! doing_action( 'give_upgrades' ) ) {
113 give_v201_create_tables();
114 Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
115 Give_Updates::$background_updater->dispatch();
116 }
117
118 $did_upgrade = true;
119
120 case version_compare( $give_version, '2.0.3', '<' ):
121 give_v203_upgrades();
122 $did_upgrade = true;
123
124 case version_compare( $give_version, '2.2.0', '<' ):
125 give_v220_upgrades();
126 $did_upgrade = true;
127
128 case version_compare( $give_version, '2.2.1', '<' ):
129 give_v221_upgrades();
130 $did_upgrade = true;
131
132 case version_compare( $give_version, '2.3.0', '<' ):
133 give_v230_upgrades();
134 $did_upgrade = true;
135
136 case version_compare( $give_version, '2.5.0', '<' ):
137 give_v250_upgrades();
138 $did_upgrade = true;
139
140 case version_compare( $give_version, '2.5.8', '<' ):
141 give_v258_upgrades();
142 $did_upgrade = true;
143
144 case version_compare( $give_version, '2.5.11', '<' ):
145 give_v2511_upgrades();
146 $did_upgrade = true;
147
148 case version_compare( $give_version, '2.6.3', '<' ):
149 give_v263_upgrades();
150 $did_upgrade = true;
151
152 case version_compare( $give_version, '2.7.0', '<' ):
153 // Flush rewrite rules. It will help to store register route for embed form.
154 flush_rewrite_rules();
155
156 give_v270_upgrades();
157
158 $did_upgrade = true;
159
160 case version_compare( $give_version, '2.9.0', '<' ):
161 give_v290_remove_old_export_files();
162 $did_upgrade = true;
163 }
164
165 if ( $did_upgrade || version_compare( $give_version, GIVE_VERSION, '<' ) ) {
166 update_option( 'give_version', GIVE_VERSION, false );
167 }
168 }
169
170 add_action( 'admin_init', 'give_do_automatic_upgrades', 0 );
171 add_action( 'give_upgrades', 'give_do_automatic_upgrades', 0 );
172
173 /**
174 * Display Upgrade Notices.
175 *
176 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
177 *
178 * @since 1.0
179 * @since 1.8.12 Update new update process code.
180 *
181 * @param Give_Updates $give_updates
182 *
183 * @return void
184 */
185 function give_show_upgrade_notices( $give_updates ) {
186 // v1.3.2 Upgrades
187 $give_updates->register(
188 [
189 'id' => 'upgrade_give_payment_customer_id',
190 'version' => '1.3.2',
191 'callback' => 'give_v132_upgrade_give_payment_customer_id',
192 ]
193 );
194
195 // v1.3.4 Upgrades ensure the user has gone through 1.3.4.
196 $give_updates->register(
197 [
198 'id' => 'upgrade_give_offline_status',
199 'depend' => 'upgrade_give_payment_customer_id',
200 'version' => '1.3.4',
201 'callback' => 'give_v134_upgrade_give_offline_status',
202 ]
203 );
204
205 // v1.8 form metadata upgrades.
206 $give_updates->register(
207 [
208 'id' => 'v18_upgrades_form_metadata',
209 'version' => '1.8',
210 'callback' => 'give_v18_upgrades_form_metadata',
211 ]
212 );
213
214 // v1.8.9 Upgrades
215 $give_updates->register(
216 [
217 'id' => 'v189_upgrades_levels_post_meta',
218 'version' => '1.8.9',
219 'callback' => 'give_v189_upgrades_levels_post_meta_callback',
220 ]
221 );
222
223 // v1.8.12 Upgrades
224 $give_updates->register(
225 [
226 'id' => 'v1812_update_amount_values',
227 'version' => '1.8.12',
228 'callback' => 'give_v1812_update_amount_values_callback',
229 ]
230 );
231
232 // v1.8.12 Upgrades
233 $give_updates->register(
234 [
235 'id' => 'v1812_update_donor_purchase_values',
236 'version' => '1.8.12',
237 'callback' => 'give_v1812_update_donor_purchase_value_callback',
238 ]
239 );
240
241 // v1.8.13 Upgrades for donor
242 $give_updates->register(
243 [
244 'id' => 'v1813_update_donor_user_roles',
245 'version' => '1.8.13',
246 'callback' => 'give_v1813_update_donor_user_roles_callback',
247 ]
248 );
249
250 // v1.8.17 Upgrades for donations.
251 $give_updates->register(
252 [
253 'id' => 'v1817_update_donation_iranian_currency_code',
254 'version' => '1.8.17',
255 'callback' => 'give_v1817_update_donation_iranian_currency_code',
256 ]
257 );
258
259 // v1.8.17 Upgrades for cleanup of user roles.
260 $give_updates->register(
261 [
262 'id' => 'v1817_cleanup_user_roles',
263 'version' => '1.8.17',
264 'callback' => 'give_v1817_cleanup_user_roles',
265 ]
266 );
267
268 // v1.8.18 Upgrades for assigning custom amount to existing set donations.
269 $give_updates->register(
270 [
271 'id' => 'v1818_assign_custom_amount_set_donation',
272 'version' => '1.8.18',
273 'callback' => 'give_v1818_assign_custom_amount_set_donation',
274 ]
275 );
276
277 // v1.8.18 Cleanup the Give Worker Role Caps.
278 $give_updates->register(
279 [
280 'id' => 'v1818_give_worker_role_cleanup',
281 'version' => '1.8.18',
282 'callback' => 'give_v1818_give_worker_role_cleanup',
283 ]
284 );
285
286 // v2.0.0 Upgrades
287 $give_updates->register(
288 [
289 'id' => 'v20_upgrades_form_metadata',
290 'version' => '2.0.0',
291 'callback' => 'give_v20_upgrades_form_metadata_callback',
292 ]
293 );
294
295 // v2.0.0 User Address Upgrades
296 $give_updates->register(
297 [
298 'id' => 'v20_upgrades_user_address',
299 'version' => '2.0.0',
300 'callback' => 'give_v20_upgrades_user_address',
301 ]
302 );
303
304 // v2.0.0 Upgrades
305 $give_updates->register(
306 [
307 'id' => 'v20_upgrades_payment_metadata',
308 'version' => '2.0.0',
309 'callback' => 'give_v20_upgrades_payment_metadata_callback',
310 ]
311 );
312
313 // v2.0.0 Donor Name Upgrades
314 $give_updates->register(
315 [
316 'id' => 'v20_upgrades_donor_name',
317 'version' => '2.0.0',
318 'callback' => 'give_v20_upgrades_donor_name',
319 ]
320 );
321
322 // v2.0.0 Upgrades
323 $give_updates->register(
324 [
325 'id' => 'v20_move_metadata_into_new_table',
326 'version' => '2.0.0',
327 'callback' => 'give_v20_move_metadata_into_new_table_callback',
328 'depend' => [ 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ],
329 ]
330 );
331
332 // v2.0.0 Upgrades
333 $give_updates->register(
334 [
335 'id' => 'v20_rename_donor_tables',
336 'version' => '2.0.0',
337 'callback' => 'give_v20_rename_donor_tables_callback',
338 'depend' => [
339 'v20_move_metadata_into_new_table',
340 'v20_logs_upgrades',
341 'v20_upgrades_form_metadata',
342 'v20_upgrades_payment_metadata',
343 'v20_upgrades_user_address',
344 'v20_upgrades_donor_name',
345 ],
346 ]
347 );
348
349 // v2.0.1 Upgrades
350 $give_updates->register(
351 [
352 'id' => 'v201_upgrades_payment_metadata',
353 'version' => '2.0.1',
354 'callback' => 'give_v201_upgrades_payment_metadata_callback',
355 ]
356 );
357
358 // v2.0.1 Upgrades
359 $give_updates->register(
360 [
361 'id' => 'v201_add_missing_donors',
362 'version' => '2.0.1',
363 'callback' => 'give_v201_add_missing_donors_callback',
364 ]
365 );
366
367 // Run v2.0.0 Upgrades again in 2.0.1
368 $give_updates->register(
369 [
370 'id' => 'v201_move_metadata_into_new_table',
371 'version' => '2.0.1',
372 'callback' => 'give_v201_move_metadata_into_new_table_callback',
373 'depend' => [ 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ],
374 ]
375 );
376
377 // v2.1 Verify Form Status Upgrade.
378 $give_updates->register(
379 [
380 'id' => 'v210_verify_form_status_upgrades',
381 'version' => '2.1.0',
382 'callback' => 'give_v210_verify_form_status_upgrades_callback',
383 ]
384 );
385
386 // v2.1.3 Delete non attached donation meta.
387 $give_updates->register(
388 [
389 'id' => 'v213_delete_donation_meta',
390 'version' => '2.1.3',
391 'callback' => 'give_v213_delete_donation_meta_callback',
392 'depends' => [ 'v201_move_metadata_into_new_table' ],
393 ]
394 );
395
396 // v2.1.5 Add additional capability to the give_manager role.
397 $give_updates->register(
398 [
399 'id' => 'v215_update_donor_user_roles',
400 'version' => '2.1.5',
401 'callback' => 'give_v215_update_donor_user_roles_callback',
402 ]
403 );
404
405 // v2.2.4 set each donor to anonymous by default.
406 $give_updates->register(
407 [
408 'id' => 'v224_update_donor_meta',
409 'version' => '2.2.4',
410 'callback' => 'give_v224_update_donor_meta_callback',
411 ]
412 );
413
414 // v2.2.4 Associate form IDs with donor meta of anonymous donations.
415 $give_updates->register(
416 [
417 'id' => 'v224_update_donor_meta_forms_id',
418 'version' => '2.2.4',
419 'callback' => 'give_v224_update_donor_meta_forms_id_callback',
420 'depend' => 'v224_update_donor_meta',
421 ]
422 );
423
424 // v2.3.0 Move donor notes to custom comment table.
425 $give_updates->register(
426 [
427 'id' => 'v230_move_donor_note',
428 'version' => '2.3.0',
429 'callback' => 'give_v230_move_donor_note_callback',
430 ]
431 );
432
433 // v2.3.0 Move donation notes to custom comment table.
434 $give_updates->register(
435 [
436 'id' => 'v230_move_donation_note',
437 'version' => '2.3.0',
438 'callback' => 'give_v230_move_donation_note_callback',
439 ]
440 );
441
442 // v2.3.0 remove donor wall related donor meta data.
443 $give_updates->register(
444 [
445 'id' => 'v230_delete_donor_wall_related_donor_data',
446 'version' => '2.3.0',
447 'depend' => [
448 'v224_update_donor_meta',
449 'v224_update_donor_meta_forms_id',
450 'v230_move_donor_note',
451 'v230_move_donation_note',
452 ],
453 'callback' => 'give_v230_delete_dw_related_donor_data_callback',
454 ]
455 );
456
457 // v2.3.0 remove donor wall related comment meta data.
458 $give_updates->register(
459 [
460 'id' => 'v230_delete_donor_wall_related_comment_data',
461 'version' => '2.3.0',
462 'callback' => 'give_v230_delete_dw_related_comment_data_callback',
463 'depend' => [
464 'v230_move_donor_note',
465 'v230_move_donation_note',
466 ],
467 ]
468 );
469
470 // v2.4.0 Update donation form goal progress data.
471 $give_updates->register(
472 [
473 'id' => 'v240_update_form_goal_progress',
474 'version' => '2.4.0',
475 'callback' => 'give_v240_update_form_goal_progress_callback',
476 ]
477 );
478
479 $give_updates->register(
480 [
481 'id' => 'v270_store_stripe_account_for_donation',
482 'version' => '2.7.0',
483 'callback' => 'give_v270_store_stripe_account_for_donation_callback',
484 ]
485 );
486 }
487
488 add_action( 'give_register_updates', 'give_show_upgrade_notices' );
489
490 /**
491 * Triggers all upgrade functions
492 *
493 * This function is usually triggered via AJAX
494 *
495 * @since 1.0
496 * @return void
497 */
498 function give_trigger_upgrades() {
499
500 if ( ! current_user_can( 'manage_give_settings' ) ) {
501 wp_die(
502 esc_html__( 'You do not have permission to do GiveWP upgrades.', 'give' ),
503 esc_html__( 'Error', 'give' ),
504 [
505 'response' => 403,
506 ]
507 );
508 }
509
510 $give_version = get_option( 'give_version' );
511
512 if ( ! $give_version ) {
513 // 1.0 is the first version to use this option so we must add it.
514 $give_version = '1.0';
515 add_option( 'give_version', $give_version, '', false );
516 }
517
518 update_option( 'give_version', GIVE_VERSION, false );
519 delete_option( 'give_doing_upgrade' );
520
521 if ( DOING_AJAX ) {
522 die( 'complete' );
523 } // End if().
524 }
525
526 add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
527
528
529 /**
530 * Upgrades the
531 *
532 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
533 *
534 * @since 1.3.2
535 */
536 function give_v132_upgrade_give_payment_customer_id() {
537 global $wpdb;
538
539 /* @var Give_Updates $give_updates */
540 $give_updates = Give_Updates::get_instance();
541
542 // UPDATE DB METAKEYS.
543 $sql = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
544 $query = $wpdb->query( $sql );
545
546 $give_updates->percentage = 100;
547 give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
548 }
549
550
551 /**
552 * Upgrades the Offline Status
553 *
554 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
555 *
556 * @since 1.3.4
557 */
558 function give_v134_upgrade_give_offline_status() {
559 global $wpdb;
560
561 /* @var Give_Updates $give_updates */
562 $give_updates = Give_Updates::get_instance();
563
564 // Get abandoned offline payments.
565 $select = "SELECT ID FROM $wpdb->posts p ";
566 $join = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
567 $where = "WHERE p.post_type = 'give_payment' ";
568 $where .= "AND ( p.post_status = 'abandoned' )";
569 $where .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
570
571 $sql = $select . $join . $where;
572 $found_payments = $wpdb->get_col( $sql );
573
574 foreach ( $found_payments as $payment ) {
575
576 // Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
577 $modified_time = get_post_modified_time( 'U', false, $payment );
578
579 // 1450124863 = 12/10/2015 20:42:25.
580 if ( $modified_time >= 1450124863 ) {
581
582 give_update_payment_status( $payment, 'pending' );
583
584 }
585 }
586
587 $give_updates->percentage = 100;
588 give_set_upgrade_complete( 'upgrade_give_offline_status' );
589 }
590
591
592 /**
593 * Cleanup User Roles
594 *
595 * This upgrade routine removes unused roles and roles with typos
596 *
597 * @since 1.5.2
598 */
599 function give_v152_cleanup_users() {
600
601 $give_version = get_option( 'give_version' );
602
603 if ( ! $give_version ) {
604 // 1.0 is the first version to use this option so we must add it.
605 $give_version = '1.0';
606 }
607
608 $give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
609
610 // v1.5.2 Upgrades
611 if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
612
613 // Delete all caps with "ss".
614 // Also delete all unused "campaign" roles.
615 $delete_caps = [
616 'delete_give_formss',
617 'delete_others_give_formss',
618 'delete_private_give_formss',
619 'delete_published_give_formss',
620 'read_private_forms',
621 'edit_give_formss',
622 'edit_others_give_formss',
623 'edit_private_give_formss',
624 'edit_published_give_formss',
625 'publish_give_formss',
626 'read_private_give_formss',
627 'assign_give_campaigns_terms',
628 'delete_give_campaigns',
629 'delete_give_campaigns_terms',
630 'delete_give_campaignss',
631 'delete_others_give_campaignss',
632 'delete_private_give_campaignss',
633 'delete_published_give_campaignss',
634 'edit_give_campaigns',
635 'edit_give_campaigns_terms',
636 'edit_give_campaignss',
637 'edit_others_give_campaignss',
638 'edit_private_give_campaignss',
639 'edit_published_give_campaignss',
640 'manage_give_campaigns_terms',
641 'publish_give_campaignss',
642 'read_give_campaigns',
643 'read_private_give_campaignss',
644 'view_give_campaigns_stats',
645 'delete_give_paymentss',
646 'delete_others_give_paymentss',
647 'delete_private_give_paymentss',
648 'delete_published_give_paymentss',
649 'edit_give_paymentss',
650 'edit_others_give_paymentss',
651 'edit_private_give_paymentss',
652 'edit_published_give_paymentss',
653 'publish_give_paymentss',
654 'read_private_give_paymentss',
655 ];
656
657 global $wp_roles;
658 foreach ( $delete_caps as $cap ) {
659 foreach ( array_keys( $wp_roles->roles ) as $role ) {
660 $wp_roles->remove_cap( $role, $cap );
661 }
662 }
663
664 // Create Give plugin roles.
665 $roles = new Give_Roles();
666 $roles->add_roles();
667 $roles->add_caps();
668
669 // The Update Ran.
670 update_option( 'give_version', GIVE_VERSION, false );
671 give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
672 delete_option( 'give_doing_upgrade' );
673
674 }// End if().
675
676 }
677
678 add_action( 'admin_init', 'give_v152_cleanup_users' );
679
680 /**
681 * 1.6 Upgrade routine to create the customer meta table.
682 *
683 * @since 1.6
684 * @return void
685 */
686 function give_v16_upgrades() {
687 // Create the donor databases.
688 $donors_db = new Give_DB_Donors();
689 $donors_db->create_table();
690 $donor_meta = new Give_DB_Donor_Meta();
691 $donor_meta->create_table();
692 }
693
694 /**
695 * 1.7 Upgrades.
696 *
697 * a. Update license api data for plugin addons.
698 * b. Cleanup user roles.
699 *
700 * @since 1.7
701 * @return void
702 */
703 function give_v17_upgrades() {
704 // Upgrade license data.
705 give_v17_upgrade_addon_license_data();
706 give_v17_cleanup_roles();
707 }
708
709 /**
710 * Upgrade license data
711 *
712 * @since 1.7
713 */
714 function give_v17_upgrade_addon_license_data() {
715 $give_options = give_get_settings();
716
717 $api_url = 'https://givewp.com/give-sl-api/';
718
719 // Get addons license key.
720 $addons = [];
721 foreach ( $give_options as $key => $value ) {
722 if ( false !== strpos( $key, '_license_key' ) ) {
723 $addons[ $key ] = $value;
724 }
725 }
726
727 // Bailout: We do not have any addon license data to upgrade.
728 if ( empty( $addons ) ) {
729 return false;
730 }
731
732 foreach ( $addons as $key => $addon_license ) {
733
734 // Get addon shortname.
735 $shortname = str_replace( '_license_key', '', $key );
736
737 // Addon license option name.
738 $addon_license_option_name = $shortname . '_license_active';
739
740 // bailout if license is empty.
741 if ( empty( $addon_license ) ) {
742 delete_option( $addon_license_option_name );
743 continue;
744 }
745
746 // Get addon name.
747 $addon_name = [];
748 $addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
749 foreach ( $addon_name_parts as $name_part ) {
750
751 // Fix addon name
752 switch ( $name_part ) {
753 case 'authorizenet':
754 $name_part = 'authorize.net';
755 break;
756 }
757
758 $addon_name[] = ucfirst( $name_part );
759 }
760
761 $addon_name = implode( ' ', $addon_name );
762
763 // Data to send to the API.
764 $api_params = [
765 'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
766 'license' => $addon_license,
767 'item_name' => urlencode( $addon_name ),
768 'url' => home_url(),
769 ];
770
771 // Call the API.
772 $response = wp_remote_post(
773 $api_url,
774 [
775 'timeout' => 15,
776 'sslverify' => false,
777 'body' => $api_params,
778 ]
779 );
780
781 // Make sure there are no errors.
782 if ( is_wp_error( $response ) ) {
783 delete_option( $addon_license_option_name );
784 continue;
785 }
786
787 // Tell WordPress to look for updates.
788 set_site_transient( 'update_plugins', null );
789
790 // Decode license data.
791 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
792 update_option( $addon_license_option_name, $license_data, false );
793 }// End foreach().
794 }
795
796
797 /**
798 * Cleanup User Roles.
799 *
800 * This upgrade routine removes unused roles and roles with typos.
801 *
802 * @since 1.7
803 */
804 function give_v17_cleanup_roles() {
805
806 // Delete all caps with "_give_forms_" and "_give_payments_".
807 // These roles have no usage; the proper is singular.
808 $delete_caps = [
809 'view_give_forms_stats',
810 'delete_give_forms_terms',
811 'assign_give_forms_terms',
812 'edit_give_forms_terms',
813 'manage_give_forms_terms',
814 'view_give_payments_stats',
815 'manage_give_payments_terms',
816 'edit_give_payments_terms',
817 'assign_give_payments_terms',
818 'delete_give_payments_terms',
819 ];
820
821 global $wp_roles;
822 foreach ( $delete_caps as $cap ) {
823 foreach ( array_keys( $wp_roles->roles ) as $role ) {
824 $wp_roles->remove_cap( $role, $cap );
825 }
826 }
827
828 // Set roles again.
829 $roles = new Give_Roles();
830 $roles->add_roles();
831 $roles->add_caps();
832
833 }
834
835 /**
836 * 1.8 Upgrades.
837 *
838 * a. Upgrade checkbox settings to radio button settings.
839 * a. Update form meta for new metabox settings.
840 *
841 * @since 1.8
842 * @return void
843 */
844 function give_v18_upgrades() {
845 // Upgrade checkbox settings to radio button settings.
846 give_v18_upgrades_core_setting();
847 }
848
849 /**
850 * Upgrade core settings.
851 *
852 * @since 1.8
853 * @return void
854 */
855 function give_v18_upgrades_core_setting() {
856 // Core settings which changes from checkbox to radio.
857 $core_setting_names = array_merge(
858 array_keys( give_v18_renamed_core_settings() ),
859 [
860 'uninstall_on_delete',
861 'scripts_footer',
862 'test_mode',
863 'email_access',
864 'terms',
865 'give_offline_donation_enable_billing_fields',
866 ]
867 );
868
869 // Bailout: If not any setting define.
870 if ( $give_settings = get_option( 'give_settings' ) ) {
871
872 $setting_changed = false;
873
874 // Loop: check each setting field.
875 foreach ( $core_setting_names as $setting_name ) {
876 // New setting name.
877 $new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
878
879 // Continue: If setting already set.
880 if (
881 array_key_exists( $new_setting_name, $give_settings )
882 && in_array( $give_settings[ $new_setting_name ], [ 'enabled', 'disabled' ] )
883 ) {
884 continue;
885 }
886
887 // Set checkbox value to radio value.
888 $give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
889
890 // @see https://github.com/impress-org/give/issues/1063.
891 if ( false !== strpos( $setting_name, 'disable_' ) ) {
892
893 $give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
894 } elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
895
896 $give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
897 }
898
899 // Tell bot to update core setting to db.
900 if ( ! $setting_changed ) {
901 $setting_changed = true;
902 }
903 }
904
905 // Update setting only if they changed.
906 if ( $setting_changed ) {
907 update_option( 'give_settings', $give_settings, false );
908 }
909 }// End if().
910
911 give_set_upgrade_complete( 'v18_upgrades_core_setting' );
912 }
913
914 /**
915 * Upgrade form metadata for new metabox settings.
916 *
917 * @since 1.8
918 * @return void
919 */
920 function give_v18_upgrades_form_metadata() {
921 /* @var Give_Updates $give_updates */
922 $give_updates = Give_Updates::get_instance();
923
924 // form query
925 $forms = new WP_Query(
926 [
927 'paged' => $give_updates->step,
928 'status' => 'any',
929 'order' => 'ASC',
930 'post_type' => 'give_forms',
931 'posts_per_page' => 20,
932 ]
933 );
934
935 if ( $forms->have_posts() ) {
936 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
937
938 while ( $forms->have_posts() ) {
939 $forms->the_post();
940
941 // Form content.
942 // Note in version 1.8 display content setting split into display content and content placement setting.
943 // You can delete _give_content_option in future.
944 $show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
945 if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
946 $field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
947 give_update_meta( get_the_ID(), '_give_display_content', $field_value );
948
949 $field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
950 give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
951 }
952
953 // "Disable" Guest Donation. Checkbox.
954 // See: https://github.com/impress-org/give/issues/1470.
955 $guest_donation = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
956 $guest_donation_newval = ( in_array( $guest_donation, [ 'yes', 'on' ] ) ? 'disabled' : 'enabled' );
957 give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
958
959 // Offline Donations.
960 // See: https://github.com/impress-org/give/issues/1579.
961 $offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
962 if ( 'no' === $offline_donation ) {
963 $offline_donation_newval = 'global';
964 } elseif ( 'yes' === $offline_donation ) {
965 $offline_donation_newval = 'enabled';
966 } else {
967 $offline_donation_newval = 'disabled';
968 }
969 give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
970
971 // Convert yes/no setting field to enabled/disabled.
972 $form_radio_settings = [
973 // Custom Amount.
974 '_give_custom_amount',
975
976 // Donation Gaol.
977 '_give_goal_option',
978
979 // Close Form.
980 '_give_close_form_when_goal_achieved',
981
982 // Term & conditions.
983 '_give_terms_option',
984
985 // Billing fields.
986 '_give_offline_donation_enable_billing_fields_single',
987 ];
988
989 foreach ( $form_radio_settings as $meta_key ) {
990 // Get value.
991 $field_value = give_get_meta( get_the_ID(), $meta_key, true );
992
993 // Convert meta value only if it is in yes/no/none.
994 if ( in_array( $field_value, [ 'yes', 'on', 'no', 'none' ] ) ) {
995
996 $field_value = ( in_array( $field_value, [ 'yes', 'on' ] ) ? 'enabled' : 'disabled' );
997 give_update_meta( get_the_ID(), $meta_key, $field_value );
998 }
999 }
1000 }// End while().
1001
1002 wp_reset_postdata();
1003
1004 } else {
1005 // No more forms found, finish up.
1006 give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
1007 }
1008 }
1009
1010
1011 /**
1012 * Get list of core setting renamed in version 1.8.
1013 *
1014 * @since 1.8
1015 * @return array
1016 */
1017 function give_v18_renamed_core_settings() {
1018 return [
1019 'disable_paypal_verification' => 'paypal_verification',
1020 'disable_css' => 'css',
1021 'disable_forms_singular' => 'forms_singular',
1022 'disable_forms_archives' => 'forms_archives',
1023 'disable_forms_excerpt' => 'forms_excerpt',
1024 'disable_form_featured_img' => 'form_featured_img',
1025 'disable_form_sidebar' => 'form_sidebar',
1026 'disable_admin_notices' => 'admin_notices',
1027 'disable_the_content_filter' => 'the_content_filter',
1028 'enable_floatlabels' => 'floatlabels',
1029 'enable_categories' => 'categories',
1030 'enable_tags' => 'tags',
1031 ];
1032 }
1033
1034
1035 /**
1036 * Upgrade core settings.
1037 *
1038 * @since 1.8.7
1039 * @return void
1040 */
1041 function give_v187_upgrades() {
1042 global $wpdb;
1043
1044 /**
1045 * Upgrade 1: Remove stat and cache transients.
1046 */
1047 $cached_options = $wpdb->get_col(
1048 $wpdb->prepare(
1049 "
1050 SELECT *
1051 FROM {$wpdb->options}
1052 WHERE (
1053 option_name LIKE %s
1054 OR option_name LIKE %s
1055 OR option_name LIKE %s
1056 OR option_name LIKE %s
1057 OR option_name LIKE %s
1058 OR option_name LIKE %s
1059 OR option_name LIKE %s
1060 OR option_name LIKE %s
1061 OR option_name LIKE %s
1062 OR option_name LIKE %s
1063 OR option_name LIKE %s
1064 OR option_name LIKE %s
1065 OR option_name LIKE %s
1066 )
1067 ",
1068 [
1069 '%_transient_give_stats_%',
1070 'give_cache%',
1071 '%_transient_give_add_ons_feed%',
1072 '%_transient__give_ajax_works' .
1073 '%_transient_give_total_api_keys%',
1074 '%_transient_give_i18n_give_promo_hide%',
1075 '%_transient_give_contributors%',
1076 '%_transient_give_estimated_monthly_stats%',
1077 '%_transient_give_earnings_total%',
1078 '%_transient_give_i18n_give_%',
1079 '%_transient__give_installed%',
1080 '%_transient__give_activation_redirect%',
1081 '%_transient__give_hide_license_notices_shortly_%',
1082 '%give_income_total%',
1083 ]
1084 ),
1085 1
1086 );
1087
1088 // User related transients.
1089 $user_apikey_options = $wpdb->get_results(
1090 $wpdb->prepare(
1091 "SELECT user_id, meta_key
1092 FROM $wpdb->usermeta
1093 WHERE meta_value=%s",
1094 'give_user_public_key'
1095 ),
1096 ARRAY_A
1097 );
1098
1099 if ( ! empty( $user_apikey_options ) ) {
1100 foreach ( $user_apikey_options as $user ) {
1101 $cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1102 $cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1103 $cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1104 }
1105 }
1106
1107 if ( ! empty( $cached_options ) ) {
1108 foreach ( $cached_options as $option ) {
1109 switch ( true ) {
1110 case ( false !== strpos( $option, 'transient' ) ):
1111 $option = str_replace( '_transient_', '', $option );
1112 delete_transient( $option );
1113 break;
1114
1115 default:
1116 delete_option( $option );
1117 }
1118 }
1119 }
1120 }
1121
1122 /**
1123 * Update Capabilities for Give_Worker User Role.
1124 *
1125 * This upgrade routine will update access rights for Give_Worker User Role.
1126 *
1127 * @since 1.8.8
1128 */
1129 function give_v188_upgrades() {
1130
1131 global $wp_roles;
1132
1133 // Get the role object.
1134 $give_worker = get_role( 'give_worker' );
1135
1136 // A list of capabilities to add for give workers.
1137 $caps_to_add = [
1138 'edit_posts',
1139 'edit_pages',
1140 ];
1141
1142 foreach ( $caps_to_add as $cap ) {
1143 // Add the capability.
1144 $give_worker->add_cap( $cap );
1145 }
1146
1147 }
1148
1149 /**
1150 * Update Post meta for minimum and maximum amount for multi level donation forms
1151 *
1152 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1153 *
1154 * @since 1.8.9
1155 */
1156 function give_v189_upgrades_levels_post_meta_callback() {
1157 /* @var Give_Updates $give_updates */
1158 $give_updates = Give_Updates::get_instance();
1159
1160 // form query.
1161 $donation_forms = new WP_Query(
1162 [
1163 'paged' => $give_updates->step,
1164 'status' => 'any',
1165 'order' => 'ASC',
1166 'post_type' => 'give_forms',
1167 'posts_per_page' => 20,
1168 ]
1169 );
1170
1171 if ( $donation_forms->have_posts() ) {
1172 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1173
1174 while ( $donation_forms->have_posts() ) {
1175 $donation_forms->the_post();
1176 $form_id = get_the_ID();
1177
1178 // Remove formatting from _give_set_price.
1179 update_post_meta(
1180 $form_id,
1181 '_give_set_price',
1182 give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1183 );
1184
1185 // Remove formatting from _give_custom_amount_minimum.
1186 update_post_meta(
1187 $form_id,
1188 '_give_custom_amount_minimum',
1189 give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1190 );
1191
1192 // Bailout.
1193 if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1194 continue;
1195 }
1196
1197 $donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1198
1199 if ( ! empty( $donation_levels ) ) {
1200
1201 foreach ( $donation_levels as $index => $donation_level ) {
1202 if ( isset( $donation_level['_give_amount'] ) ) {
1203 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1204 }
1205 }
1206
1207 update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1208
1209 $donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1210
1211 $min_amount = min( $donation_levels_amounts );
1212 $max_amount = max( $donation_levels_amounts );
1213
1214 // Set Minimum and Maximum amount for Multi Level Donation Forms
1215 give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1216 give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1217 }
1218 }
1219
1220 /* Restore original Post Data */
1221 wp_reset_postdata();
1222 } else {
1223 // The Update Ran.
1224 give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1225 }
1226
1227 }
1228
1229
1230 /**
1231 * Give version 1.8.9 upgrades
1232 *
1233 * @since 1.8.9
1234 */
1235 function give_v189_upgrades() {
1236 /**
1237 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1238 */
1239 global $wpdb;
1240
1241 // Delete permanent notice blocker.
1242 $wpdb->query(
1243 $wpdb->prepare(
1244 "
1245 DELETE FROM $wpdb->usermeta
1246 WHERE meta_key
1247 LIKE '%%%s%%'
1248 ",
1249 '_give_hide_license_notices_permanently'
1250 )
1251 );
1252
1253 // Delete short notice blocker.
1254 $wpdb->query(
1255 $wpdb->prepare(
1256 "
1257 DELETE FROM $wpdb->options
1258 WHERE option_name
1259 LIKE '%%%s%%'
1260 ",
1261 '__give_hide_license_notices_shortly_'
1262 )
1263 );
1264 }
1265
1266 /**
1267 * 2.0 Upgrades.
1268 *
1269 * @since 2.0
1270 * @return void
1271 */
1272 function give_v20_upgrades() {
1273 // Update cache setting.
1274 give_update_option( 'cache', 'enabled' );
1275
1276 // Upgrade email settings.
1277 give_v20_upgrades_email_setting();
1278 }
1279
1280 /**
1281 * Move old email api settings to new email setting api for following emails:
1282 * 1. new offline donation [This was hard coded]
1283 * 2. offline donation instruction
1284 * 3. new donation
1285 * 4. donation receipt
1286 *
1287 * @since 2.0
1288 */
1289 function give_v20_upgrades_email_setting() {
1290 $all_setting = give_get_settings();
1291
1292 // Bailout on fresh install.
1293 if ( empty( $all_setting ) ) {
1294 return;
1295 }
1296
1297 $settings = [
1298 'offline_donation_subject' => 'offline-donation-instruction_email_subject',
1299 'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1300 'donation_subject' => 'donation-receipt_email_subject',
1301 'donation_receipt' => 'donation-receipt_email_message',
1302 'donation_notification_subject' => 'new-donation_email_subject',
1303 'donation_notification' => 'new-donation_email_message',
1304 'admin_notice_emails' => [
1305 'new-donation_recipient',
1306 'new-offline-donation_recipient',
1307 'new-donor-register_recipient',
1308 ],
1309 'admin_notices' => 'new-donation_notification',
1310 ];
1311
1312 foreach ( $settings as $old_setting => $new_setting ) {
1313 // Do not update already modified
1314 if ( ! is_array( $new_setting ) ) {
1315 if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1316 continue;
1317 }
1318 }
1319
1320 switch ( $old_setting ) {
1321 case 'admin_notices':
1322 $notification_status = give_get_option( $old_setting, 'enabled' );
1323
1324 give_update_option( $new_setting, $notification_status );
1325
1326 // @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1327 // give_delete_option( $old_setting );
1328 break;
1329
1330 // @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1331 case 'admin_notice_emails':
1332 $recipients = give_get_admin_notice_emails();
1333
1334 foreach ( $new_setting as $setting ) {
1335 // bailout if setting already exist.
1336 if ( array_key_exists( $setting, $all_setting ) ) {
1337 continue;
1338 }
1339
1340 give_update_option( $setting, $recipients );
1341 }
1342 break;
1343
1344 default:
1345 give_update_option( $new_setting, give_get_option( $old_setting ) );
1346 give_delete_option( $old_setting );
1347 }
1348 }
1349 }
1350
1351 /**
1352 * Give version 1.8.9 upgrades
1353 *
1354 * @since 1.8.9
1355 */
1356 function give_v1812_upgrades() {
1357 /**
1358 * Validate number format settings.
1359 */
1360 $give_settings = give_get_settings();
1361 $give_setting_updated = false;
1362
1363 if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1364 $give_settings['number_decimals'] = 0;
1365 $give_settings['decimal_separator'] = '';
1366 $give_setting_updated = true;
1367
1368 } elseif ( empty( $give_settings['decimal_separator'] ) ) {
1369 $give_settings['number_decimals'] = 0;
1370 $give_setting_updated = true;
1371
1372 } elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1373 $give_settings['number_decimals'] = 5;
1374 $give_setting_updated = true;
1375 }
1376
1377 if ( $give_setting_updated ) {
1378 update_option( 'give_settings', $give_settings, false );
1379 }
1380 }
1381
1382
1383 /**
1384 * Give version 1.8.12 update
1385 *
1386 * Standardized amount values to six decimal
1387 *
1388 * @see https://github.com/impress-org/give/issues/1849#issuecomment-315128602
1389 *
1390 * @since 1.8.12
1391 */
1392 function give_v1812_update_amount_values_callback() {
1393 /* @var Give_Updates $give_updates */
1394 $give_updates = Give_Updates::get_instance();
1395
1396 // form query.
1397 $donation_forms = new WP_Query(
1398 [
1399 'paged' => $give_updates->step,
1400 'status' => 'any',
1401 'order' => 'ASC',
1402 'post_type' => [ 'give_forms', 'give_payment' ],
1403 'posts_per_page' => 20,
1404 ]
1405 );
1406 if ( $donation_forms->have_posts() ) {
1407 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1408
1409 while ( $donation_forms->have_posts() ) {
1410 $donation_forms->the_post();
1411 global $post;
1412
1413 $meta = get_post_meta( $post->ID );
1414
1415 switch ( $post->post_type ) {
1416 case 'give_forms':
1417 // _give_set_price.
1418 if ( ! empty( $meta['_give_set_price'][0] ) ) {
1419 update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1420 }
1421
1422 // _give_custom_amount_minimum.
1423 if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1424 update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1425 }
1426
1427 // _give_levels_minimum_amount.
1428 if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1429 update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1430 }
1431
1432 // _give_levels_maximum_amount.
1433 if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1434 update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1435 }
1436
1437 // _give_set_goal.
1438 if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1439 update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1440 }
1441
1442 // _give_form_earnings.
1443 if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1444 update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1445 }
1446
1447 // _give_custom_amount_minimum.
1448 if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1449 $donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1450
1451 foreach ( $donation_levels as $index => $level ) {
1452 if ( empty( $level['_give_amount'] ) ) {
1453 continue;
1454 }
1455
1456 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1457 }
1458
1459 $meta['_give_donation_levels'] = $donation_levels;
1460 update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1461 }
1462
1463 break;
1464
1465 case 'give_payment':
1466 // _give_payment_total.
1467 if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1468 update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1469 }
1470
1471 break;
1472 }
1473 }
1474
1475 /* Restore original Post Data */
1476 wp_reset_postdata();
1477 } else {
1478 // The Update Ran.
1479 give_set_upgrade_complete( 'v1812_update_amount_values' );
1480 }
1481 }
1482
1483
1484 /**
1485 * Give version 1.8.12 update
1486 *
1487 * Standardized amount values to six decimal for donor
1488 *
1489 * @see https://github.com/impress-org/give/issues/1849#issuecomment-315128602
1490 *
1491 * @since 1.8.12
1492 */
1493 function give_v1812_update_donor_purchase_value_callback() {
1494 /* @var Give_Updates $give_updates */
1495 $give_updates = Give_Updates::get_instance();
1496
1497 // form query.
1498 $donors = Give()->donors->get_donors(
1499 [
1500 'number' => 20,
1501 'offset' => $give_updates->get_offset( 20 ),
1502 ]
1503 );
1504
1505 if ( ! empty( $donors ) ) {
1506 $give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1507
1508 /* @var Object $donor */
1509 foreach ( $donors as $donor ) {
1510 Give()->donors->update( $donor->id, [ 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ] );
1511 }
1512 } else {
1513 // The Update Ran.
1514 give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1515 }
1516 }
1517
1518 /**
1519 * Upgrade routine for updating user roles for existing donors.
1520 *
1521 * @since 1.8.13
1522 */
1523 function give_v1813_update_donor_user_roles_callback() {
1524 /* @var Give_Updates $give_updates */
1525 $give_updates = Give_Updates::get_instance();
1526
1527 // Fetch all the existing donors.
1528 $donors = Give()->donors->get_donors(
1529 [
1530 'number' => 20,
1531 'offset' => $give_updates->get_offset( 20 ),
1532 ]
1533 );
1534
1535 if ( ! empty( $donors ) ) {
1536 $give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1537
1538 /* @var Object $donor */
1539 foreach ( $donors as $donor ) {
1540 $user_id = $donor->user_id;
1541
1542 // Proceed, if donor is attached with user.
1543 if ( $user_id ) {
1544 $user = get_userdata( $user_id );
1545
1546 // Update user role, if user has subscriber role.
1547 if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1548 wp_update_user(
1549 [
1550 'ID' => $user_id,
1551 'role' => 'give_donor',
1552 ]
1553 );
1554 }
1555 }
1556 }
1557 } else {
1558 // The Update Ran.
1559 give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1560 }
1561 }
1562
1563
1564 /**
1565 * Version 1.8.13 automatic updates
1566 *
1567 * @since 1.8.13
1568 */
1569 function give_v1813_upgrades() {
1570 // Update admin setting.
1571 give_update_option( 'donor_default_user_role', 'give_donor' );
1572
1573 // Update Give roles.
1574 $roles = new Give_Roles();
1575 $roles->add_roles();
1576 $roles->add_caps();
1577 }
1578
1579 /**
1580 * Correct currency code for "Iranian Currency" for all of the payments.
1581 *
1582 * @since 1.8.17
1583 */
1584 function give_v1817_update_donation_iranian_currency_code() {
1585 /* @var Give_Updates $give_updates */
1586 $give_updates = Give_Updates::get_instance();
1587
1588 // form query.
1589 $payments = new WP_Query(
1590 [
1591 'paged' => $give_updates->step,
1592 'status' => 'any',
1593 'order' => 'ASC',
1594 'post_type' => [ 'give_payment' ],
1595 'posts_per_page' => 100,
1596 ]
1597 );
1598
1599 if ( $payments->have_posts() ) {
1600 $give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1601
1602 while ( $payments->have_posts() ) {
1603 $payments->the_post();
1604
1605 $payment_meta = give_get_payment_meta( get_the_ID() );
1606
1607 if ( 'RIAL' === $payment_meta['currency'] ) {
1608 $payment_meta['currency'] = 'IRR';
1609 give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1610 }
1611 }
1612 } else {
1613 // The Update Ran.
1614 give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1615 }
1616 }
1617
1618 /**
1619 * Correct currency code for "Iranian Currency" in Give setting.
1620 * Version 1.8.17 automatic updates
1621 *
1622 * @since 1.8.17
1623 */
1624 function give_v1817_upgrades() {
1625 $give_settings = give_get_settings();
1626
1627 if ( 'RIAL' === $give_settings['currency'] ) {
1628 $give_settings['currency'] = 'IRR';
1629 update_option( 'give_settings', $give_settings, false );
1630 }
1631 }
1632
1633 /**
1634 * Process Clean up of User Roles for more flexibility.
1635 *
1636 * @since 1.8.17
1637 */
1638 function give_v1817_process_cleanup_user_roles() {
1639
1640 global $wp_roles;
1641
1642 if ( ! ( $wp_roles instanceof WP_Roles ) ) {
1643 return;
1644 }
1645
1646 // Add Capabilities to user roles as required.
1647 $add_caps = [
1648 'administrator' => [
1649 'view_give_payments',
1650 ],
1651 ];
1652
1653 // Remove Capabilities to user roles as required.
1654 $remove_caps = [
1655 'give_manager' => [
1656 'edit_others_pages',
1657 'edit_others_posts',
1658 'delete_others_pages',
1659 'delete_others_posts',
1660 'manage_categories',
1661 'import',
1662 'export',
1663 ],
1664 ];
1665
1666 foreach ( $add_caps as $role => $caps ) {
1667 foreach ( $caps as $cap ) {
1668 $wp_roles->add_cap( $role, $cap );
1669 }
1670 }
1671
1672 foreach ( $remove_caps as $role => $caps ) {
1673 foreach ( $caps as $cap ) {
1674 $wp_roles->remove_cap( $role, $cap );
1675 }
1676 }
1677
1678 }
1679
1680 /**
1681 * Upgrade Routine - Clean up of User Roles for more flexibility.
1682 *
1683 * @since 1.8.17
1684 */
1685 function give_v1817_cleanup_user_roles() {
1686 /* @var Give_Updates $give_updates */
1687 $give_updates = Give_Updates::get_instance();
1688
1689 give_v1817_process_cleanup_user_roles();
1690
1691 $give_updates->percentage = 100;
1692
1693 // Create Give plugin roles.
1694 $roles = new Give_Roles();
1695 $roles->add_roles();
1696 $roles->add_caps();
1697
1698 give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1699 }
1700
1701 /**
1702 * Automatic Upgrade for release 1.8.18.
1703 *
1704 * @since 1.8.18
1705 */
1706 function give_v1818_upgrades() {
1707
1708 // Remove email_access_installed from give_settings.
1709 give_delete_option( 'email_access_installed' );
1710 }
1711
1712 /**
1713 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1714 *
1715 * @since 1.8.18
1716 */
1717 function give_v1818_assign_custom_amount_set_donation() {
1718
1719 /* @var Give_Updates $give_updates */
1720 $give_updates = Give_Updates::get_instance();
1721
1722 $donations = new WP_Query(
1723 [
1724 'paged' => $give_updates->step,
1725 'status' => 'any',
1726 'order' => 'ASC',
1727 'post_type' => [ 'give_payment' ],
1728 'posts_per_page' => 100,
1729 ]
1730 );
1731
1732 if ( $donations->have_posts() ) {
1733 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1734
1735 while ( $donations->have_posts() ) {
1736 $donations->the_post();
1737
1738 $form = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1739 $donation_meta = give_get_payment_meta( get_the_ID() );
1740
1741 // Update Donation meta with price_id set as custom, only if it is:
1742 // 1. Donation Type = Set Donation.
1743 // 2. Donation Price Id is not set to custom.
1744 // 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1745 if (
1746 $form->ID &&
1747 $form->is_set_type_donation_form() &&
1748 ( 'custom' !== $donation_meta['price_id'] ) &&
1749 $form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1750 ) {
1751 $donation_meta['price_id'] = 'custom';
1752 give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1753 give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1754 }
1755 }
1756
1757 wp_reset_postdata();
1758 } else {
1759 // Update Ran Successfully.
1760 give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1761 }
1762 }
1763
1764 /**
1765 * Upgrade Routine - Removed Give Worker caps.
1766 *
1767 * See: https://github.com/impress-org/give/issues/2476
1768 *
1769 * @since 1.8.18
1770 */
1771 function give_v1818_give_worker_role_cleanup() {
1772
1773 /* @var Give_Updates $give_updates */
1774 $give_updates = Give_Updates::get_instance();
1775
1776 global $wp_roles;
1777
1778 if ( ! ( $wp_roles instanceof WP_Roles ) ) {
1779 return;
1780 }
1781
1782 // Remove Capabilities to user roles as required.
1783 $remove_caps = [
1784 'give_worker' => [
1785 'delete_give_payments',
1786 'delete_others_give_payments',
1787 'delete_private_give_payments',
1788 'delete_published_give_payments',
1789 'edit_others_give_payments',
1790 'edit_private_give_payments',
1791 'edit_published_give_payments',
1792 'read_private_give_payments',
1793 ],
1794 ];
1795
1796 foreach ( $remove_caps as $role => $caps ) {
1797 foreach ( $caps as $cap ) {
1798 $wp_roles->remove_cap( $role, $cap );
1799 }
1800 }
1801
1802 $give_updates->percentage = 100;
1803
1804 // Create Give plugin roles.
1805 $roles = new Give_Roles();
1806 $roles->add_roles();
1807 $roles->add_caps();
1808
1809 give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1810 }
1811
1812 /**
1813 *
1814 * Upgrade form metadata for new metabox settings.
1815 *
1816 * @since 2.0
1817 * @return void
1818 */
1819 function give_v20_upgrades_form_metadata_callback() {
1820 $give_updates = Give_Updates::get_instance();
1821
1822 // form query
1823 $forms = new WP_Query(
1824 [
1825 'paged' => $give_updates->step,
1826 'status' => 'any',
1827 'order' => 'ASC',
1828 'post_type' => 'give_forms',
1829 'posts_per_page' => 100,
1830 ]
1831 );
1832
1833 if ( $forms->have_posts() ) {
1834 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1835
1836 while ( $forms->have_posts() ) {
1837 $forms->the_post();
1838 global $post;
1839
1840 // Update offline instruction email notification status.
1841 $offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1842 $offline_instruction_notification_status = give_is_setting_enabled(
1843 $offline_instruction_notification_status,
1844 [
1845 'enabled',
1846 'global',
1847 ]
1848 )
1849 ? $offline_instruction_notification_status
1850 : 'global';
1851 update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1852
1853 // Update offline instruction email message.
1854 update_post_meta(
1855 get_the_ID(),
1856 '_give_offline-donation-instruction_email_message',
1857 get_post_meta(
1858 get_the_ID(),
1859 // @todo: Delete this option later ( version > 2.0 ).
1860 '_give_offline_donation_email',
1861 true
1862 )
1863 );
1864
1865 // Update offline instruction email subject.
1866 update_post_meta(
1867 get_the_ID(),
1868 '_give_offline-donation-instruction_email_subject',
1869 get_post_meta(
1870 get_the_ID(),
1871 // @todo: Delete this option later ( version > 2.0 ).
1872 '_give_offline_donation_subject',
1873 true
1874 )
1875 );
1876
1877 }// End while().
1878
1879 wp_reset_postdata();
1880 } else {
1881 // No more forms found, finish up.
1882 give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1883 }
1884 }
1885
1886
1887 /**
1888 * Upgrade payment metadata for new metabox settings.
1889 *
1890 * @since 2.0
1891 * @return void
1892 * @global wpdb $wpdb
1893 */
1894 function give_v20_upgrades_payment_metadata_callback() {
1895 global $wpdb;
1896 $give_updates = Give_Updates::get_instance();
1897
1898 // form query
1899 $forms = new WP_Query(
1900 [
1901 'paged' => $give_updates->step,
1902 'status' => 'any',
1903 'order' => 'ASC',
1904 'post_type' => 'give_payment',
1905 'posts_per_page' => 100,
1906 ]
1907 );
1908
1909 if ( $forms->have_posts() ) {
1910 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1911
1912 while ( $forms->have_posts() ) {
1913 $forms->the_post();
1914 global $post;
1915
1916 // Split _give_payment_meta meta.
1917 // @todo Remove _give_payment_meta after releases 2.0
1918 $payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1919
1920 if ( ! empty( $payment_meta ) ) {
1921 _give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1922 }
1923
1924 $deprecated_meta_keys = [
1925 '_give_payment_customer_id' => '_give_payment_donor_id',
1926 '_give_payment_user_email' => '_give_payment_donor_email',
1927 '_give_payment_user_ip' => '_give_payment_donor_ip',
1928 ];
1929
1930 foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
1931 // Do not add new meta key if already exist.
1932 if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, $new_meta_key ) ) ) {
1933 continue;
1934 }
1935
1936 $wpdb->insert(
1937 $wpdb->postmeta,
1938 [
1939 'post_id' => $post->ID,
1940 'meta_key' => $new_meta_key,
1941 'meta_value' => give_get_meta( $post->ID, $old_meta_key, true ),
1942 ]
1943 );
1944 }
1945
1946 // Bailout
1947 if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
1948 /* @var Give_Donor $donor */
1949 $donor = new Give_Donor( $donor_id );
1950
1951 $address['line1'] = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
1952 $address['line2'] = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
1953 $address['city'] = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
1954 $address['state'] = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
1955 $address['zip'] = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
1956 $address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
1957
1958 // Save address.
1959 $donor->add_address( 'billing[]', $address );
1960 }
1961 }// End while().
1962
1963 wp_reset_postdata();
1964 } else {
1965 // @todo Delete user id meta after releases 2.0
1966 // $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1967 // No more forms found, finish up.
1968 give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1969 }
1970 }
1971
1972
1973 /**
1974 * Move payment and form metadata to new table
1975 *
1976 * @since 2.0
1977 * @return void
1978 */
1979 function give_v20_move_metadata_into_new_table_callback() {
1980 global $wpdb;
1981 $give_updates = Give_Updates::get_instance();
1982
1983 // form query
1984 $payments = new WP_Query(
1985 [
1986 'paged' => $give_updates->step,
1987 'status' => 'any',
1988 'order' => 'ASC',
1989 'post_type' => [ 'give_forms', 'give_payment' ],
1990 'posts_per_page' => 100,
1991 ]
1992 );
1993
1994 if ( $payments->have_posts() ) {
1995 $give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
1996
1997 while ( $payments->have_posts() ) {
1998 $payments->the_post();
1999 global $post;
2000
2001 $meta_data = $wpdb->get_results(
2002 $wpdb->prepare(
2003 "SELECT * FROM $wpdb->postmeta where post_id=%d",
2004 get_the_ID()
2005 ),
2006 ARRAY_A
2007 );
2008
2009 if ( ! empty( $meta_data ) ) {
2010 foreach ( $meta_data as $index => $data ) {
2011 // Check for duplicate meta values.
2012 if ( $result = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . ' WHERE meta_id=%d', $data['meta_id'] ), ARRAY_A ) ) {
2013 continue;
2014 }
2015
2016 switch ( $post->post_type ) {
2017 case 'give_forms':
2018 $data['form_id'] = $data['post_id'];
2019 unset( $data['post_id'] );
2020
2021 Give()->form_meta->insert( $data );
2022 // @todo: delete form meta from post meta table after releases 2.0.
2023 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2024
2025 break;
2026
2027 case 'give_payment':
2028 $data['payment_id'] = $data['post_id'];
2029 unset( $data['post_id'] );
2030
2031 Give()->payment_meta->insert( $data );
2032
2033 // @todo: delete donation meta from post meta table after releases 2.0.
2034 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2035
2036 break;
2037 }
2038 }
2039 }
2040 }// End while().
2041
2042 wp_reset_postdata();
2043 } else {
2044 // No more forms found, finish up.
2045 give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2046 }
2047
2048 }
2049
2050 /**
2051 * Upgrade routine for splitting donor name into first name and last name.
2052 *
2053 * @since 2.0
2054 *
2055 * @return void
2056 */
2057 function give_v20_upgrades_donor_name() {
2058 /* @var Give_Updates $give_updates */
2059 $give_updates = Give_Updates::get_instance();
2060
2061 $donors = Give()->donors->get_donors(
2062 [
2063 'paged' => $give_updates->step,
2064 'number' => 100,
2065 ]
2066 );
2067
2068 if ( $donors ) {
2069 $give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2070 // Loop through Donors
2071 foreach ( $donors as $donor ) {
2072
2073 $donor_name = explode( ' ', $donor->name, 2 );
2074 $donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2075 $donor_last_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2076
2077 // If first name meta of donor is not created, then create it.
2078 if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
2079 Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2080 }
2081
2082 // If last name meta of donor is not created, then create it.
2083 if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
2084 Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2085 }
2086
2087 // If Donor is connected with WP User then update user meta.
2088 if ( $donor->user_id ) {
2089 if ( isset( $donor_name[0] ) ) {
2090 update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
2091 }
2092 if ( isset( $donor_name[1] ) ) {
2093 update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
2094 }
2095 }
2096 }
2097 } else {
2098 // The Update Ran.
2099 give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2100 }
2101
2102 }
2103
2104 /**
2105 * Upgrade routine for user addresses.
2106 *
2107 * @since 2.0
2108 * @return void
2109 * @global wpdb $wpdb
2110 *
2111 */
2112 function give_v20_upgrades_user_address() {
2113 global $wpdb;
2114
2115 /* @var Give_Updates $give_updates */
2116 $give_updates = Give_Updates::get_instance();
2117
2118 /* @var WP_User_Query $user_query */
2119 $user_query = new WP_User_Query(
2120 [
2121 'number' => 100,
2122 'paged' => $give_updates->step,
2123 ]
2124 );
2125
2126 $users = $user_query->get_results();
2127
2128 if ( $users ) {
2129 $give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2130
2131 // Loop through Donors
2132 foreach ( $users as $user ) {
2133 /* @var Give_Donor $donor */
2134 $donor = new Give_Donor( $user->ID, true );
2135
2136 if ( ! $donor->id ) {
2137 continue;
2138 }
2139
2140 $address = $wpdb->get_var(
2141 $wpdb->prepare(
2142 "
2143 SELECT meta_value FROM {$wpdb->usermeta}
2144 WHERE user_id=%s
2145 AND meta_key=%s
2146 ",
2147 $user->ID,
2148 '_give_user_address'
2149 )
2150 );
2151
2152 if ( ! empty( $address ) ) {
2153 $address = maybe_unserialize( $address );
2154 $donor->add_address( 'personal', $address );
2155 $donor->add_address( 'billing[]', $address );
2156
2157 // @todo: delete _give_user_address from user meta after releases 2.0.
2158 /*delete_user_meta( $user->ID, '_give_user_address' );*/
2159 }
2160 }
2161 } else {
2162 // The Update Ran.
2163 give_set_upgrade_complete( 'v20_upgrades_user_address' );
2164 }
2165
2166 }
2167
2168 /**
2169 * Upgrade logs data.
2170 *
2171 * @since 2.0
2172 * @return void
2173 * @global wpdb $wpdb
2174 */
2175 function give_v20_rename_donor_tables_callback() {
2176 global $wpdb;
2177
2178 /* @var Give_Updates $give_updates */
2179 $give_updates = Give_Updates::get_instance();
2180
2181 $tables = [
2182 "{$wpdb->prefix}give_customers" => "{$wpdb->prefix}give_donors",
2183 "{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2184 ];
2185
2186 // Alter customer table
2187 foreach ( $tables as $old_table => $new_table ) {
2188 if (
2189 $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old_table ) ) &&
2190 ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $new_table ) )
2191 ) {
2192 $wpdb->query( "ALTER TABLE {$old_table} RENAME TO {$new_table}" );
2193
2194 if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2195 $wpdb->query( "ALTER TABLE {$new_table} CHANGE COLUMN customer_id donor_id bigint(20)" );
2196 }
2197 }
2198 }
2199
2200 $give_updates->percentage = 100;
2201
2202 // No more forms found, finish up.
2203 give_set_upgrade_complete( 'v20_rename_donor_tables' );
2204
2205 // Re initiate donor classes.
2206 Give()->donors = new Give_DB_Donors();
2207 Give()->donor_meta = new Give_DB_Donor_Meta();
2208 }
2209
2210
2211 /**
2212 * Create missing meta tables.
2213 *
2214 * @since 2.0.1
2215 * @return void
2216 * @global wpdb $wpdb
2217 */
2218 function give_v201_create_tables() {
2219 global $wpdb;
2220
2221 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_paymentmeta" ) ) ) {
2222 Give()->payment_meta->create_table();
2223 }
2224
2225 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_formmeta" ) ) ) {
2226 Give()->form_meta->create_table();
2227 }
2228 }
2229
2230 /**
2231 * Upgrade payment metadata for new metabox settings.
2232 *
2233 * @since 2.0.1
2234 * @return void
2235 * @global wpdb $wpdb
2236 */
2237 function give_v201_upgrades_payment_metadata_callback() {
2238 global $wpdb, $post;
2239 $give_updates = Give_Updates::get_instance();
2240 give_v201_create_tables();
2241
2242 $payments = $wpdb->get_col(
2243 "
2244 SELECT ID FROM $wpdb->posts
2245 WHERE 1=1
2246 AND (
2247 $wpdb->posts.post_date >= '2018-01-08 00:00:00'
2248 )
2249 AND $wpdb->posts.post_type = 'give_payment'
2250 AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2251 ORDER BY $wpdb->posts.post_date ASC
2252 LIMIT 100
2253 OFFSET " . $give_updates->get_offset( 100 )
2254 );
2255
2256 if ( ! empty( $payments ) ) {
2257 $give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2258
2259 foreach ( $payments as $payment_id ) {
2260 $post = get_post( $payment_id );
2261 setup_postdata( $post );
2262
2263 // Do not add new meta keys if already refactored.
2264 if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, '_give_payment_donor_id' ) ) ) {
2265 continue;
2266 }
2267
2268 // Split _give_payment_meta meta.
2269 // @todo Remove _give_payment_meta after releases 2.0
2270 $payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2271
2272 if ( ! empty( $payment_meta ) ) {
2273 _give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2274 }
2275
2276 $deprecated_meta_keys = [
2277 '_give_payment_customer_id' => '_give_payment_donor_id',
2278 '_give_payment_user_email' => '_give_payment_donor_email',
2279 '_give_payment_user_ip' => '_give_payment_donor_ip',
2280 ];
2281
2282 foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
2283 // Do not add new meta key if already exist.
2284 if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, $new_meta_key ) ) ) {
2285 continue;
2286 }
2287
2288 $wpdb->insert(
2289 $wpdb->postmeta,
2290 [
2291 'post_id' => $post->ID,
2292 'meta_key' => $new_meta_key,
2293 'meta_value' => give_get_meta( $post->ID, $old_meta_key, true ),
2294 ]
2295 );
2296 }
2297
2298 // Bailout
2299 if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
2300 /* @var Give_Donor $donor */
2301 $donor = new Give_Donor( $donor_id );
2302
2303 $address['line1'] = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
2304 $address['line2'] = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
2305 $address['city'] = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
2306 $address['state'] = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
2307 $address['zip'] = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
2308 $address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
2309
2310 // Save address.
2311 $donor->add_address( 'billing[]', $address );
2312 }
2313 }// End while().
2314
2315 wp_reset_postdata();
2316 } else {
2317 // @todo Delete user id meta after releases 2.0
2318 // $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2319 // No more forms found, finish up.
2320 give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2321 }
2322 }
2323
2324 /**
2325 * Move payment and form metadata to new table
2326 *
2327 * @since 2.0.1
2328 * @return void
2329 */
2330 function give_v201_move_metadata_into_new_table_callback() {
2331 global $wpdb, $post;
2332 $give_updates = Give_Updates::get_instance();
2333 give_v201_create_tables();
2334
2335 $payments = $wpdb->get_col(
2336 "
2337 SELECT ID FROM $wpdb->posts
2338 WHERE 1=1
2339 AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2340 AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2341 ORDER BY $wpdb->posts.post_date ASC
2342 LIMIT 100
2343 OFFSET " . $give_updates->get_offset( 100 )
2344 );
2345
2346 if ( ! empty( $payments ) ) {
2347 $give_updates->set_percentage(
2348 give_get_total_post_type_count(
2349 [
2350 'give_forms',
2351 'give_payment',
2352 ]
2353 ),
2354 $give_updates->step * 100
2355 );
2356
2357 foreach ( $payments as $payment_id ) {
2358 $post = get_post( $payment_id );
2359 setup_postdata( $post );
2360
2361 $meta_data = $wpdb->get_results(
2362 $wpdb->prepare(
2363 "SELECT * FROM $wpdb->postmeta where post_id=%d",
2364 get_the_ID()
2365 ),
2366 ARRAY_A
2367 );
2368
2369 if ( ! empty( $meta_data ) ) {
2370 foreach ( $meta_data as $index => $data ) {
2371 // Check for duplicate meta values.
2372 if ( $result = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . ' WHERE meta_id=%d', $data['meta_id'] ), ARRAY_A ) ) {
2373 continue;
2374 }
2375
2376 switch ( $post->post_type ) {
2377 case 'give_forms':
2378 $data['form_id'] = $data['post_id'];
2379 unset( $data['post_id'] );
2380
2381 Give()->form_meta->insert( $data );
2382 // @todo: delete form meta from post meta table after releases 2.0.
2383 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2384
2385 break;
2386
2387 case 'give_payment':
2388 $data['payment_id'] = $data['post_id'];
2389 unset( $data['post_id'] );
2390
2391 Give()->payment_meta->insert( $data );
2392
2393 // @todo: delete donation meta from post meta table after releases 2.0.
2394 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2395
2396 break;
2397 }
2398 }
2399 }
2400 }// End while().
2401
2402 wp_reset_postdata();
2403 } else {
2404 // No more forms found, finish up.
2405 give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2406 }
2407
2408 }
2409
2410
2411 /**
2412 * Add missing donor.
2413 *
2414 * @since 2.0.1
2415 * @return void
2416 */
2417 function give_v201_add_missing_donors_callback() {
2418 global $wpdb;
2419 give_v201_create_tables();
2420
2421 $give_updates = Give_Updates::get_instance();
2422
2423 // Bailout.
2424 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_customers" ) ) ) {
2425 Give_Updates::get_instance()->percentage = 100;
2426 give_set_upgrade_complete( 'v201_add_missing_donors' );
2427 }
2428
2429 $total_customers = $wpdb->get_var( "SELECT COUNT(id) FROM {$wpdb->prefix}give_customers " );
2430 $customers = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_customers LIMIT 20 OFFSET " . $give_updates->get_offset( 20 ) ), 'id' );
2431 $donors = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_donors" ), 'id' );
2432
2433 if ( ! empty( $customers ) ) {
2434 $give_updates->set_percentage( $total_customers, ( $give_updates->step * 20 ) );
2435
2436 $missing_donors = array_diff( $customers, $donors );
2437 $donor_data = [];
2438
2439 if ( $missing_donors ) {
2440 foreach ( $missing_donors as $donor_id ) {
2441 $donor_data[] = [
2442 'info' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customers WHERE id=%d", $donor_id ) ),
2443 'meta' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customermeta WHERE customer_id=%d", $donor_id ) ),
2444
2445 ];
2446 }
2447 }
2448
2449 if ( ! empty( $donor_data ) ) {
2450 $donor_table_name = Give()->donors->table_name;
2451 $donor_meta_table_name = Give()->donor_meta->table_name;
2452
2453 Give()->donors->table_name = "{$wpdb->prefix}give_donors";
2454 Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2455
2456 foreach ( $donor_data as $donor ) {
2457 $donor['info'][0] = (array) $donor['info'][0];
2458
2459 // Prevent duplicate meta id issue.
2460 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donors WHERE id=%d", $donor['info'][0]['id'] ) ) ) {
2461 continue;
2462 }
2463
2464 $donor_id = Give()->donors->add( $donor['info'][0] );
2465
2466 if ( ! empty( $donor['meta'] ) ) {
2467 foreach ( $donor['meta'] as $donor_meta ) {
2468 $donor_meta = (array) $donor_meta;
2469
2470 // Prevent duplicate meta id issue.
2471 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donormeta WHERE meta_id=%d", $donor_meta['meta_id'] ) ) ) {
2472 unset( $donor_meta['meta_id'] );
2473 }
2474
2475 $donor_meta['donor_id'] = $donor_meta['customer_id'];
2476 unset( $donor_meta['customer_id'] );
2477
2478 Give()->donor_meta->insert( $donor_meta );
2479 }
2480 }
2481
2482 /**
2483 * Fix donor name and address
2484 */
2485 $address = $wpdb->get_var(
2486 $wpdb->prepare(
2487 "
2488 SELECT meta_value FROM {$wpdb->usermeta}
2489 WHERE user_id=%s
2490 AND meta_key=%s
2491 ",
2492 $donor['info'][0]['user_id'],
2493 '_give_user_address'
2494 )
2495 );
2496
2497 $donor = new Give_Donor( $donor_id );
2498
2499 if ( ! empty( $address ) ) {
2500 $address = maybe_unserialize( $address );
2501 $donor->add_address( 'personal', $address );
2502 $donor->add_address( 'billing[]', $address );
2503 }
2504
2505 $donor_name = explode( ' ', $donor->name, 2 );
2506 $donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2507 $donor_last_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2508
2509 // If first name meta of donor is not created, then create it.
2510 if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
2511 Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2512 }
2513
2514 // If last name meta of donor is not created, then create it.
2515 if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
2516 Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2517 }
2518
2519 // If Donor is connected with WP User then update user meta.
2520 if ( $donor->user_id ) {
2521 if ( isset( $donor_name[0] ) ) {
2522 update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
2523 }
2524 if ( isset( $donor_name[1] ) ) {
2525 update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
2526 }
2527 }
2528 }
2529
2530 Give()->donors->table_name = $donor_table_name;
2531 Give()->donor_meta->table_name = $donor_meta_table_name;
2532 }
2533 } else {
2534 give_set_upgrade_complete( 'v201_add_missing_donors' );
2535 }
2536 }
2537
2538
2539 /**
2540 * Version 2.0.3 automatic updates
2541 *
2542 * @since 2.0.3
2543 */
2544 function give_v203_upgrades() {
2545 global $wpdb;
2546
2547 // Do not auto load option.
2548 $wpdb->update( $wpdb->options, [ 'autoload' => 'no' ], [ 'option_name' => 'give_completed_upgrades' ] );
2549
2550 // Remove from cache.
2551 $all_options = wp_load_alloptions();
2552
2553 if ( isset( $all_options['give_completed_upgrades'] ) ) {
2554 unset( $all_options['give_completed_upgrades'] );
2555 wp_cache_set( 'alloptions', $all_options, 'options' );
2556 }
2557
2558 }
2559
2560
2561 /**
2562 * Version 2.2.0 automatic updates
2563 *
2564 * @since 2.2.0
2565 */
2566 function give_v220_upgrades() {
2567 global $wpdb;
2568
2569 /**
2570 * Update 1
2571 *
2572 * Delete wp session data
2573 */
2574 give_v220_delete_wp_session_data();
2575
2576 /**
2577 * Update 2
2578 *
2579 * Rename payment table
2580 */
2581 give_v220_rename_donation_meta_type_callback();
2582
2583 /**
2584 * Update 2
2585 *
2586 * Set autoload to no to reduce result weight from WordPress query
2587 */
2588
2589 $options = [
2590 'give_settings',
2591 'give_version',
2592 'give_version_upgraded_from',
2593 'give_default_api_version',
2594 'give_site_address_before_migrate',
2595 '_give_table_check',
2596 'give_recently_activated_addons',
2597 'give_is_addon_activated',
2598 'give_last_paypal_ipn_received',
2599 'give_use_php_sessions',
2600 'give_subscriptions',
2601 '_give_subscriptions_edit_last',
2602 ];
2603
2604 // Add all table version option name
2605 // Add banner option *_active_by_user
2606 $option_like = $wpdb->get_col(
2607 "
2608 SELECT option_name
2609 FROM $wpdb->options
2610 WHERE option_name like '%give%'
2611 AND (
2612 option_name like '%_db_version%'
2613 OR option_name like '%_active_by_user%'
2614 OR option_name like '%_license_active%'
2615 )
2616 "
2617 );
2618
2619 if ( ! empty( $option_like ) ) {
2620 $options = array_merge( $options, $option_like );
2621 }
2622
2623 $options_str = '\'' . implode( "','", $options ) . '\'';
2624
2625 $wpdb->query(
2626 "
2627 UPDATE $wpdb->options
2628 SET autoload = 'no'
2629 WHERE option_name IN ( {$options_str} )
2630 "
2631 );
2632 }
2633
2634 /**
2635 * Version 2.2.1 automatic updates
2636 *
2637 * @since 2.2.1
2638 */
2639 function give_v221_upgrades() {
2640 global $wpdb;
2641
2642 /**
2643 * Update 1
2644 *
2645 * Change column length
2646 */
2647 $wpdb->query( "ALTER TABLE $wpdb->donors MODIFY email varchar(255) NOT NULL" );
2648 }
2649
2650 /**
2651 * Version 2.3.0 automatic updates
2652 *
2653 * @since 2.3.0
2654 */
2655 function give_v230_upgrades() {
2656
2657 $options_key = [
2658 'give_temp_delete_form_ids', // delete import donor
2659 'give_temp_delete_donation_ids', // delete import donor
2660 'give_temp_delete_step', // delete import donor
2661 'give_temp_delete_donor_ids', // delete import donor
2662 'give_temp_delete_step_on', // delete import donor
2663 'give_temp_delete_donation_ids', // delete test donor
2664 'give_temp_delete_donor_ids', // delete test donor
2665 'give_temp_delete_step', // delete test donor
2666 'give_temp_delete_step_on', // delete test donor
2667 'give_temp_delete_test_ids', // delete test donations
2668 'give_temp_all_payments_data', // delete all stats
2669 'give_recount_all_total', // delete all stats
2670 'give_temp_recount_all_stats', // delete all stats
2671 'give_temp_payment_items', // delete all stats
2672 'give_temp_form_ids', // delete all stats
2673 'give_temp_processed_payments', // delete all stats
2674 'give_temp_recount_form_stats', // delete form stats
2675 'give_temp_recount_earnings', // recount income
2676 'give_recount_earnings_total', // recount income
2677 'give_temp_reset_ids', // reset stats
2678 ];
2679
2680 $options_key = '\'' . implode( "','", $options_key ) . '\'';
2681
2682 global $wpdb;
2683
2684 /**
2685 * Update 1
2686 *
2687 * delete unwanted key from option table
2688 */
2689 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ( {$options_key} )" );
2690 }
2691
2692 /**
2693 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2694 *
2695 * @since 2.1
2696 */
2697 function give_v210_verify_form_status_upgrades_callback() {
2698
2699 $give_updates = Give_Updates::get_instance();
2700
2701 // form query.
2702 $donation_forms = new WP_Query(
2703 [
2704 'paged' => $give_updates->step,
2705 'status' => 'any',
2706 'order' => 'ASC',
2707 'post_type' => 'give_forms',
2708 'posts_per_page' => 20,
2709 ]
2710 );
2711
2712 if ( $donation_forms->have_posts() ) {
2713 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2714
2715 while ( $donation_forms->have_posts() ) {
2716 $donation_forms->the_post();
2717 $form_id = get_the_ID();
2718
2719 $form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2720 if ( empty( $form_closed_status ) ) {
2721 give_set_form_closed_status( $form_id );
2722 }
2723 }
2724
2725 /* Restore original Post Data */
2726 wp_reset_postdata();
2727
2728 } else {
2729
2730 // The Update Ran.
2731 give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2732 }
2733 }
2734
2735 /**
2736 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2737 *
2738 * @since 2.1
2739 */
2740 function give_v213_delete_donation_meta_callback() {
2741 global $wpdb;
2742 $give_updates = Give_Updates::get_instance();
2743 $donation_meta_table = Give()->payment_meta->table_name;
2744
2745 $donations = $wpdb->get_col(
2746 "
2747 SELECT DISTINCT payment_id
2748 FROM {$donation_meta_table}
2749 LIMIT 20
2750 OFFSET {$give_updates->get_offset( 20 )}
2751 "
2752 );
2753
2754 if ( ! empty( $donations ) ) {
2755 foreach ( $donations as $donation ) {
2756 $donation_obj = get_post( $donation );
2757
2758 if ( ! $donation_obj instanceof WP_Post ) {
2759 Give()->payment_meta->delete_all_meta( $donation );
2760 }
2761 }
2762 } else {
2763
2764 // The Update Ran.
2765 give_set_upgrade_complete( 'v213_delete_donation_meta' );
2766 }
2767 }
2768
2769 /**
2770 * Rename donation meta type
2771 *
2772 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2773 *
2774 * @since 2.2.0
2775 */
2776 function give_v220_rename_donation_meta_type_callback() {
2777 global $wpdb;
2778
2779 // Check upgrade before running.
2780 if (
2781 give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
2782 || ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_paymentmeta" ) )
2783 ) {
2784 // Complete update if skip somehow
2785 give_set_upgrade_complete( 'v220_rename_donation_meta_type' );
2786
2787 return;
2788 }
2789
2790 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta CHANGE COLUMN payment_id donation_id bigint(20)" );
2791 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta RENAME TO {$wpdb->prefix}give_donationmeta" );
2792
2793 give_set_upgrade_complete( 'v220_rename_donation_meta_type' );
2794 }
2795
2796 /**
2797 * Adds 'view_give_payments' capability to 'give_manager' user role.
2798 *
2799 * @since 2.1.5
2800 */
2801 function give_v215_update_donor_user_roles_callback() {
2802
2803 $role = get_role( 'give_manager' );
2804 $role->add_cap( 'view_give_payments' );
2805
2806 give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2807 }
2808
2809
2810 /**
2811 * Remove all wp session data from the options table, regardless of expiration.
2812 *
2813 * @since 2.2.0
2814 *
2815 * @global wpdb $wpdb
2816 */
2817 function give_v220_delete_wp_session_data() {
2818 global $wpdb;
2819
2820 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" );
2821 }
2822
2823
2824 /**
2825 * Update donor meta
2826 * Set "_give_anonymous_donor" meta key to "0" if not exist
2827 *
2828 * @since 2.2.4
2829 */
2830 function give_v224_update_donor_meta_callback() {
2831 /* @var Give_Updates $give_updates */
2832 $give_updates = Give_Updates::get_instance();
2833
2834 $donor_count = Give()->donors->count(
2835 [
2836 'number' => - 1,
2837 ]
2838 );
2839
2840 $donors = Give()->donors->get_donors(
2841 [
2842 'paged' => $give_updates->step,
2843 'number' => 100,
2844 ]
2845 );
2846
2847 if ( $donors ) {
2848 $give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2849 // Loop through Donors
2850 foreach ( $donors as $donor ) {
2851 $anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
2852
2853 // If first name meta of donor is not created, then create it.
2854 if ( ! in_array( $anonymous_metadata, [ '0', '1' ] ) ) {
2855 Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
2856 }
2857 }
2858 } else {
2859 // The Update Ran.
2860 give_set_upgrade_complete( 'v224_update_donor_meta' );
2861 }
2862 }
2863
2864 /** Update donor meta
2865 * Set "_give_anonymous_donor_forms" meta key if not exist
2866 *
2867 * @since 2.2.4
2868 */
2869 function give_v224_update_donor_meta_forms_id_callback() {
2870 $give_updates = Give_Updates::get_instance();
2871
2872 $donations = new WP_Query(
2873 [
2874 'paged' => $give_updates->step,
2875 'status' => 'any',
2876 'order' => 'ASC',
2877 'post_type' => [ 'give_payment' ],
2878 'posts_per_page' => 20,
2879 ]
2880 );
2881
2882 if ( $donations->have_posts() ) {
2883 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 20 );
2884
2885 while ( $donations->have_posts() ) {
2886 $donations->the_post();
2887
2888 $donation_id = get_the_ID();
2889
2890 $form_id = give_get_payment_form_id( $donation_id );
2891 $donor_id = give_get_payment_donor_id( $donation_id );
2892 $is_donated_as_anonymous = give_is_anonymous_donation( $donation_id );
2893
2894 $is_anonymous_donor = Give()->donor_meta->get_meta( $donor_id, "_give_anonymous_donor_form_{$form_id}", true );
2895 $is_edit_donor_meta = ! in_array( $is_anonymous_donor, [ '0', '1' ] )
2896 ? true
2897 : ( 0 !== absint( $is_anonymous_donor ) );
2898
2899 if ( $is_edit_donor_meta ) {
2900 Give()->donor_meta->update_meta( $donor_id, "_give_anonymous_donor_form_{$form_id}", absint( $is_donated_as_anonymous ) );
2901 }
2902 }
2903
2904 wp_reset_postdata();
2905 } else {
2906 give_set_upgrade_complete( 'v224_update_donor_meta_forms_id' );
2907 }
2908 }
2909
2910 /**
2911 * Add custom comment table
2912 *
2913 * @since 2.4.0
2914 */
2915 function give_v230_add_missing_comment_tables() {
2916 $custom_tables = [
2917 Give()->comment->db,
2918 Give()->comment->db_meta,
2919 ];
2920
2921 /* @var Give_DB $table */
2922 foreach ( $custom_tables as $table ) {
2923 if ( ! $table->installed() ) {
2924 $table->register_table();
2925 }
2926 }
2927 }
2928
2929
2930 /**
2931 * Move donor notes to comment table
2932 *
2933 * @since 2.3.0
2934 */
2935 function give_v230_move_donor_note_callback() {
2936 // Add comment table if missing.
2937 give_v230_add_missing_comment_tables();
2938
2939 /* @var Give_Updates $give_updates */
2940 $give_updates = Give_Updates::get_instance();
2941
2942 $donor_count = Give()->donors->count(
2943 [
2944 'number' => - 1,
2945 ]
2946 );
2947
2948 $donors = Give()->donors->get_donors(
2949 [
2950 'paged' => $give_updates->step,
2951 'number' => 100,
2952 ]
2953 );
2954
2955 if ( $donors ) {
2956 $give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2957 // Loop through Donors
2958 foreach ( $donors as $donor ) {
2959 $notes = trim( Give()->donors->get_column( 'notes', $donor->id ) );
2960
2961 // If first name meta of donor is not created, then create it.
2962 if ( ! empty( $notes ) ) {
2963 $notes = array_values( array_filter( array_map( 'trim', explode( "\n", $notes ) ), 'strlen' ) );
2964
2965 foreach ( $notes as $note ) {
2966 $note = array_map( 'trim', explode( '-', $note ) );
2967 $timestamp = strtotime( $note[0] );
2968
2969 Give()->comment->db->add(
2970 [
2971 'comment_content' => $note[1],
2972 'user_id' => absint( Give()->donors->get_column_by( 'user_id', 'id', $donor->id ) ),
2973 'comment_date' => date( 'Y-m-d H:i:s', $timestamp ),
2974 'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $timestamp ) ),
2975 'comment_parent' => $donor->id,
2976 'comment_type' => 'donor',
2977 ]
2978 );
2979 }
2980 }
2981 }
2982 } else {
2983 // The Update Ran.
2984 give_set_upgrade_complete( 'v230_move_donor_note' );
2985 }
2986 }
2987
2988 /**
2989 * Move donation notes to comment table
2990 *
2991 * @since 2.3.0
2992 */
2993 function give_v230_move_donation_note_callback() {
2994 global $wpdb;
2995
2996 // Add comment table if missing.
2997 give_v230_add_missing_Comment_tables();
2998
2999 /* @var Give_Updates $give_updates */
3000 $give_updates = Give_Updates::get_instance();
3001
3002 $donation_note_count = $wpdb->get_var(
3003 $wpdb->prepare(
3004 "
3005 SELECT count(*)
3006 FROM {$wpdb->comments}
3007 WHERE comment_type=%s
3008 ",
3009 'give_payment_note'
3010 )
3011 );
3012
3013 $query = $wpdb->prepare(
3014 "
3015 SELECT *
3016 FROM {$wpdb->comments}
3017 WHERE comment_type=%s
3018 ORDER BY comment_ID ASC
3019 LIMIT 100
3020 OFFSET %d
3021 ",
3022 'give_payment_note',
3023 $give_updates->get_offset( 100 )
3024 );
3025
3026 $comments = $wpdb->get_results( $query );
3027
3028 if ( $comments ) {
3029 $give_updates->set_percentage( $donation_note_count, $give_updates->step * 100 );
3030
3031 // Loop through Donors
3032 foreach ( $comments as $comment ) {
3033 $donation_id = $comment->comment_post_ID;
3034 $form_id = give_get_payment_form_id( $donation_id );
3035
3036 $comment_id = Give()->comment->db->add(
3037 [
3038 'comment_content' => $comment->comment_content,
3039 'user_id' => $comment->user_id,
3040 'comment_date' => date( 'Y-m-d H:i:s', strtotime( $comment->comment_date ) ),
3041 'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $comment->comment_date_gmt ) ) ),
3042 'comment_parent' => $comment->comment_post_ID,
3043 'comment_type' => is_numeric( get_comment_meta( $comment->comment_ID, '_give_donor_id', true ) )
3044 ? 'donor_donation'
3045 : 'donation',
3046 ]
3047 );
3048
3049 if ( ! $comment_id ) {
3050 continue;
3051 }
3052
3053 // @see https://github.com/impress-org/give/issues/3737#issuecomment-428460802
3054 $restricted_meta_keys = [
3055 'akismet_result',
3056 'akismet_as_submitted',
3057 'akismet_history',
3058 ];
3059
3060 if ( $comment_meta = get_comment_meta( $comment->comment_ID ) ) {
3061 foreach ( $comment_meta as $meta_key => $meta_value ) {
3062 // Skip few comment meta keys.
3063 if ( in_array( $meta_key, $restricted_meta_keys ) ) {
3064 continue;
3065 }
3066
3067 $meta_value = maybe_unserialize( $meta_value );
3068 $meta_value = is_array( $meta_value ) ? current( $meta_value ) : $meta_value;
3069
3070 Give()->comment->db_meta->update_meta( $comment_id, $meta_key, $meta_value );
3071 }
3072 }
3073
3074 Give()->comment->db_meta->update_meta( $comment_id, '_give_form_id', $form_id );
3075
3076 // Delete comment.
3077 update_comment_meta( $comment->comment_ID, '_give_comment_moved', 1 );
3078 }
3079 } else {
3080 $comment_ids = $wpdb->get_col(
3081 $wpdb->prepare(
3082 "
3083 SELECT DISTINCT comment_id
3084 FROM {$wpdb->commentmeta}
3085 WHERE meta_key=%s
3086 AND meta_value=%d
3087 ",
3088 '_give_comment_moved',
3089 1
3090 )
3091 );
3092
3093 if ( ! empty( $comment_ids ) ) {
3094 $comment_ids = "'" . implode( "','", $comment_ids ) . "'";
3095
3096 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_ID IN ({$comment_ids})" );
3097 $wpdb->query( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ({$comment_ids})" );
3098 }
3099
3100 // The Update Ran.
3101 give_set_upgrade_complete( 'v230_move_donation_note' );
3102 }
3103 }
3104
3105 /**
3106 * Delete donor wall related donor meta data
3107 *
3108 * @since 2.3.0
3109 */
3110 function give_v230_delete_dw_related_donor_data_callback() {
3111 global $wpdb;
3112
3113 $give_updates = Give_Updates::get_instance();
3114
3115 $wpdb->query( "DELETE FROM {$wpdb->donormeta} WHERE meta_key LIKE '%_give_anonymous_donor%' OR meta_key='_give_has_comment';" );
3116
3117 $give_updates->percentage = 100;
3118
3119 // The Update Ran.
3120 give_set_upgrade_complete( 'v230_delete_donor_wall_related_donor_data' );
3121 }
3122
3123 /**
3124 * Delete donor wall related comment meta data
3125 *
3126 * @since 2.3.0
3127 */
3128 function give_v230_delete_dw_related_comment_data_callback() {
3129 global $wpdb;
3130
3131 $give_updates = Give_Updates::get_instance();
3132
3133 $wpdb->query( "DELETE FROM {$wpdb->give_commentmeta} WHERE meta_key='_give_anonymous_donation';" );
3134
3135 $give_updates->percentage = 100;
3136
3137 // The Update Ran.
3138 give_set_upgrade_complete( 'v230_delete_donor_wall_related_comment_data' );
3139 }
3140
3141 /**
3142 * Update donation form goal progress data.
3143 *
3144 * @since 2.4.0
3145 */
3146 function give_v240_update_form_goal_progress_callback() {
3147
3148 /* @var Give_Updates $give_updates */
3149 $give_updates = Give_Updates::get_instance();
3150
3151 // form query
3152 $forms = new WP_Query(
3153 [
3154 'paged' => $give_updates->step,
3155 'status' => 'any',
3156 'order' => 'ASC',
3157 'post_type' => 'give_forms',
3158 'posts_per_page' => 20,
3159 ]
3160 );
3161
3162 if ( $forms->have_posts() ) {
3163 while ( $forms->have_posts() ) {
3164 $forms->the_post();
3165
3166 // Update the goal progress for donation form.
3167 give_update_goal_progress( get_the_ID() );
3168
3169 }// End while().
3170
3171 wp_reset_postdata();
3172
3173 } else {
3174
3175 // No more forms found, finish up.
3176 give_set_upgrade_complete( 'v240_update_form_goal_progress' );
3177
3178 }
3179 }
3180
3181 /**
3182 * DB upgrades for Give 2.5.0
3183 *
3184 * @since 2.5.0
3185 */
3186 function give_v250_upgrades() {
3187 global $wpdb;
3188
3189 $old_license = [];
3190 $new_license = [];
3191 $give_licenses = get_option( 'give_licenses', [] );
3192 $give_options = give_get_settings();
3193
3194 // Get add-ons license key.
3195 $addons = [];
3196 foreach ( $give_options as $key => $value ) {
3197 if ( false !== strpos( $key, '_license_key' ) ) {
3198 $addons[ $key ] = $value;
3199 }
3200 }
3201
3202 // Bailout: We do not have any add-on license data to upgrade.
3203 if ( empty( $addons ) ) {
3204 return false;
3205 }
3206
3207 foreach ( $addons as $key => $license_key ) {
3208
3209 // Get addon shortname.
3210 $addon_shortname = str_replace( '_license_key', '', $key );
3211
3212 // Addon license option name.
3213 $addon_shortname = "{$addon_shortname}_license_active";
3214 $addon_license_data = get_option( "{$addon_shortname}_license_active", [] );
3215
3216 if (
3217 ! $license_key
3218 || array_key_exists( $license_key, $give_licenses )
3219 ) {
3220 continue;
3221 }
3222
3223 $old_license[ $license_key ] = $addon_license_data;
3224 }
3225
3226 // Bailout.
3227 if ( empty( $old_license ) ) {
3228 return false;
3229 }
3230
3231 /* @var stdClass $data */
3232 foreach ( $old_license as $key => $data ) {
3233 $tmp = Give_License::request_license_api(
3234 [
3235 'edd_action' => 'check_license',
3236 'license' => $key,
3237 ],
3238 true
3239 );
3240
3241 if ( is_wp_error( $tmp ) || ! $tmp['success'] ) {
3242 continue;
3243 }
3244
3245 $new_license[ $key ] = $tmp;
3246 }
3247
3248 // Bailout.
3249 if ( empty( $new_license ) ) {
3250 return false;
3251 }
3252
3253 $give_licenses = array_merge( $give_licenses, $new_license );
3254
3255 update_option( 'give_licenses', $give_licenses );
3256
3257 /**
3258 * Delete data.
3259 */
3260
3261 // 1. license keys
3262 foreach ( get_option( 'give_settings' ) as $index => $setting ) {
3263 if ( false !== strpos( $index, '_license_key' ) ) {
3264 give_delete_option( $index );
3265 }
3266 }
3267
3268 // 2. license api data
3269 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name like '%_license_active%' AND option_name like 'give_%'" );
3270
3271 // 3. subscriptions data
3272 delete_option( '_give_subscriptions_edit_last' );
3273 delete_option( 'give_subscriptions' );
3274
3275 // 4. misc
3276 delete_option( 'give_is_addon_activated' );
3277
3278 give_refresh_licenses();
3279 }
3280
3281 /**
3282 * DB upgrades for Give 2.5.8
3283 *
3284 * @since 2.5.8
3285 */
3286 function give_v258_upgrades() {
3287
3288 $is_checkout_enabled = give_is_setting_enabled( give_get_option( 'stripe_checkout_enabled', 'disabled' ) );
3289
3290 // Bailout, if stripe checkout is not active as a gateway.
3291 if ( ! $is_checkout_enabled ) {
3292 return;
3293 }
3294
3295 $enabled_gateways = give_get_option( 'gateways', [] );
3296
3297 // Bailout, if Stripe Checkout is already enabled.
3298 if ( ! empty( $enabled_gateways['stripe_checkout'] ) ) {
3299 return;
3300 }
3301
3302 $gateways_label = give_get_option( 'gateways_label', [] );
3303 $default_gateway = give_get_option( 'default_gateway' );
3304
3305 // Set Stripe Checkout as active gateway.
3306 $enabled_gateways['stripe_checkout'] = 1;
3307
3308 // Unset Stripe - Credit Card as an active gateway.
3309 unset( $enabled_gateways['stripe'] );
3310
3311 // Set Stripe Checkout same as Stripe as they have enabled Stripe Checkout under Stripe using same label.
3312 $gateways_label['stripe_checkout'] = $gateways_label['stripe'];
3313 give_update_option( 'gateways_label', $gateways_label );
3314
3315 // If default gateway selected is `stripe` then set `stripe checkout` as default.
3316 if ( 'stripe' === $default_gateway ) {
3317 give_update_option( 'default_gateway', 'stripe_checkout' );
3318 }
3319
3320 // Update the enabled gateways in database.
3321 give_update_option( 'gateways', $enabled_gateways );
3322
3323 // Delete the old legacy settings.
3324 give_delete_option( 'stripe_checkout_enabled' );
3325 }
3326
3327
3328 /**
3329 * DB upgrades for Give 2.5.11
3330 *
3331 * @since 2.5.11
3332 */
3333 function give_v2511_upgrades() {
3334 global $wp_roles, $wpdb;
3335 $all_roles = get_editable_roles();
3336
3337 // Run code only if not a fresh install.
3338 if ( Give_Cache_Setting::get_option( 'give_version' ) ) {
3339 // Remove unused notes column from donor table.
3340 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_donors DROP COLUMN notes;" );
3341 }
3342
3343 foreach ( $all_roles as $role => $data ) {
3344 $wp_roles->remove_cap( $role, 'delete_give_form' );
3345 $wp_roles->remove_cap( $role, 'delete_give_payment' );
3346 $wp_roles->remove_cap( $role, 'edit_give_form' );
3347 $wp_roles->remove_cap( $role, 'edit_give_payment' );
3348 $wp_roles->remove_cap( $role, 'read_give_form' );
3349 $wp_roles->remove_cap( $role, 'read_give_payment' );
3350 }
3351 }
3352
3353 /**
3354 * Upgrade for version 2.6.3
3355 *
3356 * @since 2.6.3
3357 */
3358 function give_v263_upgrades() {
3359 $licenses = get_option( 'give_licenses', [] );
3360
3361 if ( $licenses ) {
3362 foreach ( $licenses as $license ) {
3363 if ( ! empty( $license['is_all_access_pass'] ) ) {
3364 // Remove single license which is part of all access pass.
3365 // @see https://github.com/impress-org/givewp/issues/4669
3366 $addonSlugs = Give_License::getAddonSlugsFromAllAccessPassLicense( $license );
3367 foreach ( $licenses as $license_key => $data ) {
3368 // Skip bundle plan license key.
3369 if ( ! empty( $data['is_all_access_pass'] ) ) {
3370 continue;
3371 }
3372
3373 if ( in_array( $data['plugin_slug'], $addonSlugs, true ) ) {
3374 unset( $licenses[ $license_key ] );
3375 }
3376 }
3377 }
3378 }
3379
3380 update_option( 'give_licenses', $licenses );
3381 }
3382 }
3383
3384
3385 /**
3386 * Upgrade routine to call for backward compatibility to manage default Stripe account.
3387 *
3388 * @since 2.7.0
3389 * @return void
3390 * @global wpdb $wpdb
3391 */
3392 function give_v270_upgrades() {
3393 global $wpdb;
3394
3395 $settingKey = '_give_stripe_get_all_accounts';
3396 $giveSettings = give_get_settings();
3397 $isStripeAccountMigrated = array_key_exists( $settingKey, $giveSettings );
3398 $stripeAccounts = $isStripeAccountMigrated ? $giveSettings[ $settingKey ] : [];
3399
3400 // Process, only when there is no Stripe accounts stored.
3401 if ( ! $isStripeAccountMigrated ) {
3402 $liveSecretKey = give_get_option( 'live_secret_key' );
3403 $testSecretKey = give_get_option( 'test_secret_key' );
3404 $livePublishableKey = give_get_option( 'live_publishable_key' );
3405 $testPublishableKey = give_get_option( 'test_publishable_key' );
3406 $isStripeConfigurationExist = $liveSecretKey || $testSecretKey || $livePublishableKey || $testPublishableKey;
3407
3408 if ( $isStripeConfigurationExist ) {
3409 // Manual API Keys are enabled.
3410 if ( ! give_get_option( 'give_stripe_user_id' ) ) {
3411 $uniqueSlug = 'account_1';
3412 $stripeAccounts[ $uniqueSlug ] = [
3413 'type' => 'manual',
3414 'account_name' => give_stripe_convert_slug_to_title( $uniqueSlug ),
3415 'account_slug' => $uniqueSlug,
3416 'account_email' => '',
3417 'account_country' => '',
3418 'account_id' => '', // This parameter will be empty for manual API Keys Stripe account.
3419 'live_secret_key' => $liveSecretKey,
3420 'test_secret_key' => $testSecretKey,
3421 'live_publishable_key' => $livePublishableKey,
3422 'test_publishable_key' => $testPublishableKey,
3423 ];
3424
3425 // Set first Stripe account as default.
3426 give_update_option( '_give_stripe_default_account', $uniqueSlug );
3427 } else {
3428
3429 $secret_key = give_get_option( 'live_secret_key' );
3430 if ( give_is_test_mode() ) {
3431 $secret_key = give_get_option( 'test_secret_key' );
3432 }
3433
3434 \Stripe\Stripe::setApiKey( $secret_key );
3435
3436 $accounts_count = is_countable( $stripeAccounts ) ? count( $stripeAccounts ) + 1 : 1;
3437 $all_account_slugs = array_keys( $stripeAccounts );
3438 $accountSlug = give_stripe_get_unique_account_slug( $all_account_slugs, $accounts_count );
3439 $accountName = give_stripe_convert_slug_to_title( $accountSlug );
3440 $accountEmail = '';
3441 $accountCountry = '';
3442 $stripeAccountId = give_get_option( 'give_stripe_user_id' );
3443 $accountDetails = give_stripe_get_account_details( $stripeAccountId );
3444
3445 // Setup Account Details for Connected Stripe Accounts.
3446 if ( ! empty( $accountDetails->id ) && 'account' === $accountDetails->object ) {
3447 $accountName = ! empty( $accountDetails->business_profile->name ) ?
3448 $accountDetails->business_profile->name :
3449 $accountDetails->settings->dashboard->display_name;
3450 $accountSlug = $accountDetails->id;
3451 $accountEmail = $accountDetails->email;
3452 $accountCountry = $accountDetails->country;
3453 }
3454
3455 $stripeAccounts[ $accountSlug ] = [
3456 'type' => 'connect',
3457 'account_name' => $accountName,
3458 'account_slug' => $accountSlug,
3459 'account_email' => $accountEmail,
3460 'account_country' => $accountCountry,
3461 'account_id' => $stripeAccountId,
3462 'live_secret_key' => $liveSecretKey,
3463 'test_secret_key' => $testSecretKey,
3464 'live_publishable_key' => $livePublishableKey,
3465 'test_publishable_key' => $testPublishableKey,
3466 ];
3467
3468 // Set first Stripe account as default.
3469 give_update_option( '_give_stripe_default_account', $accountSlug );
3470 }
3471
3472 give_update_option( $settingKey, $stripeAccounts );
3473
3474 // Remove legacy settings.
3475 give_delete_option( 'live_secret_key' );
3476 give_delete_option( 'test_secret_key' );
3477 give_delete_option( 'live_publishable_key' );
3478 give_delete_option( 'test_publishable_key' );
3479 give_delete_option( 'give_stripe_connected' );
3480 give_delete_option( 'give_stripe_user_id' );
3481 }
3482 }
3483
3484 // Do not need to go beyond this if you are on fresh install and on fresh install donationmeta property is not defined for $wpdb.
3485 // Below code is to check if site have donations which processed with Stripe payment method
3486 // if not then we will auto complete stripe background update.
3487 if ( ! property_exists( $wpdb, 'donationmeta' ) ) {
3488 return;
3489 }
3490
3491 $canStoreStripeInformationInDonation = (bool) $wpdb->get_var(
3492 $wpdb->prepare(
3493 "
3494 SELECT COUNT(donation_id)
3495 FROM $wpdb->donationmeta
3496 WHERE meta_key=%s
3497 AND meta_value LIKE %s",
3498 '_give_payment_gateway',
3499 '%stripe%'
3500 )
3501 );
3502
3503 if ( ! $canStoreStripeInformationInDonation || ! $stripeAccounts ) {
3504 give_set_upgrade_complete( 'v270_store_stripe_account_for_donation' );
3505 }
3506 }
3507
3508 /**
3509 * This manual upgrade routine is used set the default Stripe account for all the existing donations.
3510 * This process will help us to identify which Stripe account is used to process a specific donation.
3511 *
3512 * @since 2.7.0
3513 *
3514 * @return void
3515 */
3516 function give_v270_store_stripe_account_for_donation_callback() {
3517 /* @var Give_Updates $give_updates */
3518 $give_updates = Give_Updates::get_instance();
3519
3520 $donations = new WP_Query(
3521 [
3522 'paged' => $give_updates->step,
3523 'status' => 'any',
3524 'order' => 'ASC',
3525 'post_type' => [ 'give_payment' ],
3526 'posts_per_page' => 100,
3527 ]
3528 );
3529
3530 if ( $donations->have_posts() ) {
3531 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
3532
3533 while ( $donations->have_posts() ) {
3534 $donations->the_post();
3535 $donationId = get_the_ID();
3536
3537 // Continue, if the donation is not processed with any of the supported payment method of Stripe.
3538 if ( ! Stripe::isDonationPaymentMethod( give_get_payment_gateway( $donationId ) ) ) {
3539 continue;
3540 }
3541
3542 Stripe::addAccountDetail(
3543 $donationId,
3544 give_get_payment_form_id( $donationId )
3545 );
3546 }
3547
3548 wp_reset_postdata();
3549 } else {
3550 // Update Ran Successfully.
3551 give_set_upgrade_complete( 'v270_store_stripe_account_for_donation' );
3552 }
3553 }
3554
3555 /**
3556 * Removes any leftover export files that should've been deleted
3557 *
3558 * @since 2.9.0
3559 */
3560 function give_v290_remove_old_export_files() {
3561 @unlink( WP_CONTENT_DIR . '/uploads/give-payments.csv' );
3562 @unlink( WP_CONTENT_DIR . '/uploads/give-donors.csv' );
3563 }
3564