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