| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import Functions |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Functions |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.8.14 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get the Import report of the donations |
| 19 |
* |
| 20 |
* @since 1.8.13 |
| 21 |
*/ |
| 22 |
function give_import_donation_report() { |
| 23 |
return get_option( 'give_import_donation_report', [] ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Update the Import report of the donations |
| 29 |
* |
| 30 |
* @since 1.8.13 |
| 31 |
*/ |
| 32 |
function give_import_donation_report_update( $value = [] ) { |
| 33 |
update_option( 'give_import_donation_report', $value, false ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Delete the Import report of the donations |
| 38 |
* |
| 39 |
* @since 1.8.13 |
| 40 |
*/ |
| 41 |
function give_import_donation_report_reset() { |
| 42 |
update_option( 'give_import_donation_report', [], false ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Give get form data from csv if not then create and form and return the form value. |
| 47 |
* |
| 48 |
* @since 1.8.13. |
| 49 |
* |
| 50 |
* @param $data . |
| 51 |
* |
| 52 |
* @return array|bool|Give_Donate_Form|int|null|WP_Post |
| 53 |
*/ |
| 54 |
function give_import_get_form_data_from_csv( $data, $import_setting = [] ) { |
| 55 |
$new_form = false; |
| 56 |
$dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false; |
| 57 |
|
| 58 |
// Get the import report |
| 59 |
$report = give_import_donation_report(); |
| 60 |
|
| 61 |
$form = false; |
| 62 |
$meta = []; |
| 63 |
|
| 64 |
if ( ! empty( $data['form_id'] ) ) { |
| 65 |
$form = new Give_Donate_Form( $data['form_id'] ); |
| 66 |
// Add support to older php version. |
| 67 |
$form_id = $form->get_ID(); |
| 68 |
if ( empty( $form_id ) ) { |
| 69 |
$form = false; |
| 70 |
} else { |
| 71 |
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( false === $form && ! empty( $data['form_title'] ) ) { |
| 76 |
$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' ); |
| 77 |
|
| 78 |
if ( ! empty( $form->ID ) ) { |
| 79 |
|
| 80 |
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 ); |
| 81 |
|
| 82 |
$form = new Give_Donate_Form( $form->ID ); |
| 83 |
} else { |
| 84 |
$form = new Give_Donate_Form(); |
| 85 |
$args = [ |
| 86 |
'post_title' => $data['form_title'], |
| 87 |
'post_status' => 'publish', |
| 88 |
]; |
| 89 |
|
| 90 |
if ( empty( $dry_run ) ) { |
| 91 |
$form = $form->create( $args ); |
| 92 |
} |
| 93 |
|
| 94 |
$report['create_form'] = ( ! empty( $report['create_form'] ) ? ( absint( $report['create_form'] ) + 1 ) : 1 ); |
| 95 |
$new_form = true; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' ); |
| 100 |
if ( ! empty( $form->ID ) ) { |
| 101 |
$form = new Give_Donate_Form( $form->ID ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! empty( $form ) && $form->get_ID() && ! empty( $data['form_level'] ) && empty( $dry_run ) ) { |
| 106 |
|
| 107 |
$price_option = 'set'; |
| 108 |
$form_level = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) ); |
| 109 |
|
| 110 |
if ( 'custom' !== $form_level ) { |
| 111 |
$prices = (array) $form->get_prices(); |
| 112 |
$price_text = []; |
| 113 |
foreach ( $prices as $key => $price ) { |
| 114 |
if ( isset( $price['_give_id']['level_id'] ) ) { |
| 115 |
$price_text[ $price['_give_id']['level_id'] ] = ( ! empty( $price['_give_text'] ) ? strtolower( preg_replace( '/\s+/', '', $price['_give_text'] ) ) : '' ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! in_array( $form_level, $price_text ) ) { |
| 120 |
|
| 121 |
// For generating unquiet level id. |
| 122 |
$count = 1; |
| 123 |
$new_level = count( $prices ) + $count; |
| 124 |
while ( array_key_exists( $new_level, $price_text ) ) { |
| 125 |
$count ++; |
| 126 |
$new_level = count( $prices ) + $count; |
| 127 |
} |
| 128 |
|
| 129 |
$multi_level_donations = [ |
| 130 |
[ |
| 131 |
'_give_id' => [ |
| 132 |
'level_id' => $new_level, |
| 133 |
], |
| 134 |
'_give_amount' => give_sanitize_amount_for_db( $data['amount'] ), |
| 135 |
'_give_text' => $data['form_level'], |
| 136 |
], |
| 137 |
]; |
| 138 |
|
| 139 |
$price_text[ $new_level ] = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) ); |
| 140 |
|
| 141 |
if ( ! empty( $prices ) && is_array( $prices ) && ! empty( $prices[0] ) ) { |
| 142 |
// Sort $prices by amount in ascending order. |
| 143 |
$prices = wp_list_sort( $prices, '_give_amount', 'ASC' ); |
| 144 |
} else { |
| 145 |
$prices = $multi_level_donations; |
| 146 |
} |
| 147 |
|
| 148 |
// Unset _give_default key from $prices. |
| 149 |
foreach ( $prices as $key => $price ) { |
| 150 |
if ( isset( $prices[ $key ]['_give_default'] ) ) { |
| 151 |
unset( $prices[ $key ]['_give_default'] ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Set the first $price of the $prices as default. |
| 156 |
$prices[0]['_give_default'] = 'default'; |
| 157 |
} |
| 158 |
$form->price_id = array_search( $form_level, $price_text ); |
| 159 |
|
| 160 |
$donation_levels_amounts = wp_list_pluck( $prices, '_give_amount' ); |
| 161 |
$min_amount = min( $donation_levels_amounts ); |
| 162 |
$max_amount = max( $donation_levels_amounts ); |
| 163 |
|
| 164 |
$meta = [ |
| 165 |
'_give_levels_minimum_amount' => $min_amount, |
| 166 |
'_give_levels_maximum_amount' => $max_amount, |
| 167 |
'_give_donation_levels' => array_values( $prices ), |
| 168 |
]; |
| 169 |
|
| 170 |
$price_option = 'multi'; |
| 171 |
} else { |
| 172 |
$form->price_id = 'custom'; |
| 173 |
} |
| 174 |
|
| 175 |
$defaults = [ |
| 176 |
'_give_set_price' => give_sanitize_amount_for_db( $data['amount'] ), |
| 177 |
]; |
| 178 |
|
| 179 |
// If new form is created. |
| 180 |
if ( ! empty( $new_form ) ) { |
| 181 |
$new_form = [ |
| 182 |
'_give_custom_amount_text' => ( ! empty( $data['form_custom_amount_text'] ) ? $data['form_custom_amount_text'] : __( 'Custom Amount', 'give' ) ), |
| 183 |
'_give_logged_in_only' => 'enabled', |
| 184 |
'_give_custom_amount' => 'enabled', |
| 185 |
'_give_payment_import' => true, |
| 186 |
'_give_display_style' => 'radios', |
| 187 |
'_give_payment_display' => 'onpage', |
| 188 |
'give_product_notes' => 'Donation Notes', |
| 189 |
'_give_product_type' => 'default', |
| 190 |
'_give_default_gateway' => 'global', |
| 191 |
'_give_customize_offline_donations' => 'global', |
| 192 |
'_give_show_register_form' => 'both', |
| 193 |
'_give_price_option' => $price_option, |
| 194 |
]; |
| 195 |
$defaults = wp_parse_args( $defaults, $new_form ); |
| 196 |
} |
| 197 |
|
| 198 |
$meta = wp_parse_args( $meta, $defaults ); |
| 199 |
|
| 200 |
foreach ( $meta as $key => $value ) { |
| 201 |
give_update_meta( $form->get_ID(), $key, $value ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// update the report |
| 206 |
give_import_donation_report_update( $report ); |
| 207 |
|
| 208 |
return $form; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Give get user details if not then create a user. Used in Import Donation CSV. |
| 213 |
* |
| 214 |
* @since 1.8.13 |
| 215 |
* |
| 216 |
* @param $data |
| 217 |
* |
| 218 |
* @return bool|false|WP_User |
| 219 |
*/ |
| 220 |
function give_import_get_user_from_csv( $data, $import_setting = [] ) { |
| 221 |
$report = give_import_donation_report(); |
| 222 |
$dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false; |
| 223 |
$dry_run_donor_create = false; |
| 224 |
$donor_data = []; |
| 225 |
$donor_id = false; |
| 226 |
|
| 227 |
// check if donor id is not empty |
| 228 |
if ( ! empty( $data['donor_id'] ) ) { |
| 229 |
$donor_data = new Give_Donor( (int) $data['donor_id'] ); |
| 230 |
if ( ! empty( $donor_data->id ) ) { |
| 231 |
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if ( empty( $donor_data->id ) && ! empty( $data['user_id'] ) ) { |
| 236 |
$user_id = (int) $data['user_id']; |
| 237 |
$donor_data = new Give_Donor( $user_id, true ); |
| 238 |
|
| 239 |
if ( empty( $donor_data->id ) ) { |
| 240 |
$donor_data = get_user_by( 'id', $user_id ); |
| 241 |
|
| 242 |
// if no wp user is found then no donor is create with that user id |
| 243 |
if ( ! empty( $donor_data->ID ) ) { |
| 244 |
|
| 245 |
if ( empty( $dry_run ) ) { |
| 246 |
$first_name = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $donor_data->user_nicename ); |
| 247 |
$last_name = ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ( $lastname = get_user_meta( $donor_data->ID, 'last_name', true ) ) ? $lastname : '' ) ); |
| 248 |
$name = $first_name . ' ' . $last_name; |
| 249 |
$user_email = $donor_data->user_email; |
| 250 |
$donor_args = [ |
| 251 |
'name' => $name, |
| 252 |
'email' => $user_email, |
| 253 |
'user_id' => $user_id, |
| 254 |
]; |
| 255 |
|
| 256 |
$donor_data = new Give_Donor(); |
| 257 |
$donor_data->create( $donor_args ); |
| 258 |
|
| 259 |
// Adding notes that donor is being imported from CSV. |
| 260 |
$current_user = wp_get_current_user(); |
| 261 |
$donor_data->add_note( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ); |
| 262 |
|
| 263 |
// Add is used to ensure duplicate emails are not added |
| 264 |
if ( $user_email != $data['email'] && ! empty( $data['email'] ) ) { |
| 265 |
$donor_data->add_meta( 'additional_email', $data['email'] ); |
| 266 |
} |
| 267 |
} else { |
| 268 |
$dry_run_donor_create = true; |
| 269 |
$donor_data = [ 'id' => 1 ]; |
| 270 |
} |
| 271 |
|
| 272 |
$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 ); |
| 273 |
} elseif ( $dry_run ) { |
| 274 |
$donor_data = []; |
| 275 |
} |
| 276 |
} else { |
| 277 |
// Add is used to ensure duplicate emails are not added |
| 278 |
if ( $donor_data->email != $data['email'] && empty( $dry_run ) ) { |
| 279 |
$donor_data->add_meta( 'additional_email', ( ! empty( $data['email'] ) ? $data['email'] : $donor_data->email ) ); |
| 280 |
} |
| 281 |
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if ( empty( $donor_data->id ) && ! empty( $data['email'] ) && empty( $dry_run_donor_create ) ) { |
| 286 |
|
| 287 |
$donor_data = new Give_Donor( $data['email'] ); |
| 288 |
if ( empty( $donor_data->id ) ) { |
| 289 |
$donor_data = get_user_by( 'email', $data['email'] ); |
| 290 |
|
| 291 |
if ( empty( $donor_data->ID ) && isset( $import_setting['create_user'] ) && 1 === absint( $import_setting['create_user'] ) ) { |
| 292 |
$data['first_name'] = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $data['email'] ); |
| 293 |
$data['last_name'] = ( ! empty( $data['last_name'] ) ? $data['last_name'] : '' ); |
| 294 |
$give_role = (array) give_get_option( 'donor_default_user_role', get_option( 'default_role', ( ( $give_donor = wp_roles()->is_role( 'give_donor' ) ) && ! empty( $give_donor ) ? 'give_donor' : 'subscriber' ) ) ); |
| 295 |
$donor_args = [ |
| 296 |
'user_login' => $data['email'], |
| 297 |
'user_email' => $data['email'], |
| 298 |
'user_registered' => date( 'Y-m-d H:i:s' ), |
| 299 |
'user_first' => $data['first_name'], |
| 300 |
'user_last' => $data['last_name'], |
| 301 |
'user_pass' => wp_generate_password( 8, true ), |
| 302 |
'role' => $give_role, |
| 303 |
]; |
| 304 |
|
| 305 |
/** |
| 306 |
* Filter to modify user data before new user id register. |
| 307 |
* |
| 308 |
* @since 1.8.13 |
| 309 |
*/ |
| 310 |
$donor_args = (array) apply_filters( 'give_import_insert_user_args', $donor_args, $data, $import_setting ); |
| 311 |
|
| 312 |
if ( empty( $dry_run ) ) { |
| 313 |
|
| 314 |
// This action was added to remove the login when using the give register function. |
| 315 |
add_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 ); |
| 316 |
$donor_id = give_register_and_login_new_user( $donor_args ); |
| 317 |
remove_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 ); |
| 318 |
|
| 319 |
$donor_data = new Give_Donor( $donor_id, true ); |
| 320 |
$donor_data->update_meta( '_give_payment_import', true ); |
| 321 |
|
| 322 |
} else { |
| 323 |
$dry_run_donor_create = true; |
| 324 |
$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 ); |
| 325 |
} |
| 326 |
} else { |
| 327 |
$donor_id = ( ! empty( $donor_data->ID ) ? $donor_data->ID : false ); |
| 328 |
} |
| 329 |
|
| 330 |
if ( empty( $dry_run_donor_create ) && ( ! empty( $donor_id ) || ( isset( $import_setting['create_user'] ) && 0 === absint( $import_setting['create_user'] ) ) ) ) { |
| 331 |
$donor_data = new Give_Donor( $donor_id, true ); |
| 332 |
|
| 333 |
if ( empty( $donor_data->id ) ) { |
| 334 |
|
| 335 |
if ( ! empty( $data['form_id'] ) ) { |
| 336 |
$form = new Give_Donate_Form( $data['form_id'] ); |
| 337 |
} |
| 338 |
|
| 339 |
if ( empty( $dry_run ) ) { |
| 340 |
$payment_title = ( isset( $data['form_title'] ) ? $data['form_title'] : ( isset( $form ) ? $form->get_name() : __( 'New Form', 'give' ) ) ); |
| 341 |
$donor_args = [ |
| 342 |
'name' => ! is_email( $payment_title ) ? $data['first_name'] . ' ' . $data['last_name'] : '', |
| 343 |
'email' => $data['email'], |
| 344 |
]; |
| 345 |
if ( ! empty( $donor_id ) ) { |
| 346 |
$donor_args['user_id'] = $donor_id; |
| 347 |
} |
| 348 |
$donor_data->create( $donor_args ); |
| 349 |
|
| 350 |
// Adding notes that donor is being imported from CSV. |
| 351 |
$current_user = wp_get_current_user(); |
| 352 |
$donor_data->add_note( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ); |
| 353 |
} else { |
| 354 |
$dry_run_donor_create = true; |
| 355 |
} |
| 356 |
$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 ); |
| 357 |
} else { |
| 358 |
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
| 359 |
} |
| 360 |
} |
| 361 |
} else { |
| 362 |
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
// update the report |
| 367 |
give_import_donation_report_update( $report ); |
| 368 |
|
| 369 |
return $donor_data; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Return the option that are default options. |
| 374 |
* |
| 375 |
* @since 1.8.13 |
| 376 |
*/ |
| 377 |
function give_import_default_options() { |
| 378 |
/** |
| 379 |
* Filter to modify default option in the import dropdown |
| 380 |
* |
| 381 |
* @since 1.8.13 |
| 382 |
* |
| 383 |
* @return array |
| 384 |
*/ |
| 385 |
return (array) apply_filters( |
| 386 |
'give_import_default_options', |
| 387 |
[ |
| 388 |
'' => __( 'Do not import', 'give' ), |
| 389 |
] |
| 390 |
); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Return the option that are related to donations. |
| 395 |
* |
| 396 |
* @since 1.8.13 |
| 397 |
*/ |
| 398 |
function give_import_donations_options() { |
| 399 |
/** |
| 400 |
* Filter to modify donations option in the import dropdown |
| 401 |
* |
| 402 |
* @since 1.8.13 |
| 403 |
* |
| 404 |
* @return array |
| 405 |
*/ |
| 406 |
return (array) apply_filters( |
| 407 |
'give_import_donations_options', |
| 408 |
[ |
| 409 |
'id' => __( 'Donation ID', 'give' ), |
| 410 |
'amount' => [ |
| 411 |
__( 'Donation Amount', 'give' ), |
| 412 |
__( 'Amount', 'give' ), |
| 413 |
__( 'Donation Total', 'give' ), |
| 414 |
__( 'Total', 'give' ), |
| 415 |
], |
| 416 |
'currency' => [ |
| 417 |
__( 'Donation Currencies', 'give' ), |
| 418 |
__( 'Currencies', 'give' ), |
| 419 |
__( 'Currencies Code', 'give' ), |
| 420 |
__( 'Currency Code', 'give' ), |
| 421 |
__( 'Code', 'give' ), |
| 422 |
], |
| 423 |
'post_date' => [ |
| 424 |
__( 'Donation Date', 'give' ), |
| 425 |
__( 'Date', 'give' ), |
| 426 |
], |
| 427 |
'post_time' => [ |
| 428 |
__( 'Donation Time', 'give' ), |
| 429 |
__( 'Time', 'give' ), |
| 430 |
], |
| 431 |
'title_prefix' => [ |
| 432 |
__( 'Title Prefix', 'give' ), |
| 433 |
__( 'Prefix', 'give' ), |
| 434 |
], |
| 435 |
'first_name' => [ |
| 436 |
__( 'Donor First Name', 'give' ), |
| 437 |
__( 'First Name', 'give' ), |
| 438 |
__( 'Name', 'give' ), |
| 439 |
__( 'First', 'give' ), |
| 440 |
], |
| 441 |
'last_name' => [ |
| 442 |
__( 'Donor Last Name', 'give' ), |
| 443 |
__( 'Last Name', 'give' ), |
| 444 |
__( 'Last', 'give' ), |
| 445 |
], |
| 446 |
'company_name' => [ |
| 447 |
__( 'Company Name', 'give' ), |
| 448 |
__( 'Donor Company Name', 'give' ), |
| 449 |
__( 'Donor Company', 'give' ), |
| 450 |
__( 'Company', 'give' ), |
| 451 |
], |
| 452 |
'line1' => [ |
| 453 |
__( 'Address 1', 'give' ), |
| 454 |
__( 'Address', 'give' ), |
| 455 |
], |
| 456 |
'line2' => __( 'Address 2', 'give' ), |
| 457 |
'city' => __( 'City', 'give' ), |
| 458 |
'state' => [ |
| 459 |
__( 'State', 'give' ), |
| 460 |
__( 'Province', 'give' ), |
| 461 |
__( 'County', 'give' ), |
| 462 |
__( 'Region', 'give' ), |
| 463 |
], |
| 464 |
'country' => __( 'Country', 'give' ), |
| 465 |
'zip' => [ |
| 466 |
__( 'Zip Code', 'give' ), |
| 467 |
__( 'Zip', 'give' ), |
| 468 |
__( 'zipcode', 'give' ), |
| 469 |
__( 'Postal Code', 'give' ), |
| 470 |
__( 'Postal', 'give' ), |
| 471 |
], |
| 472 |
'email' => [ |
| 473 |
__( 'Donor Email', 'give' ), |
| 474 |
__( 'Email', 'give' ), |
| 475 |
__( 'Email Address', 'give' ), |
| 476 |
], |
| 477 |
'post_status' => [ |
| 478 |
__( 'Donation Status', 'give' ), |
| 479 |
__( 'Status', 'give' ), |
| 480 |
], |
| 481 |
'gateway' => [ |
| 482 |
__( 'Payment Method', 'give' ), |
| 483 |
__( 'Method', 'give' ), |
| 484 |
__( 'Payment Gateway', 'give' ), |
| 485 |
__( 'Gateway', 'give' ), |
| 486 |
], |
| 487 |
'notes' => __( 'Notes', 'give' ), |
| 488 |
'mode' => [ |
| 489 |
__( 'Payment Mode', 'give' ), |
| 490 |
__( 'Mode', 'give' ), |
| 491 |
__( 'Test Mode', 'give' ), |
| 492 |
], |
| 493 |
'donor_ip' => __( 'Donor IP Address', 'give' ), |
| 494 |
'post_meta' => __( 'Import as Meta', 'give' ), |
| 495 |
] |
| 496 |
); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Return the option that are related to donations. |
| 501 |
* |
| 502 |
* @since 1.8.13 |
| 503 |
*/ |
| 504 |
function give_import_donor_options() { |
| 505 |
/** |
| 506 |
* Filter to modify donors option in the import dropdown |
| 507 |
* |
| 508 |
* @since 1.8.13 |
| 509 |
* |
| 510 |
* @return array |
| 511 |
*/ |
| 512 |
return (array) apply_filters( |
| 513 |
'give_import_donor_options', |
| 514 |
[ |
| 515 |
'donor_id' => __( 'Donor ID', 'give' ), |
| 516 |
'user_id' => __( 'User ID', 'give' ), |
| 517 |
] |
| 518 |
); |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Return the option that are related to donations. |
| 523 |
* |
| 524 |
* @since 1.8.13 |
| 525 |
*/ |
| 526 |
function give_import_donation_form_options() { |
| 527 |
/** |
| 528 |
* Filter to modify form option in the import dropdown |
| 529 |
* |
| 530 |
* @since 1.8.13 |
| 531 |
* |
| 532 |
* @return array |
| 533 |
*/ |
| 534 |
return (array) apply_filters( |
| 535 |
'give_import_donation_form_options', |
| 536 |
[ |
| 537 |
'form_title' => [ |
| 538 |
__( 'Donation Form Title', 'give' ), |
| 539 |
__( 'Donation Form', 'give' ), |
| 540 |
__( 'Form Name', 'give' ), |
| 541 |
__( 'Title', 'give' ), |
| 542 |
__( 'Form Title', 'give' ), |
| 543 |
'ignore' => [ |
| 544 |
__( 'Title Prefix', 'give' ), |
| 545 |
__( 'Prefix', 'give' ), |
| 546 |
], |
| 547 |
], |
| 548 |
'form_id' => [ |
| 549 |
__( 'Donation Form ID', 'give' ), |
| 550 |
__( 'Form ID', 'give' ), |
| 551 |
], |
| 552 |
'form_level' => [ |
| 553 |
__( 'Donation Level', 'give' ), |
| 554 |
__( 'Level', 'give' ), |
| 555 |
__( 'Level Title', 'give' ), |
| 556 |
], |
| 557 |
'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ), |
| 558 |
] |
| 559 |
); |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Import CSV in DB |
| 564 |
* |
| 565 |
* @param int $file_id CSV id. |
| 566 |
* @param int $start Start from which csv line. |
| 567 |
* @param int $end End from which csv line. |
| 568 |
* @param string $delimiter CSV delimeter. |
| 569 |
* |
| 570 |
* @return array |
| 571 |
*/ |
| 572 |
function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = 'csv' ) { |
| 573 |
/** |
| 574 |
* Filter to modify delimiter of Import |
| 575 |
* |
| 576 |
* @since 1.8.14 |
| 577 |
* |
| 578 |
* @param string $delimiter |
| 579 |
* |
| 580 |
* @return string $delimiter |
| 581 |
*/ |
| 582 |
$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter ); |
| 583 |
|
| 584 |
$file_dir = give_get_file_data_by_file_id( $file_id ); |
| 585 |
|
| 586 |
return give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter ); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Get raw data from file data |
| 591 |
* |
| 592 |
* @since 2.1 |
| 593 |
* |
| 594 |
* @param $file_dir |
| 595 |
* @param $start |
| 596 |
* @param $end |
| 597 |
* @param $delimiter |
| 598 |
* |
| 599 |
* @return array |
| 600 |
*/ |
| 601 |
function give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter ) { |
| 602 |
$raw_data = []; |
| 603 |
|
| 604 |
$count = 0; |
| 605 |
if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) { |
| 606 |
while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) { |
| 607 |
if ( $count >= $start && $count <= $end ) { |
| 608 |
$raw_data[] = $row; |
| 609 |
} |
| 610 |
$count ++; |
| 611 |
} |
| 612 |
fclose( $handle ); |
| 613 |
} |
| 614 |
|
| 615 |
return $raw_data; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Get content from the attachment id of CSV |
| 620 |
* |
| 621 |
* @since 2.1 |
| 622 |
* |
| 623 |
* @param $file_id |
| 624 |
* |
| 625 |
* @return false|string file content |
| 626 |
*/ |
| 627 |
function give_get_file_data_by_file_id( $file_id ) { |
| 628 |
return get_attached_file( $file_id ); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
/** |
| 633 |
* Remove login when user register with give functions. |
| 634 |
* |
| 635 |
* @since 1.8.13 |
| 636 |
* |
| 637 |
* @param $value |
| 638 |
* |
| 639 |
* @return bool |
| 640 |
*/ |
| 641 |
function give_log_user_in_on_register_callback( $value ) { |
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* Add import Donation forms, donations , donor from CSV to database |
| 647 |
* |
| 648 |
* @since 1.8.13 |
| 649 |
* |
| 650 |
* @param array $raw_key Setup bu user at step 2. |
| 651 |
* @param array $row_data Feilds that are being imported from CSV |
| 652 |
* @param array $main_key First row from the CSV |
| 653 |
* @param array $import_setting Contain the global variable. |
| 654 |
* |
| 655 |
* @return bool |
| 656 |
*/ |
| 657 |
function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = [], $import_setting = [] ) { |
| 658 |
$data = array_combine( $raw_key, $row_data ); |
| 659 |
$price_id = false; |
| 660 |
$donor_id = 0; |
| 661 |
$donor_data = []; |
| 662 |
$form = []; |
| 663 |
$import_setting['create_user'] = isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1; |
| 664 |
$dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false; |
| 665 |
$_dry_run_is_duplicate = false; |
| 666 |
$dry_run_duplicate_form = false; |
| 667 |
$dry_run_duplicate_donor = false; |
| 668 |
$donation_key = empty( $import_setting['donation_key'] ) ? 1 : (int) $import_setting['donation_key']; |
| 669 |
$payment_id = false; |
| 670 |
|
| 671 |
$data = (array) apply_filters( 'give_save_import_donation_to_db', $data ); |
| 672 |
|
| 673 |
$data['amount'] = give_maybe_sanitize_amount( $data['amount'] ); |
| 674 |
$diff = []; |
| 675 |
|
| 676 |
if ( ! empty( $dry_run ) && 1 !== $donation_key ) { |
| 677 |
$csv_raw_data = empty( $import_setting['csv_raw_data'] ) ? [] : $import_setting['csv_raw_data']; |
| 678 |
$donors_list = empty( $import_setting['donors_list'] ) ? [] : $import_setting['donors_list']; |
| 679 |
$key = $donation_key - 1; |
| 680 |
for ( $i = 0; $i < $key; $i ++ ) { |
| 681 |
$csv_data = array_combine( $raw_key, $csv_raw_data[ $i ] ); |
| 682 |
$csv_data['amount'] = give_maybe_sanitize_amount( $csv_data['amount'] ); |
| 683 |
// check for duplicate donations |
| 684 |
$diff = array_diff( $csv_data, $data ); |
| 685 |
if ( empty( $diff ) ) { |
| 686 |
$_dry_run_is_duplicate = true; |
| 687 |
$dry_run_duplicate_form = true; |
| 688 |
$dry_run_duplicate_donor = true; |
| 689 |
} else { |
| 690 |
// check for duplicate donation form with form id |
| 691 |
if ( ! empty( $csv_data['form_id'] ) && ! empty( $data['form_id'] ) && $csv_data['form_id'] === $data['form_id'] ) { |
| 692 |
$form = new Give_Donate_Form( $data['form_id'] ); |
| 693 |
$form_id = $form->get_ID(); |
| 694 |
if ( ! empty( $form_id ) ) { |
| 695 |
$dry_run_duplicate_form = true; |
| 696 |
} |
| 697 |
} |
| 698 |
// check for duplicate donation form with form title |
| 699 |
if ( empty( $dry_run_duplicate_form ) && ! empty( $csv_data['form_title'] ) && ! empty( $data['form_title'] ) && $csv_data['form_title'] === $data['form_title'] ) { |
| 700 |
$dry_run_duplicate_form = true; |
| 701 |
} |
| 702 |
|
| 703 |
// check for duplicate donor by donor id |
| 704 |
if ( ! empty( $csv_data['donor_id'] ) && ! empty( $data['donor_id'] ) && $csv_data['donor_id'] === $data['donor_id'] ) { |
| 705 |
$donor = array_search( (int) $data['donor_id'], array_column( 'id', $donors_list ) ); |
| 706 |
if ( ! empty( $donor ) ) { |
| 707 |
$dry_run_duplicate_donor = true; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
// check for duplicate donor by user id |
| 712 |
if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['user_id'] ) && ! empty( $data['user_id'] ) && $csv_data['user_id'] === $data['user_id'] ) { |
| 713 |
$donor = array_search( (int) $data['user_id'], array_column( 'user_id', $donors_list ) ); |
| 714 |
if ( ! empty( $donor ) ) { |
| 715 |
$dry_run_duplicate_donor = true; |
| 716 |
} else { |
| 717 |
$donor = get_user_by( 'id', $csv_data['user_id'] ); |
| 718 |
if ( ! empty( $donor->ID ) ) { |
| 719 |
$dry_run_duplicate_donor = true; |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
// check for duplicate donor by donor id |
| 725 |
if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['email'] ) && ! empty( $data['email'] ) && $csv_data['email'] === $data['email'] ) { |
| 726 |
$dry_run_duplicate_donor = true; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
if ( empty( $dry_run_duplicate_donor ) ) { |
| 733 |
// Here come the login function. |
| 734 |
$donor_data = give_import_get_user_from_csv( $data, $import_setting ); |
| 735 |
if ( empty( $dry_run ) ) { |
| 736 |
if ( ! empty( $donor_data->id ) ) { |
| 737 |
$donor_id = $donor_data->id; |
| 738 |
} else { |
| 739 |
return $payment_id; |
| 740 |
} |
| 741 |
} |
| 742 |
} else { |
| 743 |
// Get the report |
| 744 |
$report = give_import_donation_report(); |
| 745 |
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
| 746 |
// update the report |
| 747 |
give_import_donation_report_update( $report ); |
| 748 |
} |
| 749 |
|
| 750 |
if ( empty( $dry_run_duplicate_form ) ) { |
| 751 |
// get form data or register a form data. |
| 752 |
$form = give_import_get_form_data_from_csv( $data, $import_setting ); |
| 753 |
if ( false == $form && empty( $dry_run ) ) { |
| 754 |
return $payment_id; |
| 755 |
} else { |
| 756 |
$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false; |
| 757 |
} |
| 758 |
} else { |
| 759 |
// Get the report |
| 760 |
$report = give_import_donation_report(); |
| 761 |
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 ); |
| 762 |
// update the report |
| 763 |
give_import_donation_report_update( $report ); |
| 764 |
} |
| 765 |
|
| 766 |
// Get the report |
| 767 |
$report = give_import_donation_report(); |
| 768 |
|
| 769 |
$status = give_import_donation_get_status( $data ); |
| 770 |
$country = ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' ); |
| 771 |
$state = ( ! empty( $data['state'] ) ? ( ( $state_code = array_search( $data['state'], give_get_states( $country ) ) ) ? $state_code : $data['state'] ) : '' ); |
| 772 |
|
| 773 |
$address = [ |
| 774 |
'line1' => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ), |
| 775 |
'line2' => ( ! empty( $data['line2'] ) ? give_clean( $data['line2'] ) : '' ), |
| 776 |
'city' => ( ! empty( $data['city'] ) ? give_clean( $data['city'] ) : '' ), |
| 777 |
'zip' => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ), |
| 778 |
'state' => $state, |
| 779 |
'country' => $country, |
| 780 |
]; |
| 781 |
|
| 782 |
$test_mode = [ 'test', 'true' ]; |
| 783 |
$post_date = current_time( 'mysql' ); |
| 784 |
if ( ! empty( $data['post_date'] ) ) { |
| 785 |
if ( ! empty( $data['post_time'] ) ) { |
| 786 |
$post_date = mysql2date( 'Y-m-d', $data['post_date'] ); |
| 787 |
$post_date = mysql2date( 'Y-m-d H:i:s', $post_date . ' ' . $data['post_time'] ); |
| 788 |
} else { |
| 789 |
$post_date = mysql2date( 'Y-m-d H:i:s', $data['post_date'] ); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
// Create payment_data array |
| 794 |
$payment_data = [ |
| 795 |
'donor_id' => $donor_id, |
| 796 |
'price' => $data['amount'], |
| 797 |
'status' => $status, |
| 798 |
'currency' => ! empty( $data['currency'] ) && array_key_exists( $data['currency'], give_get_currencies_list() ) ? $data['currency'] : give_get_currency(), |
| 799 |
'user_info' => [ |
| 800 |
'id' => $donor_id, |
| 801 |
'email' => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ), |
| 802 |
'first_name' => ( ! empty( $data['first_name'] ) ? $data['first_name'] : ( ! empty( $donor_id ) && ( $first_name = get_user_meta( $donor_id, 'first_name', true ) ) ? $first_name : $donor_data->name ) ), |
| 803 |
'last_name' => ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ! empty( $donor_id ) && ( $last_name = get_user_meta( $donor_id, 'last_name', true ) ) ? $last_name : $donor_data->name ) ), |
| 804 |
'address' => $address, |
| 805 |
'title' => ! empty( $data['title_prefix'] ) ? $data['title_prefix'] : '', |
| 806 |
], |
| 807 |
'gateway' => ( ! empty( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ), |
| 808 |
'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : ( method_exists( $form, 'get_name' ) ? $form->get_name() : '' ) ), |
| 809 |
'give_form_id' => method_exists( $form, 'get_ID' ) ? $form->get_ID() : '', |
| 810 |
'give_price_id' => $price_id, |
| 811 |
'purchase_key' => strtolower( md5( uniqid() ) ), |
| 812 |
'user_email' => $data['email'], |
| 813 |
'post_date' => $post_date, |
| 814 |
'mode' => ( ! empty( $data['mode'] ) ? ( in_array( strtolower( $data['mode'] ), $test_mode ) ? 'test' : 'live' ) : ( isset( $import_setting['mode'] ) ? ( true == (bool) $import_setting['mode'] ? 'test' : 'live' ) : ( give_is_test_mode() ? 'test' : 'live' ) ) ), |
| 815 |
]; |
| 816 |
|
| 817 |
/** |
| 818 |
* Filter to modify payment Data before getting imported. |
| 819 |
* |
| 820 |
* @since 2.1.0 |
| 821 |
* |
| 822 |
* @param array $payment_data payment data |
| 823 |
* @param array $payment_data donation data |
| 824 |
* @param array $donor_data donor data |
| 825 |
* @param object $donor_data form object |
| 826 |
* |
| 827 |
* @return array $payment_data payment data |
| 828 |
*/ |
| 829 |
$payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form ); |
| 830 |
|
| 831 |
// Get the report |
| 832 |
$report = give_import_donation_report(); |
| 833 |
|
| 834 |
// Check for duplicate code. |
| 835 |
$donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ); |
| 836 |
if ( false !== $donation_duplicate || ! empty( $_dry_run_is_duplicate ) ) { |
| 837 |
$report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate; |
| 838 |
$report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 ); |
| 839 |
} else { |
| 840 |
|
| 841 |
if ( empty( $dry_run ) ) { |
| 842 |
add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 ); |
| 843 |
add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 ); |
| 844 |
add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 ); |
| 845 |
add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 ); |
| 846 |
add_filter( 'give_is_stop_email_notification', '__return_true' ); |
| 847 |
|
| 848 |
// if it status is other then pending then first change the donation status to pending and after adding the payment meta update the donation status. |
| 849 |
if ( 'pending' !== $status ) { |
| 850 |
unset( $payment_data['status'] ); |
| 851 |
} |
| 852 |
|
| 853 |
$payment_id = give_insert_payment( $payment_data ); |
| 854 |
remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 ); |
| 855 |
remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 ); |
| 856 |
remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 ); |
| 857 |
remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 ); |
| 858 |
remove_filter( 'give_is_stop_email_notification', '__return_true' ); |
| 859 |
|
| 860 |
if ( $payment_id ) { |
| 861 |
|
| 862 |
$payment = new Give_Payment( $payment_id ); |
| 863 |
|
| 864 |
$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 ); |
| 865 |
|
| 866 |
$payment->update_meta( '_give_payment_import', true ); |
| 867 |
|
| 868 |
if ( ! empty( $import_setting['csv'] ) ) { |
| 869 |
$payment->update_meta( '_give_payment_import_id', $import_setting['csv'] ); |
| 870 |
} |
| 871 |
|
| 872 |
// Insert Company Name. |
| 873 |
if ( ! empty( $data['company_name'] ) ) { |
| 874 |
$payment->update_meta( '_give_donation_company', $data['company_name'] ); |
| 875 |
$donor_data->update_meta( '_give_donor_company', $data['company_name'] ); |
| 876 |
} |
| 877 |
|
| 878 |
// Insert Donor IP address. |
| 879 |
if ( ! empty( $data['donor_ip'] ) ) { |
| 880 |
$payment->update_meta( '_give_payment_donor_ip', $data['donor_ip'] ); |
| 881 |
} |
| 882 |
|
| 883 |
// Insert Notes. |
| 884 |
if ( ! empty( $data['notes'] ) ) { |
| 885 |
$payment->add_note( $data['notes'] ); |
| 886 |
} |
| 887 |
|
| 888 |
$meta_exists = array_keys( $raw_key, 'post_meta' ); |
| 889 |
if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) { |
| 890 |
foreach ( $meta_exists as $meta_exist ) { |
| 891 |
if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) { |
| 892 |
$payment->update_meta( $main_key[ $meta_exist ], $row_data[ $meta_exist ] ); |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
// update the donation status if it's other then pending |
| 898 |
if ( 'pending' !== $status ) { |
| 899 |
$payment->update_status( $status ); |
| 900 |
} |
| 901 |
} else { |
| 902 |
$report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 ); |
| 903 |
$payment_id = false; |
| 904 |
} |
| 905 |
|
| 906 |
/** |
| 907 |
* Fire after payment is imported and payment meta is also being imported. |
| 908 |
* |
| 909 |
* @since 2.1.0 |
| 910 |
* |
| 911 |
* @param int $payment payment id |
| 912 |
* @param array $payment_data payment data |
| 913 |
* @param array $payment_data donation data |
| 914 |
* @param array $donor_data donor data |
| 915 |
* @param object $donor_data form object |
| 916 |
*/ |
| 917 |
do_action( 'give_import_after_import_payment', $payment, $payment_data, $data, $donor_data, $form ); |
| 918 |
} else { |
| 919 |
$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 ); |
| 920 |
$payment_id = true; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
// update the report |
| 925 |
give_import_donation_report_update( $report ); |
| 926 |
|
| 927 |
return $payment_id; |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* Get Donation form status |
| 932 |
* |
| 933 |
* @since 2.0.2 |
| 934 |
* |
| 935 |
* @param array $data donation data that is goingt o get imported |
| 936 |
* |
| 937 |
* @return string $status Donation status. |
| 938 |
*/ |
| 939 |
function give_import_donation_get_status( $data ) { |
| 940 |
if ( empty( $data['post_status'] ) ) { |
| 941 |
return 'publish'; |
| 942 |
} |
| 943 |
|
| 944 |
$status = 'publish'; |
| 945 |
|
| 946 |
$donation_status = trim( $data['post_status'] ); |
| 947 |
$donation_status_key = strtolower( preg_replace( '/\s+/', '', $donation_status ) ); |
| 948 |
|
| 949 |
foreach ( give_get_payment_statuses() as $key => $value ) { |
| 950 |
$match = false; |
| 951 |
if ( $key === $donation_status_key ) { |
| 952 |
$match = true; |
| 953 |
} elseif ( stristr( $donation_status, $value ) ) { |
| 954 |
$match = true; |
| 955 |
} |
| 956 |
|
| 957 |
if ( ! empty( $match ) ) { |
| 958 |
$status = $key; |
| 959 |
break; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
return $status; |
| 964 |
} |
| 965 |
|
| 966 |
/** |
| 967 |
* Alter donor information when importing donations from CSV |
| 968 |
* |
| 969 |
* @since 1.8.13 |
| 970 |
* |
| 971 |
* @param $donor |
| 972 |
* @param $payment_id |
| 973 |
* @param $payment_data |
| 974 |
* |
| 975 |
* @return Give_Donor |
| 976 |
*/ |
| 977 |
function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) { |
| 978 |
$old_donor = $donor; |
| 979 |
if ( ! empty( $payment_data['donor_id'] ) ) { |
| 980 |
$donor_id = absint( $payment_data['donor_id'] ); |
| 981 |
$donor = new Give_Donor( $donor_id ); |
| 982 |
if ( ! empty( $donor->id ) ) { |
| 983 |
return $donor; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
return $old_donor; |
| 988 |
} |
| 989 |
|
| 990 |
/* |
| 991 |
* Give update purchase_count of give customer. |
| 992 |
* |
| 993 |
* @since 1.8.13 |
| 994 |
*/ |
| 995 |
function give_import_donation_insert_payment( $payment_id, $payment_data ) { |
| 996 |
// Update Give Customers purchase_count |
| 997 |
if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) { |
| 998 |
$donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true ); |
| 999 |
if ( ! empty( $donor_id ) ) { |
| 1000 |
$donor = new Give_Donor( $donor_id ); |
| 1001 |
$donor->increase_purchase_count(); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* Add author id in in donation post |
| 1008 |
* |
| 1009 |
* @since 1.8.13 |
| 1010 |
*/ |
| 1011 |
function give_donation_import_give_insert_payment_args( $args, $payment_data ) { |
| 1012 |
if ( ! empty( $payment_data['user_info']['id'] ) ) { |
| 1013 |
$args['post_author'] = (int) $payment_data['user_info']['id']; |
| 1014 |
} |
| 1015 |
|
| 1016 |
return $args; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Check if Import donation is duplicate |
| 1021 |
* |
| 1022 |
* @since 1.8.13 |
| 1023 |
*/ |
| 1024 |
function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) { |
| 1025 |
$return = false; |
| 1026 |
if ( ! empty( $data['post_date'] ) ) { |
| 1027 |
$post_date = mysql2date( 'Y-m-d-H-i-s', $payment_data['post_date'] ); |
| 1028 |
$post_date = explode( '-', $post_date ); |
| 1029 |
$args = [ |
| 1030 |
'output' => 'post', |
| 1031 |
'cache_results' => false, |
| 1032 |
'no_found_rows' => true, |
| 1033 |
'update_post_meta_cache' => false, |
| 1034 |
'update_post_term_cache' => false, |
| 1035 |
'fields' => 'ids', |
| 1036 |
'date_query' => [ |
| 1037 |
[ |
| 1038 |
'year' => $post_date[0], |
| 1039 |
'month' => $post_date[1], |
| 1040 |
'day' => $post_date[2], |
| 1041 |
'hour' => $post_date[3], |
| 1042 |
'minute' => $post_date[4], |
| 1043 |
'second' => $post_date[5], |
| 1044 |
], |
| 1045 |
], |
| 1046 |
'meta_query' => [ |
| 1047 |
[ |
| 1048 |
'key' => '_give_payment_total', |
| 1049 |
'value' => preg_replace( '/[\$,]/', '', $payment_data['price'] ), |
| 1050 |
'compare' => 'LIKE', |
| 1051 |
], |
| 1052 |
[ |
| 1053 |
'key' => '_give_payment_form_id', |
| 1054 |
'value' => $payment_data['give_form_id'], |
| 1055 |
'type' => 'numeric', |
| 1056 |
'compare' => '=', |
| 1057 |
], |
| 1058 |
[ |
| 1059 |
'key' => '_give_payment_gateway', |
| 1060 |
'value' => $payment_data['gateway'], |
| 1061 |
'compare' => '=', |
| 1062 |
], |
| 1063 |
[ |
| 1064 |
'key' => '_give_payment_donor_id', |
| 1065 |
'value' => isset( $donor_data->id ) ? $donor_data->id : '', |
| 1066 |
'compare' => '=', |
| 1067 |
], |
| 1068 |
[ |
| 1069 |
'key' => '_give_payment_mode', |
| 1070 |
'value' => $payment_data['mode'], |
| 1071 |
'compare' => '=', |
| 1072 |
], |
| 1073 |
], |
| 1074 |
]; |
| 1075 |
|
| 1076 |
$payments = new Give_Payments_Query( $args ); |
| 1077 |
$donations = $payments->get_payments(); |
| 1078 |
if ( ! empty( $donations ) ) { |
| 1079 |
$return = $donations; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
/** |
| 1084 |
* Filter to modify donation which is getting add is duplicate or not. |
| 1085 |
* |
| 1086 |
* @since 1.8.18 |
| 1087 |
*/ |
| 1088 |
return apply_filters( 'give_check_import_donation_duplicate', $return, $payment_data, $data, $form, $donor_data ); |
| 1089 |
} |
| 1090 |
|
| 1091 |
/** |
| 1092 |
* Record payment notes that is being imported from CSV. |
| 1093 |
* |
| 1094 |
* @since 1.8.13 |
| 1095 |
* |
| 1096 |
* @param int $payment_id The ID number of the payment. |
| 1097 |
* |
| 1098 |
* @return void |
| 1099 |
*/ |
| 1100 |
function give_donation_import_insert_default_payment_note( $payment_id ) { |
| 1101 |
$current_user = wp_get_current_user(); |
| 1102 |
give_insert_payment_note( $payment_id, wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) ); |
| 1103 |
} |
| 1104 |
|
| 1105 |
/** |
| 1106 |
* Return Import Page URL |
| 1107 |
* |
| 1108 |
* @since 1.8.13 |
| 1109 |
* |
| 1110 |
* @param array $parameter |
| 1111 |
* |
| 1112 |
* @return string URL |
| 1113 |
*/ |
| 1114 |
function give_import_page_url( $parameter = [] ) { |
| 1115 |
$defalut_query_arg = [ |
| 1116 |
'post_type' => 'give_forms', |
| 1117 |
'page' => 'give-tools', |
| 1118 |
'tab' => 'import', |
| 1119 |
'importer-type' => 'import_donations', |
| 1120 |
]; |
| 1121 |
$import_query_arg = wp_parse_args( $parameter, $defalut_query_arg ); |
| 1122 |
|
| 1123 |
return add_query_arg( $import_query_arg, admin_url( 'edit.php' ) ); |
| 1124 |
} |
| 1125 |
|