PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.7.2
GiveWP – Donation Plugin and Fundraising Platform v2.7.2
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 5 years ago upgrade-functions.php 6 years ago
upgrade-functions.php
3819 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', preg_replace( '/[^0-9.].*/', '', 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', preg_replace( '/[^0-9.].*/', '', 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_welcome' => 'welcome',
1047 'disable_forms_singular' => 'forms_singular',
1048 'disable_forms_archives' => 'forms_archives',
1049 'disable_forms_excerpt' => 'forms_excerpt',
1050 'disable_form_featured_img' => 'form_featured_img',
1051 'disable_form_sidebar' => 'form_sidebar',
1052 'disable_admin_notices' => 'admin_notices',
1053 'disable_the_content_filter' => 'the_content_filter',
1054 'enable_floatlabels' => 'floatlabels',
1055 'enable_categories' => 'categories',
1056 'enable_tags' => 'tags',
1057 ];
1058 }
1059
1060
1061 /**
1062 * Upgrade core settings.
1063 *
1064 * @since 1.8.7
1065 * @return void
1066 */
1067 function give_v187_upgrades() {
1068 global $wpdb;
1069
1070 /**
1071 * Upgrade 1: Remove stat and cache transients.
1072 */
1073 $cached_options = $wpdb->get_col(
1074 $wpdb->prepare(
1075 "
1076 SELECT *
1077 FROM {$wpdb->options}
1078 WHERE (
1079 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 OR option_name LIKE %s
1092 )
1093 ",
1094 [
1095 '%_transient_give_stats_%',
1096 'give_cache%',
1097 '%_transient_give_add_ons_feed%',
1098 '%_transient__give_ajax_works' .
1099 '%_transient_give_total_api_keys%',
1100 '%_transient_give_i18n_give_promo_hide%',
1101 '%_transient_give_contributors%',
1102 '%_transient_give_estimated_monthly_stats%',
1103 '%_transient_give_earnings_total%',
1104 '%_transient_give_i18n_give_%',
1105 '%_transient__give_installed%',
1106 '%_transient__give_activation_redirect%',
1107 '%_transient__give_hide_license_notices_shortly_%',
1108 '%give_income_total%',
1109 ]
1110 ),
1111 1
1112 );
1113
1114 // User related transients.
1115 $user_apikey_options = $wpdb->get_results(
1116 $wpdb->prepare(
1117 "SELECT user_id, meta_key
1118 FROM $wpdb->usermeta
1119 WHERE meta_value=%s",
1120 'give_user_public_key'
1121 ),
1122 ARRAY_A
1123 );
1124
1125 if ( ! empty( $user_apikey_options ) ) {
1126 foreach ( $user_apikey_options as $user ) {
1127 $cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1128 $cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1129 $cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1130 }
1131 }
1132
1133 if ( ! empty( $cached_options ) ) {
1134 foreach ( $cached_options as $option ) {
1135 switch ( true ) {
1136 case ( false !== strpos( $option, 'transient' ) ):
1137 $option = str_replace( '_transient_', '', $option );
1138 delete_transient( $option );
1139 break;
1140
1141 default:
1142 delete_option( $option );
1143 }
1144 }
1145 }
1146 }
1147
1148 /**
1149 * Update Capabilities for Give_Worker User Role.
1150 *
1151 * This upgrade routine will update access rights for Give_Worker User Role.
1152 *
1153 * @since 1.8.8
1154 */
1155 function give_v188_upgrades() {
1156
1157 global $wp_roles;
1158
1159 // Get the role object.
1160 $give_worker = get_role( 'give_worker' );
1161
1162 // A list of capabilities to add for give workers.
1163 $caps_to_add = [
1164 'edit_posts',
1165 'edit_pages',
1166 ];
1167
1168 foreach ( $caps_to_add as $cap ) {
1169 // Add the capability.
1170 $give_worker->add_cap( $cap );
1171 }
1172
1173 }
1174
1175 /**
1176 * Update Post meta for minimum and maximum amount for multi level donation forms
1177 *
1178 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1179 *
1180 * @since 1.8.9
1181 */
1182 function give_v189_upgrades_levels_post_meta_callback() {
1183 /* @var Give_Updates $give_updates */
1184 $give_updates = Give_Updates::get_instance();
1185
1186 // form query.
1187 $donation_forms = new WP_Query(
1188 [
1189 'paged' => $give_updates->step,
1190 'status' => 'any',
1191 'order' => 'ASC',
1192 'post_type' => 'give_forms',
1193 'posts_per_page' => 20,
1194 ]
1195 );
1196
1197 if ( $donation_forms->have_posts() ) {
1198 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1199
1200 while ( $donation_forms->have_posts() ) {
1201 $donation_forms->the_post();
1202 $form_id = get_the_ID();
1203
1204 // Remove formatting from _give_set_price.
1205 update_post_meta(
1206 $form_id,
1207 '_give_set_price',
1208 give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1209 );
1210
1211 // Remove formatting from _give_custom_amount_minimum.
1212 update_post_meta(
1213 $form_id,
1214 '_give_custom_amount_minimum',
1215 give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1216 );
1217
1218 // Bailout.
1219 if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1220 continue;
1221 }
1222
1223 $donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1224
1225 if ( ! empty( $donation_levels ) ) {
1226
1227 foreach ( $donation_levels as $index => $donation_level ) {
1228 if ( isset( $donation_level['_give_amount'] ) ) {
1229 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1230 }
1231 }
1232
1233 update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1234
1235 $donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1236
1237 $min_amount = min( $donation_levels_amounts );
1238 $max_amount = max( $donation_levels_amounts );
1239
1240 // Set Minimum and Maximum amount for Multi Level Donation Forms
1241 give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1242 give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1243 }
1244 }
1245
1246 /* Restore original Post Data */
1247 wp_reset_postdata();
1248 } else {
1249 // The Update Ran.
1250 give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1251 }
1252
1253 }
1254
1255
1256 /**
1257 * Give version 1.8.9 upgrades
1258 *
1259 * @since 1.8.9
1260 */
1261 function give_v189_upgrades() {
1262 /**
1263 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1264 */
1265 global $wpdb;
1266
1267 // Delete permanent notice blocker.
1268 $wpdb->query(
1269 $wpdb->prepare(
1270 "
1271 DELETE FROM $wpdb->usermeta
1272 WHERE meta_key
1273 LIKE '%%%s%%'
1274 ",
1275 '_give_hide_license_notices_permanently'
1276 )
1277 );
1278
1279 // Delete short notice blocker.
1280 $wpdb->query(
1281 $wpdb->prepare(
1282 "
1283 DELETE FROM $wpdb->options
1284 WHERE option_name
1285 LIKE '%%%s%%'
1286 ",
1287 '__give_hide_license_notices_shortly_'
1288 )
1289 );
1290 }
1291
1292 /**
1293 * 2.0 Upgrades.
1294 *
1295 * @since 2.0
1296 * @return void
1297 */
1298 function give_v20_upgrades() {
1299 // Update cache setting.
1300 give_update_option( 'cache', 'enabled' );
1301
1302 // Upgrade email settings.
1303 give_v20_upgrades_email_setting();
1304 }
1305
1306 /**
1307 * Move old email api settings to new email setting api for following emails:
1308 * 1. new offline donation [This was hard coded]
1309 * 2. offline donation instruction
1310 * 3. new donation
1311 * 4. donation receipt
1312 *
1313 * @since 2.0
1314 */
1315 function give_v20_upgrades_email_setting() {
1316 $all_setting = give_get_settings();
1317
1318 // Bailout on fresh install.
1319 if ( empty( $all_setting ) ) {
1320 return;
1321 }
1322
1323 $settings = [
1324 'offline_donation_subject' => 'offline-donation-instruction_email_subject',
1325 'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1326 'donation_subject' => 'donation-receipt_email_subject',
1327 'donation_receipt' => 'donation-receipt_email_message',
1328 'donation_notification_subject' => 'new-donation_email_subject',
1329 'donation_notification' => 'new-donation_email_message',
1330 'admin_notice_emails' => [
1331 'new-donation_recipient',
1332 'new-offline-donation_recipient',
1333 'new-donor-register_recipient',
1334 ],
1335 'admin_notices' => 'new-donation_notification',
1336 ];
1337
1338 foreach ( $settings as $old_setting => $new_setting ) {
1339 // Do not update already modified
1340 if ( ! is_array( $new_setting ) ) {
1341 if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1342 continue;
1343 }
1344 }
1345
1346 switch ( $old_setting ) {
1347 case 'admin_notices':
1348 $notification_status = give_get_option( $old_setting, 'enabled' );
1349
1350 give_update_option( $new_setting, $notification_status );
1351
1352 // @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1353 // give_delete_option( $old_setting );
1354 break;
1355
1356 // @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1357 case 'admin_notice_emails':
1358 $recipients = give_get_admin_notice_emails();
1359
1360 foreach ( $new_setting as $setting ) {
1361 // bailout if setting already exist.
1362 if ( array_key_exists( $setting, $all_setting ) ) {
1363 continue;
1364 }
1365
1366 give_update_option( $setting, $recipients );
1367 }
1368 break;
1369
1370 default:
1371 give_update_option( $new_setting, give_get_option( $old_setting ) );
1372 give_delete_option( $old_setting );
1373 }
1374 }
1375 }
1376
1377 /**
1378 * Give version 1.8.9 upgrades
1379 *
1380 * @since 1.8.9
1381 */
1382 function give_v1812_upgrades() {
1383 /**
1384 * Validate number format settings.
1385 */
1386 $give_settings = give_get_settings();
1387 $give_setting_updated = false;
1388
1389 if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1390 $give_settings['number_decimals'] = 0;
1391 $give_settings['decimal_separator'] = '';
1392 $give_setting_updated = true;
1393
1394 } elseif ( empty( $give_settings['decimal_separator'] ) ) {
1395 $give_settings['number_decimals'] = 0;
1396 $give_setting_updated = true;
1397
1398 } elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1399 $give_settings['number_decimals'] = 5;
1400 $give_setting_updated = true;
1401 }
1402
1403 if ( $give_setting_updated ) {
1404 update_option( 'give_settings', $give_settings, false );
1405 }
1406 }
1407
1408
1409 /**
1410 * Give version 1.8.12 update
1411 *
1412 * Standardized amount values to six decimal
1413 *
1414 * @see https://github.com/impress-org/give/issues/1849#issuecomment-315128602
1415 *
1416 * @since 1.8.12
1417 */
1418 function give_v1812_update_amount_values_callback() {
1419 /* @var Give_Updates $give_updates */
1420 $give_updates = Give_Updates::get_instance();
1421
1422 // form query.
1423 $donation_forms = new WP_Query(
1424 [
1425 'paged' => $give_updates->step,
1426 'status' => 'any',
1427 'order' => 'ASC',
1428 'post_type' => [ 'give_forms', 'give_payment' ],
1429 'posts_per_page' => 20,
1430 ]
1431 );
1432 if ( $donation_forms->have_posts() ) {
1433 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1434
1435 while ( $donation_forms->have_posts() ) {
1436 $donation_forms->the_post();
1437 global $post;
1438
1439 $meta = get_post_meta( $post->ID );
1440
1441 switch ( $post->post_type ) {
1442 case 'give_forms':
1443 // _give_set_price.
1444 if ( ! empty( $meta['_give_set_price'][0] ) ) {
1445 update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1446 }
1447
1448 // _give_custom_amount_minimum.
1449 if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1450 update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1451 }
1452
1453 // _give_levels_minimum_amount.
1454 if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1455 update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1456 }
1457
1458 // _give_levels_maximum_amount.
1459 if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1460 update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1461 }
1462
1463 // _give_set_goal.
1464 if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1465 update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1466 }
1467
1468 // _give_form_earnings.
1469 if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1470 update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1471 }
1472
1473 // _give_custom_amount_minimum.
1474 if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1475 $donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1476
1477 foreach ( $donation_levels as $index => $level ) {
1478 if ( empty( $level['_give_amount'] ) ) {
1479 continue;
1480 }
1481
1482 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1483 }
1484
1485 $meta['_give_donation_levels'] = $donation_levels;
1486 update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1487 }
1488
1489 break;
1490
1491 case 'give_payment':
1492 // _give_payment_total.
1493 if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1494 update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1495 }
1496
1497 break;
1498 }
1499 }
1500
1501 /* Restore original Post Data */
1502 wp_reset_postdata();
1503 } else {
1504 // The Update Ran.
1505 give_set_upgrade_complete( 'v1812_update_amount_values' );
1506 }
1507 }
1508
1509
1510 /**
1511 * Give version 1.8.12 update
1512 *
1513 * Standardized amount values to six decimal for donor
1514 *
1515 * @see https://github.com/impress-org/give/issues/1849#issuecomment-315128602
1516 *
1517 * @since 1.8.12
1518 */
1519 function give_v1812_update_donor_purchase_value_callback() {
1520 /* @var Give_Updates $give_updates */
1521 $give_updates = Give_Updates::get_instance();
1522
1523 // form query.
1524 $donors = Give()->donors->get_donors(
1525 [
1526 'number' => 20,
1527 'offset' => $give_updates->get_offset( 20 ),
1528 ]
1529 );
1530
1531 if ( ! empty( $donors ) ) {
1532 $give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1533
1534 /* @var Object $donor */
1535 foreach ( $donors as $donor ) {
1536 Give()->donors->update( $donor->id, [ 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ] );
1537 }
1538 } else {
1539 // The Update Ran.
1540 give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1541 }
1542 }
1543
1544 /**
1545 * Upgrade routine for updating user roles for existing donors.
1546 *
1547 * @since 1.8.13
1548 */
1549 function give_v1813_update_donor_user_roles_callback() {
1550 /* @var Give_Updates $give_updates */
1551 $give_updates = Give_Updates::get_instance();
1552
1553 // Fetch all the existing donors.
1554 $donors = Give()->donors->get_donors(
1555 [
1556 'number' => 20,
1557 'offset' => $give_updates->get_offset( 20 ),
1558 ]
1559 );
1560
1561 if ( ! empty( $donors ) ) {
1562 $give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1563
1564 /* @var Object $donor */
1565 foreach ( $donors as $donor ) {
1566 $user_id = $donor->user_id;
1567
1568 // Proceed, if donor is attached with user.
1569 if ( $user_id ) {
1570 $user = get_userdata( $user_id );
1571
1572 // Update user role, if user has subscriber role.
1573 if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1574 wp_update_user(
1575 [
1576 'ID' => $user_id,
1577 'role' => 'give_donor',
1578 ]
1579 );
1580 }
1581 }
1582 }
1583 } else {
1584 // The Update Ran.
1585 give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1586 }
1587 }
1588
1589
1590 /**
1591 * Version 1.8.13 automatic updates
1592 *
1593 * @since 1.8.13
1594 */
1595 function give_v1813_upgrades() {
1596 // Update admin setting.
1597 give_update_option( 'donor_default_user_role', 'give_donor' );
1598
1599 // Update Give roles.
1600 $roles = new Give_Roles();
1601 $roles->add_roles();
1602 $roles->add_caps();
1603 }
1604
1605 /**
1606 * Correct currency code for "Iranian Currency" for all of the payments.
1607 *
1608 * @since 1.8.17
1609 */
1610 function give_v1817_update_donation_iranian_currency_code() {
1611 /* @var Give_Updates $give_updates */
1612 $give_updates = Give_Updates::get_instance();
1613
1614 // form query.
1615 $payments = new WP_Query(
1616 [
1617 'paged' => $give_updates->step,
1618 'status' => 'any',
1619 'order' => 'ASC',
1620 'post_type' => [ 'give_payment' ],
1621 'posts_per_page' => 100,
1622 ]
1623 );
1624
1625 if ( $payments->have_posts() ) {
1626 $give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1627
1628 while ( $payments->have_posts() ) {
1629 $payments->the_post();
1630
1631 $payment_meta = give_get_payment_meta( get_the_ID() );
1632
1633 if ( 'RIAL' === $payment_meta['currency'] ) {
1634 $payment_meta['currency'] = 'IRR';
1635 give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1636 }
1637 }
1638 } else {
1639 // The Update Ran.
1640 give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1641 }
1642 }
1643
1644 /**
1645 * Correct currency code for "Iranian Currency" in Give setting.
1646 * Version 1.8.17 automatic updates
1647 *
1648 * @since 1.8.17
1649 */
1650 function give_v1817_upgrades() {
1651 $give_settings = give_get_settings();
1652
1653 if ( 'RIAL' === $give_settings['currency'] ) {
1654 $give_settings['currency'] = 'IRR';
1655 update_option( 'give_settings', $give_settings, false );
1656 }
1657 }
1658
1659 /**
1660 * Process Clean up of User Roles for more flexibility.
1661 *
1662 * @since 1.8.17
1663 */
1664 function give_v1817_process_cleanup_user_roles() {
1665
1666 global $wp_roles;
1667
1668 if ( ! ( $wp_roles instanceof WP_Roles ) ) {
1669 return;
1670 }
1671
1672 // Add Capabilities to user roles as required.
1673 $add_caps = [
1674 'administrator' => [
1675 'view_give_payments',
1676 ],
1677 ];
1678
1679 // Remove Capabilities to user roles as required.
1680 $remove_caps = [
1681 'give_manager' => [
1682 'edit_others_pages',
1683 'edit_others_posts',
1684 'delete_others_pages',
1685 'delete_others_posts',
1686 'manage_categories',
1687 'import',
1688 'export',
1689 ],
1690 ];
1691
1692 foreach ( $add_caps as $role => $caps ) {
1693 foreach ( $caps as $cap ) {
1694 $wp_roles->add_cap( $role, $cap );
1695 }
1696 }
1697
1698 foreach ( $remove_caps as $role => $caps ) {
1699 foreach ( $caps as $cap ) {
1700 $wp_roles->remove_cap( $role, $cap );
1701 }
1702 }
1703
1704 }
1705
1706 /**
1707 * Upgrade Routine - Clean up of User Roles for more flexibility.
1708 *
1709 * @since 1.8.17
1710 */
1711 function give_v1817_cleanup_user_roles() {
1712 /* @var Give_Updates $give_updates */
1713 $give_updates = Give_Updates::get_instance();
1714
1715 give_v1817_process_cleanup_user_roles();
1716
1717 $give_updates->percentage = 100;
1718
1719 // Create Give plugin roles.
1720 $roles = new Give_Roles();
1721 $roles->add_roles();
1722 $roles->add_caps();
1723
1724 give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1725 }
1726
1727 /**
1728 * Automatic Upgrade for release 1.8.18.
1729 *
1730 * @since 1.8.18
1731 */
1732 function give_v1818_upgrades() {
1733
1734 // Remove email_access_installed from give_settings.
1735 give_delete_option( 'email_access_installed' );
1736 }
1737
1738 /**
1739 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1740 *
1741 * @since 1.8.18
1742 */
1743 function give_v1818_assign_custom_amount_set_donation() {
1744
1745 /* @var Give_Updates $give_updates */
1746 $give_updates = Give_Updates::get_instance();
1747
1748 $donations = new WP_Query(
1749 [
1750 'paged' => $give_updates->step,
1751 'status' => 'any',
1752 'order' => 'ASC',
1753 'post_type' => [ 'give_payment' ],
1754 'posts_per_page' => 100,
1755 ]
1756 );
1757
1758 if ( $donations->have_posts() ) {
1759 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1760
1761 while ( $donations->have_posts() ) {
1762 $donations->the_post();
1763
1764 $form = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1765 $donation_meta = give_get_payment_meta( get_the_ID() );
1766
1767 // Update Donation meta with price_id set as custom, only if it is:
1768 // 1. Donation Type = Set Donation.
1769 // 2. Donation Price Id is not set to custom.
1770 // 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1771 if (
1772 $form->ID &&
1773 $form->is_set_type_donation_form() &&
1774 ( 'custom' !== $donation_meta['price_id'] ) &&
1775 $form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1776 ) {
1777 $donation_meta['price_id'] = 'custom';
1778 give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1779 give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1780 }
1781 }
1782
1783 wp_reset_postdata();
1784 } else {
1785 // Update Ran Successfully.
1786 give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1787 }
1788 }
1789
1790 /**
1791 * Upgrade Routine - Removed Give Worker caps.
1792 *
1793 * See: https://github.com/impress-org/give/issues/2476
1794 *
1795 * @since 1.8.18
1796 */
1797 function give_v1818_give_worker_role_cleanup() {
1798
1799 /* @var Give_Updates $give_updates */
1800 $give_updates = Give_Updates::get_instance();
1801
1802 global $wp_roles;
1803
1804 if ( ! ( $wp_roles instanceof WP_Roles ) ) {
1805 return;
1806 }
1807
1808 // Remove Capabilities to user roles as required.
1809 $remove_caps = [
1810 'give_worker' => [
1811 'delete_give_payments',
1812 'delete_others_give_payments',
1813 'delete_private_give_payments',
1814 'delete_published_give_payments',
1815 'edit_others_give_payments',
1816 'edit_private_give_payments',
1817 'edit_published_give_payments',
1818 'read_private_give_payments',
1819 ],
1820 ];
1821
1822 foreach ( $remove_caps as $role => $caps ) {
1823 foreach ( $caps as $cap ) {
1824 $wp_roles->remove_cap( $role, $cap );
1825 }
1826 }
1827
1828 $give_updates->percentage = 100;
1829
1830 // Create Give plugin roles.
1831 $roles = new Give_Roles();
1832 $roles->add_roles();
1833 $roles->add_caps();
1834
1835 give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1836 }
1837
1838 /**
1839 *
1840 * Upgrade form metadata for new metabox settings.
1841 *
1842 * @since 2.0
1843 * @return void
1844 */
1845 function give_v20_upgrades_form_metadata_callback() {
1846 $give_updates = Give_Updates::get_instance();
1847
1848 // form query
1849 $forms = new WP_Query(
1850 [
1851 'paged' => $give_updates->step,
1852 'status' => 'any',
1853 'order' => 'ASC',
1854 'post_type' => 'give_forms',
1855 'posts_per_page' => 100,
1856 ]
1857 );
1858
1859 if ( $forms->have_posts() ) {
1860 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1861
1862 while ( $forms->have_posts() ) {
1863 $forms->the_post();
1864 global $post;
1865
1866 // Update offline instruction email notification status.
1867 $offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1868 $offline_instruction_notification_status = give_is_setting_enabled(
1869 $offline_instruction_notification_status,
1870 [
1871 'enabled',
1872 'global',
1873 ]
1874 )
1875 ? $offline_instruction_notification_status
1876 : 'global';
1877 update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1878
1879 // Update offline instruction email message.
1880 update_post_meta(
1881 get_the_ID(),
1882 '_give_offline-donation-instruction_email_message',
1883 get_post_meta(
1884 get_the_ID(),
1885 // @todo: Delete this option later ( version > 2.0 ).
1886 '_give_offline_donation_email',
1887 true
1888 )
1889 );
1890
1891 // Update offline instruction email subject.
1892 update_post_meta(
1893 get_the_ID(),
1894 '_give_offline-donation-instruction_email_subject',
1895 get_post_meta(
1896 get_the_ID(),
1897 // @todo: Delete this option later ( version > 2.0 ).
1898 '_give_offline_donation_subject',
1899 true
1900 )
1901 );
1902
1903 }// End while().
1904
1905 wp_reset_postdata();
1906 } else {
1907 // No more forms found, finish up.
1908 give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1909 }
1910 }
1911
1912
1913 /**
1914 * Upgrade payment metadata for new metabox settings.
1915 *
1916 * @since 2.0
1917 * @global wpdb $wpdb
1918 * @return void
1919 */
1920 function give_v20_upgrades_payment_metadata_callback() {
1921 global $wpdb;
1922 $give_updates = Give_Updates::get_instance();
1923
1924 // form query
1925 $forms = new WP_Query(
1926 [
1927 'paged' => $give_updates->step,
1928 'status' => 'any',
1929 'order' => 'ASC',
1930 'post_type' => 'give_payment',
1931 'posts_per_page' => 100,
1932 ]
1933 );
1934
1935 if ( $forms->have_posts() ) {
1936 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1937
1938 while ( $forms->have_posts() ) {
1939 $forms->the_post();
1940 global $post;
1941
1942 // Split _give_payment_meta meta.
1943 // @todo Remove _give_payment_meta after releases 2.0
1944 $payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1945
1946 if ( ! empty( $payment_meta ) ) {
1947 _give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1948 }
1949
1950 $deprecated_meta_keys = [
1951 '_give_payment_customer_id' => '_give_payment_donor_id',
1952 '_give_payment_user_email' => '_give_payment_donor_email',
1953 '_give_payment_user_ip' => '_give_payment_donor_ip',
1954 ];
1955
1956 foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
1957 // Do not add new meta key if already exist.
1958 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 ) ) ) {
1959 continue;
1960 }
1961
1962 $wpdb->insert(
1963 $wpdb->postmeta,
1964 [
1965 'post_id' => $post->ID,
1966 'meta_key' => $new_meta_key,
1967 'meta_value' => give_get_meta( $post->ID, $old_meta_key, true ),
1968 ]
1969 );
1970 }
1971
1972 // Bailout
1973 if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
1974 /* @var Give_Donor $donor */
1975 $donor = new Give_Donor( $donor_id );
1976
1977 $address['line1'] = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
1978 $address['line2'] = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
1979 $address['city'] = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
1980 $address['state'] = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
1981 $address['zip'] = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
1982 $address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
1983
1984 // Save address.
1985 $donor->add_address( 'billing[]', $address );
1986 }
1987 }// End while().
1988
1989 wp_reset_postdata();
1990 } else {
1991 // @todo Delete user id meta after releases 2.0
1992 // $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1993 // No more forms found, finish up.
1994 give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1995 }
1996 }
1997
1998
1999 /**
2000 * Upgrade logs data.
2001 *
2002 * @since 2.0
2003 * @return void
2004 */
2005 function give_v20_logs_upgrades_callback() {
2006 global $wpdb;
2007 $give_updates = Give_Updates::get_instance();
2008
2009 // form query
2010 $forms = new WP_Query(
2011 [
2012 'paged' => $give_updates->step,
2013 'order' => 'DESC',
2014 'post_type' => 'give_log',
2015 'post_status' => 'any',
2016 'posts_per_page' => 100,
2017 ]
2018 );
2019
2020 if ( $forms->have_posts() ) {
2021 $give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
2022
2023 while ( $forms->have_posts() ) {
2024 $forms->the_post();
2025 global $post;
2026
2027 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
2028 continue;
2029 }
2030
2031 $term = get_the_terms( $post->ID, 'give_log_type' );
2032 $term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : [];
2033 $term_name = ! empty( $term ) ? $term->slug : '';
2034
2035 $log_data = [
2036 'ID' => $post->ID,
2037 'log_title' => $post->post_title,
2038 'log_content' => $post->post_content,
2039 'log_parent' => 0,
2040 'log_type' => $term_name,
2041 'log_date' => $post->post_date,
2042 'log_date_gmt' => $post->post_date_gmt,
2043 ];
2044 $log_meta = [];
2045
2046 if ( $old_log_meta = get_post_meta( $post->ID ) ) {
2047 foreach ( $old_log_meta as $meta_key => $meta_value ) {
2048 switch ( $meta_key ) {
2049 case '_give_log_payment_id':
2050 $log_data['log_parent'] = current( $meta_value );
2051 $log_meta['_give_log_form_id'] = $post->post_parent;
2052 break;
2053
2054 default:
2055 $log_meta[ $meta_key ] = current( $meta_value );
2056 }
2057 }
2058 }
2059
2060 if ( 'api_request' === $term_name ) {
2061 $log_meta['_give_log_api_query'] = $post->post_excerpt;
2062 }
2063
2064 $wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
2065
2066 if ( ! empty( $log_meta ) ) {
2067 foreach ( $log_meta as $meta_key => $meta_value ) {
2068 Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2069 }
2070 }
2071
2072 $logIDs[] = $post->ID;
2073 }// End while().
2074
2075 wp_reset_postdata();
2076 } else {
2077 // @todo: Delete terms and taxonomy after releases 2.0.
2078 /*
2079 $terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
2080 if ( ! empty( $terms ) ) {
2081 foreach ( $terms as $term ) {
2082 wp_delete_term( $term, 'give_log_type' );
2083 }
2084 }*/
2085
2086 // @todo: Delete logs after releases 2.0.
2087 /*
2088 $logIDs = get_posts( array(
2089 'order' => 'DESC',
2090 'post_type' => 'give_log',
2091 'post_status' => 'any',
2092 'posts_per_page' => - 1,
2093 'fields' => 'ids',
2094 )
2095 );*/
2096
2097 /*
2098 if ( ! empty( $logIDs ) ) {
2099 foreach ( $logIDs as $log ) {
2100 // Delete term relationship and posts.
2101 wp_delete_object_term_relationships( $log, 'give_log_type' );
2102 wp_delete_post( $log, true );
2103 }
2104 }*/
2105
2106 // @todo: Unregister taxonomy after releases 2.0.
2107 /*unregister_taxonomy( 'give_log_type' );*/
2108
2109 // Delete log cache.
2110 Give()->logs->delete_cache();
2111
2112 // No more forms found, finish up.
2113 give_set_upgrade_complete( 'v20_logs_upgrades' );
2114 }
2115 }
2116
2117
2118 /**
2119 * Move payment and form metadata to new table
2120 *
2121 * @since 2.0
2122 * @return void
2123 */
2124 function give_v20_move_metadata_into_new_table_callback() {
2125 global $wpdb;
2126 $give_updates = Give_Updates::get_instance();
2127
2128 // form query
2129 $payments = new WP_Query(
2130 [
2131 'paged' => $give_updates->step,
2132 'status' => 'any',
2133 'order' => 'ASC',
2134 'post_type' => [ 'give_forms', 'give_payment' ],
2135 'posts_per_page' => 100,
2136 ]
2137 );
2138
2139 if ( $payments->have_posts() ) {
2140 $give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2141
2142 while ( $payments->have_posts() ) {
2143 $payments->the_post();
2144 global $post;
2145
2146 $meta_data = $wpdb->get_results(
2147 $wpdb->prepare(
2148 "SELECT * FROM $wpdb->postmeta where post_id=%d",
2149 get_the_ID()
2150 ),
2151 ARRAY_A
2152 );
2153
2154 if ( ! empty( $meta_data ) ) {
2155 foreach ( $meta_data as $index => $data ) {
2156 // Check for duplicate meta values.
2157 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 ) ) {
2158 continue;
2159 }
2160
2161 switch ( $post->post_type ) {
2162 case 'give_forms':
2163 $data['form_id'] = $data['post_id'];
2164 unset( $data['post_id'] );
2165
2166 Give()->form_meta->insert( $data );
2167 // @todo: delete form meta from post meta table after releases 2.0.
2168 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2169
2170 break;
2171
2172 case 'give_payment':
2173 $data['payment_id'] = $data['post_id'];
2174 unset( $data['post_id'] );
2175
2176 Give()->payment_meta->insert( $data );
2177
2178 // @todo: delete donation meta from post meta table after releases 2.0.
2179 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2180
2181 break;
2182 }
2183 }
2184 }
2185 }// End while().
2186
2187 wp_reset_postdata();
2188 } else {
2189 // No more forms found, finish up.
2190 give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2191 }
2192
2193 }
2194
2195 /**
2196 * Upgrade routine for splitting donor name into first name and last name.
2197 *
2198 * @since 2.0
2199 *
2200 * @return void
2201 */
2202 function give_v20_upgrades_donor_name() {
2203 /* @var Give_Updates $give_updates */
2204 $give_updates = Give_Updates::get_instance();
2205
2206 $donors = Give()->donors->get_donors(
2207 [
2208 'paged' => $give_updates->step,
2209 'number' => 100,
2210 ]
2211 );
2212
2213 if ( $donors ) {
2214 $give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2215 // Loop through Donors
2216 foreach ( $donors as $donor ) {
2217
2218 $donor_name = explode( ' ', $donor->name, 2 );
2219 $donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2220 $donor_last_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2221
2222 // If first name meta of donor is not created, then create it.
2223 if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
2224 Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2225 }
2226
2227 // If last name meta of donor is not created, then create it.
2228 if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
2229 Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2230 }
2231
2232 // If Donor is connected with WP User then update user meta.
2233 if ( $donor->user_id ) {
2234 if ( isset( $donor_name[0] ) ) {
2235 update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
2236 }
2237 if ( isset( $donor_name[1] ) ) {
2238 update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
2239 }
2240 }
2241 }
2242 } else {
2243 // The Update Ran.
2244 give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2245 }
2246
2247 }
2248
2249 /**
2250 * Upgrade routine for user addresses.
2251 *
2252 * @since 2.0
2253 * @global wpdb $wpdb
2254 *
2255 * @return void
2256 */
2257 function give_v20_upgrades_user_address() {
2258 global $wpdb;
2259
2260 /* @var Give_Updates $give_updates */
2261 $give_updates = Give_Updates::get_instance();
2262
2263 /* @var WP_User_Query $user_query */
2264 $user_query = new WP_User_Query(
2265 [
2266 'number' => 100,
2267 'paged' => $give_updates->step,
2268 ]
2269 );
2270
2271 $users = $user_query->get_results();
2272
2273 if ( $users ) {
2274 $give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2275
2276 // Loop through Donors
2277 foreach ( $users as $user ) {
2278 /* @var Give_Donor $donor */
2279 $donor = new Give_Donor( $user->ID, true );
2280
2281 if ( ! $donor->id ) {
2282 continue;
2283 }
2284
2285 $address = $wpdb->get_var(
2286 $wpdb->prepare(
2287 "
2288 SELECT meta_value FROM {$wpdb->usermeta}
2289 WHERE user_id=%s
2290 AND meta_key=%s
2291 ",
2292 $user->ID,
2293 '_give_user_address'
2294 )
2295 );
2296
2297 if ( ! empty( $address ) ) {
2298 $address = maybe_unserialize( $address );
2299 $donor->add_address( 'personal', $address );
2300 $donor->add_address( 'billing[]', $address );
2301
2302 // @todo: delete _give_user_address from user meta after releases 2.0.
2303 /*delete_user_meta( $user->ID, '_give_user_address' );*/
2304 }
2305 }
2306 } else {
2307 // The Update Ran.
2308 give_set_upgrade_complete( 'v20_upgrades_user_address' );
2309 }
2310
2311 }
2312
2313 /**
2314 * Upgrade logs data.
2315 *
2316 * @since 2.0
2317 * @global wpdb $wpdb
2318 * @return void
2319 */
2320 function give_v20_rename_donor_tables_callback() {
2321 global $wpdb;
2322
2323 /* @var Give_Updates $give_updates */
2324 $give_updates = Give_Updates::get_instance();
2325
2326 $tables = [
2327 "{$wpdb->prefix}give_customers" => "{$wpdb->prefix}give_donors",
2328 "{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2329 ];
2330
2331 // Alter customer table
2332 foreach ( $tables as $old_table => $new_table ) {
2333 if (
2334 $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old_table ) ) &&
2335 ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $new_table ) )
2336 ) {
2337 $wpdb->query( "ALTER TABLE {$old_table} RENAME TO {$new_table}" );
2338
2339 if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2340 $wpdb->query( "ALTER TABLE {$new_table} CHANGE COLUMN customer_id donor_id bigint(20)" );
2341 }
2342 }
2343 }
2344
2345 $give_updates->percentage = 100;
2346
2347 // No more forms found, finish up.
2348 give_set_upgrade_complete( 'v20_rename_donor_tables' );
2349
2350 // Re initiate donor classes.
2351 Give()->donors = new Give_DB_Donors();
2352 Give()->donor_meta = new Give_DB_Donor_Meta();
2353 }
2354
2355
2356 /**
2357 * Create missing meta tables.
2358 *
2359 * @since 2.0.1
2360 * @global wpdb $wpdb
2361 * @return void
2362 */
2363 function give_v201_create_tables() {
2364 global $wpdb;
2365
2366 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_paymentmeta" ) ) ) {
2367 Give()->payment_meta->create_table();
2368 }
2369
2370 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_formmeta" ) ) ) {
2371 Give()->form_meta->create_table();
2372 }
2373
2374 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_logs" ) ) ) {
2375 Give()->logs->log_db->create_table();
2376 }
2377
2378 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_logmeta" ) ) ) {
2379 Give()->logs->logmeta_db->create_table();
2380 }
2381 }
2382
2383 /**
2384 * Upgrade payment metadata for new metabox settings.
2385 *
2386 * @since 2.0.1
2387 * @global wpdb $wpdb
2388 * @return void
2389 */
2390 function give_v201_upgrades_payment_metadata_callback() {
2391 global $wpdb, $post;
2392 $give_updates = Give_Updates::get_instance();
2393 give_v201_create_tables();
2394
2395 $payments = $wpdb->get_col(
2396 "
2397 SELECT ID FROM $wpdb->posts
2398 WHERE 1=1
2399 AND (
2400 $wpdb->posts.post_date >= '2018-01-08 00:00:00'
2401 )
2402 AND $wpdb->posts.post_type = 'give_payment'
2403 AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2404 ORDER BY $wpdb->posts.post_date ASC
2405 LIMIT 100
2406 OFFSET " . $give_updates->get_offset( 100 )
2407 );
2408
2409 if ( ! empty( $payments ) ) {
2410 $give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2411
2412 foreach ( $payments as $payment_id ) {
2413 $post = get_post( $payment_id );
2414 setup_postdata( $post );
2415
2416 // Do not add new meta keys if already refactored.
2417 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' ) ) ) {
2418 continue;
2419 }
2420
2421 // Split _give_payment_meta meta.
2422 // @todo Remove _give_payment_meta after releases 2.0
2423 $payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2424
2425 if ( ! empty( $payment_meta ) ) {
2426 _give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2427 }
2428
2429 $deprecated_meta_keys = [
2430 '_give_payment_customer_id' => '_give_payment_donor_id',
2431 '_give_payment_user_email' => '_give_payment_donor_email',
2432 '_give_payment_user_ip' => '_give_payment_donor_ip',
2433 ];
2434
2435 foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
2436 // Do not add new meta key if already exist.
2437 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 ) ) ) {
2438 continue;
2439 }
2440
2441 $wpdb->insert(
2442 $wpdb->postmeta,
2443 [
2444 'post_id' => $post->ID,
2445 'meta_key' => $new_meta_key,
2446 'meta_value' => give_get_meta( $post->ID, $old_meta_key, true ),
2447 ]
2448 );
2449 }
2450
2451 // Bailout
2452 if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
2453 /* @var Give_Donor $donor */
2454 $donor = new Give_Donor( $donor_id );
2455
2456 $address['line1'] = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
2457 $address['line2'] = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
2458 $address['city'] = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
2459 $address['state'] = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
2460 $address['zip'] = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
2461 $address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
2462
2463 // Save address.
2464 $donor->add_address( 'billing[]', $address );
2465 }
2466 }// End while().
2467
2468 wp_reset_postdata();
2469 } else {
2470 // @todo Delete user id meta after releases 2.0
2471 // $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2472 // No more forms found, finish up.
2473 give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2474 }
2475 }
2476
2477 /**
2478 * Move payment and form metadata to new table
2479 *
2480 * @since 2.0.1
2481 * @return void
2482 */
2483 function give_v201_move_metadata_into_new_table_callback() {
2484 global $wpdb, $post;
2485 $give_updates = Give_Updates::get_instance();
2486 give_v201_create_tables();
2487
2488 $payments = $wpdb->get_col(
2489 "
2490 SELECT ID FROM $wpdb->posts
2491 WHERE 1=1
2492 AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2493 AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2494 ORDER BY $wpdb->posts.post_date ASC
2495 LIMIT 100
2496 OFFSET " . $give_updates->get_offset( 100 )
2497 );
2498
2499 if ( ! empty( $payments ) ) {
2500 $give_updates->set_percentage(
2501 give_get_total_post_type_count(
2502 [
2503 'give_forms',
2504 'give_payment',
2505 ]
2506 ),
2507 $give_updates->step * 100
2508 );
2509
2510 foreach ( $payments as $payment_id ) {
2511 $post = get_post( $payment_id );
2512 setup_postdata( $post );
2513
2514 $meta_data = $wpdb->get_results(
2515 $wpdb->prepare(
2516 "SELECT * FROM $wpdb->postmeta where post_id=%d",
2517 get_the_ID()
2518 ),
2519 ARRAY_A
2520 );
2521
2522 if ( ! empty( $meta_data ) ) {
2523 foreach ( $meta_data as $index => $data ) {
2524 // Check for duplicate meta values.
2525 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 ) ) {
2526 continue;
2527 }
2528
2529 switch ( $post->post_type ) {
2530 case 'give_forms':
2531 $data['form_id'] = $data['post_id'];
2532 unset( $data['post_id'] );
2533
2534 Give()->form_meta->insert( $data );
2535 // @todo: delete form meta from post meta table after releases 2.0.
2536 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2537
2538 break;
2539
2540 case 'give_payment':
2541 $data['payment_id'] = $data['post_id'];
2542 unset( $data['post_id'] );
2543
2544 Give()->payment_meta->insert( $data );
2545
2546 // @todo: delete donation meta from post meta table after releases 2.0.
2547 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2548
2549 break;
2550 }
2551 }
2552 }
2553 }// End while().
2554
2555 wp_reset_postdata();
2556 } else {
2557 // No more forms found, finish up.
2558 give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2559 }
2560
2561 }
2562
2563 /**
2564 * Move data to new log table.
2565 *
2566 * @since 2.0.1
2567 * @return void
2568 */
2569 function give_v201_logs_upgrades_callback() {
2570 global $wpdb, $post;
2571 $give_updates = Give_Updates::get_instance();
2572 give_v201_create_tables();
2573
2574 $logs = $wpdb->get_col(
2575 "
2576 SELECT ID FROM $wpdb->posts
2577 WHERE 1=1
2578 AND $wpdb->posts.post_type = 'give_log'
2579 AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2580 ORDER BY $wpdb->posts.post_date ASC
2581 LIMIT 100
2582 OFFSET " . $give_updates->get_offset( 100 )
2583 );
2584
2585 if ( ! empty( $logs ) ) {
2586 $give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2587
2588 foreach ( $logs as $log_id ) {
2589 $post = get_post( $log_id );
2590 setup_postdata( $post );
2591
2592 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
2593 continue;
2594 }
2595
2596 $term = get_the_terms( $post->ID, 'give_log_type' );
2597 $term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : [];
2598 $term_name = ! empty( $term ) ? $term->slug : '';
2599
2600 $log_data = [
2601 'ID' => $post->ID,
2602 'log_title' => $post->post_title,
2603 'log_content' => $post->post_content,
2604 'log_parent' => 0,
2605 'log_type' => $term_name,
2606 'log_date' => $post->post_date,
2607 'log_date_gmt' => $post->post_date_gmt,
2608 ];
2609 $log_meta = [];
2610
2611 if ( $old_log_meta = get_post_meta( $post->ID ) ) {
2612 foreach ( $old_log_meta as $meta_key => $meta_value ) {
2613 switch ( $meta_key ) {
2614 case '_give_log_payment_id':
2615 $log_data['log_parent'] = current( $meta_value );
2616 $log_meta['_give_log_form_id'] = $post->post_parent;
2617 break;
2618
2619 default:
2620 $log_meta[ $meta_key ] = current( $meta_value );
2621 }
2622 }
2623 }
2624
2625 if ( 'api_request' === $term_name ) {
2626 $log_meta['_give_log_api_query'] = $post->post_excerpt;
2627 }
2628
2629 $wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
2630
2631 if ( ! empty( $log_meta ) ) {
2632 foreach ( $log_meta as $meta_key => $meta_value ) {
2633 Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2634 }
2635 }
2636
2637 $logIDs[] = $post->ID;
2638 }// End while().
2639
2640 wp_reset_postdata();
2641 } else {
2642 // Delete log cache.
2643 Give()->logs->delete_cache();
2644
2645 // No more forms found, finish up.
2646 give_set_upgrade_complete( 'v201_logs_upgrades' );
2647 }
2648 }
2649
2650
2651 /**
2652 * Add missing donor.
2653 *
2654 * @since 2.0.1
2655 * @return void
2656 */
2657 function give_v201_add_missing_donors_callback() {
2658 global $wpdb;
2659 give_v201_create_tables();
2660
2661 $give_updates = Give_Updates::get_instance();
2662
2663 // Bailout.
2664 if ( ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_customers" ) ) ) {
2665 Give_Updates::get_instance()->percentage = 100;
2666 give_set_upgrade_complete( 'v201_add_missing_donors' );
2667 }
2668
2669 $total_customers = $wpdb->get_var( "SELECT COUNT(id) FROM {$wpdb->prefix}give_customers " );
2670 $customers = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_customers LIMIT 20 OFFSET " . $give_updates->get_offset( 20 ) ), 'id' );
2671 $donors = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_donors" ), 'id' );
2672
2673 if ( ! empty( $customers ) ) {
2674 $give_updates->set_percentage( $total_customers, ( $give_updates->step * 20 ) );
2675
2676 $missing_donors = array_diff( $customers, $donors );
2677 $donor_data = [];
2678
2679 if ( $missing_donors ) {
2680 foreach ( $missing_donors as $donor_id ) {
2681 $donor_data[] = [
2682 'info' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customers WHERE id=%d", $donor_id ) ),
2683 'meta' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customermeta WHERE customer_id=%d", $donor_id ) ),
2684
2685 ];
2686 }
2687 }
2688
2689 if ( ! empty( $donor_data ) ) {
2690 $donor_table_name = Give()->donors->table_name;
2691 $donor_meta_table_name = Give()->donor_meta->table_name;
2692
2693 Give()->donors->table_name = "{$wpdb->prefix}give_donors";
2694 Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2695
2696 foreach ( $donor_data as $donor ) {
2697 $donor['info'][0] = (array) $donor['info'][0];
2698
2699 // Prevent duplicate meta id issue.
2700 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donors WHERE id=%d", $donor['info'][0]['id'] ) ) ) {
2701 continue;
2702 }
2703
2704 $donor_id = Give()->donors->add( $donor['info'][0] );
2705
2706 if ( ! empty( $donor['meta'] ) ) {
2707 foreach ( $donor['meta'] as $donor_meta ) {
2708 $donor_meta = (array) $donor_meta;
2709
2710 // Prevent duplicate meta id issue.
2711 if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donormeta WHERE meta_id=%d", $donor_meta['meta_id'] ) ) ) {
2712 unset( $donor_meta['meta_id'] );
2713 }
2714
2715 $donor_meta['donor_id'] = $donor_meta['customer_id'];
2716 unset( $donor_meta['customer_id'] );
2717
2718 Give()->donor_meta->insert( $donor_meta );
2719 }
2720 }
2721
2722 /**
2723 * Fix donor name and address
2724 */
2725 $address = $wpdb->get_var(
2726 $wpdb->prepare(
2727 "
2728 SELECT meta_value FROM {$wpdb->usermeta}
2729 WHERE user_id=%s
2730 AND meta_key=%s
2731 ",
2732 $donor['info'][0]['user_id'],
2733 '_give_user_address'
2734 )
2735 );
2736
2737 $donor = new Give_Donor( $donor_id );
2738
2739 if ( ! empty( $address ) ) {
2740 $address = maybe_unserialize( $address );
2741 $donor->add_address( 'personal', $address );
2742 $donor->add_address( 'billing[]', $address );
2743 }
2744
2745 $donor_name = explode( ' ', $donor->name, 2 );
2746 $donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2747 $donor_last_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2748
2749 // If first name meta of donor is not created, then create it.
2750 if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
2751 Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2752 }
2753
2754 // If last name meta of donor is not created, then create it.
2755 if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
2756 Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2757 }
2758
2759 // If Donor is connected with WP User then update user meta.
2760 if ( $donor->user_id ) {
2761 if ( isset( $donor_name[0] ) ) {
2762 update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
2763 }
2764 if ( isset( $donor_name[1] ) ) {
2765 update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
2766 }
2767 }
2768 }
2769
2770 Give()->donors->table_name = $donor_table_name;
2771 Give()->donor_meta->table_name = $donor_meta_table_name;
2772 }
2773 } else {
2774 give_set_upgrade_complete( 'v201_add_missing_donors' );
2775 }
2776 }
2777
2778
2779 /**
2780 * Version 2.0.3 automatic updates
2781 *
2782 * @since 2.0.3
2783 */
2784 function give_v203_upgrades() {
2785 global $wpdb;
2786
2787 // Do not auto load option.
2788 $wpdb->update( $wpdb->options, [ 'autoload' => 'no' ], [ 'option_name' => 'give_completed_upgrades' ] );
2789
2790 // Remove from cache.
2791 $all_options = wp_load_alloptions();
2792
2793 if ( isset( $all_options['give_completed_upgrades'] ) ) {
2794 unset( $all_options['give_completed_upgrades'] );
2795 wp_cache_set( 'alloptions', $all_options, 'options' );
2796 }
2797
2798 }
2799
2800
2801 /**
2802 * Version 2.2.0 automatic updates
2803 *
2804 * @since 2.2.0
2805 */
2806 function give_v220_upgrades() {
2807 global $wpdb;
2808
2809 /**
2810 * Update 1
2811 *
2812 * Delete wp session data
2813 */
2814 give_v220_delete_wp_session_data();
2815
2816 /**
2817 * Update 2
2818 *
2819 * Rename payment table
2820 */
2821 give_v220_rename_donation_meta_type_callback();
2822
2823 /**
2824 * Update 2
2825 *
2826 * Set autoload to no to reduce result weight from WordPress query
2827 */
2828
2829 $options = [
2830 'give_settings',
2831 'give_version',
2832 'give_version_upgraded_from',
2833 'give_default_api_version',
2834 'give_site_address_before_migrate',
2835 '_give_table_check',
2836 'give_recently_activated_addons',
2837 'give_is_addon_activated',
2838 'give_last_paypal_ipn_received',
2839 'give_use_php_sessions',
2840 'give_subscriptions',
2841 '_give_subscriptions_edit_last',
2842 ];
2843
2844 // Add all table version option name
2845 // Add banner option *_active_by_user
2846 $option_like = $wpdb->get_col(
2847 "
2848 SELECT option_name
2849 FROM $wpdb->options
2850 WHERE option_name like '%give%'
2851 AND (
2852 option_name like '%_db_version%'
2853 OR option_name like '%_active_by_user%'
2854 OR option_name like '%_license_active%'
2855 )
2856 "
2857 );
2858
2859 if ( ! empty( $option_like ) ) {
2860 $options = array_merge( $options, $option_like );
2861 }
2862
2863 $options_str = '\'' . implode( "','", $options ) . '\'';
2864
2865 $wpdb->query(
2866 "
2867 UPDATE $wpdb->options
2868 SET autoload = 'no'
2869 WHERE option_name IN ( {$options_str} )
2870 "
2871 );
2872 }
2873
2874 /**
2875 * Version 2.2.1 automatic updates
2876 *
2877 * @since 2.2.1
2878 */
2879 function give_v221_upgrades() {
2880 global $wpdb;
2881
2882 /**
2883 * Update 1
2884 *
2885 * Change column length
2886 */
2887 $wpdb->query( "ALTER TABLE $wpdb->donors MODIFY email varchar(255) NOT NULL" );
2888 }
2889
2890 /**
2891 * Version 2.3.0 automatic updates
2892 *
2893 * @since 2.3.0
2894 */
2895 function give_v230_upgrades() {
2896
2897 $options_key = [
2898 'give_temp_delete_form_ids', // delete import donor
2899 'give_temp_delete_donation_ids', // delete import donor
2900 'give_temp_delete_step', // delete import donor
2901 'give_temp_delete_donor_ids', // delete import donor
2902 'give_temp_delete_step_on', // delete import donor
2903 'give_temp_delete_donation_ids', // delete test donor
2904 'give_temp_delete_donor_ids', // delete test donor
2905 'give_temp_delete_step', // delete test donor
2906 'give_temp_delete_step_on', // delete test donor
2907 'give_temp_delete_test_ids', // delete test donations
2908 'give_temp_all_payments_data', // delete all stats
2909 'give_recount_all_total', // delete all stats
2910 'give_temp_recount_all_stats', // delete all stats
2911 'give_temp_payment_items', // delete all stats
2912 'give_temp_form_ids', // delete all stats
2913 'give_temp_processed_payments', // delete all stats
2914 'give_temp_recount_form_stats', // delete form stats
2915 'give_temp_recount_earnings', // recount income
2916 'give_recount_earnings_total', // recount income
2917 'give_temp_reset_ids', // reset stats
2918 ];
2919
2920 $options_key = '\'' . implode( "','", $options_key ) . '\'';
2921
2922 global $wpdb;
2923
2924 /**
2925 * Update 1
2926 *
2927 * delete unwanted key from option table
2928 */
2929 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ( {$options_key} )" );
2930 }
2931
2932 /**
2933 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2934 *
2935 * @since 2.1
2936 */
2937 function give_v210_verify_form_status_upgrades_callback() {
2938
2939 $give_updates = Give_Updates::get_instance();
2940
2941 // form query.
2942 $donation_forms = new WP_Query(
2943 [
2944 'paged' => $give_updates->step,
2945 'status' => 'any',
2946 'order' => 'ASC',
2947 'post_type' => 'give_forms',
2948 'posts_per_page' => 20,
2949 ]
2950 );
2951
2952 if ( $donation_forms->have_posts() ) {
2953 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2954
2955 while ( $donation_forms->have_posts() ) {
2956 $donation_forms->the_post();
2957 $form_id = get_the_ID();
2958
2959 $form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2960 if ( empty( $form_closed_status ) ) {
2961 give_set_form_closed_status( $form_id );
2962 }
2963 }
2964
2965 /* Restore original Post Data */
2966 wp_reset_postdata();
2967
2968 } else {
2969
2970 // The Update Ran.
2971 give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2972 }
2973 }
2974
2975 /**
2976 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2977 *
2978 * @since 2.1
2979 */
2980 function give_v213_delete_donation_meta_callback() {
2981 global $wpdb;
2982 $give_updates = Give_Updates::get_instance();
2983 $donation_meta_table = Give()->payment_meta->table_name;
2984
2985 $donations = $wpdb->get_col(
2986 "
2987 SELECT DISTINCT payment_id
2988 FROM {$donation_meta_table}
2989 LIMIT 20
2990 OFFSET {$give_updates->get_offset( 20 )}
2991 "
2992 );
2993
2994 if ( ! empty( $donations ) ) {
2995 foreach ( $donations as $donation ) {
2996 $donation_obj = get_post( $donation );
2997
2998 if ( ! $donation_obj instanceof WP_Post ) {
2999 Give()->payment_meta->delete_all_meta( $donation );
3000 }
3001 }
3002 } else {
3003
3004 // The Update Ran.
3005 give_set_upgrade_complete( 'v213_delete_donation_meta' );
3006 }
3007 }
3008
3009 /**
3010 * Rename donation meta type
3011 *
3012 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
3013 *
3014 * @since 2.2.0
3015 */
3016 function give_v220_rename_donation_meta_type_callback() {
3017 global $wpdb;
3018
3019 // Check upgrade before running.
3020 if (
3021 give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
3022 || ! $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_paymentmeta" ) )
3023 ) {
3024 // Complete update if skip somehow
3025 give_set_upgrade_complete( 'v220_rename_donation_meta_type' );
3026
3027 return;
3028 }
3029
3030 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta CHANGE COLUMN payment_id donation_id bigint(20)" );
3031 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta RENAME TO {$wpdb->prefix}give_donationmeta" );
3032
3033 give_set_upgrade_complete( 'v220_rename_donation_meta_type' );
3034 }
3035
3036 /**
3037 * Adds 'view_give_payments' capability to 'give_manager' user role.
3038 *
3039 * @since 2.1.5
3040 */
3041 function give_v215_update_donor_user_roles_callback() {
3042
3043 $role = get_role( 'give_manager' );
3044 $role->add_cap( 'view_give_payments' );
3045
3046 give_set_upgrade_complete( 'v215_update_donor_user_roles' );
3047 }
3048
3049
3050 /**
3051 * Remove all wp session data from the options table, regardless of expiration.
3052 *
3053 * @since 2.2.0
3054 *
3055 * @global wpdb $wpdb
3056 */
3057 function give_v220_delete_wp_session_data() {
3058 global $wpdb;
3059
3060 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" );
3061 }
3062
3063
3064 /**
3065 * Update donor meta
3066 * Set "_give_anonymous_donor" meta key to "0" if not exist
3067 *
3068 * @since 2.2.4
3069 */
3070 function give_v224_update_donor_meta_callback() {
3071 /* @var Give_Updates $give_updates */
3072 $give_updates = Give_Updates::get_instance();
3073
3074 $donor_count = Give()->donors->count(
3075 [
3076 'number' => - 1,
3077 ]
3078 );
3079
3080 $donors = Give()->donors->get_donors(
3081 [
3082 'paged' => $give_updates->step,
3083 'number' => 100,
3084 ]
3085 );
3086
3087 if ( $donors ) {
3088 $give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
3089 // Loop through Donors
3090 foreach ( $donors as $donor ) {
3091 $anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
3092
3093 // If first name meta of donor is not created, then create it.
3094 if ( ! in_array( $anonymous_metadata, [ '0', '1' ] ) ) {
3095 Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
3096 }
3097 }
3098 } else {
3099 // The Update Ran.
3100 give_set_upgrade_complete( 'v224_update_donor_meta' );
3101 }
3102 }
3103
3104 /** Update donor meta
3105 * Set "_give_anonymous_donor_forms" meta key if not exist
3106 *
3107 * @since 2.2.4
3108 */
3109 function give_v224_update_donor_meta_forms_id_callback() {
3110 $give_updates = Give_Updates::get_instance();
3111
3112 $donations = new WP_Query(
3113 [
3114 'paged' => $give_updates->step,
3115 'status' => 'any',
3116 'order' => 'ASC',
3117 'post_type' => [ 'give_payment' ],
3118 'posts_per_page' => 20,
3119 ]
3120 );
3121
3122 if ( $donations->have_posts() ) {
3123 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 20 );
3124
3125 while ( $donations->have_posts() ) {
3126 $donations->the_post();
3127
3128 $donation_id = get_the_ID();
3129
3130 $form_id = give_get_payment_form_id( $donation_id );
3131 $donor_id = give_get_payment_donor_id( $donation_id );
3132 $is_donated_as_anonymous = give_is_anonymous_donation( $donation_id );
3133
3134 $is_anonymous_donor = Give()->donor_meta->get_meta( $donor_id, "_give_anonymous_donor_form_{$form_id}", true );
3135 $is_edit_donor_meta = ! in_array( $is_anonymous_donor, [ '0', '1' ] )
3136 ? true
3137 : ( 0 !== absint( $is_anonymous_donor ) );
3138
3139 if ( $is_edit_donor_meta ) {
3140 Give()->donor_meta->update_meta( $donor_id, "_give_anonymous_donor_form_{$form_id}", absint( $is_donated_as_anonymous ) );
3141 }
3142 }
3143
3144 wp_reset_postdata();
3145 } else {
3146 give_set_upgrade_complete( 'v224_update_donor_meta_forms_id' );
3147 }
3148 }
3149
3150 /**
3151 * Add custom comment table
3152 *
3153 * @since 2.4.0
3154 */
3155 function give_v230_add_missing_comment_tables() {
3156 $custom_tables = [
3157 Give()->comment->db,
3158 Give()->comment->db_meta,
3159 ];
3160
3161 /* @var Give_DB $table */
3162 foreach ( $custom_tables as $table ) {
3163 if ( ! $table->installed() ) {
3164 $table->register_table();
3165 }
3166 }
3167 }
3168
3169
3170 /**
3171 * Move donor notes to comment table
3172 *
3173 * @since 2.3.0
3174 */
3175 function give_v230_move_donor_note_callback() {
3176 // Add comment table if missing.
3177 give_v230_add_missing_comment_tables();
3178
3179 /* @var Give_Updates $give_updates */
3180 $give_updates = Give_Updates::get_instance();
3181
3182 $donor_count = Give()->donors->count(
3183 [
3184 'number' => - 1,
3185 ]
3186 );
3187
3188 $donors = Give()->donors->get_donors(
3189 [
3190 'paged' => $give_updates->step,
3191 'number' => 100,
3192 ]
3193 );
3194
3195 if ( $donors ) {
3196 $give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
3197 // Loop through Donors
3198 foreach ( $donors as $donor ) {
3199 $notes = trim( Give()->donors->get_column( 'notes', $donor->id ) );
3200
3201 // If first name meta of donor is not created, then create it.
3202 if ( ! empty( $notes ) ) {
3203 $notes = array_values( array_filter( array_map( 'trim', explode( "\n", $notes ) ), 'strlen' ) );
3204
3205 foreach ( $notes as $note ) {
3206 $note = array_map( 'trim', explode( '-', $note ) );
3207 $timestamp = strtotime( $note[0] );
3208
3209 Give()->comment->db->add(
3210 [
3211 'comment_content' => $note[1],
3212 'user_id' => absint( Give()->donors->get_column_by( 'user_id', 'id', $donor->id ) ),
3213 'comment_date' => date( 'Y-m-d H:i:s', $timestamp ),
3214 'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $timestamp ) ),
3215 'comment_parent' => $donor->id,
3216 'comment_type' => 'donor',
3217 ]
3218 );
3219 }
3220 }
3221 }
3222 } else {
3223 // The Update Ran.
3224 give_set_upgrade_complete( 'v230_move_donor_note' );
3225 }
3226 }
3227
3228 /**
3229 * Move donation notes to comment table
3230 *
3231 * @since 2.3.0
3232 */
3233 function give_v230_move_donation_note_callback() {
3234 global $wpdb;
3235
3236 // Add comment table if missing.
3237 give_v230_add_missing_Comment_tables();
3238
3239 /* @var Give_Updates $give_updates */
3240 $give_updates = Give_Updates::get_instance();
3241
3242 $donation_note_count = $wpdb->get_var(
3243 $wpdb->prepare(
3244 "
3245 SELECT count(*)
3246 FROM {$wpdb->comments}
3247 WHERE comment_type=%s
3248 ",
3249 'give_payment_note'
3250 )
3251 );
3252
3253 $query = $wpdb->prepare(
3254 "
3255 SELECT *
3256 FROM {$wpdb->comments}
3257 WHERE comment_type=%s
3258 ORDER BY comment_ID ASC
3259 LIMIT 100
3260 OFFSET %d
3261 ",
3262 'give_payment_note',
3263 $give_updates->get_offset( 100 )
3264 );
3265
3266 $comments = $wpdb->get_results( $query );
3267
3268 if ( $comments ) {
3269 $give_updates->set_percentage( $donation_note_count, $give_updates->step * 100 );
3270
3271 // Loop through Donors
3272 foreach ( $comments as $comment ) {
3273 $donation_id = $comment->comment_post_ID;
3274 $form_id = give_get_payment_form_id( $donation_id );
3275
3276 $comment_id = Give()->comment->db->add(
3277 [
3278 'comment_content' => $comment->comment_content,
3279 'user_id' => $comment->user_id,
3280 'comment_date' => date( 'Y-m-d H:i:s', strtotime( $comment->comment_date ) ),
3281 'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $comment->comment_date_gmt ) ) ),
3282 'comment_parent' => $comment->comment_post_ID,
3283 'comment_type' => is_numeric( get_comment_meta( $comment->comment_ID, '_give_donor_id', true ) )
3284 ? 'donor_donation'
3285 : 'donation',
3286 ]
3287 );
3288
3289 if ( ! $comment_id ) {
3290 continue;
3291 }
3292
3293 // @see https://github.com/impress-org/give/issues/3737#issuecomment-428460802
3294 $restricted_meta_keys = [
3295 'akismet_result',
3296 'akismet_as_submitted',
3297 'akismet_history',
3298 ];
3299
3300 if ( $comment_meta = get_comment_meta( $comment->comment_ID ) ) {
3301 foreach ( $comment_meta as $meta_key => $meta_value ) {
3302 // Skip few comment meta keys.
3303 if ( in_array( $meta_key, $restricted_meta_keys ) ) {
3304 continue;
3305 }
3306
3307 $meta_value = maybe_unserialize( $meta_value );
3308 $meta_value = is_array( $meta_value ) ? current( $meta_value ) : $meta_value;
3309
3310 Give()->comment->db_meta->update_meta( $comment_id, $meta_key, $meta_value );
3311 }
3312 }
3313
3314 Give()->comment->db_meta->update_meta( $comment_id, '_give_form_id', $form_id );
3315
3316 // Delete comment.
3317 update_comment_meta( $comment->comment_ID, '_give_comment_moved', 1 );
3318 }
3319 } else {
3320 $comment_ids = $wpdb->get_col(
3321 $wpdb->prepare(
3322 "
3323 SELECT DISTINCT comment_id
3324 FROM {$wpdb->commentmeta}
3325 WHERE meta_key=%s
3326 AND meta_value=%d
3327 ",
3328 '_give_comment_moved',
3329 1
3330 )
3331 );
3332
3333 if ( ! empty( $comment_ids ) ) {
3334 $comment_ids = "'" . implode( "','", $comment_ids ) . "'";
3335
3336 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_ID IN ({$comment_ids})" );
3337 $wpdb->query( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ({$comment_ids})" );
3338 }
3339
3340 // The Update Ran.
3341 give_set_upgrade_complete( 'v230_move_donation_note' );
3342 }
3343 }
3344
3345 /**
3346 * Delete donor wall related donor meta data
3347 *
3348 * @since 2.3.0
3349 */
3350 function give_v230_delete_dw_related_donor_data_callback() {
3351 global $wpdb;
3352
3353 $give_updates = Give_Updates::get_instance();
3354
3355 $wpdb->query( "DELETE FROM {$wpdb->donormeta} WHERE meta_key LIKE '%_give_anonymous_donor%' OR meta_key='_give_has_comment';" );
3356
3357 $give_updates->percentage = 100;
3358
3359 // The Update Ran.
3360 give_set_upgrade_complete( 'v230_delete_donor_wall_related_donor_data' );
3361 }
3362
3363 /**
3364 * Delete donor wall related comment meta data
3365 *
3366 * @since 2.3.0
3367 */
3368 function give_v230_delete_dw_related_comment_data_callback() {
3369 global $wpdb;
3370
3371 $give_updates = Give_Updates::get_instance();
3372
3373 $wpdb->query( "DELETE FROM {$wpdb->give_commentmeta} WHERE meta_key='_give_anonymous_donation';" );
3374
3375 $give_updates->percentage = 100;
3376
3377 // The Update Ran.
3378 give_set_upgrade_complete( 'v230_delete_donor_wall_related_comment_data' );
3379 }
3380
3381 /**
3382 * Update donation form goal progress data.
3383 *
3384 * @since 2.4.0
3385 */
3386 function give_v240_update_form_goal_progress_callback() {
3387
3388 /* @var Give_Updates $give_updates */
3389 $give_updates = Give_Updates::get_instance();
3390
3391 // form query
3392 $forms = new WP_Query(
3393 [
3394 'paged' => $give_updates->step,
3395 'status' => 'any',
3396 'order' => 'ASC',
3397 'post_type' => 'give_forms',
3398 'posts_per_page' => 20,
3399 ]
3400 );
3401
3402 if ( $forms->have_posts() ) {
3403 while ( $forms->have_posts() ) {
3404 $forms->the_post();
3405
3406 // Update the goal progress for donation form.
3407 give_update_goal_progress( get_the_ID() );
3408
3409 }// End while().
3410
3411 wp_reset_postdata();
3412
3413 } else {
3414
3415 // No more forms found, finish up.
3416 give_set_upgrade_complete( 'v240_update_form_goal_progress' );
3417
3418 }
3419 }
3420
3421
3422 /**
3423 * Manual update handler for v241_remove_sale_logs
3424 *
3425 * @since 2.4.1
3426 */
3427 function give_v241_remove_sale_logs_callback() {
3428 global $wpdb;
3429
3430 $log_table = Give()->logs->log_db->table_name;
3431 $log_meta_table = Give()->logs->logmeta_db->table_name;
3432
3433 $sql = "DELETE {$log_table}, {$log_meta_table}
3434 FROM {$log_table}
3435 INNER JOIN {$log_meta_table} ON {$log_meta_table}.log_id={$log_table}.ID
3436 WHERE log_type='sale'
3437 ";
3438
3439 // Remove donation logs.
3440 $wpdb->query( $sql );
3441
3442 give_set_upgrade_complete( 'v241_remove_sale_logs' );
3443 }
3444
3445
3446 /**
3447 * DB upgrades for Give 2.5.0
3448 *
3449 * @since 2.5.0
3450 */
3451 function give_v250_upgrades() {
3452 global $wpdb;
3453
3454 $old_license = [];
3455 $new_license = [];
3456 $give_licenses = get_option( 'give_licenses', [] );
3457 $give_options = give_get_settings();
3458
3459 // Get add-ons license key.
3460 $addons = [];
3461 foreach ( $give_options as $key => $value ) {
3462 if ( false !== strpos( $key, '_license_key' ) ) {
3463 $addons[ $key ] = $value;
3464 }
3465 }
3466
3467 // Bailout: We do not have any add-on license data to upgrade.
3468 if ( empty( $addons ) ) {
3469 return false;
3470 }
3471
3472 foreach ( $addons as $key => $license_key ) {
3473
3474 // Get addon shortname.
3475 $addon_shortname = str_replace( '_license_key', '', $key );
3476
3477 // Addon license option name.
3478 $addon_shortname = "{$addon_shortname}_license_active";
3479 $addon_license_data = get_option( "{$addon_shortname}_license_active", [] );
3480
3481 if (
3482 ! $license_key
3483 || array_key_exists( $license_key, $give_licenses )
3484 ) {
3485 continue;
3486 }
3487
3488 $old_license[ $license_key ] = $addon_license_data;
3489 }
3490
3491 // Bailout.
3492 if ( empty( $old_license ) ) {
3493 return false;
3494 }
3495
3496 /* @var stdClass $data */
3497 foreach ( $old_license as $key => $data ) {
3498 $tmp = Give_License::request_license_api(
3499 [
3500 'edd_action' => 'check_license',
3501 'license' => $key,
3502 ],
3503 true
3504 );
3505
3506 if ( is_wp_error( $tmp ) || ! $tmp['success'] ) {
3507 continue;
3508 }
3509
3510 $new_license[ $key ] = $tmp;
3511 }
3512
3513 // Bailout.
3514 if ( empty( $new_license ) ) {
3515 return false;
3516 }
3517
3518 $give_licenses = array_merge( $give_licenses, $new_license );
3519
3520 update_option( 'give_licenses', $give_licenses );
3521
3522 /**
3523 * Delete data.
3524 */
3525
3526 // 1. license keys
3527 foreach ( get_option( 'give_settings' ) as $index => $setting ) {
3528 if ( false !== strpos( $index, '_license_key' ) ) {
3529 give_delete_option( $index );
3530 }
3531 }
3532
3533 // 2. license api data
3534 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name like '%_license_active%' AND option_name like 'give_%'" );
3535
3536 // 3. subscriptions data
3537 delete_option( '_give_subscriptions_edit_last' );
3538 delete_option( 'give_subscriptions' );
3539
3540 // 4. misc
3541 delete_option( 'give_is_addon_activated' );
3542
3543 give_refresh_licenses();
3544 }
3545
3546 /**
3547 * DB upgrades for Give 2.5.8
3548 *
3549 * @since 2.5.8
3550 */
3551 function give_v258_upgrades() {
3552
3553 $is_checkout_enabled = give_is_setting_enabled( give_get_option( 'stripe_checkout_enabled', 'disabled' ) );
3554
3555 // Bailout, if stripe checkout is not active as a gateway.
3556 if ( ! $is_checkout_enabled ) {
3557 return;
3558 }
3559
3560 $enabled_gateways = give_get_option( 'gateways', [] );
3561
3562 // Bailout, if Stripe Checkout is already enabled.
3563 if ( ! empty( $enabled_gateways['stripe_checkout'] ) ) {
3564 return;
3565 }
3566
3567 $gateways_label = give_get_option( 'gateways_label', [] );
3568 $default_gateway = give_get_option( 'default_gateway' );
3569
3570 // Set Stripe Checkout as active gateway.
3571 $enabled_gateways['stripe_checkout'] = 1;
3572
3573 // Unset Stripe - Credit Card as an active gateway.
3574 unset( $enabled_gateways['stripe'] );
3575
3576 // Set Stripe Checkout same as Stripe as they have enabled Stripe Checkout under Stripe using same label.
3577 $gateways_label['stripe_checkout'] = $gateways_label['stripe'];
3578 give_update_option( 'gateways_label', $gateways_label );
3579
3580 // If default gateway selected is `stripe` then set `stripe checkout` as default.
3581 if ( 'stripe' === $default_gateway ) {
3582 give_update_option( 'default_gateway', 'stripe_checkout' );
3583 }
3584
3585 // Update the enabled gateways in database.
3586 give_update_option( 'gateways', $enabled_gateways );
3587
3588 // Delete the old legacy settings.
3589 give_delete_option( 'stripe_checkout_enabled' );
3590 }
3591
3592
3593 /**
3594 * DB upgrades for Give 2.5.11
3595 *
3596 * @since 2.5.11
3597 */
3598 function give_v2511_upgrades() {
3599 global $wp_roles, $wpdb;
3600 $all_roles = get_editable_roles();
3601
3602 // Run code only if not a fresh install.
3603 if ( Give_Cache_Setting::get_option( 'give_version' ) ) {
3604 // Remove unused notes column from donor table.
3605 $wpdb->query( "ALTER TABLE {$wpdb->prefix}give_donors DROP COLUMN notes;" );
3606 }
3607
3608 foreach ( $all_roles as $role => $data ) {
3609 $wp_roles->remove_cap( $role, 'delete_give_form' );
3610 $wp_roles->remove_cap( $role, 'delete_give_payment' );
3611 $wp_roles->remove_cap( $role, 'edit_give_form' );
3612 $wp_roles->remove_cap( $role, 'edit_give_payment' );
3613 $wp_roles->remove_cap( $role, 'read_give_form' );
3614 $wp_roles->remove_cap( $role, 'read_give_payment' );
3615 }
3616 }
3617
3618 /**
3619 * Upgrade for version 2.6.3
3620 *
3621 * @since 2.6.3
3622 */
3623 function give_v263_upgrades() {
3624 $licenses = get_option( 'give_licenses', [] );
3625
3626 if ( $licenses ) {
3627 foreach ( $licenses as $license ) {
3628 if ( ! empty( $license['is_all_access_pass'] ) ) {
3629 // Remove single license which is part of all access pass.
3630 // @see https://github.com/impress-org/givewp/issues/4669
3631 $addonSlugs = Give_License::getAddonSlugsFromAllAccessPassLicense( $license );
3632 foreach ( $licenses as $license_key => $data ) {
3633 // Skip bundle plan license key.
3634 if ( ! empty( $data['is_all_access_pass'] ) ) {
3635 continue;
3636 }
3637
3638 if ( in_array( $data['plugin_slug'], $addonSlugs, true ) ) {
3639 unset( $licenses[ $license_key ] );
3640 }
3641 }
3642 }
3643 }
3644
3645 update_option( 'give_licenses', $licenses );
3646 }
3647 }
3648
3649
3650 /**
3651 * Upgrade routine to call for backward compatibility to manage default Stripe account.
3652 *
3653 * @since 2.7.0
3654 * @global wpdb $wpdb
3655 * @return void
3656 */
3657 function give_v270_upgrades() {
3658 global $wpdb;
3659
3660 $settingKey = '_give_stripe_get_all_accounts';
3661 $giveSettings = give_get_settings();
3662 $isStripeAccountMigrated = array_key_exists( $settingKey, $giveSettings );
3663 $stripeAccounts = $isStripeAccountMigrated ? $giveSettings[ $settingKey ] : [];
3664
3665 // Process, only when there is no Stripe accounts stored.
3666 if ( ! $isStripeAccountMigrated ) {
3667 $liveSecretKey = give_get_option( 'live_secret_key' );
3668 $testSecretKey = give_get_option( 'test_secret_key' );
3669 $livePublishableKey = give_get_option( 'live_publishable_key' );
3670 $testPublishableKey = give_get_option( 'test_publishable_key' );
3671 $isStripeConfigurationExist = $liveSecretKey || $testSecretKey || $livePublishableKey || $testPublishableKey;
3672
3673 if ( $isStripeConfigurationExist ) {
3674 // Manual API Keys are enabled.
3675 if ( ! give_get_option( 'give_stripe_user_id' ) ) {
3676 $uniqueSlug = 'account_1';
3677 $stripeAccounts[ $uniqueSlug ] = [
3678 'type' => 'manual',
3679 'account_name' => give_stripe_convert_slug_to_title( $uniqueSlug ),
3680 'account_slug' => $uniqueSlug,
3681 'account_email' => '',
3682 'account_country' => '',
3683 'account_id' => '', // This parameter will be empty for manual API Keys Stripe account.
3684 'live_secret_key' => $liveSecretKey,
3685 'test_secret_key' => $testSecretKey,
3686 'live_publishable_key' => $livePublishableKey,
3687 'test_publishable_key' => $testPublishableKey,
3688 ];
3689
3690 // Set first Stripe account as default.
3691 give_update_option( '_give_stripe_default_account', $uniqueSlug );
3692 } else {
3693
3694 $secret_key = give_get_option( 'live_secret_key' );
3695 if ( give_is_test_mode() ) {
3696 $secret_key = give_get_option( 'test_secret_key' );
3697 }
3698
3699 \Stripe\Stripe::setApiKey( $secret_key );
3700
3701 $accounts_count = is_countable( $stripeAccounts ) ? count( $stripeAccounts ) + 1 : 1;
3702 $all_account_slugs = array_keys( $stripeAccounts );
3703 $accountSlug = give_stripe_get_unique_account_slug( $all_account_slugs, $accounts_count );
3704 $accountName = give_stripe_convert_slug_to_title( $accountSlug );
3705 $accountEmail = '';
3706 $accountCountry = '';
3707 $stripeAccountId = give_get_option( 'give_stripe_user_id' );
3708 $accountDetails = give_stripe_get_account_details( $stripeAccountId );
3709
3710 // Setup Account Details for Connected Stripe Accounts.
3711 if ( ! empty( $accountDetails->id ) && 'account' === $accountDetails->object ) {
3712 $accountName = ! empty( $accountDetails->business_profile->name ) ?
3713 $accountDetails->business_profile->name :
3714 $accountDetails->settings->dashboard->display_name;
3715 $accountSlug = $accountDetails->id;
3716 $accountEmail = $accountDetails->email;
3717 $accountCountry = $accountDetails->country;
3718 }
3719
3720 $stripeAccounts[ $accountSlug ] = [
3721 'type' => 'connect',
3722 'account_name' => $accountName,
3723 'account_slug' => $accountSlug,
3724 'account_email' => $accountEmail,
3725 'account_country' => $accountCountry,
3726 'account_id' => $stripeAccountId,
3727 'live_secret_key' => $liveSecretKey,
3728 'test_secret_key' => $testSecretKey,
3729 'live_publishable_key' => $livePublishableKey,
3730 'test_publishable_key' => $testPublishableKey,
3731 ];
3732
3733 // Set first Stripe account as default.
3734 give_update_option( '_give_stripe_default_account', $accountSlug );
3735 }
3736
3737 give_update_option( $settingKey, $stripeAccounts );
3738
3739 // Remove legacy settings.
3740 give_delete_option( 'live_secret_key' );
3741 give_delete_option( 'test_secret_key' );
3742 give_delete_option( 'live_publishable_key' );
3743 give_delete_option( 'test_publishable_key' );
3744 give_delete_option( 'give_stripe_connected' );
3745 give_delete_option( 'give_stripe_user_id' );
3746 }
3747 }
3748
3749 // Do not need to go beyond this if you are on fresh install and on fresh install donationmeta property is not defined for $wpdb.
3750 // Below code is to check if site have donations which processed with Stripe payment method
3751 // if not then we will auto complete stripe background update.
3752 if ( ! property_exists( $wpdb, 'donationmeta' ) ) {
3753 return;
3754 }
3755
3756 $canStoreStripeInformationInDonation = (bool) $wpdb->get_var(
3757 $wpdb->prepare(
3758 "
3759 SELECT COUNT(donation_id)
3760 FROM $wpdb->donationmeta
3761 WHERE meta_key=%s
3762 AND meta_value LIKE %s",
3763 '_give_payment_gateway',
3764 '%stripe%'
3765 )
3766 );
3767
3768 if ( ! $canStoreStripeInformationInDonation || ! $stripeAccounts ) {
3769 give_set_upgrade_complete( 'v270_store_stripe_account_for_donation' );
3770 }
3771 }
3772
3773 /**
3774 * This manual upgrade routine is used set the default Stripe account for all the existing donations.
3775 * This process will help us to identify which Stripe account is used to process a specific donation.
3776 *
3777 * @since 2.7.0
3778 *
3779 * @return void
3780 */
3781 function give_v270_store_stripe_account_for_donation_callback() {
3782 /* @var Give_Updates $give_updates */
3783 $give_updates = Give_Updates::get_instance();
3784
3785 $donations = new WP_Query(
3786 [
3787 'paged' => $give_updates->step,
3788 'status' => 'any',
3789 'order' => 'ASC',
3790 'post_type' => [ 'give_payment' ],
3791 'posts_per_page' => 100,
3792 ]
3793 );
3794
3795 if ( $donations->have_posts() ) {
3796 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
3797
3798 while ( $donations->have_posts() ) {
3799 $donations->the_post();
3800 $donationId = get_the_ID();
3801
3802 // Continue, if the donation is not processed with any of the supported payment method of Stripe.
3803 if ( ! Stripe::isDonationPaymentMethod( give_get_payment_gateway( $donationId ) ) ) {
3804 continue;
3805 }
3806
3807 Stripe::addAccountDetail(
3808 $donationId,
3809 give_get_payment_form_id( $donationId )
3810 );
3811 }
3812
3813 wp_reset_postdata();
3814 } else {
3815 // Update Ran Successfully.
3816 give_set_upgrade_complete( 'v270_store_stripe_account_for_donation' );
3817 }
3818 }
3819