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