-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.php
188 lines (156 loc) · 6.44 KB
/
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
<?php
@include_once 'commons.php';
$options = stripslashes_deep($_POST['options']);
$options_profile = get_option('newsletter_profile');
if ($action == 'import') {
@set_time_limit(100000);
$csv = stripslashes($_POST['csv']);
$lines = explode("\n", $csv);
$error = array();
// Subscriber array with base user data from selected lists and other default values
$subscriber = array();
for ($i=1; $i<=NEWSLETTER_LIST_MAX; $i++)
{
$list = 'list_' . $i;
if (isset($options[$list])) $subscriber[$list] = 1;
else {
if ($options['mode'] == 'overwrite') $subscriber[$list] = 0;
}
}
foreach ($lines as $line) {
// Parse the CSV line
$line = trim($line);
if ($line == '') continue;
if ($line[0] == '#') continue;
$separator = $options['separator'];
if ($separator == 'tab') $separator = "\t";
$data = explode($separator, $line);
// Builds a subscriber data structure
$subscriber['email'] = $newsletter->normalize_email($data[0]);
if (!$newsletter->is_email($subscriber['email']))
{
$error[] = '[INVALID EMAIL] ' . $line;
continue;
}
$subscriber['name'] = $newsletter->normalize_name($data[1]);
$subscriber['surname'] = $newsletter->normalize_name($data[2]);
for ($i=1; $i<=NEWSLETTER_PROFILE_MAX; $i++) {
if (isset($data[$i+2])) $subscriber['profile_' . $i] = $data[$i+2];
}
// May by here for a previous saving
unset($subscriber['id']);
// Try to load the user by email
$user = $wpdb->get_row($wpdb->prepare("select * from " . $wpdb->prefix .
"newsletter where email=%s", $subscriber['email']), ARRAY_A);
// If the user is new, we simply add it
if (empty($user)) {
newsletter_save($subscriber);
continue;
}
if ($options['mode'] == 'skip') {
$error[] = '[DUPLICATE] ' . $line;
continue;
}
if ($options['mode'] == 'overwrite') {
$subscriber['id'] = $user['id'];
newsletter_save($subscriber);
continue;
}
if ($options['mode'] == 'update') {
newsletter_save(array_merge($user, $subscriber));
}
}
}
$nc = new NewsletterControls();
$nc->errors($errors);
$nc->messages($messages);
$lists = array('0' => 'All');
for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {
$lists['' . $i] = '(' . $i . ') ' . $options_profile['list_' . $i];
}
?>
<div class="wrap">
<h2>Newsletter Import/Export</h2>
<form method="post" action="">
<?php $nc->init(); ?>
<h3>Export</h3>
<table class="form-table">
<tr>
<td>
<?php $nc->select('list', $lists); ?>
<?php $nc->button('export', 'Export'); ?>
</td>
</tr>
</table>
</form>
<h3>Import</h3>
<p>
Please consider to break up your input list if you get errors, blank pages or partially imported lists: it can be a time/resource limit
of your provider. It's safe to import the same list a second time, no duplications will occur.
</p>
<p>
Import list format is:<br /><br />
<b>email 1</b><i>[separator]</i><b>first name 1</b><i>[separator]</i><b>last name 1</b><i>[new line]</i><br />
<b>email 2</b><i>[separator]</i><b>first name 2</b><i>[separator]</i><b>last name 2</b><i>[new line]</i><br />
<br />
where the [separator] must be selected from the available ones. The "name" field is optional, while the "email" field is
mandatory. Empty lines and lines starting with "#" will be skipped.
</p>
<?php require_once 'header.php'; ?>
<?php if (!empty($error)) { ?>
<h3><?php _e('Rows with errors', 'newsletter'); ?></h3>
<textarea wrap="off" style="width: 100%; height: 150px; font-size: 11px; font-family: monospace"><?php echo htmlspecialchars(implode("\n", $error))?></textarea>
<?php } ?>
<form method="post" action="">
<?php $nc->init(); ?>
<h3><?php _e('CSV text with subscribers', 'newsletter'); ?></h3>
<table class="form-table">
<tr valign="top">
<th>Lists to associate</th>
<td>
<?php for ($i=1; $i<=NEWSLETTER_LIST_MAX; $i++) { ?>
<?php $nc->checkbox('list_' . $i, '(' . $i . ') ' . htmlspecialchars($options_profile['list_' . $i])); ?><br />
<?php } ?>
<div class="hints">
Every new imported or updated subscribers will be associate with selected lists above.
</div>
</td>
</tr>
<!--
<tr valign="top">
<th>Follow up</th>
<td>
<?php $nc->select('followup', array('none'=>'None', 'activate'=>'Activate')); ?>
</td>
</tr>
-->
<tr valign="top">
<th>Import mode</th>
<td>
If the email is already present:
<?php $nc->select('mode', array('update'=>'Update', 'overwrite'=>'Overwrite', 'skip'=>'Skip')); ?>
<div class="hints">
<strong>Update</strong>: the user data is updated: if a user was associated to some lists, those associations will be
kept and the new one added; his name is updated as well.<br />
<strong>Overwrite</strong>: recreate the subscriber with specified data (CSV+lists).<br />
<strong>Skip</strong>: leave untouched user's data when he's already subscribed.
</div>
</td>
</tr>
<tr valign="top">
<th>Separator</th>
<td>
<?php $nc->select('separator', array(';'=>'Semicolon', ','=>'Comma', 'tab'=>'Tabulation')); ?>
</td>
</tr>
<tr valign="top">
<th>CSV text</th>
<td>
<textarea name="csv" wrap="off" style="width: 100%; height: 300px; font-size: 11px; font-family: monospace"></textarea>
</td>
</table>
<p class="submit">
<?php $nc->button('import', 'Import'); ?>
</p>
</form>
</div>