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