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