import.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. if (!defined('ABSPATH'))
  3. exit;
  4. require_once NEWSLETTER_INCLUDES_DIR . '/controls.php';
  5. $controls = new NewsletterControls();
  6. $module = NewsletterUsers::instance();
  7. $options_profile = get_option('newsletter_profile');
  8. if ($controls->is_action('import')) {
  9. $mode = $controls->data['mode'];
  10. // TODO: to be removed, it's not safe
  11. @set_time_limit(0);
  12. $results = '';
  13. if (is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
  14. $lines = file($_FILES['csv_file']['tmp_name']);
  15. } else {
  16. $csv = stripslashes($controls->data['csv']);
  17. $lines = explode("\n", $csv);
  18. }
  19. // Set the selected preferences inside the
  20. if (!isset($controls->data['preferences']) || !is_array($controls->data['preferences']))
  21. $controls->data['preferences'] = array();
  22. // if ($options['followup'] == 'activate') {
  23. // $subscriber['followup'] = 1;
  24. // }
  25. $error_count = 0;
  26. $added_count = 0;
  27. $updated_count = 0;
  28. $skipped_count = 0;
  29. foreach ($lines as &$line) {
  30. // Parse the CSV line
  31. $line = trim($line);
  32. if ($line == '') {
  33. continue;
  34. }
  35. if ($line[0] == '#' || $line[0] == ';') {
  36. continue;
  37. }
  38. $separator = $controls->data['separator'];
  39. if ($separator == 'tab') {
  40. $separator = "\t";
  41. }
  42. $data = explode($separator, $line);
  43. // Builds a subscriber data structure
  44. $email = $newsletter->normalize_email($data[0]);
  45. if (empty($email)) {
  46. continue;
  47. }
  48. if (!$newsletter->is_email($email)) {
  49. $results .= '[INVALID EMAIL] ' . $line . "\n";
  50. $error_count++;
  51. continue;
  52. }
  53. $subscriber = $module->get_user($email, ARRAY_A);
  54. if ($subscriber == null) {
  55. $subscriber = array();
  56. $subscriber['email'] = $email;
  57. if (isset($data[1])) {
  58. $subscriber['name'] = $module->normalize_name($data[1]);
  59. }
  60. if (isset($data[2])) {
  61. $subscriber['surname'] = $module->normalize_name($data[2]);
  62. }
  63. if (isset($data[3])) {
  64. $subscriber['sex'] = $module->normalize_sex($data[3]);
  65. }
  66. $subscriber['status'] = $controls->data['import_as'];
  67. foreach ($controls->data['preferences'] as $i) {
  68. $subscriber['list_' . $i] = 1;
  69. }
  70. $module->save_user($subscriber);
  71. $results .= '[ADDED] ' . $line . "\n";
  72. $added_count++;
  73. } else {
  74. if ($mode == 'skip') {
  75. $results .= '[SKIPPED] ' . $line . "\n";
  76. $skipped_count++;
  77. continue;
  78. }
  79. if ($mode == 'overwrite') {
  80. if (isset($data[1])) {
  81. $subscriber['name'] = $module->normalize_name($data[1]);
  82. }
  83. if (isset($data[2])) {
  84. $subscriber['surname'] = $module->normalize_name($data[2]);
  85. }
  86. if (isset($data[3])) {
  87. $subscriber['sex'] = $module->normalize_sex($data[3]);
  88. }
  89. if (isset($controls->data['override_status'])) {
  90. $subscriber['status'] = $controls->data['import_as'];
  91. }
  92. // Prepare the preference to zero
  93. for ($i = 1; $i < NEWSLETTER_LIST_MAX; $i++) {
  94. $subscriber['list_' . $i] = 0;
  95. }
  96. foreach ($controls->data['preferences'] as $i) {
  97. $subscriber['list_' . $i] = 1;
  98. }
  99. }
  100. if ($mode == 'update') {
  101. $subscriber['name'] = $module->normalize_name($data[1]);
  102. $subscriber['surname'] = $module->normalize_name($data[2]);
  103. if (isset($data[3])) {
  104. $subscriber['sex'] = $module->normalize_sex($data[3]);
  105. }
  106. if (isset($controls->data['override_status'])) {
  107. $subscriber['status'] = $controls->data['import_as'];
  108. }
  109. foreach ($controls->data['preferences'] as $i) {
  110. $subscriber['list_' . $i] = 1;
  111. }
  112. }
  113. NewsletterUsers::instance()->save_user($subscriber);
  114. $results .= '[UPDATED] ' . $line . "\n";
  115. $updated_count++;
  116. }
  117. }
  118. if ($error_count) {
  119. $controls->errors = "Import completed but with errors.";
  120. }
  121. $controls->messages = "Import completed: $error_count errors, $added_count added, $updated_count updated, $skipped_count skipped.";
  122. }
  123. ?>
  124. <div class="wrap" id="tnp-wrap">
  125. <?php include NEWSLETTER_DIR . '/tnp-header.php'; ?>
  126. <div id="tnp-heading">
  127. <h2><?php _e('Import', 'newsletter') ?></h2>
  128. <p>
  129. The import and export functions <strong>ARE NOT for backup</strong>. If you want to backup you should consider to backup the
  130. wp_newsletter* tables. Please, read on bottom of this page the data format to use and other important notes.</p>
  131. </div>
  132. <div id="tnp-body" class="tnp-users tnp-users-import">
  133. <?php if (!empty($results)) { ?>
  134. <h3>Results</h3>
  135. <textarea wrap="off" style="width: 100%; height: 150px; font-size: 11px; font-family: monospace"><?php echo esc_html($results) ?></textarea>
  136. <?php } ?>
  137. <form method="post" enctype="multipart/form-data">
  138. <?php $controls->init(); ?>
  139. <table class="form-table">
  140. <tr>
  141. <th><?php _e('Import Subscribers As', 'newsletter') ?></th>
  142. <td>
  143. <?php $controls->select('import_as', array('C' => __('Confirmed', 'newsletter'), 'S' => __('Not confirmed', 'newsletter'))); ?>
  144. <br>
  145. <?php $controls->checkbox('override_status', __('Override status of existing users', 'newsletter')) ?>
  146. </td>
  147. </tr>
  148. <tr>
  149. <th><?php _e('Import mode', 'newsletter') ?></th>
  150. <td>
  151. <?php $controls->select('mode', array('update' => 'Update', 'overwrite' => 'Overwrite', 'skip' => 'Skip')); ?>
  152. if email is already present
  153. <p class="description">
  154. <strong>Update</strong>: <?php _e('user data will be updated, existing preferences will be left untouched and new ones will be added.', 'newsletter') ?><br />
  155. <strong>Overwrite</strong>: <?php _e('user data will be overwritten with new informations (like name and preferences).', 'newsletter') ?><br />
  156. <strong>Skip</strong>: <?php _e('user data will be left untouched if already present.', 'newsletter') ?>
  157. </p>
  158. </td>
  159. </tr>
  160. <tr>
  161. <th><?php _e('Lists', 'newsletter') ?></th>
  162. <td>
  163. <?php $controls->preferences_group('preferences', true); ?>
  164. <div class="hints">
  165. Every new imported or updated subscriber will be associate with selected preferences above.
  166. </div>
  167. </td>
  168. </tr>
  169. <tr>
  170. <th><?php _e('Field separator', 'newsletter') ?></th>
  171. <td>
  172. <?php $controls->select('separator', array(';' => 'Semicolon', ',' => 'Comma', 'tab' => 'Tabulation')); ?>
  173. </td>
  174. </tr>
  175. <tr>
  176. <th>
  177. <?php _e('CSV file', 'newsletter') ?>
  178. <div class="tnp-tip">
  179. <span class="tip-button">Tip</span>
  180. <span class="tip-content">
  181. Upload a CSV file, see format description <a href="#import_format">below</a>.
  182. </span>
  183. </div>
  184. </th>
  185. <td>
  186. <input type="file" name="csv_file" />
  187. </td>
  188. </tr>
  189. <tr>
  190. <th>CSV text
  191. <div class="tnp-tip">
  192. <span class="tip-button">Tip</span>
  193. <span class="tip-content">
  194. Simply paste CSV text here.
  195. </span>
  196. </div>
  197. </th>
  198. <td>
  199. <textarea name="options[csv]" wrap="off" style="width: 100%; height: 200px; font-size: 11px; font-family: monospace"><?php echo $controls->data['csv']; ?></textarea>
  200. </td>
  201. </tr>
  202. <tr>
  203. <th>&nbsp;</th><td><?php $controls->button('import', 'Import'); ?></td>
  204. </tr>
  205. <tr>
  206. <th>
  207. <a name="import_format"></a>
  208. Data format<br>and other notes
  209. <div class="tnp-tip">
  210. <span class="tip-button">Tip</span>
  211. <span class="tip-content">Consider to split up your input list if you get errors, blank pages or partially imported lists: it can be a time/resource limit
  212. of your provider. It's safe to import the same list a second time, no duplications will occur.</span>
  213. </th>
  214. <td>
  215. <p>
  216. Import list format is:
  217. <p><strong>email</strong><i>[separator]</i><strong>first name</strong><i>[separator]</i><strong>last name</strong><i>[separator]</i><strong>gender</strong><i>[new line]</i></p>
  218. Example:
  219. <p style="border: 1px solid #bfbfbf">
  220. email1@example.com;first name 1;last name 1;m<br />
  221. email2@example.com;first name 2;last name 2;f
  222. </p>
  223. <p>
  224. where [separator] must be selected from the available ones. Empty lines and lines starting with "#" will be skipped. There is
  225. no separator escaping mechanism, so be sure that field values do not contain the selected separator. The only required field is the email
  226. all other fields are options. Gender must be "m" or "f".
  227. </p>
  228. </td>
  229. </tr>
  230. </table>
  231. </form>
  232. </div>
  233. <?php include NEWSLETTER_DIR . '/tnp-footer.php'; ?>
  234. </div>