1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Import Functions |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Functions |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
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', array() ); |
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 = array() ) { |
33
|
|
|
update_option( 'give_import_donation_report', $value ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Delete the Import report of the donations |
39
|
|
|
* |
40
|
|
|
* @since 1.8.13 |
41
|
|
|
*/ |
42
|
|
|
function give_import_donation_report_reset() { |
43
|
|
|
update_option( 'give_import_donation_report', array() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Give get form data from csv if not then create and form and return the form value. |
48
|
|
|
* |
49
|
|
|
* @since 1.8.13. |
50
|
|
|
* |
51
|
|
|
* @param $data . |
52
|
|
|
* |
53
|
|
|
* @return array|bool|Give_Donate_Form|int|null|WP_Post |
54
|
|
|
*/ |
55
|
|
|
function give_import_get_form_data_from_csv( $data, $import_setting = array() ) { |
|
|
|
|
56
|
|
|
$new_form = false; |
57
|
|
|
|
58
|
|
|
// Get the import report |
59
|
|
|
$report = give_import_donation_report(); |
60
|
|
|
|
61
|
|
|
$form = false; |
62
|
|
|
$meta = array(); |
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
|
|
View Code Duplication |
if ( empty( $form_id ) ) { |
|
|
|
|
69
|
|
|
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 ); |
70
|
|
|
$form = false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( false === $form && ! empty( $data['form_title'] ) ) { |
75
|
|
|
$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' ); |
76
|
|
|
|
77
|
|
|
if ( ! empty( $form->ID ) ) { |
78
|
|
|
|
79
|
|
|
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 ); |
80
|
|
|
|
81
|
|
|
$form = new Give_Donate_Form( $form->ID ); |
|
|
|
|
82
|
|
|
} else { |
83
|
|
|
$form = new Give_Donate_Form(); |
84
|
|
|
$args = array( |
85
|
|
|
'post_title' => $data['form_title'], |
86
|
|
|
'post_status' => 'publish', |
87
|
|
|
); |
88
|
|
|
$form = $form->create( $args ); |
|
|
|
|
89
|
|
|
$report['create_form'] = ( ! empty( $report['create_form'] ) ? ( absint( $report['create_form'] ) + 1 ) : 1 ); |
90
|
|
|
$new_form = true; |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' ); |
95
|
|
|
$form = new Give_Donate_Form( $form->ID ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( ! empty( $form ) && $form->get_ID() ) { |
99
|
|
|
if ( ! empty( $data['form_level'] ) && 'custom' != (string) strtolower( $data['form_level'] ) ) { |
100
|
|
|
$prices = (array) $form->get_prices(); |
101
|
|
|
$price_text = array(); |
102
|
|
View Code Duplication |
foreach ( $prices as $key => $price ) { |
|
|
|
|
103
|
|
|
if ( isset( $price['_give_id']['level_id'] ) ) { |
104
|
|
|
$price_text[ $price['_give_id']['level_id'] ] = ( ! empty( $price['_give_text'] ) ? $price['_give_text'] : '' ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ( ! in_array( $data['form_level'], $price_text ) ) { |
109
|
|
|
|
110
|
|
|
// For generating unquiet level id. |
111
|
|
|
$count = 1; |
112
|
|
|
$new_level = count( $prices ) + $count; |
113
|
|
|
while ( array_key_exists( $new_level, $price_text ) ) { |
114
|
|
|
$count ++; |
115
|
|
|
$new_level = count( $prices ) + $count; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$multi_level_donations = array( |
119
|
|
|
array( |
120
|
|
|
'_give_id' => array( |
121
|
|
|
'level_id' => $new_level, |
122
|
|
|
), |
123
|
|
|
'_give_amount' => give_sanitize_amount_for_db( $data['amount'] ), |
124
|
|
|
'_give_text' => $data['form_level'], |
125
|
|
|
), |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$price_text[ $new_level ] = $data['form_level']; |
129
|
|
|
|
130
|
|
|
if ( ! empty( $prices ) && is_array( $prices ) && ! empty( $prices[0] ) ) { |
131
|
|
|
$prices = wp_parse_args( $multi_level_donations, $prices ); |
132
|
|
|
|
133
|
|
|
// Sort $prices by amount in ascending order. |
134
|
|
|
$prices = wp_list_sort( $prices, '_give_amount', 'ASC' ); |
135
|
|
|
} else { |
136
|
|
|
$prices = $multi_level_donations; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Unset _give_default key from $prices. |
140
|
|
|
foreach ( $prices as $key => $price ) { |
141
|
|
|
if ( isset( $prices[ $key ]['_give_default'] ) ) { |
142
|
|
|
unset( $prices[ $key ]['_give_default'] ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Set the first $price of the $prices as defalut. |
147
|
|
|
$prices[0]['_give_default'] = 'default'; |
148
|
|
|
} |
149
|
|
|
$form->price_id = array_search( $data['form_level'], $price_text ); |
150
|
|
|
|
151
|
|
|
$donation_levels_amounts = wp_list_pluck( $prices, '_give_amount' ); |
152
|
|
|
$min_amount = min( $donation_levels_amounts ); |
153
|
|
|
$max_amount = max( $donation_levels_amounts ); |
154
|
|
|
|
155
|
|
|
$meta = array( |
156
|
|
|
'_give_levels_minimum_amount' => $min_amount, |
157
|
|
|
'_give_levels_maximum_amount' => $max_amount, |
158
|
|
|
'_give_price_option' => 'multi', |
159
|
|
|
'_give_donation_levels' => array_values( $prices ), |
160
|
|
|
); |
161
|
|
|
} else { |
162
|
|
|
$form->price_id = 'custom'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$defaults = array( |
166
|
|
|
'_give_set_price' => give_sanitize_amount_for_db( $data['amount'] ), |
167
|
|
|
'_give_price_option' => 'set', |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
// If new form is created. |
171
|
|
|
if ( ! empty( $new_form ) ) { |
172
|
|
|
$new_form = array( |
173
|
|
|
'_give_custom_amount_text' => ( ! empty( $data['form_custom_amount_text'] ) ? $data['form_custom_amount_text'] : 'Custom' ), |
174
|
|
|
'_give_logged_in_only' => 'enabled', |
175
|
|
|
'_give_custom_amount' => 'enabled', |
176
|
|
|
'_give_payment_import' => true, |
177
|
|
|
'_give_display_style' => 'radios', |
178
|
|
|
'_give_payment_display' => 'onpage', |
179
|
|
|
'give_product_notes' => 'Donation Notes', |
180
|
|
|
'_give_product_type' => 'default', |
181
|
|
|
'_give_default_gateway' => 'global', |
182
|
|
|
'_give_show_register_form' => 'both', |
183
|
|
|
); |
184
|
|
|
$defaults = wp_parse_args( $defaults, $new_form ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$meta = wp_parse_args( $meta, $defaults ); |
188
|
|
|
|
189
|
|
|
foreach ( $meta as $key => $value ) { |
190
|
|
|
give_update_meta( $form->get_ID(), $key, $value ); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// update the report |
195
|
|
|
give_import_donation_report_update( $report ); |
196
|
|
|
|
197
|
|
|
return $form; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Give get user details if not then create a user. Used in Import Donation CSV. |
202
|
|
|
* |
203
|
|
|
* @since 1.8.13 |
204
|
|
|
* |
205
|
|
|
* @param $data |
206
|
|
|
* |
207
|
|
|
* @return bool|false|WP_User |
208
|
|
|
*/ |
209
|
|
|
function give_import_get_user_from_csv( $data, $import_setting = array() ) { |
210
|
|
|
$report = give_import_donation_report(); |
211
|
|
|
$donor_data = false; |
212
|
|
|
$customer_id = false; |
|
|
|
|
213
|
|
|
|
214
|
|
|
// check if donor id is not empty |
215
|
|
|
if ( ! empty( $data['donor_id'] ) ) { |
216
|
|
|
$donor_data = new Give_Donor( (int) $data['donor_id'] ); |
|
|
|
|
217
|
|
View Code Duplication |
if ( ! empty( $donor_data->id ) ) { |
|
|
|
|
218
|
|
|
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ( empty( $donor_data->id ) && ! empty( $data['user_id'] ) ) { |
223
|
|
|
$user_id = (int) $data['user_id']; |
224
|
|
|
$donor_data = new Give_Donor( $user_id, true ); |
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
227
|
|
|
if ( empty( $donor_data->id ) ) { |
228
|
|
|
$donor_data = get_user_by( 'id', $user_id ); |
229
|
|
|
if ( ! empty( $donor_data->ID ) ) { |
230
|
|
|
$first_name = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $donor_data->user_nicename ); |
231
|
|
|
$last_name = ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ( $lastname = get_user_meta( $donor_data->ID, 'last_name', true ) ) ? $lastname : '' ) ); |
|
|
|
|
232
|
|
|
$name = $first_name . ' ' . $last_name; |
233
|
|
|
$user_email = $donor_data->user_email; |
234
|
|
|
$donor_args = array( |
235
|
|
|
'name' => $name, |
236
|
|
|
'email' => $user_email, |
237
|
|
|
'user_id' => $user_id, |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$donor_data = new Give_Donor(); |
241
|
|
|
$donor_data->create( $donor_args ); |
242
|
|
|
|
243
|
|
|
// Adding notes that donor is being imported from CSV. |
244
|
|
|
$current_user = wp_get_current_user(); |
245
|
|
|
$donor_data->add_note( esc_html( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ) ); |
246
|
|
|
|
247
|
|
|
// Add is used to ensure duplicate emails are not added |
248
|
|
View Code Duplication |
if ( $user_email != $data['email'] && ! empty( $data['email'] ) ) { |
|
|
|
|
249
|
|
|
$donor_data->add_meta( 'additional_email', $data['email'] ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 ); |
253
|
|
|
} |
254
|
|
|
} else { |
255
|
|
|
// Add is used to ensure duplicate emails are not added |
256
|
|
View Code Duplication |
if ( $donor_data->email != $data['email'] ) { |
|
|
|
|
257
|
|
|
$donor_data->add_meta( 'additional_email', ( ! empty( $data['email'] ) ? $data['email'] : $donor_data->email ) ); |
258
|
|
|
} |
259
|
|
|
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if ( empty( $donor_data->id ) && ! empty( $data['email'] ) ) { |
264
|
|
|
|
265
|
|
|
$donor_data = new Give_Donor( $data['email'] ); |
266
|
|
|
if ( empty( $donor_data->id ) ) { |
267
|
|
|
$donor_data = get_user_by( 'email', $data['email'] ); |
268
|
|
|
|
269
|
|
|
if ( empty( $donor_data->ID ) && isset( $import_setting['create_user'] ) && 1 === absint( $import_setting['create_user'] ) ) { |
270
|
|
|
$data['first_name'] = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $data['email'] ); |
271
|
|
|
$data['last_name'] = ( ! empty( $data['last_name'] ) ? $data['last_name'] : '' ); |
272
|
|
|
$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' ) ) ); |
273
|
|
|
$donor_args = array( |
274
|
|
|
'user_login' => $data['email'], |
275
|
|
|
'user_email' => $data['email'], |
276
|
|
|
'user_registered' => date( 'Y-m-d H:i:s' ), |
277
|
|
|
'user_first' => $data['first_name'], |
278
|
|
|
'user_last' => $data['last_name'], |
279
|
|
|
'user_pass' => wp_generate_password( 8, true ), |
280
|
|
|
'role' => $give_role, |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Filter to modify user data before new user id register. |
285
|
|
|
* |
286
|
|
|
* @since 1.8.13 |
287
|
|
|
*/ |
288
|
|
|
$donor_args = (array) apply_filters( 'give_import_insert_user_args', $donor_args, $data, $import_setting ); |
289
|
|
|
|
290
|
|
|
// This action was added to remove the login when using the give register function. |
291
|
|
|
add_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 ); |
292
|
|
|
$customer_id = give_register_and_login_new_user( $donor_args ); |
293
|
|
|
remove_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 ); |
294
|
|
|
|
295
|
|
|
update_user_meta( $customer_id, '_give_payment_import', true ); |
|
|
|
|
296
|
|
|
$donor_data = new Give_Donor( $customer_id, true ); |
|
|
|
|
297
|
|
|
} else { |
298
|
|
|
$customer_id = ( ! empty( $donor_data->ID ) ? $donor_data->ID : false ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
if ( ! empty( $customer_id ) || ( isset( $import_setting['create_user'] ) && 0 === absint( $import_setting['create_user'] ) ) ) { |
302
|
|
|
$donor_data = new Give_Donor( $customer_id, true ); |
303
|
|
|
|
304
|
|
|
if ( empty( $donor_data->id ) ) { |
305
|
|
|
|
306
|
|
|
if ( ! empty( $data['form_id'] ) ) { |
307
|
|
|
$form = new Give_Donate_Form( $data['form_id'] ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$payment_title = ( isset( $data['form_title'] ) ? $data['form_title'] : ( isset( $form ) ? $form->get_name() : esc_html__( 'New Form', 'give' ) ) ); |
311
|
|
|
$donor_args = array( |
312
|
|
|
'name' => ! is_email( $payment_title ) ? $data['first_name'] . ' ' . $data['last_name'] : '', |
313
|
|
|
'email' => $data['email'], |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
if ( ! empty( $customer_id ) ) { |
317
|
|
|
$donor_args['user_id'] = $customer_id; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$donor_data->create( $donor_args ); |
321
|
|
|
|
322
|
|
|
// Adding notes that donor is being imported from CSV. |
323
|
|
|
$current_user = wp_get_current_user(); |
324
|
|
|
$donor_data->add_note( esc_html( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ) ); |
325
|
|
|
|
326
|
|
|
$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 ); |
327
|
|
View Code Duplication |
} else { |
|
|
|
|
328
|
|
|
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
View Code Duplication |
} else { |
|
|
|
|
332
|
|
|
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 ); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
// update the report |
336
|
|
|
give_import_donation_report_update( $report ); |
337
|
|
|
|
338
|
|
|
return $donor_data; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Return the option that are default options. |
343
|
|
|
* |
344
|
|
|
* @since 1.8.13 |
345
|
|
|
*/ |
346
|
|
|
function give_import_default_options() { |
347
|
|
|
/** |
348
|
|
|
* Filter to modify default option in the import dropdown |
349
|
|
|
* |
350
|
|
|
* @since 1.8.13 |
351
|
|
|
* |
352
|
|
|
* @return array |
353
|
|
|
*/ |
354
|
|
|
return (array) apply_filters( 'give_import_default_options', array( |
355
|
|
|
'' => __( 'Do not import', 'give' ), |
356
|
|
|
) ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Return the option that are related to donations. |
361
|
|
|
* |
362
|
|
|
* @since 1.8.13 |
363
|
|
|
*/ |
364
|
|
|
function give_import_donations_options() { |
365
|
|
|
/** |
366
|
|
|
* Filter to modify donations option in the import dropdown |
367
|
|
|
* |
368
|
|
|
* @since 1.8.13 |
369
|
|
|
* |
370
|
|
|
* @return array |
371
|
|
|
*/ |
372
|
|
|
return (array) apply_filters( 'give_import_donations_options', array( |
373
|
|
|
'id' => __( 'Donation ID', 'give' ), |
374
|
|
|
'amount' => array( |
375
|
|
|
__( 'Donation Amount', 'give' ), |
376
|
|
|
__( 'Amount', 'give' ) |
|
|
|
|
377
|
|
|
), |
378
|
|
|
'post_date' => array( |
379
|
|
|
__( 'Donation Date', 'give' ), |
380
|
|
|
__( 'Date', 'give' ), |
381
|
|
|
), |
382
|
|
|
'first_name' => array( |
383
|
|
|
__( 'Donor First Name', 'give' ), |
384
|
|
|
__( 'First Name', 'give' ), |
385
|
|
|
__( 'Name', 'give' ), |
386
|
|
|
), |
387
|
|
|
'last_name' => array( |
388
|
|
|
__( 'Donor Last Name', 'give' ), |
389
|
|
|
__( 'Last Name', 'give' ), |
390
|
|
|
), |
391
|
|
|
'line1' => array( |
392
|
|
|
__( 'Address 1', 'give' ), |
393
|
|
|
__( 'Address', 'give' ), |
394
|
|
|
), |
395
|
|
|
'line2' => __( 'Address 2', 'give' ), |
396
|
|
|
'city' => __( 'City', 'give' ), |
397
|
|
|
'state' => array( |
398
|
|
|
__( 'State', 'give' ), |
399
|
|
|
__( 'Province', 'give' ), |
400
|
|
|
__( 'County', 'give' ), |
401
|
|
|
__( 'Region', 'give' ), |
402
|
|
|
), |
403
|
|
|
'country' => __( 'Country', 'give' ), |
404
|
|
|
'zip' => array( |
405
|
|
|
__( 'Zip', 'give' ), |
406
|
|
|
__( 'Zip Code', 'give' ), |
407
|
|
|
__( 'Postal Code', 'give' ), |
408
|
|
|
), |
409
|
|
|
'email' => array( |
410
|
|
|
__( 'Donor Email', 'give' ), |
411
|
|
|
__( 'Email', 'give' ) |
|
|
|
|
412
|
|
|
), |
413
|
|
|
'post_status' => array( |
414
|
|
|
__( 'Donation Status', 'give' ), |
415
|
|
|
__( 'Status', 'give' ), |
416
|
|
|
), |
417
|
|
|
'gateway' => array( |
418
|
|
|
__( 'Payment Method', 'give' ), |
419
|
|
|
__( 'Method', 'give' ), |
420
|
|
|
), |
421
|
|
|
'notes' => __( 'Notes', 'give' ), |
422
|
|
|
'mode' => array( |
423
|
|
|
__( 'Test Mode', 'give' ), |
424
|
|
|
__( 'Mode', 'give' ), |
425
|
|
|
), |
426
|
|
|
'post_meta' => __( 'Import as Meta', 'give' ), |
427
|
|
|
) ); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Return the option that are related to donations. |
432
|
|
|
* |
433
|
|
|
* @since 1.8.13 |
434
|
|
|
*/ |
435
|
|
|
function give_import_donor_options() { |
436
|
|
|
/** |
437
|
|
|
* Filter to modify donors option in the import dropdown |
438
|
|
|
* |
439
|
|
|
* @since 1.8.13 |
440
|
|
|
* |
441
|
|
|
* @return array |
442
|
|
|
*/ |
443
|
|
|
return (array) apply_filters( 'give_import_donor_options', array( |
444
|
|
|
'donor_id' => __( 'Donor ID', 'give' ), |
445
|
|
|
'user_id' => __( 'User ID', 'give' ), |
446
|
|
|
) ); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Return the option that are related to donations. |
451
|
|
|
* |
452
|
|
|
* @since 1.8.13 |
453
|
|
|
*/ |
454
|
|
|
function give_import_donation_form_options() { |
455
|
|
|
/** |
456
|
|
|
* Filter to modify form option in the import dropdown |
457
|
|
|
* |
458
|
|
|
* @since 1.8.13 |
459
|
|
|
* |
460
|
|
|
* @return array |
461
|
|
|
*/ |
462
|
|
|
return (array) apply_filters( 'give_import_donation_form_options', array( |
463
|
|
|
'form_title' => array( |
464
|
|
|
__( 'Donation Form Title', 'give' ), |
465
|
|
|
__( 'Donation Form', 'give' ), |
466
|
|
|
__( 'Form Name', 'give' ), |
467
|
|
|
__( 'Title', 'give' ), |
468
|
|
|
), |
469
|
|
|
'form_id' => array( |
470
|
|
|
__( 'Donation Form ID', 'give' ), |
471
|
|
|
__( 'Form ID', 'give' ) |
|
|
|
|
472
|
|
|
), |
473
|
|
|
'form_level' => array( |
474
|
|
|
__( 'Donation Level', 'give' ), |
475
|
|
|
__( 'Level', 'give' ), |
476
|
|
|
), |
477
|
|
|
'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ), |
478
|
|
|
) ); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Import CSV in DB |
483
|
|
|
* |
484
|
|
|
* @param int $file_id CSV id |
485
|
|
|
* @param int $start Start from which csv line. |
486
|
|
|
* @param int $end End from which csv line. |
487
|
|
|
* @param string $delimiter CSV delimeter. |
488
|
|
|
* |
489
|
|
|
* @return array |
490
|
|
|
*/ |
491
|
|
|
function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = 'csv' ) { |
492
|
|
|
/** |
493
|
|
|
* Filter to modify delimiter of Import. |
494
|
|
|
* |
495
|
|
|
* @since |
496
|
|
|
* 1.8.14 |
497
|
|
|
* |
498
|
|
|
* Return string $delimiter. |
499
|
|
|
*/ |
500
|
|
|
$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter ); |
501
|
|
|
|
502
|
|
|
$raw_data = array(); |
503
|
|
|
$file_dir = get_attached_file( $file_id ); |
504
|
|
|
$count = 0; |
505
|
|
|
if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) { |
506
|
|
|
while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) { |
507
|
|
|
if ( $count >= $start && $count <= $end ) { |
508
|
|
|
$raw_data[] = $row; |
509
|
|
|
} |
510
|
|
|
$count ++; |
511
|
|
|
} |
512
|
|
|
fclose( $handle ); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
return $raw_data; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Remove login when user register with give functions. |
521
|
|
|
* |
522
|
|
|
* @since 1.8.13 |
523
|
|
|
* |
524
|
|
|
* @param $value |
525
|
|
|
* |
526
|
|
|
* @return bool |
527
|
|
|
*/ |
528
|
|
|
function give_log_user_in_on_register_callback( $value ) { |
|
|
|
|
529
|
|
|
return false; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Add import Donation forms, donations , donor from CSV to database |
534
|
|
|
* |
535
|
|
|
* @since 1.8.13 |
536
|
|
|
* |
537
|
|
|
* @param array $raw_key Setup bu user at step 2. |
538
|
|
|
* @param array $row_data Feilds that are being imported from CSV |
539
|
|
|
* @param array $main_key First row from the CSV |
540
|
|
|
* @param array $import_setting Contain the global variable. |
541
|
|
|
* |
542
|
|
|
* @return bool |
543
|
|
|
*/ |
544
|
|
|
function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = array(), $import_setting = array() ) { |
545
|
|
|
$data = array_combine( $raw_key, $row_data ); |
546
|
|
|
$price_id = false; |
|
|
|
|
547
|
|
|
$customer_id = 0; |
548
|
|
|
$import_setting['create_user'] = ( isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1 ); |
549
|
|
|
|
550
|
|
|
$data = (array) apply_filters( 'give_save_import_donation_to_db', $data ); |
551
|
|
|
|
552
|
|
|
$data['amount'] = give_maybe_sanitize_amount( $data['amount'] ); |
553
|
|
|
|
554
|
|
|
// Here come the login function. |
555
|
|
|
$donor_data = give_import_get_user_from_csv( $data, $import_setting ); |
556
|
|
|
if ( ! empty( $donor_data->id ) ) { |
557
|
|
|
if ( ! empty( $donor_data->user_id ) ) { |
558
|
|
|
$customer_id = $donor_data->user_id; |
559
|
|
|
} elseif ( ! empty( $data['user_id'] ) ) { |
560
|
|
|
$customer_id = $data['user_id']; |
561
|
|
|
} |
562
|
|
|
} else { |
563
|
|
|
return false; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
// get form data or register a form data. |
567
|
|
|
$form = give_import_get_form_data_from_csv( $data, $import_setting ); |
568
|
|
|
if ( false == $form ) { |
569
|
|
|
return false; |
570
|
|
|
} else { |
571
|
|
|
$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false; |
572
|
|
|
} |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
575
|
|
|
$address = array( |
576
|
|
|
'line1' => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ), |
577
|
|
|
'line2' => ( ! empty( $data['line1'] ) ? give_clean( $data['line2'] ) : '' ), |
578
|
|
|
'city' => ( ! empty( $data['line1'] ) ? give_clean( $data['city'] ) : '' ), |
579
|
|
|
'zip' => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ), |
580
|
|
|
'state' => ( ! empty( $data['state'] ) ? give_clean( $data['state'] ) : '' ), |
581
|
|
|
'country' => ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' ), |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
//Create payment_data array |
585
|
|
|
$payment_data = array( |
586
|
|
|
'donor_id' => $donor_data->id, |
587
|
|
|
'price' => $data['amount'], |
588
|
|
|
'status' => ( ! empty( $data['post_status'] ) ? $data['post_status'] : 'publish' ), |
589
|
|
|
'currency' => give_get_currency(), |
590
|
|
|
'user_info' => array( |
591
|
|
|
'id' => $customer_id, |
592
|
|
|
'email' => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ), |
593
|
|
|
'first_name' => ( ! empty( $data['first_name'] ) ? $data['first_name'] : ( ! empty( $customer_id ) && ( $first_name = get_user_meta( $customer_id, 'first_name', true ) ) ? $first_name : $donor_data->name ) ), |
|
|
|
|
594
|
|
|
'last_name' => ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ! empty( $customer_id ) && ( $last_name = get_user_meta( $customer_id, 'last_name', true ) ) ? $last_name : $donor_data->name ) ), |
|
|
|
|
595
|
|
|
'address' => $address, |
596
|
|
|
), |
597
|
|
|
'gateway' => ( ! empty( $data['gateway'] ) && 'offline' != strtolower( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ), |
598
|
|
|
'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : $form->get_name() ), |
599
|
|
|
'give_form_id' => (string) $form->get_ID(), |
600
|
|
|
'give_price_id' => $price_id, |
601
|
|
|
'purchase_key' => strtolower( md5( uniqid() ) ), |
602
|
|
|
'user_email' => $data['email'], |
603
|
|
|
'post_date' => ( ! empty( $data['post_date'] ) ? mysql2date( 'Y-m-d H:i:s', $data['post_date'] ) : current_time( 'mysql' ) ), |
604
|
|
|
'mode' => ( ! empty( $data['mode'] ) ? ( 'true' == (string) $data['mode'] || 'TRUE' == (string) $data['mode'] ? 'test' : 'live' ) : ( isset( $import_setting['mode'] ) ? ( true == (bool) $import_setting['mode'] ? 'test' : 'live' ) : ( give_is_test_mode() ? 'test' : 'live' ) ) ), |
|
|
|
|
605
|
|
|
); |
606
|
|
|
|
607
|
|
|
$payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form ); |
608
|
|
|
|
609
|
|
|
// Get the report |
610
|
|
|
$report = give_import_donation_report(); |
611
|
|
|
|
612
|
|
|
// Check for duplicate code. |
613
|
|
|
$donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ); |
614
|
|
|
if ( false !== $donation_duplicate ) { |
615
|
|
|
$report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate; |
616
|
|
|
$report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 ); |
617
|
|
|
} else { |
618
|
|
|
add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 ); |
619
|
|
|
add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 ); |
620
|
|
|
add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 ); |
621
|
|
|
add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 ); |
622
|
|
|
$payment = give_insert_payment( $payment_data ); |
623
|
|
|
remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 ); |
624
|
|
|
remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 ); |
625
|
|
|
remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 ); |
626
|
|
|
remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 ); |
627
|
|
|
|
628
|
|
|
if ( $payment ) { |
|
|
|
|
629
|
|
|
|
630
|
|
|
$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 ); |
631
|
|
|
|
632
|
|
|
update_post_meta( $payment, '_give_payment_import', true ); |
633
|
|
|
|
634
|
|
|
if ( ! empty( $import_setting['csv'] ) ) { |
635
|
|
|
update_post_meta( $payment, '_give_payment_import_id', $import_setting['csv'] ); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
// Insert Notes. |
639
|
|
|
if ( ! empty( $data['notes'] ) ) { |
640
|
|
|
give_insert_payment_note( $payment, $data['notes'] ); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
$meta_exists = array_keys( $raw_key, 'post_meta' ); |
644
|
|
|
if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) { |
645
|
|
|
foreach ( $meta_exists as $meta_exist ) { |
646
|
|
|
if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) { |
647
|
|
|
update_post_meta( $payment, $main_key[ $meta_exist ], $row_data[ $meta_exist ] ); |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
} else { |
652
|
|
|
$report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 ); |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
// update the report |
657
|
|
|
give_import_donation_report_update( $report ); |
658
|
|
|
|
659
|
|
|
return true; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Alter donor information when importing donations from CSV |
664
|
|
|
* |
665
|
|
|
* @since 1.8.13 |
666
|
|
|
* |
667
|
|
|
* @param $donor |
668
|
|
|
* @param $payment_id |
669
|
|
|
* @param $payment_data |
670
|
|
|
* |
671
|
|
|
* @return Give_Donor |
672
|
|
|
*/ |
673
|
|
|
function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) { |
|
|
|
|
674
|
|
|
$old_donor = $donor; |
675
|
|
|
if ( ! empty( $payment_data['donor_id'] ) ) { |
676
|
|
|
$donor_id = absint( $payment_data['donor_id'] ); |
677
|
|
|
$donor = new Give_Donor( $donor_id ); |
678
|
|
|
if ( ! empty( $donor->id ) ) { |
679
|
|
|
return $donor; |
680
|
|
|
} |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
return $old_donor; |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/* |
687
|
|
|
* Give update purchase_count of give customer. |
688
|
|
|
* |
689
|
|
|
* @since 1.8.13 |
690
|
|
|
*/ |
691
|
|
|
function give_import_donation_insert_payment( $payment_id, $payment_data ) { |
692
|
|
|
// Update Give Customers purchase_count |
693
|
|
|
if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) { |
694
|
|
|
$donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true ); |
695
|
|
|
if ( ! empty( $donor_id ) ) { |
696
|
|
|
$donor = new Give_Donor( $donor_id ); |
|
|
|
|
697
|
|
|
$donor->increase_purchase_count(); |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Add author id in in donation post |
704
|
|
|
* |
705
|
|
|
* @since 1.8.13 |
706
|
|
|
*/ |
707
|
|
|
function give_donation_import_give_insert_payment_args( $args, $payment_data ) { |
708
|
|
|
if ( ! empty( $payment_data['user_info']['id'] ) ) { |
709
|
|
|
$args['post_author'] = (int) $payment_data['user_info']['id']; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
return $args; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Check if Import donation is duplicate |
717
|
|
|
* |
718
|
|
|
* @since 1.8.13 |
719
|
|
|
*/ |
720
|
|
|
function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) { |
721
|
|
|
$return = false; |
722
|
|
|
if ( ! empty( $data['post_date'] ) ) { |
723
|
|
|
$post_date = mysql2date( 'Y-m-d-H-i-s', $data['post_date'] ); |
724
|
|
|
$post_date = explode( '-', $post_date ); |
725
|
|
|
$args = array( |
726
|
|
|
'output' => 'post', |
727
|
|
|
'cache_results' => false, |
728
|
|
|
'no_found_rows' => true, |
729
|
|
|
'update_post_meta_cache' => false, |
730
|
|
|
'update_post_term_cache' => false, |
731
|
|
|
'fields' => 'ids', |
732
|
|
|
'date_query' => array( |
733
|
|
|
array( |
734
|
|
|
'year' => $post_date[0], |
735
|
|
|
'month' => $post_date[1], |
736
|
|
|
'day' => $post_date[2], |
737
|
|
|
'hour' => $post_date[3], |
738
|
|
|
'minute' => $post_date[4], |
739
|
|
|
'second' => $post_date[5], |
740
|
|
|
), |
741
|
|
|
), |
742
|
|
|
'meta_query' => array( |
|
|
|
|
743
|
|
|
array( |
744
|
|
|
'key' => '_give_payment_total', |
745
|
|
|
'value' => preg_replace( '/[\$,]/', '', $payment_data['price'] ), |
746
|
|
|
'compare' => 'LIKE', |
747
|
|
|
), |
748
|
|
|
array( |
749
|
|
|
'key' => '_give_payment_form_id', |
750
|
|
|
'value' => $payment_data['give_form_id'], |
751
|
|
|
'type' => 'numeric', |
752
|
|
|
'compare' => '=', |
753
|
|
|
), |
754
|
|
|
array( |
755
|
|
|
'key' => '_give_payment_gateway', |
756
|
|
|
'value' => $payment_data['gateway'], |
757
|
|
|
'compare' => '=', |
758
|
|
|
), |
759
|
|
|
array( |
760
|
|
|
'key' => '_give_payment_customer_id', |
761
|
|
|
'value' => $donor_data->id, |
762
|
|
|
'compare' => '=', |
763
|
|
|
), |
764
|
|
|
), |
765
|
|
|
); |
766
|
|
|
|
767
|
|
|
$payments = new Give_Payments_Query( $args ); |
768
|
|
|
$donations = $payments->get_payments(); |
769
|
|
|
if ( ! empty( $donations ) ) { |
770
|
|
|
$return = $donations; |
771
|
|
|
} |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Filter to modify donation which is getting add is duplicate or not. |
776
|
|
|
* |
777
|
|
|
* @since 1.8.18 |
778
|
|
|
*/ |
779
|
|
|
return apply_filters( 'give_check_import_donation_duplicate', $return, $payment_data, $data, $form, $donor_data ); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
/** |
783
|
|
|
* Record payment notes that is being imported from CSV. |
784
|
|
|
* |
785
|
|
|
* @since 1.8.13 |
786
|
|
|
* |
787
|
|
|
* @param int $payment_id The ID number of the payment. |
788
|
|
|
* |
789
|
|
|
* @return void |
790
|
|
|
*/ |
791
|
|
|
function give_donation_import_insert_default_payment_note( $payment_id ) { |
792
|
|
|
$current_user = wp_get_current_user(); |
793
|
|
|
give_insert_payment_note( $payment_id, esc_html( wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) ) ); |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
/** |
797
|
|
|
* Return Import Page URL |
798
|
|
|
* |
799
|
|
|
* @since 1.8.13 |
800
|
|
|
* |
801
|
|
|
* @param array $parameter |
802
|
|
|
* |
803
|
|
|
* @return string URL |
804
|
|
|
*/ |
805
|
|
|
function give_import_page_url( $parameter = array() ) { |
806
|
|
|
$defalut_query_arg = array( |
807
|
|
|
'post_type' => 'give_forms', |
808
|
|
|
'page' => 'give-tools', |
809
|
|
|
'tab' => 'import', |
810
|
|
|
'importer-type' => 'import_donations', |
811
|
|
|
); |
812
|
|
|
$import_query_arg = wp_parse_args( $parameter, $defalut_query_arg ); |
813
|
|
|
|
814
|
|
|
return add_query_arg( $import_query_arg, admin_url( 'edit.php' ) ); |
815
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.