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