PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.0
GiveWP – Donation Plugin and Fundraising Platform v2.0.0
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 8 years ago class-give-updates.php 8 years ago upgrade-functions.php 8 years ago
upgrade-functions.php
2108 lines
1 <?php
2 /**
3 * Upgrade Functions
4 *
5 * @package Give
6 * @subpackage Admin/Upgrades
7 * @copyright Copyright (c) 2016, WordImpress
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
82 if ( $did_upgrade ) {
83 update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
84 }
85 }
86
87 add_action( 'admin_init', 'give_do_automatic_upgrades' );
88 add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
89
90 /**
91 * Display Upgrade Notices.
92 *
93 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
94 *
95 * @since 1.0
96 * @since 1.8.12 Update new update process code.
97 *
98 * @param Give_Updates $give_updates
99 *
100 * @return void
101 */
102 function give_show_upgrade_notices( $give_updates ) {
103 // v1.3.2 Upgrades
104 $give_updates->register(
105 array(
106 'id' => 'upgrade_give_payment_customer_id',
107 'version' => '1.3.2',
108 'callback' => 'give_v132_upgrade_give_payment_customer_id',
109 )
110 );
111
112 // v1.3.4 Upgrades ensure the user has gone through 1.3.4.
113 $give_updates->register(
114 array(
115 'id' => 'upgrade_give_offline_status',
116 'depend' => 'upgrade_give_payment_customer_id',
117 'version' => '1.3.4',
118 'callback' => 'give_v134_upgrade_give_offline_status',
119 )
120 );
121
122 // v1.8 form metadata upgrades.
123 $give_updates->register(
124 array(
125 'id' => 'v18_upgrades_form_metadata',
126 'version' => '1.8',
127 'callback' => 'give_v18_upgrades_form_metadata',
128 )
129 );
130
131 // v1.8.9 Upgrades
132 $give_updates->register(
133 array(
134 'id' => 'v189_upgrades_levels_post_meta',
135 'version' => '1.8.9',
136 'callback' => 'give_v189_upgrades_levels_post_meta_callback',
137 )
138 );
139
140 // v1.8.12 Upgrades
141 $give_updates->register(
142 array(
143 'id' => 'v1812_update_amount_values',
144 'version' => '1.8.12',
145 'callback' => 'give_v1812_update_amount_values_callback',
146 )
147 );
148
149 // v1.8.12 Upgrades
150 $give_updates->register(
151 array(
152 'id' => 'v1812_update_donor_purchase_values',
153 'version' => '1.8.12',
154 'callback' => 'give_v1812_update_donor_purchase_value_callback',
155 )
156 );
157
158 // v1.8.13 Upgrades for donor
159 $give_updates->register(
160 array(
161 'id' => 'v1813_update_donor_user_roles',
162 'version' => '1.8.13',
163 'callback' => 'give_v1813_update_donor_user_roles_callback',
164 )
165 );
166
167 // v1.8.17 Upgrades for donations.
168 $give_updates->register( array(
169 'id' => 'v1817_update_donation_iranian_currency_code',
170 'version' => '1.8.17',
171 'callback' => 'give_v1817_update_donation_iranian_currency_code',
172 ) );
173
174 // v1.8.17 Upgrades for cleanup of user roles.
175 $give_updates->register( array(
176 'id' => 'v1817_cleanup_user_roles',
177 'version' => '1.8.17',
178 'callback' => 'give_v1817_cleanup_user_roles',
179 ) );
180
181 // v1.8.18 Upgrades for assigning custom amount to existing set donations.
182 $give_updates->register( array(
183 'id' => 'v1818_assign_custom_amount_set_donation',
184 'version' => '1.8.18',
185 'callback' => 'give_v1818_assign_custom_amount_set_donation',
186 ) );
187
188 // v1.8.18 Cleanup the Give Worker Role Caps.
189 $give_updates->register( array(
190 'id' => 'v1818_give_worker_role_cleanup',
191 'version' => '1.8.18',
192 'callback' => 'give_v1818_give_worker_role_cleanup',
193 ) );
194
195 // v2.0.0 Upgrades
196 $give_updates->register(
197 array(
198 'id' => 'v20_upgrades_form_metadata',
199 'version' => '2.0.0',
200 'callback' => 'give_v20_upgrades_form_metadata_callback',
201 )
202 );
203
204 // v2.0.0 User Address Upgrades
205 $give_updates->register(
206 array(
207 'id' => 'v20_upgrades_user_address',
208 'version' => '2.0.0',
209 'callback' => 'give_v20_upgrades_user_address',
210 )
211 );
212
213 // v2.0.0 Upgrades
214 $give_updates->register(
215 array(
216 'id' => 'v20_upgrades_payment_metadata',
217 'version' => '2.0.0',
218 'callback' => 'give_v20_upgrades_payment_metadata_callback',
219 )
220 );
221
222 // v2.0.0 Upgrades
223 $give_updates->register(
224 array(
225 'id' => 'v20_logs_upgrades',
226 'version' => '2.0.0',
227 'callback' => 'give_v20_logs_upgrades_callback',
228
229 )
230 );
231
232 // v2.0.0 Donor Name Upgrades
233 $give_updates->register(
234 array(
235 'id' => 'v20_upgrades_donor_name',
236 'version' => '2.0.0',
237 'callback' => 'give_v20_upgrades_donor_name',
238 )
239 );
240
241 // v2.0.0 Upgrades
242 $give_updates->register(
243 array(
244 'id' => 'v20_move_metadata_into_new_table',
245 'version' => '2.0.0',
246 'callback' => 'give_v20_move_metadata_into_new_table_callback',
247 'depend' => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
248 )
249 );
250
251 // v2.0.0 Upgrades
252 $give_updates->register(
253 array(
254 'id' => 'v20_rename_donor_tables',
255 'version' => '2.0.0',
256 'callback' => 'give_v20_rename_donor_tables_callback',
257 'depend' => array(
258 'v20_move_metadata_into_new_table',
259 'v20_logs_upgrades',
260 'v20_upgrades_form_metadata',
261 'v20_upgrades_payment_metadata',
262 'v20_upgrades_user_address',
263 'v20_upgrades_donor_name'
264 ),
265 )
266 );
267 }
268
269 add_action( 'give_register_updates', 'give_show_upgrade_notices' );
270
271 /**
272 * Triggers all upgrade functions
273 *
274 * This function is usually triggered via AJAX
275 *
276 * @since 1.0
277 * @return void
278 */
279 function give_trigger_upgrades() {
280
281 if ( ! current_user_can( 'manage_give_settings' ) ) {
282 wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
283 'response' => 403,
284 ) );
285 }
286
287 $give_version = get_option( 'give_version' );
288
289 if ( ! $give_version ) {
290 // 1.0 is the first version to use this option so we must add it.
291 $give_version = '1.0';
292 add_option( 'give_version', $give_version );
293 }
294
295 update_option( 'give_version', GIVE_VERSION );
296 delete_option( 'give_doing_upgrade' );
297
298 if ( DOING_AJAX ) {
299 die( 'complete' );
300 } // End if().
301 }
302
303 add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
304
305
306 /**
307 * Upgrades the
308 *
309 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
310 *
311 * @since 1.3.2
312 */
313 function give_v132_upgrade_give_payment_customer_id() {
314 global $wpdb;
315
316 /* @var Give_Updates $give_updates */
317 $give_updates = Give_Updates::get_instance();
318
319 if ( ! current_user_can( 'manage_give_settings' ) ) {
320 wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
321 'response' => 403,
322 ) );
323 }
324
325 give_ignore_user_abort();
326
327 // UPDATE DB METAKEYS.
328 $sql = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
329 $query = $wpdb->query( $sql );
330
331 $give_updates->percentage = 100;
332 give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
333 }
334
335
336 /**
337 * Upgrades the Offline Status
338 *
339 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
340 *
341 * @since 1.3.4
342 */
343 function give_v134_upgrade_give_offline_status() {
344 global $wpdb;
345
346 /* @var Give_Updates $give_updates */
347 $give_updates = Give_Updates::get_instance();
348
349 // Get abandoned offline payments.
350 $select = "SELECT ID FROM $wpdb->posts p ";
351 $join = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
352 $where = "WHERE p.post_type = 'give_payment' ";
353 $where .= "AND ( p.post_status = 'abandoned' )";
354 $where .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
355
356 $sql = $select . $join . $where;
357 $found_payments = $wpdb->get_col( $sql );
358
359 foreach ( $found_payments as $payment ) {
360
361 // Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
362 $modified_time = get_post_modified_time( 'U', false, $payment );
363
364 // 1450124863 = 12/10/2015 20:42:25.
365 if ( $modified_time >= 1450124863 ) {
366
367 give_update_payment_status( $payment, 'pending' );
368
369 }
370 }
371
372 $give_updates->percentage = 100;
373 give_set_upgrade_complete( 'upgrade_give_offline_status' );
374 }
375
376
377 /**
378 * Cleanup User Roles
379 *
380 * This upgrade routine removes unused roles and roles with typos
381 *
382 * @since 1.5.2
383 */
384 function give_v152_cleanup_users() {
385
386 $give_version = get_option( 'give_version' );
387
388 if ( ! $give_version ) {
389 // 1.0 is the first version to use this option so we must add it.
390 $give_version = '1.0';
391 }
392
393 $give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
394
395 // v1.5.2 Upgrades
396 if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
397
398 // Delete all caps with "ss".
399 // Also delete all unused "campaign" roles.
400 $delete_caps = array(
401 'delete_give_formss',
402 'delete_others_give_formss',
403 'delete_private_give_formss',
404 'delete_published_give_formss',
405 'read_private_forms',
406 'edit_give_formss',
407 'edit_others_give_formss',
408 'edit_private_give_formss',
409 'edit_published_give_formss',
410 'publish_give_formss',
411 'read_private_give_formss',
412 'assign_give_campaigns_terms',
413 'delete_give_campaigns',
414 'delete_give_campaigns_terms',
415 'delete_give_campaignss',
416 'delete_others_give_campaignss',
417 'delete_private_give_campaignss',
418 'delete_published_give_campaignss',
419 'edit_give_campaigns',
420 'edit_give_campaigns_terms',
421 'edit_give_campaignss',
422 'edit_others_give_campaignss',
423 'edit_private_give_campaignss',
424 'edit_published_give_campaignss',
425 'manage_give_campaigns_terms',
426 'publish_give_campaignss',
427 'read_give_campaigns',
428 'read_private_give_campaignss',
429 'view_give_campaigns_stats',
430 'delete_give_paymentss',
431 'delete_others_give_paymentss',
432 'delete_private_give_paymentss',
433 'delete_published_give_paymentss',
434 'edit_give_paymentss',
435 'edit_others_give_paymentss',
436 'edit_private_give_paymentss',
437 'edit_published_give_paymentss',
438 'publish_give_paymentss',
439 'read_private_give_paymentss',
440 );
441
442 global $wp_roles;
443 foreach ( $delete_caps as $cap ) {
444 foreach ( array_keys( $wp_roles->roles ) as $role ) {
445 $wp_roles->remove_cap( $role, $cap );
446 }
447 }
448
449 // Create Give plugin roles.
450 $roles = new Give_Roles();
451 $roles->add_roles();
452 $roles->add_caps();
453
454 // The Update Ran.
455 update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
456 give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
457 delete_option( 'give_doing_upgrade' );
458
459 }// End if().
460
461 }
462
463 add_action( 'admin_init', 'give_v152_cleanup_users' );
464
465 /**
466 * 1.6 Upgrade routine to create the customer meta table.
467 *
468 * @since 1.6
469 * @return void
470 */
471 function give_v16_upgrades() {
472 // Create the donor databases.
473 $donors_db = new Give_DB_Donors();
474 $donors_db->create_table();
475 $donor_meta = new Give_DB_Donor_Meta();
476 $donor_meta->create_table();
477 }
478
479 /**
480 * 1.7 Upgrades.
481 *
482 * a. Update license api data for plugin addons.
483 * b. Cleanup user roles.
484 *
485 * @since 1.7
486 * @return void
487 */
488 function give_v17_upgrades() {
489 // Upgrade license data.
490 give_v17_upgrade_addon_license_data();
491 give_v17_cleanup_roles();
492 }
493
494 /**
495 * Upgrade license data
496 *
497 * @since 1.7
498 */
499 function give_v17_upgrade_addon_license_data() {
500 $give_options = give_get_settings();
501
502 $api_url = 'https://givewp.com/give-sl-api/';
503
504 // Get addons license key.
505 $addons = array();
506 foreach ( $give_options as $key => $value ) {
507 if ( false !== strpos( $key, '_license_key' ) ) {
508 $addons[ $key ] = $value;
509 }
510 }
511
512 // Bailout: We do not have any addon license data to upgrade.
513 if ( empty( $addons ) ) {
514 return false;
515 }
516
517 foreach ( $addons as $key => $addon_license ) {
518
519 // Get addon shortname.
520 $shortname = str_replace( '_license_key', '', $key );
521
522 // Addon license option name.
523 $addon_license_option_name = $shortname . '_license_active';
524
525 // bailout if license is empty.
526 if ( empty( $addon_license ) ) {
527 delete_option( $addon_license_option_name );
528 continue;
529 }
530
531 // Get addon name.
532 $addon_name = array();
533 $addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
534 foreach ( $addon_name_parts as $name_part ) {
535
536 // Fix addon name
537 switch ( $name_part ) {
538 case 'authorizenet' :
539 $name_part = 'authorize.net';
540 break;
541 }
542
543 $addon_name[] = ucfirst( $name_part );
544 }
545
546 $addon_name = implode( ' ', $addon_name );
547
548 // Data to send to the API.
549 $api_params = array(
550 'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
551 'license' => $addon_license,
552 'item_name' => urlencode( $addon_name ),
553 'url' => home_url(),
554 );
555
556 // Call the API.
557 $response = wp_remote_post(
558 $api_url,
559 array(
560 'timeout' => 15,
561 'sslverify' => false,
562 'body' => $api_params,
563 )
564 );
565
566 // Make sure there are no errors.
567 if ( is_wp_error( $response ) ) {
568 delete_option( $addon_license_option_name );
569 continue;
570 }
571
572 // Tell WordPress to look for updates.
573 set_site_transient( 'update_plugins', null );
574
575 // Decode license data.
576 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
577 update_option( $addon_license_option_name, $license_data );
578 }// End foreach().
579 }
580
581
582 /**
583 * Cleanup User Roles.
584 *
585 * This upgrade routine removes unused roles and roles with typos.
586 *
587 * @since 1.7
588 */
589 function give_v17_cleanup_roles() {
590
591 // Delete all caps with "_give_forms_" and "_give_payments_".
592 // These roles have no usage; the proper is singular.
593 $delete_caps = array(
594 'view_give_forms_stats',
595 'delete_give_forms_terms',
596 'assign_give_forms_terms',
597 'edit_give_forms_terms',
598 'manage_give_forms_terms',
599 'view_give_payments_stats',
600 'manage_give_payments_terms',
601 'edit_give_payments_terms',
602 'assign_give_payments_terms',
603 'delete_give_payments_terms',
604 );
605
606 global $wp_roles;
607 foreach ( $delete_caps as $cap ) {
608 foreach ( array_keys( $wp_roles->roles ) as $role ) {
609 $wp_roles->remove_cap( $role, $cap );
610 }
611 }
612
613 // Set roles again.
614 $roles = new Give_Roles();
615 $roles->add_roles();
616 $roles->add_caps();
617
618 }
619
620 /**
621 * 1.8 Upgrades.
622 *
623 * a. Upgrade checkbox settings to radio button settings.
624 * a. Update form meta for new metabox settings.
625 *
626 * @since 1.8
627 * @return void
628 */
629 function give_v18_upgrades() {
630 // Upgrade checkbox settings to radio button settings.
631 give_v18_upgrades_core_setting();
632 }
633
634 /**
635 * Upgrade core settings.
636 *
637 * @since 1.8
638 * @return void
639 */
640 function give_v18_upgrades_core_setting() {
641 // Core settings which changes from checkbox to radio.
642 $core_setting_names = array_merge(
643 array_keys( give_v18_renamed_core_settings() ),
644 array(
645 'uninstall_on_delete',
646 'scripts_footer',
647 'test_mode',
648 'email_access',
649 'terms',
650 'give_offline_donation_enable_billing_fields',
651 )
652 );
653
654 // Bailout: If not any setting define.
655 if ( $give_settings = get_option( 'give_settings' ) ) {
656
657 $setting_changed = false;
658
659 // Loop: check each setting field.
660 foreach ( $core_setting_names as $setting_name ) {
661 // New setting name.
662 $new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
663
664 // Continue: If setting already set.
665 if (
666 array_key_exists( $new_setting_name, $give_settings )
667 && in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
668 ) {
669 continue;
670 }
671
672 // Set checkbox value to radio value.
673 $give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
674
675 // @see https://github.com/WordImpress/Give/issues/1063.
676 if ( false !== strpos( $setting_name, 'disable_' ) ) {
677
678 $give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
679 } elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
680
681 $give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
682 }
683
684 // Tell bot to update core setting to db.
685 if ( ! $setting_changed ) {
686 $setting_changed = true;
687 }
688 }
689
690 // Update setting only if they changed.
691 if ( $setting_changed ) {
692 update_option( 'give_settings', $give_settings );
693 }
694 }// End if().
695
696 give_set_upgrade_complete( 'v18_upgrades_core_setting' );
697 }
698
699 /**
700 * Upgrade form metadata for new metabox settings.
701 *
702 * @since 1.8
703 * @return void
704 */
705 function give_v18_upgrades_form_metadata() {
706 /* @var Give_Updates $give_updates */
707 $give_updates = Give_Updates::get_instance();
708
709 // form query
710 $forms = new WP_Query( array(
711 'paged' => $give_updates->step,
712 'status' => 'any',
713 'order' => 'ASC',
714 'post_type' => 'give_forms',
715 'posts_per_page' => 20,
716 )
717 );
718
719 if ( $forms->have_posts() ) {
720 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
721
722 while ( $forms->have_posts() ) {
723 $forms->the_post();
724
725 // Form content.
726 // Note in version 1.8 display content setting split into display content and content placement setting.
727 // You can delete _give_content_option in future.
728 $show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
729 if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
730 $field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
731 give_update_meta( get_the_ID(), '_give_display_content', $field_value );
732
733 $field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
734 give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
735 }
736
737 // "Disable" Guest Donation. Checkbox.
738 // See: https://github.com/WordImpress/Give/issues/1470.
739 $guest_donation = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
740 $guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
741 give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
742
743 // Offline Donations.
744 // See: https://github.com/WordImpress/Give/issues/1579.
745 $offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
746 if ( 'no' === $offline_donation ) {
747 $offline_donation_newval = 'global';
748 } elseif ( 'yes' === $offline_donation ) {
749 $offline_donation_newval = 'enabled';
750 } else {
751 $offline_donation_newval = 'disabled';
752 }
753 give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
754
755 // Convert yes/no setting field to enabled/disabled.
756 $form_radio_settings = array(
757 // Custom Amount.
758 '_give_custom_amount',
759
760 // Donation Gaol.
761 '_give_goal_option',
762
763 // Close Form.
764 '_give_close_form_when_goal_achieved',
765
766 // Term & conditions.
767 '_give_terms_option',
768
769 // Billing fields.
770 '_give_offline_donation_enable_billing_fields_single',
771 );
772
773 foreach ( $form_radio_settings as $meta_key ) {
774 // Get value.
775 $field_value = give_get_meta( get_the_ID(), $meta_key, true );
776
777 // Convert meta value only if it is in yes/no/none.
778 if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
779
780 $field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
781 give_update_meta( get_the_ID(), $meta_key, $field_value );
782 }
783 }
784 }// End while().
785
786 wp_reset_postdata();
787
788 } else {
789 // No more forms found, finish up.
790 give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
791 }
792 }
793
794
795 /**
796 * Get list of core setting renamed in version 1.8.
797 *
798 * @since 1.8
799 * @return array
800 */
801 function give_v18_renamed_core_settings() {
802 return array(
803 'disable_paypal_verification' => 'paypal_verification',
804 'disable_css' => 'css',
805 'disable_welcome' => 'welcome',
806 'disable_forms_singular' => 'forms_singular',
807 'disable_forms_archives' => 'forms_archives',
808 'disable_forms_excerpt' => 'forms_excerpt',
809 'disable_form_featured_img' => 'form_featured_img',
810 'disable_form_sidebar' => 'form_sidebar',
811 'disable_admin_notices' => 'admin_notices',
812 'disable_the_content_filter' => 'the_content_filter',
813 'enable_floatlabels' => 'floatlabels',
814 'enable_categories' => 'categories',
815 'enable_tags' => 'tags',
816 );
817 }
818
819
820 /**
821 * Upgrade core settings.
822 *
823 * @since 1.8.7
824 * @return void
825 */
826 function give_v187_upgrades() {
827 global $wpdb;
828
829 /**
830 * Upgrade 1: Remove stat and cache transients.
831 */
832 $cached_options = $wpdb->get_col(
833 $wpdb->prepare(
834 "
835 SELECT *
836 FROM {$wpdb->options}
837 WHERE (
838 option_name LIKE %s
839 OR option_name LIKE %s
840 OR option_name LIKE %s
841 OR option_name LIKE %s
842 OR option_name LIKE %s
843 OR option_name LIKE %s
844 OR option_name LIKE %s
845 OR option_name LIKE %s
846 OR option_name LIKE %s
847 OR option_name LIKE %s
848 OR option_name LIKE %s
849 OR option_name LIKE %s
850 OR option_name LIKE %s
851 )
852 ",
853 array(
854 '%_transient_give_stats_%',
855 'give_cache%',
856 '%_transient_give_add_ons_feed%',
857 '%_transient__give_ajax_works' .
858 '%_transient_give_total_api_keys%',
859 '%_transient_give_i18n_give_promo_hide%',
860 '%_transient_give_contributors%',
861 '%_transient_give_estimated_monthly_stats%',
862 '%_transient_give_earnings_total%',
863 '%_transient_give_i18n_give_%',
864 '%_transient__give_installed%',
865 '%_transient__give_activation_redirect%',
866 '%_transient__give_hide_license_notices_shortly_%',
867 '%give_income_total%',
868 )
869 ),
870 1
871 );
872
873 // User related transients.
874 $user_apikey_options = $wpdb->get_results(
875 $wpdb->prepare(
876 "SELECT user_id, meta_key
877 FROM $wpdb->usermeta
878 WHERE meta_value=%s",
879 'give_user_public_key'
880 ),
881 ARRAY_A
882 );
883
884 if ( ! empty( $user_apikey_options ) ) {
885 foreach ( $user_apikey_options as $user ) {
886 $cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
887 $cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
888 $cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
889 }
890 }
891
892 if ( ! empty( $cached_options ) ) {
893 foreach ( $cached_options as $option ) {
894 switch ( true ) {
895 case ( false !== strpos( $option, 'transient' ) ):
896 $option = str_replace( '_transient_', '', $option );
897 delete_transient( $option );
898 break;
899
900 default:
901 delete_option( $option );
902 }
903 }
904 }
905 }
906
907 /**
908 * Update Capabilities for Give_Worker User Role.
909 *
910 * This upgrade routine will update access rights for Give_Worker User Role.
911 *
912 * @since 1.8.8
913 */
914 function give_v188_upgrades() {
915
916 global $wp_roles;
917
918 // Get the role object.
919 $give_worker = get_role( 'give_worker' );
920
921 // A list of capabilities to add for give workers.
922 $caps_to_add = array(
923 'edit_posts',
924 'edit_pages',
925 );
926
927 foreach ( $caps_to_add as $cap ) {
928 // Add the capability.
929 $give_worker->add_cap( $cap );
930 }
931
932 }
933
934 /**
935 * Update Post meta for minimum and maximum amount for multi level donation forms
936 *
937 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
938 *
939 * @since 1.8.9
940 */
941 function give_v189_upgrades_levels_post_meta_callback() {
942 /* @var Give_Updates $give_updates */
943 $give_updates = Give_Updates::get_instance();
944
945 // form query.
946 $donation_forms = new WP_Query( array(
947 'paged' => $give_updates->step,
948 'status' => 'any',
949 'order' => 'ASC',
950 'post_type' => 'give_forms',
951 'posts_per_page' => 20,
952 )
953 );
954
955 if ( $donation_forms->have_posts() ) {
956 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
957
958 while ( $donation_forms->have_posts() ) {
959 $donation_forms->the_post();
960 $form_id = get_the_ID();
961
962 // Remove formatting from _give_set_price.
963 update_post_meta(
964 $form_id,
965 '_give_set_price',
966 give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
967 );
968
969 // Remove formatting from _give_custom_amount_minimum.
970 update_post_meta(
971 $form_id,
972 '_give_custom_amount_minimum',
973 give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
974 );
975
976 // Bailout.
977 if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
978 continue;
979 }
980
981 $donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
982
983 if ( ! empty( $donation_levels ) ) {
984
985 foreach ( $donation_levels as $index => $donation_level ) {
986 if ( isset( $donation_level['_give_amount'] ) ) {
987 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
988 }
989 }
990
991 update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
992
993 $donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
994
995 $min_amount = min( $donation_levels_amounts );
996 $max_amount = max( $donation_levels_amounts );
997
998 // Set Minimum and Maximum amount for Multi Level Donation Forms
999 give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1000 give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1001 }
1002
1003 }
1004
1005 /* Restore original Post Data */
1006 wp_reset_postdata();
1007 } else {
1008 // The Update Ran.
1009 give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1010 }
1011
1012 }
1013
1014
1015 /**
1016 * Give version 1.8.9 upgrades
1017 *
1018 * @since 1.8.9
1019 */
1020 function give_v189_upgrades() {
1021 /**
1022 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1023 */
1024 global $wpdb;
1025
1026 // Delete permanent notice blocker.
1027 $wpdb->query(
1028 $wpdb->prepare(
1029 "
1030 DELETE FROM $wpdb->usermeta
1031 WHERE meta_key
1032 LIKE '%%%s%%'
1033 ",
1034 '_give_hide_license_notices_permanently'
1035 )
1036 );
1037
1038 // Delete short notice blocker.
1039 $wpdb->query(
1040 $wpdb->prepare(
1041 "
1042 DELETE FROM $wpdb->options
1043 WHERE option_name
1044 LIKE '%%%s%%'
1045 ",
1046 '__give_hide_license_notices_shortly_'
1047 )
1048 );
1049 }
1050
1051 /**
1052 * 2.0 Upgrades.
1053 *
1054 * @since 2.0
1055 * @return void
1056 */
1057 function give_v20_upgrades() {
1058 // Update cache setting.
1059 give_update_option( 'cache', 'enabled' );
1060
1061 // Upgrade email settings.
1062 give_v20_upgrades_email_setting();
1063 }
1064
1065 /**
1066 * Move old email api settings to new email setting api for following emails:
1067 * 1. new offline donation [This was hard coded]
1068 * 2. offline donation instruction
1069 * 3. new donation
1070 * 4. donation receipt
1071 *
1072 * @since 2.0
1073 */
1074 function give_v20_upgrades_email_setting() {
1075 $all_setting = give_get_settings();
1076
1077 // Bailout on fresh install.
1078 if ( empty( $all_setting ) ) {
1079 return;
1080 }
1081
1082 $settings = array(
1083 'offline_donation_subject' => 'offline-donation-instruction_email_subject',
1084 'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1085 'donation_subject' => 'donation-receipt_email_subject',
1086 'donation_receipt' => 'donation-receipt_email_message',
1087 'donation_notification_subject' => 'new-donation_email_subject',
1088 'donation_notification' => 'new-donation_email_message',
1089 'admin_notice_emails' => array(
1090 'new-donation_recipient',
1091 'new-offline-donation_recipient',
1092 'new-donor-register_recipient',
1093 ),
1094 'admin_notices' => 'new-donation_notification',
1095 );
1096
1097 foreach ( $settings as $old_setting => $new_setting ) {
1098 // Do not update already modified
1099 if ( ! is_array( $new_setting ) ) {
1100 if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1101 continue;
1102 }
1103 }
1104
1105 switch ( $old_setting ) {
1106 case 'admin_notices':
1107 $notification_status = give_get_option( $old_setting, 'enabled' );
1108
1109 give_update_option( $new_setting, $notification_status );
1110
1111 // @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1112 // give_delete_option( $old_setting );
1113
1114 break;
1115
1116 // @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1117 case 'admin_notice_emails':
1118 $recipients = give_get_admin_notice_emails();
1119
1120 foreach ( $new_setting as $setting ) {
1121 // bailout if setting already exist.
1122 if ( array_key_exists( $setting, $all_setting ) ) {
1123 continue;
1124 }
1125
1126 give_update_option( $setting, $recipients );
1127 }
1128 break;
1129
1130 default:
1131 give_update_option( $new_setting, give_get_option( $old_setting ) );
1132 give_delete_option( $old_setting );
1133 }
1134 }
1135 }
1136
1137 /**
1138 * Give version 1.8.9 upgrades
1139 *
1140 * @since 1.8.9
1141 */
1142 function give_v1812_upgrades() {
1143 /**
1144 * Validate number format settings.
1145 */
1146 $give_settings = give_get_settings();
1147 $give_setting_updated = false;
1148
1149 if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1150 $give_settings['number_decimals'] = 0;
1151 $give_settings['decimal_separator'] = '';
1152 $give_setting_updated = true;
1153
1154 } elseif ( empty( $give_settings['decimal_separator'] ) ) {
1155 $give_settings['number_decimals'] = 0;
1156 $give_setting_updated = true;
1157
1158 } elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1159 $give_settings['number_decimals'] = 5;
1160 $give_setting_updated = true;
1161 }
1162
1163 if ( $give_setting_updated ) {
1164 update_option( 'give_settings', $give_settings );
1165 }
1166 }
1167
1168
1169 /**
1170 * Give version 1.8.12 update
1171 *
1172 * Standardized amount values to six decimal
1173 *
1174 * @see https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1175 *
1176 * @since 1.8.12
1177 */
1178 function give_v1812_update_amount_values_callback() {
1179 /* @var Give_Updates $give_updates */
1180 $give_updates = Give_Updates::get_instance();
1181
1182 // form query.
1183 $donation_forms = new WP_Query( array(
1184 'paged' => $give_updates->step,
1185 'status' => 'any',
1186 'order' => 'ASC',
1187 'post_type' => array( 'give_forms', 'give_payment' ),
1188 'posts_per_page' => 20,
1189 )
1190 );
1191 if ( $donation_forms->have_posts() ) {
1192 $give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1193
1194 while ( $donation_forms->have_posts() ) {
1195 $donation_forms->the_post();
1196 global $post;
1197
1198 $meta = get_post_meta( $post->ID );
1199
1200 switch ( $post->post_type ) {
1201 case 'give_forms':
1202 // _give_set_price.
1203 if ( ! empty( $meta['_give_set_price'][0] ) ) {
1204 update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1205 }
1206
1207 // _give_custom_amount_minimum.
1208 if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1209 update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1210 }
1211
1212 // _give_levels_minimum_amount.
1213 if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1214 update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1215 }
1216
1217 // _give_levels_maximum_amount.
1218 if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1219 update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1220 }
1221
1222 // _give_set_goal.
1223 if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1224 update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1225 }
1226
1227 // _give_form_earnings.
1228 if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1229 update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1230 }
1231
1232 // _give_custom_amount_minimum.
1233 if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1234 $donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1235
1236 foreach ( $donation_levels as $index => $level ) {
1237 if ( empty( $level['_give_amount'] ) ) {
1238 continue;
1239 }
1240
1241 $donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1242 }
1243
1244 $meta['_give_donation_levels'] = $donation_levels;
1245 update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1246 }
1247
1248
1249 break;
1250
1251 case 'give_payment':
1252 // _give_payment_total.
1253 if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1254 update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1255 }
1256
1257 break;
1258 }
1259 }
1260
1261 /* Restore original Post Data */
1262 wp_reset_postdata();
1263 } else {
1264 // The Update Ran.
1265 give_set_upgrade_complete( 'v1812_update_amount_values' );
1266 }
1267 }
1268
1269
1270 /**
1271 * Give version 1.8.12 update
1272 *
1273 * Standardized amount values to six decimal for donor
1274 *
1275 * @see https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1276 *
1277 * @since 1.8.12
1278 */
1279 function give_v1812_update_donor_purchase_value_callback() {
1280 /* @var Give_Updates $give_updates */
1281 $give_updates = Give_Updates::get_instance();
1282 $offset = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1283
1284 // form query.
1285 $donors = Give()->donors->get_donors( array(
1286 'number' => 20,
1287 'offset' => $offset,
1288 )
1289 );
1290
1291 if ( ! empty( $donors ) ) {
1292 $give_updates->set_percentage( Give()->donors->count(), $offset );
1293
1294 /* @var Object $donor */
1295 foreach ( $donors as $donor ) {
1296 Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1297 }
1298 } else {
1299 // The Update Ran.
1300 give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1301 }
1302 }
1303
1304 /**
1305 * Upgrade routine for updating user roles for existing donors.
1306 *
1307 * @since 1.8.13
1308 */
1309 function give_v1813_update_donor_user_roles_callback() {
1310 /* @var Give_Updates $give_updates */
1311 $give_updates = Give_Updates::get_instance();
1312 $offset = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1313
1314 // Fetch all the existing donors.
1315 $donors = Give()->donors->get_donors( array(
1316 'number' => 20,
1317 'offset' => $offset,
1318 )
1319 );
1320
1321 if ( ! empty( $donors ) ) {
1322 $give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1323
1324 /* @var Object $donor */
1325 foreach ( $donors as $donor ) {
1326 $user_id = $donor->user_id;
1327
1328 // Proceed, if donor is attached with user.
1329 if ( $user_id ) {
1330 $user = get_userdata( $user_id );
1331
1332 // Update user role, if user has subscriber role.
1333 if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1334 wp_update_user(
1335 array(
1336 'ID' => $user_id,
1337 'role' => 'give_donor',
1338 )
1339 );
1340 }
1341 }
1342 }
1343 } else {
1344 // The Update Ran.
1345 give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1346 }
1347 }
1348
1349
1350 /**
1351 * Version 1.8.13 automatic updates
1352 *
1353 * @since 1.8.13
1354 */
1355 function give_v1813_upgrades() {
1356 // Update admin setting.
1357 give_update_option( 'donor_default_user_role', 'give_donor' );
1358
1359 // Update Give roles.
1360 $roles = new Give_Roles();
1361 $roles->add_roles();
1362 $roles->add_caps();
1363 }
1364
1365 /**
1366 * Correct currency code for "Iranian Currency" for all of the payments.
1367 *
1368 * @since 1.8.17
1369 */
1370 function give_v1817_update_donation_iranian_currency_code() {
1371 /* @var Give_Updates $give_updates */
1372 $give_updates = Give_Updates::get_instance();
1373
1374 // form query.
1375 $payments = new WP_Query( array(
1376 'paged' => $give_updates->step,
1377 'status' => 'any',
1378 'order' => 'ASC',
1379 'post_type' => array( 'give_payment' ),
1380 'posts_per_page' => 100,
1381 )
1382 );
1383
1384 if ( $payments->have_posts() ) {
1385 $give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1386
1387 while ( $payments->have_posts() ) {
1388 $payments->the_post();
1389
1390 $payment_meta = give_get_payment_meta( get_the_ID() );
1391
1392 if ( 'RIAL' === $payment_meta['currency'] ) {
1393 $payment_meta['currency'] = 'IRR';
1394 give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1395 }
1396
1397 }
1398
1399 } else {
1400 // The Update Ran.
1401 give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1402 }
1403 }
1404
1405 /**
1406 * Correct currency code for "Iranian Currency" in Give setting.
1407 * Version 1.8.17 automatic updates
1408 *
1409 * @since 1.8.17
1410 */
1411 function give_v1817_upgrades() {
1412 $give_settings = give_get_settings();
1413
1414 if ( 'RIAL' === $give_settings['currency'] ) {
1415 $give_settings['currency'] = 'IRR';
1416 update_option( 'give_settings', $give_settings );
1417 }
1418 }
1419
1420 /**
1421 * Process Clean up of User Roles for more flexibility.
1422 *
1423 * @since 1.8.17
1424 */
1425 function give_v1817_process_cleanup_user_roles() {
1426
1427 global $wp_roles;
1428
1429 if( ! ( $wp_roles instanceof WP_Roles ) ) {
1430 return;
1431 }
1432
1433 // Add Capabilities to user roles as required.
1434 $add_caps = array(
1435 'administrator' => array(
1436 'view_give_payments',
1437 ),
1438 );
1439
1440 // Remove Capabilities to user roles as required.
1441 $remove_caps = array(
1442 'give_manager' => array(
1443 'edit_others_pages',
1444 'edit_others_posts',
1445 'delete_others_pages',
1446 'delete_others_posts',
1447 'manage_categories',
1448 'import',
1449 'export',
1450 ),
1451 );
1452
1453 foreach ( $add_caps as $role => $caps ) {
1454 foreach ( $caps as $cap ) {
1455 $wp_roles->add_cap( $role, $cap );
1456 }
1457 }
1458
1459 foreach ( $remove_caps as $role => $caps ) {
1460 foreach ( $caps as $cap ) {
1461 $wp_roles->remove_cap( $role, $cap );
1462 }
1463 }
1464
1465 }
1466
1467 /**
1468 * Upgrade Routine - Clean up of User Roles for more flexibility.
1469 *
1470 * @since 1.8.17
1471 */
1472 function give_v1817_cleanup_user_roles() {
1473 /* @var Give_Updates $give_updates */
1474 $give_updates = Give_Updates::get_instance();
1475
1476 give_v1817_process_cleanup_user_roles();
1477
1478 $give_updates->percentage = 100;
1479
1480 // Create Give plugin roles.
1481 $roles = new Give_Roles();
1482 $roles->add_roles();
1483 $roles->add_caps();
1484
1485 give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1486 }
1487
1488 /**
1489 * Automatic Upgrade for release 1.8.18.
1490 *
1491 * @since 1.8.18
1492 */
1493 function give_v1818_upgrades() {
1494
1495 // Remove email_access_installed from give_settings.
1496 give_delete_option( 'email_access_installed' );
1497 }
1498
1499 /**
1500 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1501 *
1502 * @since 1.8.18
1503 */
1504 function give_v1818_assign_custom_amount_set_donation() {
1505
1506 /* @var Give_Updates $give_updates */
1507 $give_updates = Give_Updates::get_instance();
1508
1509 $donations = new WP_Query( array(
1510 'paged' => $give_updates->step,
1511 'status' => 'any',
1512 'order' => 'ASC',
1513 'post_type' => array( 'give_payment' ),
1514 'posts_per_page' => 100,
1515 )
1516 );
1517
1518 if ( $donations->have_posts() ) {
1519 $give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1520
1521 while ( $donations->have_posts() ) {
1522 $donations->the_post();
1523
1524 $form = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1525 $donation_meta = give_get_payment_meta( get_the_ID() );
1526
1527 // Update Donation meta with price_id set as custom, only if it is:
1528 // 1. Donation Type = Set Donation.
1529 // 2. Donation Price Id is not set to custom.
1530 // 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1531 if (
1532 $form->ID &&
1533 $form->is_set_type_donation_form() &&
1534 ( 'custom' !== $donation_meta['price_id'] ) &&
1535 $form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1536 ) {
1537 $donation_meta['price_id'] = 'custom';
1538 give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1539 give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1540 }
1541 }
1542
1543 wp_reset_postdata();
1544 } else {
1545 // Update Ran Successfully.
1546 give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1547 }
1548 }
1549
1550 /**
1551 * Upgrade Routine - Removed Give Worker caps.
1552 *
1553 * See: https://github.com/WordImpress/Give/issues/2476
1554 *
1555 * @since 1.8.18
1556 */
1557 function give_v1818_give_worker_role_cleanup(){
1558
1559 /* @var Give_Updates $give_updates */
1560 $give_updates = Give_Updates::get_instance();
1561
1562 global $wp_roles;
1563
1564 if( ! ( $wp_roles instanceof WP_Roles ) ) {
1565 return;
1566 }
1567
1568 // Remove Capabilities to user roles as required.
1569 $remove_caps = array(
1570 'give_worker' => array(
1571 'delete_give_payments',
1572 'delete_others_give_payments',
1573 'delete_private_give_payments',
1574 'delete_published_give_payments',
1575 'edit_others_give_payments',
1576 'edit_private_give_payments',
1577 'edit_published_give_payments',
1578 'read_private_give_payments',
1579 ),
1580 );
1581
1582 foreach ( $remove_caps as $role => $caps ) {
1583 foreach( $caps as $cap ) {
1584 $wp_roles->remove_cap( $role, $cap );
1585 }
1586 }
1587
1588 $give_updates->percentage = 100;
1589
1590 // Create Give plugin roles.
1591 $roles = new Give_Roles();
1592 $roles->add_roles();
1593 $roles->add_caps();
1594
1595 give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1596 }
1597
1598 /**
1599 *
1600 * Upgrade form metadata for new metabox settings.
1601 *
1602 * @since 2.0
1603 * @return void
1604 */
1605 function give_v20_upgrades_form_metadata_callback() {
1606 $give_updates = Give_Updates::get_instance();
1607
1608 // form query
1609 $forms = new WP_Query( array(
1610 'paged' => $give_updates->step,
1611 'status' => 'any',
1612 'order' => 'ASC',
1613 'post_type' => 'give_forms',
1614 'posts_per_page' => 100,
1615 )
1616 );
1617
1618 if ( $forms->have_posts() ) {
1619 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1620
1621 while ( $forms->have_posts() ) {
1622 $forms->the_post();
1623 global $post;
1624
1625 // Update offline instruction email notification status.
1626 $offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1627 $offline_instruction_notification_status = give_is_setting_enabled( $offline_instruction_notification_status, array(
1628 'enabled',
1629 'global',
1630 ) )
1631 ? $offline_instruction_notification_status
1632 : 'global';
1633 update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1634
1635 // Update offline instruction email message.
1636 update_post_meta(
1637 get_the_ID(),
1638 '_give_offline-donation-instruction_email_message',
1639 get_post_meta(
1640 get_the_ID(),
1641 // @todo: Delete this option later ( version > 2.0 ).
1642 '_give_offline_donation_email',
1643 true
1644 )
1645 );
1646
1647 // Update offline instruction email subject.
1648 update_post_meta(
1649 get_the_ID(),
1650 '_give_offline-donation-instruction_email_subject',
1651 get_post_meta(
1652 get_the_ID(),
1653 // @todo: Delete this option later ( version > 2.0 ).
1654 '_give_offline_donation_subject',
1655 true
1656 )
1657 );
1658
1659
1660 }// End while().
1661
1662 wp_reset_postdata();
1663 } else {
1664 // No more forms found, finish up.
1665 give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1666 }
1667 }
1668
1669
1670 /**
1671 * Upgrade payment metadata for new metabox settings.
1672 *
1673 * @since 2.0
1674 * @global wpdb $wpdb
1675 * @return void
1676 */
1677 function give_v20_upgrades_payment_metadata_callback() {
1678 global $wpdb;
1679 $give_updates = Give_Updates::get_instance();
1680
1681 // form query
1682 $forms = new WP_Query( array(
1683 'paged' => $give_updates->step,
1684 'status' => 'any',
1685 'order' => 'ASC',
1686 'post_type' => 'give_payment',
1687 'posts_per_page' => 100,
1688 )
1689 );
1690
1691 if ( $forms->have_posts() ) {
1692 $give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1693
1694 while ( $forms->have_posts() ) {
1695 $forms->the_post();
1696 global $post;
1697
1698 // Split _give_payment_meta meta.
1699 // @todo Remove _give_payment_meta after releases 2.0
1700 $payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1701
1702 if ( ! empty( $payment_meta ) ) {
1703 _give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1704 }
1705
1706 $deprecated_meta_keys = array(
1707 '_give_payment_customer_id' => '_give_payment_donor_id',
1708 '_give_payment_user_email' => '_give_payment_donor_email',
1709 '_give_payment_user_ip' => '_give_payment_donor_ip',
1710 );
1711
1712 foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
1713 // Do not add new meta key if already exist.
1714 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 ) ) ) {
1715 continue;
1716 }
1717
1718 $wpdb->insert(
1719 $wpdb->postmeta,
1720 array(
1721 'post_id' => $post->ID,
1722 'meta_key' => $new_meta_key,
1723 'meta_value' => give_get_meta( $post->ID, $old_meta_key, true )
1724 )
1725 );
1726 }
1727
1728 // Bailout
1729 if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
1730 /* @var Give_Donor $donor */
1731 $donor = new Give_Donor( $donor_id );
1732
1733 $address['line1'] = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
1734 $address['line2'] = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
1735 $address['city'] = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
1736 $address['state'] = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
1737 $address['zip'] = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
1738 $address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
1739
1740 // Save address.
1741 $donor->add_address( 'billing[]', $address );
1742 }
1743
1744 }// End while().
1745
1746 wp_reset_postdata();
1747 } else {
1748 // @todo Delete user id meta after releases 2.0
1749 // $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1750
1751 // No more forms found, finish up.
1752 give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1753 }
1754 }
1755
1756
1757 /**
1758 * Upgrade logs data.
1759 *
1760 * @since 2.0
1761 * @return void
1762 */
1763 function give_v20_logs_upgrades_callback() {
1764 global $wpdb;
1765 $give_updates = Give_Updates::get_instance();
1766
1767 // form query
1768 $forms = new WP_Query( array(
1769 'paged' => $give_updates->step,
1770 'order' => 'DESC',
1771 'post_type' => 'give_log',
1772 'post_status' => 'any',
1773 'posts_per_page' => 100,
1774 )
1775 );
1776
1777 if ( $forms->have_posts() ) {
1778 $give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1779
1780 while ( $forms->have_posts() ) {
1781 $forms->the_post();
1782 global $post;
1783
1784 if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
1785 continue;
1786 }
1787
1788 $term = get_the_terms( $post->ID, 'give_log_type' );
1789 $term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1790 $term_name = ! empty( $term ) ? $term->slug : '';
1791
1792 $log_data = array(
1793 'ID' => $post->ID,
1794 'log_title' => $post->post_title,
1795 'log_content' => $post->post_content,
1796 'log_parent' => 0,
1797 'log_type' => $term_name,
1798 'log_date' => $post->post_date,
1799 'log_date_gmt' => $post->post_date_gmt,
1800 );
1801 $log_meta = array();
1802
1803 if ( $old_log_meta = get_post_meta( $post->ID ) ) {
1804 foreach ( $old_log_meta as $meta_key => $meta_value ) {
1805 switch ( $meta_key ) {
1806 case '_give_log_payment_id':
1807 $log_data['log_parent'] = current( $meta_value );
1808 $log_meta['_give_log_form_id'] = $post->post_parent;
1809 break;
1810
1811 default:
1812 $log_meta[ $meta_key ] = current( $meta_value );
1813 }
1814 }
1815 }
1816
1817 if ( 'api_request' === $term_name ) {
1818 $log_meta['_give_log_api_query'] = $post->post_excerpt;
1819 }
1820
1821 $wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
1822
1823 if ( ! empty( $log_meta ) ) {
1824 foreach ( $log_meta as $meta_key => $meta_value ) {
1825 Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1826 }
1827 }
1828
1829 $logIDs[] = $post->ID;
1830 }// End while().
1831
1832 wp_reset_postdata();
1833 } else {
1834 // @todo: Delete terms and taxonomy after releases 2.0.
1835 /*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1836 if ( ! empty( $terms ) ) {
1837 foreach ( $terms as $term ) {
1838 wp_delete_term( $term, 'give_log_type' );
1839 }
1840 }*/
1841
1842 // @todo: Delete logs after releases 2.0.
1843 /*$logIDs = get_posts( array(
1844 'order' => 'DESC',
1845 'post_type' => 'give_log',
1846 'post_status' => 'any',
1847 'posts_per_page' => - 1,
1848 'fields' => 'ids',
1849 )
1850 );*/
1851
1852 /*if ( ! empty( $logIDs ) ) {
1853 foreach ( $logIDs as $log ) {
1854 // Delete term relationship and posts.
1855 wp_delete_object_term_relationships( $log, 'give_log_type' );
1856 wp_delete_post( $log, true );
1857 }
1858 }*/
1859
1860 // @todo: Unregister taxonomy after releases 2.0.
1861 /*unregister_taxonomy( 'give_log_type' );*/
1862
1863 // Delete log cache.
1864 Give()->logs->delete_cache();
1865
1866 // No more forms found, finish up.
1867 give_set_upgrade_complete( 'v20_logs_upgrades' );
1868 }
1869 }
1870
1871
1872 /**
1873 * Move payment and form metadata to new table
1874 *
1875 * @since 2.0
1876 * @return void
1877 */
1878 function give_v20_move_metadata_into_new_table_callback() {
1879 global $wpdb;
1880 $give_updates = Give_Updates::get_instance();
1881
1882 // form query
1883 $payments = new WP_Query( array(
1884 'paged' => $give_updates->step,
1885 'status' => 'any',
1886 'order' => 'ASC',
1887 'post_type' => array( 'give_forms', 'give_payment' ),
1888 'posts_per_page' => 100,
1889 )
1890 );
1891
1892 if ( $payments->have_posts() ) {
1893 $give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
1894
1895 while ( $payments->have_posts() ) {
1896 $payments->the_post();
1897 global $post;
1898
1899 $meta_data = $wpdb->get_results(
1900 $wpdb->prepare(
1901 "SELECT * FROM $wpdb->postmeta where post_id=%d",
1902 get_the_ID()
1903 ),
1904 ARRAY_A
1905 );
1906
1907 if ( ! empty( $meta_data ) ) {
1908 foreach ( $meta_data as $index => $data ) {
1909 // Check for duplicate meta values.
1910 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 ) ) {
1911 continue;
1912 }
1913
1914 switch ( $post->post_type ) {
1915 case 'give_forms':
1916 $data['form_id'] = $data['post_id'];
1917 unset( $data['post_id'] );
1918
1919 Give()->form_meta->insert( $data );
1920 // @todo: delete form meta from post meta table after releases 2.0.
1921 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
1922
1923 break;
1924
1925 case 'give_payment':
1926 $data['payment_id'] = $data['post_id'];
1927 unset( $data['post_id'] );
1928
1929 Give()->payment_meta->insert( $data );
1930
1931 // @todo: delete donation meta from post meta table after releases 2.0.
1932 /*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
1933
1934 break;
1935 }
1936 }
1937 }
1938
1939 }// End while().
1940
1941 wp_reset_postdata();
1942 } else {
1943 // No more forms found, finish up.
1944 give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
1945 }
1946
1947 }
1948
1949 /**
1950 * Upgrade routine for splitting donor name into first name and last name.
1951 *
1952 * @since 2.0
1953 *
1954 * @return void
1955 */
1956 function give_v20_upgrades_donor_name() {
1957 /* @var Give_Updates $give_updates */
1958 $give_updates = Give_Updates::get_instance();
1959
1960 $donors = Give()->donors->get_donors( array(
1961 'paged' => $give_updates->step,
1962 'number' => 100,
1963 ) );
1964
1965 if ( $donors ) {
1966 $give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
1967 // Loop through Donors
1968 foreach ( $donors as $donor ) {
1969
1970 $donor_name = explode( ' ', $donor->name, 2 );
1971 $donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
1972 $donor_last_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
1973
1974 // If first name meta of donor is not created, then create it.
1975 if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
1976 Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
1977 }
1978
1979 // If last name meta of donor is not created, then create it.
1980 if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
1981 Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
1982 }
1983
1984 // If Donor is connected with WP User then update user meta.
1985 if ( $donor->user_id ) {
1986 if ( isset( $donor_name[0] ) ) {
1987 update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
1988 }
1989 if ( isset( $donor_name[1] ) ) {
1990 update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
1991 }
1992 }
1993 }
1994
1995 } else {
1996 // The Update Ran.
1997 give_set_upgrade_complete( 'v20_upgrades_donor_name' );
1998 }
1999
2000 }
2001
2002 /**
2003 * Upgrade routine for user addresses.
2004 *
2005 * @since 2.0
2006 * @global wpdb $wpdb
2007 *
2008 * @return void
2009 */
2010 function give_v20_upgrades_user_address() {
2011 global $wpdb;
2012
2013 /* @var Give_Updates $give_updates */
2014 $give_updates = Give_Updates::get_instance();
2015
2016 /* @var WP_User_Query $user_query */
2017 $user_query = new WP_User_Query(
2018 array(
2019 'number' => 100,
2020 'paged' => $give_updates->step,
2021 )
2022 );
2023
2024 $users = $user_query->get_results();
2025
2026 if ( $users ) {
2027 $give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2028
2029 // Loop through Donors
2030 foreach ( $users as $user ) {
2031 /* @var Give_Donor $donor */
2032 $donor = new Give_Donor( $user->ID, true );
2033
2034 if ( ! $donor->id ) {
2035 continue;
2036 }
2037
2038 $address = $wpdb->get_var(
2039 $wpdb->prepare(
2040 "
2041 SELECT meta_value FROM {$wpdb->usermeta}
2042 WHERE user_id=%s
2043 AND meta_key=%s
2044 ",
2045 $user->ID,
2046 '_give_user_address'
2047 )
2048 );
2049
2050 if ( ! empty( $address ) ) {
2051 $address = maybe_unserialize( $address );
2052 $donor->add_address( 'personal', $address );
2053 $donor->add_address( 'billing[]', $address );
2054
2055
2056 // @todo: delete _give_user_address from user meta after releases 2.0.
2057 /*delete_user_meta( $user->ID, '_give_user_address' );*/
2058 }
2059 }
2060
2061 } else {
2062 // The Update Ran.
2063 give_set_upgrade_complete( 'v20_upgrades_user_address' );
2064 }
2065
2066 }
2067
2068 /**
2069 * Upgrade logs data.
2070 *
2071 * @since 2.0
2072 * @global wpdb $wpdb
2073 * @return void
2074 */
2075 function give_v20_rename_donor_tables_callback() {
2076 global $wpdb;
2077
2078 /* @var Give_Updates $give_updates */
2079 $give_updates = Give_Updates::get_instance();
2080
2081 $tables = array(
2082 "{$wpdb->prefix}give_customers" => "{$wpdb->prefix}give_donors",
2083 "{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2084 );
2085
2086 // Alter customer table
2087 foreach ( $tables as $old_table => $new_table ) {
2088 if (
2089 $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $old_table ) ) &&
2090 ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $new_table ) )
2091 ) {
2092 $wpdb->query( "ALTER TABLE {$old_table} RENAME TO {$new_table}" );
2093
2094 if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2095 $wpdb->query( "ALTER TABLE {$new_table} CHANGE COLUMN customer_id donor_id bigint(20)" );
2096 }
2097 }
2098 }
2099
2100 $give_updates->percentage = 100;
2101
2102 // No more forms found, finish up.
2103 give_set_upgrade_complete( 'v20_rename_donor_tables' );
2104
2105 // Re initiate donor classes.
2106 Give()->donors = new Give_DB_Donors();
2107 Give()->donor_meta = new Give_DB_Donor_Meta();
2108 }