|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Donations Import Class |
|
4
|
|
|
* |
|
5
|
|
|
* This class handles donations import. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Give |
|
8
|
|
|
* @subpackage Classes/Give_Import_Donations |
|
9
|
|
|
* @copyright Copyright (c) 2017, WordImpress |
|
10
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
11
|
|
|
* @since 1.8.14 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; // Exit if accessed directly |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if ( ! class_exists( 'Give_Import_Donations' ) ) { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Give_Import_Donations. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.8.14 |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Give_Import_Donations { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Importer type |
|
29
|
|
|
* |
|
30
|
|
|
* @since 1.8.13 |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $importer_type = 'import_donations'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @since |
|
39
|
|
|
* @access private |
|
40
|
|
|
* @var |
|
41
|
|
|
*/ |
|
42
|
|
|
static private $instance; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Importing donation per page. |
|
46
|
|
|
* |
|
47
|
|
|
* @since 1.8.14 |
|
48
|
|
|
* |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
public static $per_page = 5; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Singleton pattern. |
|
55
|
|
|
* |
|
56
|
|
|
* @since |
|
57
|
|
|
* @access private |
|
58
|
|
|
*/ |
|
59
|
|
|
private function __construct() { |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @since |
|
67
|
|
|
* @access public |
|
68
|
|
|
* |
|
69
|
|
|
* @return static |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function get_instance() { |
|
72
|
|
|
if ( null === static::$instance ) { |
|
73
|
|
|
self::$instance = new static(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return self::$instance; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Setup |
|
81
|
|
|
* |
|
82
|
|
|
* @since 1.8.14 |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setup() { |
|
87
|
|
|
$this->setup_hooks(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Setup Hooks. |
|
93
|
|
|
* |
|
94
|
|
|
* @since 1.8.14 |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
private function setup_hooks() { |
|
99
|
|
|
if ( ! $this->is_donations_import_page() ) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Do not render main import tools page. |
|
104
|
|
|
remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) ); |
|
105
|
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
// Render donation import page |
|
108
|
|
|
add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) ); |
|
109
|
|
|
|
|
110
|
|
|
// Print the HTML. |
|
111
|
|
|
add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 ); |
|
112
|
|
|
|
|
113
|
|
|
// Run when form submit. |
|
114
|
|
|
add_action( 'give-tools_save_import', array( $this, 'save' ) ); |
|
115
|
|
|
|
|
116
|
|
|
add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 ); |
|
117
|
|
|
|
|
118
|
|
|
// Used to add submit button. |
|
119
|
|
|
add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Update notice |
|
125
|
|
|
* |
|
126
|
|
|
* @since 1.8.14 |
|
127
|
|
|
* |
|
128
|
|
|
* @param $messages |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
*/ |
|
132
|
|
|
public function update_notices( $messages ) { |
|
133
|
|
|
if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) { |
|
|
|
|
|
|
134
|
|
|
unset( $messages['give-setting-updated'] ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $messages; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Print submit and nonce button. |
|
142
|
|
|
* |
|
143
|
|
|
* @since 1.8.14 |
|
144
|
|
|
*/ |
|
145
|
|
|
public function submit() { |
|
146
|
|
|
wp_nonce_field( 'give-save-settings', '_give-save-settings' ); |
|
147
|
|
|
?> |
|
148
|
|
|
<input type="hidden" class="import-step" id="import-step" name="step" value="<?php echo $this->get_step(); ?>"/> |
|
|
|
|
|
|
149
|
|
|
<input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/> |
|
|
|
|
|
|
150
|
|
|
<?php |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Print the HTML for importer. |
|
155
|
|
|
* |
|
156
|
|
|
* @since 1.8.14 |
|
157
|
|
|
*/ |
|
158
|
|
|
public function html() { |
|
159
|
|
|
$step = $this->get_step(); |
|
160
|
|
|
|
|
161
|
|
|
// Show progress. |
|
162
|
|
|
$this->render_progress(); |
|
163
|
|
|
?> |
|
164
|
|
|
<section> |
|
165
|
|
|
<table class="widefat export-options-table give-table <?php echo "step-{$step}"; ?>" id="<?php echo "step-{$step}"; ?>"> |
|
|
|
|
|
|
166
|
|
|
<tbody> |
|
167
|
|
|
<?php |
|
168
|
|
|
switch ( $this->get_step() ) { |
|
169
|
|
|
case 1: |
|
170
|
|
|
$this->render_media_csv(); |
|
171
|
|
|
break; |
|
172
|
|
|
|
|
173
|
|
|
case 2: |
|
174
|
|
|
$this->render_dropdown(); |
|
175
|
|
|
break; |
|
176
|
|
|
|
|
177
|
|
|
case 3: |
|
178
|
|
|
$this->start_import(); |
|
179
|
|
|
break; |
|
180
|
|
|
|
|
181
|
|
|
case 4: |
|
182
|
|
|
$this->import_success(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ( false === $this->check_for_dropdown_or_import() ) { |
|
186
|
|
|
?> |
|
187
|
|
|
<tr valign="top"> |
|
188
|
|
|
<th></th> |
|
189
|
|
|
<th> |
|
190
|
|
|
<input type="submit" |
|
191
|
|
|
class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>" |
|
|
|
|
|
|
192
|
|
|
id="recount-stats-submit" |
|
193
|
|
|
value="<?php esc_attr_e( 'Submit', 'give' ); ?>"/> |
|
194
|
|
|
</th> |
|
195
|
|
|
</tr> |
|
196
|
|
|
<?php |
|
197
|
|
|
} |
|
198
|
|
|
?> |
|
199
|
|
|
</tbody> |
|
200
|
|
|
</table> |
|
201
|
|
|
</section> |
|
202
|
|
|
<?php |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Show success notice |
|
207
|
|
|
* |
|
208
|
|
|
* @since 1.8.14 |
|
209
|
|
|
*/ |
|
210
|
|
|
public function import_success() { |
|
211
|
|
|
|
|
212
|
|
|
$delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false ); |
|
|
|
|
|
|
213
|
|
|
$csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false ); |
|
|
|
|
|
|
214
|
|
|
if ( ! empty( $delete_csv ) && ! empty( $csv ) ) { |
|
215
|
|
|
wp_delete_attachment( $csv, true ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$report = give_import_donation_report(); |
|
219
|
|
|
$report_html = array( |
|
220
|
|
|
'duplicate_donor' => array( |
|
221
|
|
|
__( '%s duplicate %s detected', 'give' ), |
|
222
|
|
|
__( 'donor', 'give' ), |
|
223
|
|
|
__( 'donors', 'give' ), |
|
224
|
|
|
), |
|
225
|
|
|
'create_donor' => array( |
|
226
|
|
|
__( '%s %s created', 'give' ), |
|
227
|
|
|
__( 'donor', 'give' ), |
|
228
|
|
|
__( 'donors', 'give' ), |
|
229
|
|
|
), |
|
230
|
|
|
'create_form' => array( |
|
231
|
|
|
__( '%s donation %s created', 'give' ), |
|
232
|
|
|
__( 'form', 'give' ), |
|
233
|
|
|
__( 'forms', 'give' ), |
|
234
|
|
|
), |
|
235
|
|
|
'duplicate_donation' => array( |
|
236
|
|
|
__( '%s duplicate %s detected', 'give' ), |
|
237
|
|
|
__( 'donation', 'give' ), |
|
238
|
|
|
__( 'donations', 'give' ), |
|
239
|
|
|
), |
|
240
|
|
|
'create_donation' => array( |
|
241
|
|
|
__( '%s %s imported', 'give' ), |
|
242
|
|
|
__( 'donation', 'give' ), |
|
243
|
|
|
__( 'donations', 'give' ), |
|
244
|
|
|
), |
|
245
|
|
|
); |
|
246
|
|
|
$total = (int) $_GET['total']; |
|
|
|
|
|
|
247
|
|
|
-- $total; |
|
248
|
|
|
$success = (bool) $_GET['success']; |
|
|
|
|
|
|
249
|
|
|
?> |
|
250
|
|
|
<tr valign="top" class="give-import-dropdown"> |
|
251
|
|
|
<th colspan="2"> |
|
252
|
|
|
<h2> |
|
253
|
|
|
<?php |
|
254
|
|
|
if ( $success ) { |
|
255
|
|
|
echo sprintf( |
|
|
|
|
|
|
256
|
|
|
__( 'Import complete! %s donations processed', 'give' ), |
|
257
|
|
|
"<strong>{$total}</strong>" |
|
258
|
|
|
); |
|
259
|
|
|
} else { |
|
260
|
|
|
echo sprintf( |
|
|
|
|
|
|
261
|
|
|
__( 'Failed to import %s donations', 'give' ), |
|
262
|
|
|
"<strong>{$total}</strong>" |
|
263
|
|
|
); |
|
264
|
|
|
} |
|
265
|
|
|
?> |
|
266
|
|
|
</h2> |
|
267
|
|
|
|
|
268
|
|
|
<?php |
|
269
|
|
|
$text = __( 'Import Donation', 'give' ); |
|
270
|
|
|
$query_arg = array( |
|
271
|
|
|
'post_type' => 'give_forms', |
|
272
|
|
|
'page' => 'give-tools', |
|
273
|
|
|
'tab' => 'import', |
|
274
|
|
|
); |
|
275
|
|
|
if ( $success ) { |
|
276
|
|
|
$query_arg = array( |
|
277
|
|
|
'post_type' => 'give_forms', |
|
278
|
|
|
'page' => 'give-payment-history', |
|
279
|
|
|
); |
|
280
|
|
|
$text = __( 'View Donations', 'give' ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
foreach ( $report as $key => $value ) { |
|
284
|
|
|
if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) { |
|
285
|
|
|
?> |
|
286
|
|
|
<p> |
|
287
|
|
|
<?php echo esc_html( wp_sprintf( $report_html[ $key ][0], $value, _n( $report_html[ $key ][1], $report_html[ $key ][2], $value, 'give' ) ) ); ?> |
|
|
|
|
|
|
288
|
|
|
</p> |
|
289
|
|
|
<?php |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
?> |
|
293
|
|
|
|
|
294
|
|
|
<p> |
|
295
|
|
|
<a class="button button-large button-secondary" href="<?php echo add_query_arg( $query_arg, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a> |
|
|
|
|
|
|
296
|
|
|
</p> |
|
297
|
|
|
</th> |
|
298
|
|
|
</tr> |
|
299
|
|
|
<?php |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Will start Import |
|
304
|
|
|
* |
|
305
|
|
|
* @since 1.8.14 |
|
306
|
|
|
*/ |
|
307
|
|
|
public function start_import() { |
|
308
|
|
|
// Reset the donation form report. |
|
309
|
|
|
give_import_donation_report_reset(); |
|
310
|
|
|
|
|
311
|
|
|
$csv = (int) $_REQUEST['csv']; |
|
|
|
|
|
|
312
|
|
|
$index_start = 1; |
|
313
|
|
|
$index_end = 1; |
|
|
|
|
|
|
314
|
|
|
$next = true; |
|
315
|
|
|
$total = self::get_csv_total( $csv ); |
|
316
|
|
|
if ( self::$per_page < $total ) { |
|
317
|
|
|
$total_ajax = ceil( $total / self::$per_page ); |
|
318
|
|
|
$index_end = self::$per_page; |
|
319
|
|
|
} else { |
|
320
|
|
|
$total_ajax = 1; |
|
321
|
|
|
$index_end = $total; |
|
322
|
|
|
$next = false; |
|
323
|
|
|
} |
|
324
|
|
|
$current_percentage = 100 / ( $total_ajax + 1 ); |
|
325
|
|
|
|
|
326
|
|
|
?> |
|
327
|
|
|
<tr valign="top" class="give-import-dropdown"> |
|
328
|
|
|
<th colspan="2"> |
|
329
|
|
|
<h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ) ?></h2> |
|
330
|
|
|
<p class="give-field-description"><?php esc_html_e( 'Your donations are now being imported...', 'give' ) ?></p> |
|
331
|
|
|
</th> |
|
332
|
|
|
</tr> |
|
333
|
|
|
|
|
334
|
|
|
<tr valign="top" class="give-import-dropdown"> |
|
335
|
|
|
<th colspan="2"> |
|
336
|
|
|
<span class="spinner is-active"></span> |
|
337
|
|
|
<div class="give-progress" |
|
338
|
|
|
data-current="1" |
|
339
|
|
|
data-total_ajax="<?php echo $total_ajax; ?>" |
|
|
|
|
|
|
340
|
|
|
data-start="<?php echo $index_start; ?>" |
|
|
|
|
|
|
341
|
|
|
data-end="<?php echo $index_end; ?>" |
|
|
|
|
|
|
342
|
|
|
data-next="<?php echo $next; ?>" |
|
|
|
|
|
|
343
|
|
|
data-total="<?php echo $total; ?>" |
|
|
|
|
|
|
344
|
|
|
data-per_page="<?php echo self::$per_page; ?>"> |
|
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
<div style="width: <?php echo $current_percentage; ?>%"></div> |
|
|
|
|
|
|
347
|
|
|
</div> |
|
348
|
|
|
<input type="hidden" value="3" name="step"> |
|
349
|
|
|
<input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto" |
|
|
|
|
|
|
350
|
|
|
class="mapto"> |
|
351
|
|
|
<input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv"> |
|
|
|
|
|
|
352
|
|
|
<input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode"> |
|
|
|
|
|
|
353
|
|
|
<input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user" |
|
|
|
|
|
|
354
|
|
|
class="create_user"> |
|
355
|
|
|
<input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv" |
|
|
|
|
|
|
356
|
|
|
class="delete_csv"> |
|
357
|
|
|
<input type="hidden" value="<?php echo $_REQUEST['delimiter']; ?>" name="delimiter"> |
|
|
|
|
|
|
358
|
|
|
<input type="hidden" value='<?php echo maybe_serialize( self::get_importer( $csv ) ); ?>' |
|
|
|
|
|
|
359
|
|
|
name="main_key" |
|
360
|
|
|
class="main_key"> |
|
361
|
|
|
</th> |
|
362
|
|
|
</tr> |
|
363
|
|
|
|
|
364
|
|
|
<script type="text/javascript"> |
|
365
|
|
|
jQuery(document).ready(function () { |
|
366
|
|
|
give_on_donation_import_start(); |
|
367
|
|
|
}); |
|
368
|
|
|
</script> |
|
369
|
|
|
<?php |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Will return true if importing can be started or not else false. |
|
374
|
|
|
* |
|
375
|
|
|
* @since 1.8.14 |
|
376
|
|
|
*/ |
|
377
|
|
|
public function check_for_dropdown_or_import() { |
|
378
|
|
|
$return = true; |
|
379
|
|
|
if ( isset( $_REQUEST['mapto'] ) ) { |
|
380
|
|
|
$mapto = (array) $_REQUEST['mapto']; |
|
|
|
|
|
|
381
|
|
|
if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) { |
|
382
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv-form', __( 'Please select Form ID or Form Name options from the dropdown.', 'give' ) ); |
|
383
|
|
|
$return = false; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
if ( false === in_array( 'amount', $mapto ) ) { |
|
387
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv-amount', __( 'Please select Amount option from the dropdown.', 'give' ) ); |
|
388
|
|
|
$return = false; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) { |
|
392
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv-donor', __( 'Please select Email id or Customer ID options from the dropdown.', 'give' ) ); |
|
393
|
|
|
$return = false; |
|
394
|
|
|
} |
|
395
|
|
|
} else { |
|
396
|
|
|
$return = false; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
return $return; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* Print the Dropdown option for CSV. |
|
404
|
|
|
* |
|
405
|
|
|
* @since 1.8.14 |
|
406
|
|
|
*/ |
|
407
|
|
|
public function render_dropdown() { |
|
408
|
|
|
$csv = (int) $_GET['csv']; |
|
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
// TO check if the CSV files that is being add is valid or not if not then redirect to first step again |
|
411
|
|
|
$has_error = $this->csv_check( $csv ); |
|
|
|
|
|
|
412
|
|
|
if ( $has_error ) { |
|
413
|
|
|
$url = give_import_page_url(); |
|
414
|
|
|
?> |
|
415
|
|
|
<script type="text/javascript"> |
|
416
|
|
|
window.location = "<?php echo $url; ?>" |
|
|
|
|
|
|
417
|
|
|
</script> |
|
418
|
|
|
<?php |
|
419
|
|
|
} else { |
|
420
|
|
|
?> |
|
421
|
|
|
<tr valign="top" class="give-import-dropdown"> |
|
422
|
|
|
<th colspan="2"> |
|
423
|
|
|
<h2 id="give-import-title"><?php esc_html_e( 'Map CSV fields to donations', 'give' ) ?></h2> |
|
424
|
|
|
<p class="give-field-description"><?php esc_html_e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p> |
|
425
|
|
|
</th> |
|
426
|
|
|
</tr> |
|
427
|
|
|
|
|
428
|
|
|
<tr valign="top" class="give-import-dropdown"> |
|
429
|
|
|
<th><b><?php esc_html_e( 'Column name', 'give' ); ?></b></th> |
|
430
|
|
|
<th><b><?php esc_html_e( 'Map to field', 'give' ); ?></b></th> |
|
431
|
|
|
</tr> |
|
432
|
|
|
|
|
433
|
|
|
<?php |
|
434
|
|
|
$raw_key = $this->get_importer( $csv ); |
|
435
|
|
|
$donations = give_import_donations_options(); |
|
436
|
|
|
$donors = give_import_donor_options(); |
|
437
|
|
|
$forms = give_import_donation_form_options(); |
|
438
|
|
|
|
|
439
|
|
|
foreach ( $raw_key as $index => $value ) { |
|
|
|
|
|
|
440
|
|
|
?> |
|
441
|
|
|
<tr valign="top" class="give-import-option"> |
|
442
|
|
|
<th><?php echo $value; ?></th> |
|
|
|
|
|
|
443
|
|
|
<th> |
|
444
|
|
|
<?php |
|
445
|
|
|
$this->get_columns( $index, $donations, $donors, $forms, $value ); |
|
446
|
|
|
?> |
|
447
|
|
|
</th> |
|
448
|
|
|
</tr> |
|
449
|
|
|
<?php |
|
450
|
|
|
} |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* @param $option_value |
|
456
|
|
|
* @param $value |
|
457
|
|
|
* |
|
458
|
|
|
* @return string |
|
459
|
|
|
*/ |
|
460
|
|
|
public function selected( $option_value, $value ) { |
|
461
|
|
|
$selected = ''; |
|
462
|
|
|
if ( stristr( $value, $option_value ) ) { |
|
463
|
|
|
$selected = 'selected'; |
|
464
|
|
|
} elseif ( strrpos( $value, '_' ) && stristr( $option_value, 'Import as Meta' ) ) { |
|
465
|
|
|
$selected = 'selected'; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
return $selected; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Print the columns from the CSV. |
|
473
|
|
|
* |
|
474
|
|
|
* @since 1.8.14 |
|
475
|
|
|
*/ |
|
476
|
|
|
public function get_columns( $index, $donations, $donors, $forms, $value = false ) { |
|
477
|
|
|
$default = give_import_default_options(); |
|
478
|
|
|
?> |
|
479
|
|
|
<select name="mapto[<?php echo $index; ?>]"> |
|
|
|
|
|
|
480
|
|
|
<?php |
|
481
|
|
View Code Duplication |
foreach ( $default as $option => $option_value ) { |
|
|
|
|
|
|
482
|
|
|
$checked = $this->selected( $option_value, $value ); |
|
483
|
|
|
?> |
|
484
|
|
|
<option value="<?php echo $option; ?>" <?php echo $checked; ?> ><?php echo $option_value; ?></option> |
|
|
|
|
|
|
485
|
|
|
<?php |
|
486
|
|
|
} |
|
487
|
|
|
?> |
|
488
|
|
|
<optgroup label="Donations"> |
|
489
|
|
|
<?php |
|
490
|
|
View Code Duplication |
foreach ( $donations as $option => $option_value ) { |
|
|
|
|
|
|
491
|
|
|
$checked = $this->selected( $option_value, $value ); |
|
492
|
|
|
?> |
|
493
|
|
|
<option value="<?php echo $option; ?>" <?php echo $checked; ?> ><?php echo $option_value; ?></option> |
|
|
|
|
|
|
494
|
|
|
<?php |
|
495
|
|
|
} |
|
496
|
|
|
?> |
|
497
|
|
|
</optgroup> |
|
498
|
|
|
|
|
499
|
|
|
<optgroup label="Donors"> |
|
500
|
|
|
<?php |
|
501
|
|
View Code Duplication |
foreach ( $donors as $option => $option_value ) { |
|
|
|
|
|
|
502
|
|
|
$checked = $this->selected( $option_value, $value ); |
|
503
|
|
|
?> |
|
504
|
|
|
<option value="<?php echo $option; ?>" <?php echo $checked; ?> ><?php echo $option_value; ?></option> |
|
|
|
|
|
|
505
|
|
|
<?php |
|
506
|
|
|
} |
|
507
|
|
|
?> |
|
508
|
|
|
</optgroup> |
|
509
|
|
|
|
|
510
|
|
|
<optgroup label="Forms"> |
|
511
|
|
|
<?php |
|
512
|
|
View Code Duplication |
foreach ( $forms as $option => $option_value ) { |
|
|
|
|
|
|
513
|
|
|
$checked = $this->selected( $option_value, $value ); |
|
514
|
|
|
?> |
|
515
|
|
|
<option value="<?php echo $option; ?>" <?php echo $checked; ?> ><?php echo $option_value; ?></option> |
|
|
|
|
|
|
516
|
|
|
<?php |
|
517
|
|
|
} |
|
518
|
|
|
?> |
|
519
|
|
|
</optgroup> |
|
520
|
|
|
|
|
521
|
|
|
<?php |
|
522
|
|
|
do_action( 'give_import_dropdown_option', $index, $donations, $donors, $forms, $value ); |
|
523
|
|
|
?> |
|
524
|
|
|
</select> |
|
525
|
|
|
<?php |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Get column count of csv file. |
|
530
|
|
|
* |
|
531
|
|
|
* @since 1.8.14 |
|
532
|
|
|
* |
|
533
|
|
|
* @param $file_id |
|
534
|
|
|
* |
|
535
|
|
|
* @return bool|int |
|
536
|
|
|
*/ |
|
537
|
|
|
public function get_csv_total( $file_id ) { |
|
538
|
|
|
$total = false; |
|
539
|
|
|
if ( $file_id ) { |
|
540
|
|
|
$file_dir = get_attached_file( $file_id ); |
|
541
|
|
|
if ( $file_dir ) { |
|
542
|
|
|
$file = new SplFileObject( $file_dir, 'r' ); |
|
543
|
|
|
$file->seek( PHP_INT_MAX ); |
|
544
|
|
|
$total = $file->key() + 1; |
|
545
|
|
|
} |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
return $total; |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* Get the CSV fields title from the CSV. |
|
553
|
|
|
* |
|
554
|
|
|
* @since 1.8.14 |
|
555
|
|
|
* |
|
556
|
|
|
* @param (int) $file_id |
|
557
|
|
|
* @param int $index |
|
558
|
|
|
* @param string $delimiter |
|
559
|
|
|
* |
|
560
|
|
|
* @return array|bool $raw_data title of the CSV file fields |
|
561
|
|
|
*/ |
|
562
|
|
|
public function get_importer( $file_id, $index = 0, $delimiter = ',' ) { |
|
563
|
|
|
$raw_data = false; |
|
564
|
|
|
$file_dir = get_attached_file( $file_id ); |
|
565
|
|
|
if ( $file_dir ) { |
|
566
|
|
|
if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) { |
|
567
|
|
|
$raw_data = fgetcsv( $handle, $index, $delimiter ); |
|
568
|
|
|
// Remove BOM signature from the first item. |
|
569
|
|
|
if ( isset( $raw_data[0] ) ) { |
|
570
|
|
|
$raw_data[0] = $this->remove_utf8_bom( $raw_data[0] ); |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
return $raw_data; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Remove UTF-8 BOM signature. |
|
580
|
|
|
* |
|
581
|
|
|
* @since 1.8.14 |
|
582
|
|
|
* |
|
583
|
|
|
* @param string $string String to handle. |
|
584
|
|
|
* |
|
585
|
|
|
* @return string |
|
586
|
|
|
*/ |
|
587
|
|
|
public function remove_utf8_bom( $string ) { |
|
588
|
|
|
if ( 'efbbbf' === substr( bin2hex( $string ), 0, 6 ) ) { |
|
589
|
|
|
$string = substr( $string, 3 ); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
return $string; |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
|
|
596
|
|
|
/** |
|
597
|
|
|
* Is used to show the process when user upload the donor form. |
|
598
|
|
|
* |
|
599
|
|
|
* @since 1.8.14 |
|
600
|
|
|
*/ |
|
601
|
|
|
public function render_progress() { |
|
602
|
|
|
$step = $this->get_step(); |
|
603
|
|
|
?> |
|
604
|
|
|
<ol class="give-progress-steps"> |
|
605
|
|
|
<li class="<?php echo( 1 === $step ? 'active' : '' ); ?>"> |
|
|
|
|
|
|
606
|
|
|
<?php esc_html_e( 'Upload CSV file', 'give' ); ?> |
|
607
|
|
|
</li> |
|
608
|
|
|
<li class="<?php echo( 2 === $step ? 'active' : '' ); ?>"> |
|
|
|
|
|
|
609
|
|
|
<?php esc_html_e( 'Column mapping', 'give' ); ?> |
|
610
|
|
|
</li> |
|
611
|
|
|
<li class="<?php echo( 3 === $step ? 'active' : '' ); ?>"> |
|
|
|
|
|
|
612
|
|
|
<?php esc_html_e( 'Import', 'give' ); ?> |
|
613
|
|
|
</li> |
|
614
|
|
|
<li class="<?php echo( 4 === $step ? 'active' : '' ); ?>"> |
|
|
|
|
|
|
615
|
|
|
<?php esc_html_e( 'Done!', 'give' ); ?> |
|
616
|
|
|
</li> |
|
617
|
|
|
</ol> |
|
618
|
|
|
<?php |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Will return the import step. |
|
623
|
|
|
* |
|
624
|
|
|
* @since 1.8.14 |
|
625
|
|
|
* |
|
626
|
|
|
* @return int $step on which step doest the import is on. |
|
627
|
|
|
*/ |
|
628
|
|
|
public function get_step() { |
|
629
|
|
|
$step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 ); |
|
|
|
|
|
|
630
|
|
|
$on_step = 1; |
|
631
|
|
|
|
|
632
|
|
|
if ( empty( $step ) || 1 === $step ) { |
|
633
|
|
|
$on_step = 1; |
|
634
|
|
|
} elseif ( $this->check_for_dropdown_or_import() ) { |
|
635
|
|
|
$on_step = 3; |
|
636
|
|
|
} elseif ( 2 === $step ) { |
|
637
|
|
|
$on_step = 2; |
|
638
|
|
|
} elseif ( 4 === $step ) { |
|
639
|
|
|
$on_step = 4; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
return $on_step; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* Render donations import page |
|
647
|
|
|
* |
|
648
|
|
|
* @since 1.8.14 |
|
649
|
|
|
*/ |
|
650
|
|
|
public function render_page() { |
|
651
|
|
|
include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-donations.php'; |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
/** |
|
655
|
|
|
* Add CSV upload HTMl |
|
656
|
|
|
* |
|
657
|
|
|
* Print the html of the file upload from which CSV will be uploaded. |
|
658
|
|
|
* |
|
659
|
|
|
* @since 1.8.14 |
|
660
|
|
|
* @return void |
|
661
|
|
|
*/ |
|
662
|
|
|
public function render_media_csv() { |
|
663
|
|
|
?> |
|
664
|
|
|
<tr valign="top"> |
|
665
|
|
|
<th colspan="2"> |
|
666
|
|
|
<h2 id="give-import-title"><?php esc_html_e( 'Import donations from a CSV file', 'give' ) ?></h2> |
|
667
|
|
|
<p class="give-field-description"><?php esc_html_e( 'This tool allows you to import (or merge) donation data to give from a CSV file.', 'give' ) ?></p> |
|
668
|
|
|
</th> |
|
669
|
|
|
</tr> |
|
670
|
|
|
<?php |
|
671
|
|
|
$csv = ( isset( $_REQUEST['csv'] ) ? give_clean( $_POST['csv'] ) : '' ); |
|
|
|
|
|
|
672
|
|
|
$delimiter = ( isset( $_REQUEST['delimiter'] ) ? give_clean( $_POST['delimiter'] ) : ',' ); |
|
|
|
|
|
|
673
|
|
|
$mode = ( ! empty( $_REQUEST['mode'] ) ? 'on' : '' ); |
|
|
|
|
|
|
674
|
|
|
$create_user = ( isset( $_REQUEST['create_user'] ) && isset( $_REQUEST['csv'] ) && 1 === absint( $_REQUEST['create_user'] ) ? 'on' : ( isset( $_REQUEST['csv'] ) ? '' : 'on' ) ); |
|
|
|
|
|
|
675
|
|
|
$delete_csv = ( isset( $_REQUEST['delete_csv'] ) && isset( $_REQUEST['csv'] ) && 1 === absint( $_REQUEST['delete_csv'] ) ? 'on' : ( isset( $_REQUEST['csv'] ) ? '' : 'on' ) ); |
|
|
|
|
|
|
676
|
|
|
|
|
677
|
|
|
$settings = array( |
|
678
|
|
|
array( |
|
679
|
|
|
'id' => 'csv', |
|
680
|
|
|
'name' => __( 'Choose a CSV file:', 'give' ), |
|
681
|
|
|
'type' => 'file', |
|
682
|
|
|
'attributes' => array( 'editing' => 'false', 'library' => 'text' ), |
|
683
|
|
|
'fvalue' => 'id', |
|
684
|
|
|
'default' => $csv, |
|
685
|
|
|
), |
|
686
|
|
|
array( |
|
687
|
|
|
'id' => 'delimiter', |
|
688
|
|
|
'name' => __( 'CSV Delimiter:', 'give' ), |
|
689
|
|
|
'type' => 'text', |
|
690
|
|
|
'attributes' => array( 'placeholder' => ',', 'size' => '2' ), |
|
691
|
|
|
'default' => $delimiter, |
|
692
|
|
|
), |
|
693
|
|
|
array( |
|
694
|
|
|
'id' => 'mode', |
|
695
|
|
|
'name' => __( 'Test Mode:', 'give' ), |
|
696
|
|
|
'type' => 'checkbox', |
|
697
|
|
|
'default' => $mode, |
|
698
|
|
|
), |
|
699
|
|
|
array( |
|
700
|
|
|
'id' => 'create_user', |
|
701
|
|
|
'name' => __( 'Create WP users for new donors:', 'give' ), |
|
702
|
|
|
'type' => 'checkbox', |
|
703
|
|
|
'default' => $create_user, |
|
704
|
|
|
), |
|
705
|
|
|
array( |
|
706
|
|
|
'id' => 'delete_csv', |
|
707
|
|
|
'name' => __( 'Delete CSV after import:', 'give' ), |
|
708
|
|
|
'type' => 'checkbox', |
|
709
|
|
|
'default' => $delete_csv, |
|
710
|
|
|
), |
|
711
|
|
|
); |
|
712
|
|
|
|
|
713
|
|
|
$settings = apply_filters( 'give_import_file_upload_html', $settings ); |
|
714
|
|
|
|
|
715
|
|
|
Give_Admin_Settings::output_fields( $settings, 'give_settings' ); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
/** |
|
719
|
|
|
* Run when user click on the submit button. |
|
720
|
|
|
* |
|
721
|
|
|
* @since 1.8.14 |
|
722
|
|
|
*/ |
|
723
|
|
|
public function save() { |
|
724
|
|
|
// Get the current step. |
|
725
|
|
|
$step = Give_Import_Donations::get_step(); |
|
726
|
|
|
|
|
727
|
|
|
// Validation for first step. |
|
728
|
|
|
if ( 1 === $step ) { |
|
729
|
|
|
$csv = absint( $_POST['csv'] ); |
|
|
|
|
|
|
730
|
|
|
|
|
731
|
|
|
$has_error = $this->csv_check( $csv ); |
|
732
|
|
|
|
|
733
|
|
|
if ( false == $has_error ) { |
|
|
|
|
|
|
734
|
|
|
|
|
735
|
|
|
$url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array( |
|
736
|
|
|
'step' => '2', |
|
737
|
|
|
'importer-type' => $this->importer_type, |
|
738
|
|
|
'csv' => $csv, |
|
739
|
|
|
'delimiter' => ( isset( $_REQUEST['delimiter'] ) ) ? give_clean( $_REQUEST['delimiter'] ) : ',', |
|
|
|
|
|
|
740
|
|
|
'mode' => ( isset( $_REQUEST['mode'] ) ) ? give_clean( $_REQUEST['mode'] ) : '0', |
|
|
|
|
|
|
741
|
|
|
'create_user' => ( isset( $_REQUEST['create_user'] ) ) ? give_clean( $_REQUEST['create_user'] ) : '0', |
|
|
|
|
|
|
742
|
|
|
'delete_csv' => ( isset( $_REQUEST['delete_csv'] ) ) ? give_clean( $_REQUEST['delete_csv'] ) : '0', |
|
|
|
|
|
|
743
|
|
|
) ) ); |
|
744
|
|
|
?> |
|
745
|
|
|
<script type="text/javascript"> |
|
746
|
|
|
window.location = "<?php echo $url; ?>" |
|
|
|
|
|
|
747
|
|
|
</script> |
|
748
|
|
|
<?php |
|
749
|
|
|
} |
|
750
|
|
|
} |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
/** |
|
754
|
|
|
* Check if user uploaded csv is valid or not. |
|
755
|
|
|
* |
|
756
|
|
|
* @since 1.8.14 |
|
757
|
|
|
* |
|
758
|
|
|
* param int $csv Id of the CSV files. |
|
759
|
|
|
* |
|
760
|
|
|
* return bool $has_error CSV is valid or not. |
|
761
|
|
|
*/ |
|
762
|
|
|
public function csv_check( $csv = false ) { |
|
763
|
|
|
$has_error = false; |
|
764
|
|
|
if ( $csv ) { |
|
765
|
|
|
if ( ! wp_get_attachment_url( $csv ) ) { |
|
766
|
|
|
$has_error = true; |
|
767
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide the ID to a valid CSV file.', 'give' ) ); |
|
768
|
|
|
} elseif ( ( $mime_type = get_post_mime_type( $csv ) ) && ! strpos( $mime_type, 'csv' ) ) { |
|
769
|
|
|
$has_error = true; |
|
770
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide the ID to a valid CSV file.', 'give' ) ); |
|
771
|
|
|
} |
|
772
|
|
|
} else { |
|
773
|
|
|
$has_error = true; |
|
774
|
|
|
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide the ID to a valid CSV file.', 'give' ) ); |
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
|
|
return $has_error; |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
/** |
|
781
|
|
|
* Get settings array. |
|
782
|
|
|
* |
|
783
|
|
|
* @since 1.8.14 |
|
784
|
|
|
* @return array |
|
785
|
|
|
*/ |
|
786
|
|
|
public function get_settings() { |
|
787
|
|
|
// Hide save button. |
|
788
|
|
|
$GLOBALS['give_hide_save_button'] = true; |
|
789
|
|
|
|
|
790
|
|
|
/** |
|
791
|
|
|
* Filter the settings. |
|
792
|
|
|
* |
|
793
|
|
|
* @since 1.8.14 |
|
794
|
|
|
* |
|
795
|
|
|
* @param array $settings |
|
796
|
|
|
*/ |
|
797
|
|
|
$settings = apply_filters( |
|
798
|
|
|
'give_get_settings_' . $this->id, |
|
799
|
|
|
array( |
|
800
|
|
|
array( |
|
801
|
|
|
'id' => 'give_tools_import', |
|
802
|
|
|
'type' => 'title', |
|
803
|
|
|
'table_html' => false, |
|
804
|
|
|
), |
|
805
|
|
|
array( |
|
806
|
|
|
'id' => 'import', |
|
807
|
|
|
'name' => __( 'Import', 'give' ), |
|
808
|
|
|
'type' => 'tools_import', |
|
809
|
|
|
), |
|
810
|
|
|
array( |
|
811
|
|
|
'id' => 'give_tools_import', |
|
812
|
|
|
'type' => 'sectionend', |
|
813
|
|
|
'table_html' => false, |
|
814
|
|
|
), |
|
815
|
|
|
) |
|
816
|
|
|
); |
|
817
|
|
|
|
|
818
|
|
|
// Output. |
|
819
|
|
|
return $settings; |
|
820
|
|
|
} |
|
821
|
|
|
|
|
822
|
|
|
/** |
|
823
|
|
|
* Render report import field |
|
824
|
|
|
* |
|
825
|
|
|
* @since 1.8.14 |
|
826
|
|
|
* @access public |
|
827
|
|
|
* |
|
828
|
|
|
* @param $field |
|
829
|
|
|
* @param $option_value |
|
830
|
|
|
*/ |
|
831
|
|
|
public function render_import_field( $field, $option_value ) { |
|
832
|
|
|
include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-imports.php'; |
|
833
|
|
|
} |
|
834
|
|
|
|
|
835
|
|
|
/** |
|
836
|
|
|
* Get if current page import donations page or not |
|
837
|
|
|
* |
|
838
|
|
|
* @since 1.8.14 |
|
839
|
|
|
* @return bool |
|
840
|
|
|
*/ |
|
841
|
|
|
private function is_donations_import_page() { |
|
842
|
|
|
return 'import' === give_get_current_setting_tab() && |
|
843
|
|
|
isset( $_GET['importer-type'] ) && |
|
|
|
|
|
|
844
|
|
|
$this->importer_type === give_clean( $_GET['importer-type'] ); |
|
|
|
|
|
|
845
|
|
|
} |
|
846
|
|
|
} |
|
847
|
|
|
|
|
848
|
|
|
Give_Import_Donations::get_instance()->setup(); |
|
849
|
|
|
} |