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