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