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