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