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