users.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. defined('ABSPATH') || exit;
  3. require_once NEWSLETTER_INCLUDES_DIR . '/module.php';
  4. class NewsletterUsers extends NewsletterModule {
  5. static $instance;
  6. /**
  7. * @return NewsletterUsers
  8. */
  9. static function instance() {
  10. if (self::$instance == null) {
  11. self::$instance = new NewsletterUsers();
  12. }
  13. return self::$instance;
  14. }
  15. function __construct() {
  16. parent::__construct('users', '1.3.0');
  17. add_action('init', array($this, 'hook_init'));
  18. }
  19. function hook_init() {
  20. if (is_admin()) {
  21. add_action('wp_ajax_newsletter_users_export', array($this, 'hook_wp_ajax_newsletter_users_export'));
  22. }
  23. }
  24. function hook_wp_ajax_newsletter_users_export() {
  25. $newsletter = Newsletter::instance();
  26. if ($newsletter->is_allowed()) {
  27. require_once NEWSLETTER_INCLUDES_DIR . '/controls.php';
  28. $controls = new NewsletterControls();
  29. if ($controls->is_action('export')) {
  30. $this->export($controls->data);
  31. }
  32. } else {
  33. die('Not allowed.');
  34. }
  35. }
  36. function upgrade() {
  37. global $wpdb, $charset_collate;
  38. parent::upgrade();
  39. $sql = "CREATE TABLE `" . $wpdb->prefix . "newsletter` (
  40. `name` varchar(100) NOT NULL DEFAULT '',
  41. `email` varchar(100) NOT NULL DEFAULT '',
  42. `token` varchar(50) NOT NULL DEFAULT '',
  43. `language` varchar(10) NOT NULL DEFAULT '',
  44. `status` varchar(1) NOT NULL DEFAULT 'S',
  45. `id` int(11) NOT NULL AUTO_INCREMENT,
  46. `profile` mediumtext,
  47. `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  48. `updated` int(11) NOT NULL DEFAULT '0',
  49. `last_activity` int(11) NOT NULL DEFAULT '0',
  50. `followup_step` tinyint(4) NOT NULL DEFAULT '0',
  51. `followup_time` bigint(20) NOT NULL DEFAULT '0',
  52. `followup` tinyint(4) NOT NULL DEFAULT '0',
  53. `surname` varchar(100) NOT NULL DEFAULT '',
  54. `sex` char(1) NOT NULL DEFAULT 'n',
  55. `feed_time` bigint(20) NOT NULL DEFAULT '0',
  56. `feed` tinyint(4) NOT NULL DEFAULT '0',
  57. `referrer` varchar(50) NOT NULL DEFAULT '',
  58. `ip` varchar(50) NOT NULL DEFAULT '',
  59. `wp_user_id` int(11) NOT NULL DEFAULT '0',
  60. `http_referer` varchar(255) NOT NULL DEFAULT '',
  61. `geo` tinyint(4) NOT NULL DEFAULT '0',
  62. `country` varchar(4) NOT NULL DEFAULT '',
  63. `region` varchar(100) NOT NULL DEFAULT '',
  64. `city` varchar(100) NOT NULL DEFAULT '',
  65. `bounce_type` varchar(50) NOT NULL DEFAULT '',
  66. `bounce_time` int(11) NOT NULL DEFAULT '0',
  67. `unsub_email_id` int(11) NOT NULL DEFAULT '0',
  68. `unsub_time` int(11) NOT NULL DEFAULT '0',\n";
  69. for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {
  70. $sql .= "`list_$i` tinyint(4) NOT NULL DEFAULT '0',\n";
  71. }
  72. for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {
  73. $sql .= "`profile_$i` varchar(255) NOT NULL DEFAULT '',\n";
  74. }
  75. // Leave as last
  76. $sql .= "`test` tinyint(4) NOT NULL DEFAULT '0',\n";
  77. $sql .= "PRIMARY KEY (`id`),\nUNIQUE KEY `email` (`email`),\nKEY `wp_user_id` (`wp_user_id`)\n) $charset_collate;";
  78. dbDelta($sql);
  79. if ($this->old_version < '1.2.7') {
  80. $this->query("update " . NEWSLETTER_USERS_TABLE . " set geo=1 where country<>''");
  81. }
  82. if ($this->old_version > '1.2.5' && $this->old_version < '1.2.9') {
  83. $this->upgrade_query("ALTER TABLE " . NEWSLETTER_USERS_TABLE . " DROP COLUMN last_ip;");
  84. }
  85. }
  86. function admin_menu() {
  87. $this->add_menu_page('index', 'Subscribers');
  88. $this->add_admin_page('new', 'New subscriber');
  89. $this->add_admin_page('edit', 'Subscribers Edit');
  90. $this->add_admin_page('massive', 'Massive Management');
  91. $this->add_admin_page('export', 'Export');
  92. $this->add_admin_page('import', 'Import');
  93. $this->add_admin_page('statistics', 'Statistics');
  94. }
  95. function export($options = null) {
  96. global $wpdb;
  97. header('Content-Type: application/octet-stream');
  98. header('Content-Disposition: attachment; filename="newsletter-subscribers.csv"');
  99. // BOM
  100. echo "\xEF\xBB\xBF";
  101. $sep = ';';
  102. if ($options) {
  103. $sep = $options['separator'];
  104. }
  105. if ($sep == 'tab') {
  106. $sep = "\t";
  107. }
  108. // CSV header
  109. echo '"Email"' . $sep . '"Name"' . $sep . '"Surname"' . $sep . '"Gender"' . $sep . '"Status"' . $sep . '"Date"' . $sep . '"Token"' . $sep;
  110. // In table profiles
  111. for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {
  112. echo '"Profile ' . $i . '"' . $sep; // To adjust with field name
  113. }
  114. // Lists
  115. for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {
  116. echo '"List ' . $i . '"' . $sep;
  117. }
  118. echo '"Feed by mail"' . $sep . '"Follow up"' . $sep;
  119. echo '"IP"' . $sep . '"Referrer"' . $sep . '"Country"';
  120. echo "\n";
  121. $page = 0;
  122. while (true) {
  123. $query = "select * from " . NEWSLETTER_USERS_TABLE . "";
  124. $list = (int) $_POST['options']['list'];
  125. if (!empty($list)) {
  126. $query .= " where list_" . $list . "=1";
  127. }
  128. $recipients = $wpdb->get_results($query . " order by email limit " . $page * 500 . ",500");
  129. for ($i = 0; $i < count($recipients); $i++) {
  130. echo '"' . $recipients[$i]->email . '"' . $sep . '"' . $this->sanitize_csv($recipients[$i]->name) .
  131. '"' . $sep . '"' . $this->sanitize_csv($recipients[$i]->surname) .
  132. '"' . $sep . '"' . $recipients[$i]->sex .
  133. '"' . $sep . '"' . $recipients[$i]->status . '"' . $sep . '"' . $recipients[$i]->created . '"' . $sep . '"' . $recipients[$i]->token . '"' . $sep;
  134. for ($j = 1; $j <= NEWSLETTER_PROFILE_MAX; $j++) {
  135. $column = 'profile_' . $j;
  136. echo '"' . $this->sanitize_csv($recipients[$i]->$column) . '"' . $sep;
  137. }
  138. for ($j = 1; $j <= NEWSLETTER_LIST_MAX; $j++) {
  139. $list = 'list_' . $j;
  140. echo '"' . $recipients[$i]->$list . '"' . $sep;
  141. }
  142. echo '"' . $recipients[$i]->feed . '"' . $sep;
  143. echo '"' . $recipients[$i]->followup . '"' . $sep;
  144. echo '"' . $recipients[$i]->ip . '"' . $sep;
  145. echo '"' . $recipients[$i]->referrer . '"' . $sep;
  146. echo '"' . $recipients[$i]->country . '"' . $sep;
  147. echo "\n";
  148. flush();
  149. }
  150. if (count($recipients) < 500)
  151. break;
  152. $page++;
  153. }
  154. die();
  155. }
  156. function sanitize_csv($text) {
  157. $text = str_replace('"', "'", $text);
  158. $text = str_replace("\n", ' ', $text);
  159. $text = str_replace("\r", ' ', $text);
  160. $text = str_replace(";", ' ', $text);
  161. return $text;
  162. }
  163. }
  164. NewsletterUsers::instance();