-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespresso_import.php
400 lines (358 loc) · 18.9 KB
/
espresso_import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php defined('ABSPATH') || exit('No direct script access allowed');
/*
Plugin Name: Event Espresso - EE4 Voucher Import
Plugin URI: http://eventespresso.com/
Description: Allows the import of social coupons into Event Espresso 4
Version: 0.2
Author: Event Espresso
Author URI: http://www.eventespresso.com
Copyright (c) 2008-2015 Event Espresso All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
define("ESPRESSO_VOUCHER_IMPORT_PLUGINPATH", "/" . plugin_basename(dirname(__FILE__)) . "/");
define("ESPRESSO_VOUCHER_PLUGINFULLURL", WP_PLUGIN_URL . ESPRESSO_VOUCHER_IMPORT_PLUGINPATH);
function ee4_promo_import_admin_page()
{
global $ee4_promo_import;
$ee4_promo_import = add_options_page(
__('EE4 Promotions Import', 'event_espresso'),
__('EE4 Promo Import', 'event_espresso'),
'read',
'espresso_ee4_promo_import',
'espresso_ee4_promo_import'
);
}
add_action('admin_menu', 'ee4_promo_import_admin_page');
function espresso_ee4_promo_import()
{
// Set up max_file_size var.
$max_file_size = 1048576;
//Ensure EVENT_ESPRESSO_UPLOAD_DIR has been setup on this site and is writable.
EEH_File::ensure_folder_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR);
//Set form values.
$yes_no_values = array(
array('id' => true, 'text' => __('Yes', 'event_espresso')),
array('id' => false, 'text' => __('No', 'event_espresso'))
);
$type_values = array(
array('id' => 2, 'text' => __('Percent Discount', 'event_espresso')),
array('id' => 3, 'text' => __('Dollar Discount', 'event_espresso'))
);
$scope_values = array(
array('id' => 'Event', 'text' => __('Event', 'event_espresso')),
);
?>
<h3>EE4 Voucher Import</h3>
<ul>
<li>
<p>This page is for importing your voucher codes from a comma separated file (CSV) directly into the the database.</p>
<p style=" font-weight:bold">Usage:</p>
<ol>
<li>I have included a template file <a href="<?php echo ESPRESSO_VOUCHER_PLUGINFULLURL; ?>vouchers.csv">here</a> that I recommend you download and use. It is very easy to work with it in excel, just remember to save it as a csv and not excel sheet.</li>
<li>The file name should be vouchers.csv in order for it to work.</li>
<li>One final note, you will see that the header row, first column has a 0 while other rows have a 1. This tells the upload to ignore rows that have the 0 identifier and only use rows with the 1.</li>
</ol>
<?php voucher_uploader(1, array("csv"), $max_file_size, EVENT_ESPRESSO_UPLOAD_DIR); ?>
</li>
</ul>
<form action='admin.php?page=espresso_ee4_promo_import&action=voucher_import' method='post' enctype='multipart/form-data'>
<table class="form-table" id="promotion-details-form">
<tr>
<th scope="row">
<label for="PRC_name"><?php _e('Name', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<input type="text" class="regular-text" id="PRC_name" name="PRC_name">
</td>
</tr>
<tr>
<th scope="row">
<label for="PRT_ID"><?php _e('Type', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<?php echo EEH_Form_Fields::select_input('PRT_ID', $type_values, 2); ?>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRC_amount"><?php _e('Amount', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<input type="text" class="regular-text ee-numeric" id="PRC_amount" name="PRC_amount" value="100">
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_uses"><?php _e('Apply Promo to ALL Scope Items', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<?php echo EEH_Form_Fields::select_input('PRO_global', $yes_no_values, false); ?>
<p class="description"><?php _e('If set to "Yes" then this promotion will be applied to ALL items of the Scope type selected above, without having to manually select the individual items via the "Promotion applies to..." metabox in the sidebar.', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_scope"><?php _e('Scope (applied to)', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<?php echo EEH_Form_Fields::select_input('PRO_scope', $scope_values, true); ?>
<p class="description"><?php _e('This determines what type of items the promotion can be applied to (see sidebar to select items)', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_event_id"><?php _e('Event ID this Promotion applies to', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<input type="text" class="regular-text ee-numeric" id="PRO_event_id" name="PRO_event_id" value="">
<p class="description"><?php _e('Set the ID of the promotions should apply to', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_exclusive"><?php _e('Promo Is Exclusive', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<?php
echo EEH_Form_Fields::select_input('PRO_exclusive', $yes_no_values, true);
?>
<p class="description"><?php _e('If set to "Yes" then this promotion can not be combined with any other promotions', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_uses"><?php _e('Number of Uses', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<input type="text" class="regular-text ee-numeric" id="PRO_uses" name="PRO_uses" value="1">
<p class="description"><?php _e('per scope item (see above) - leave blank for no limit', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_start"><?php _e('Valid From', 'event_espresso'); ?></label>
</th>
<td class="field-column ee-date-column">
<input type="text" data-context="start" data-container="main" data-next-field="#PRO_end" class="regular-text ee-datepicker" id="PRO_start" name="PRO_start">
<p class="description"><?php _e('Date and time the promotion is valid from, must be in the format YYYY-MM-DD H:MM am/pm (Example - 2017-12-25 8:00 pm)', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_end"><?php _e('Valid Until', 'event_espresso'); ?></label>
</th>
<td class="field-column ee-date-column">
<input type="text" data-context="end" data-container="main" data-next-field="#PRO_uses" class="regular-text ee-datepicker" id="PRO_end" name="PRO_end">
<p class="description"><?php _e('Date and time the promotion is valid until, must be in the format YYYY-MM-DD H:MM am/pm (Example - 2017-12-25 8:00 pm)', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRC_desc"><?php _e('Banner Text / Description', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<textarea class="ee-full-textarea-inp" id="PRC_desc" name="PRC_desc"></textarea>
<p class="description"><?php _e('This is the text that will be displayed in the Promotion Banners if they are being used (see Settings Tab) as well as anywhere that the Promotion details are listed.', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_accept_msg"><?php _e('Accepted Message', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<textarea class="ee-full-textarea-inp" id="PRO_accept_msg" name="PRO_accept_msg"></textarea>
<p class="description"><?php _e('If using Promotion Codes, this will be shown when a code has been successfully verified and applied to a registrant\'s order.', 'event_espresso'); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="PRO_decline_msg"><?php _e('Declined Message', 'event_espresso'); ?></label>
</th>
<td class="field-column">
<textarea class="ee-full-textarea-inp" id="PRO_decline_msg" name="PRO_decline_msg"></textarea>
<p class="description"><?php _e('If using Promotion Codes, this will be shown when a code entered by a registrant can not be verified or applied to their order.', 'event_espresso'); ?></p>
</td>
</tr>
</table>
<p>Upload voucher CSV:</p>
<p><font color='red'>*</font><input type='file' name='file[]'>
<input type='hidden' name='submitted' value='true' id='<?php echo time(); ?>'>
<input name='action' type='hidden' value='voucher_import' />
<?php wp_nonce_field('espresso_voucher_import'); ?>
<input type='hidden' name='MAX_FILE_SIZE' value='"<?php echo $max_file_size; ?>"'>
<input class='button-primary' type='submit' value='Upload Vouchers'></p>
</form>
<?php
}
/*
* uploader([int num_uploads [, arr file_types [, int file_size [, str upload_dir ]]]]);
* num_uploads = Number of uploads to handle at once.
* file_types = An array of all the file types you wish to use. The default is txt only.
* file_size = The maximum file size of EACH file. A non-number will results in using the default 1mb filesize.
* upload_dir = The directory to upload to, make sure this ends with a /
*/
function voucher_uploader(
$num_of_uploads = 1,
$file_types_array = array("csv"),
$max_file_size = 1048576,
$upload_dir = "none",
$success_messages = '',
$error_messages = ''
) {
if (!is_numeric($max_file_size)) {
$max_file_size = 1048576;
}
if (!empty($_POST["submitted"])) {
foreach ($_FILES["file"]["error"] as $key => $value) {
if ($_FILES["file"]["name"][ $key ] != "") {
if ($value == UPLOAD_ERR_OK) {
$origfilename = $_FILES["file"]["name"][ $key ];
$filename = explode(".", $_FILES["file"]["name"][ $key ]);
$filenameext = $filename[ count($filename) - 1 ];
unset($filename[ count($filename) - 1 ]);
$filename = implode(".", $filename);
$filename = substr($filename, 0, 15) . "." . $filenameext;
$file_ext_allow = false;
for ($x = 0; $x < count($file_types_array); $x++) {
if ($filenameext == $file_types_array[ $x ]) {
$file_ext_allow = true;
}
}
if ($file_ext_allow) {
if ($_FILES["file"]["size"][ $key ] < $max_file_size) {
if (move_uploaded_file($_FILES["file"]["tmp_name"][ $key ], $upload_dir . $filename)) {
$success_messages .= "<p>File uploaded successfully. - <a href='" . $upload_dir . $filename . "' target='_blank'>" . $filename . "</a></p>";
} else {
$error_messages .= '<p>'.$origfilename . " was not successfully uploaded</p>";
}
} else {
$error_messages .= '<p>'.$origfilename . " was too big, not uploaded</p>";
}
} else {
$error_messages .= '<p>'.$origfilename . " had an invalid file extension, not uploaded</p>";
}
} else {
$error_messages .= '<p>The file was not successfully uploaded</p>';
}
}
}
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'voucher_import') {
load_vouchers_to_db($success_messages, $error_messages, $upload_dir, $filename);
}
}
function load_vouchers_to_db($success_messages, $error_messages, $upload_dir, $filename)
{
$retrieved_nonce = $_REQUEST['_wpnonce'];
if (! wp_verify_nonce($retrieved_nonce, 'espresso_voucher_import')) {
// Failed Nonce check, lets get out of here.
die('Failed security check, try again.');
}
global $wpdb;
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = $upload_dir . $filename;
if (!file_exists($csvfile)) {
$error_messages .= '<p>File not found. Make sure you specified the correct path.</p>';
espresso_display_voucher_import_messages($success_messages, $error_messages);
exit;
}
$file = fopen($csvfile, "r");
if (!$file) {
$error_messages .= '<p>Error opening data file.</p>';
espresso_display_voucher_import_messages($success_messages, $error_messages);
exit;
}
$size = filesize($csvfile);
if (!$size) {
$error_messages .= '<p>File is empty.</p>';
espresso_display_voucher_import_messages($success_messages, $error_messages);
exit;
}
$promo_codes = array();
for ($i = 0; $row = fgetcsv($file); ++$i) {
if ($row[0] == '1') {
$promo_codes[] = $row[1];
}
}
fclose($file);
$total_records = 0;
$PRC_ID_variable = null;
/*echo '<pre style="height:auto;border:2px solid lightblue;">' . print_r( $dataStrings, true ) . '</pre><br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>';die();*/
foreach ($promo_codes as $promo_code) {
// Add voucher data
$promotion_values = array(
'PRO_ID' => 0,
'PRO_code' => sanitize_text_field($promo_code),
'PRO_scope' => !empty($_POST['PRO_scope']) ? sanitize_text_field($_POST['PRO_scope']) : 'Event',
'PRO_start' => !empty($_POST['PRO_start']) ? sanitize_text_field($_POST['PRO_start']) : null,
'PRO_end' => !empty($_POST['PRO_end']) ? sanitize_text_field($_POST['PRO_end']) : null,
'PRO_global' => is_numeric($_POST['PRO_global']) ? true : false,
'PRO_exclusive' => is_numeric($_POST['PRO_exclusive']) ? true : false,
'PRO_uses' => !empty($_POST['PRO_uses']) && is_numeric($_POST['PRO_uses']) ? $_POST['PRO_uses'] : 1,
'PRO_accept_msg' => !empty($_POST['PRO_accept_msg']) ? sanitize_text_field($_POST['PRO_accept_msg']) : '',
'PRO_decline_msg' => !empty($_POST['PRO_decline_msg']) ? sanitize_text_field($_POST['PRO_decline_msg']) : ''
);
$promo_price_values = array(
// If this is not the first coupon insert, use the PRC_ID to group all the coupons together.
'PRC_ID' => $PRC_ID_variable != null ? $PRC_ID_variable : 0,
'PRC_name' => isset($_POST['PRC_name']) ? sanitize_text_field($_POST['PRC_name']) : __('Social Coupon Promotion', 'event_espresso'),
'PRT_ID' => !empty($_POST['PRT_ID']) && is_numeric($_POST['PRT_ID']) ? $_POST['PRT_ID'] : 2,
'PRC_amount' => !empty($_POST['PRC_amount']) && is_numeric($_POST['PRC_amount']) ? $_POST['PRC_amount'] : 100,
'PRC_desc' => !empty($_POST['PRC_desc']) ? sanitize_text_field($_POST['PRC_desc']) : ''
);
// first handle the price object
$price = EE_Price::new_instance($promo_price_values);
// save price
$price->save();
// next handle the promotions
$promotion = EE_Promotion::new_instance($promotion_values, null, array( 'Y-m-d', 'g:i a' ));
// new promotion so let's add the price id for the price relation
$promotion->set('PRC_ID', $price->ID());
// save promotion
$promotion->save();
$pro_objects = $promotion->promotion_objects();
// Add the promotion objects for the specific event these promotions are for.
if (!is_numeric($_POST['PRO_global']) && !empty($_POST['PRO_event_id'])) {
$promotion_obj = EE_Promotion_Object::new_instance(
array(
'PRO_ID' => $promotion->ID(),
'OBJ_ID' => is_numeric($_POST['PRO_event_id']) ? $_POST['PRO_event_id'] : 0,
'POB_type' => 'Event',
'POB_used' => 0
)
);
$promotion_obj->save();
}
// Store the new Price ID if this is the first coupon insert.
$PRC_ID_variable = $PRC_ID_variable == null ? $price->ID() : $PRC_ID_variable;
$total_records++;
}
unlink($csvfile);
if (!file_exists($csvfile)) {
$success_messages .= '<p>Temporary upload file has been successfully deleted.</p>';
}
$success_messages .='<p>Added a total of '.$total_records.' vouchers to the database.</p>';
espresso_display_voucher_import_messages($success_messages, $error_messages);
}
function espresso_display_voucher_import_messages($success_messages = '', $error_messages = '')
{
if ($success_messages != '') {
// showMessage( $success_messages );
echo '<div id="message1" class="updated fade"><p>' . $success_messages . '</p></div>';
}
if ($error_messages != '') {
// showMessage( $error_messages, true );
echo '<div id="message2" class="error fade fade-away"><p>' . $error_messages . '</p></div>';
}
}