index.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. defined('ABSPATH') || exit;
  3. require_once NEWSLETTER_INCLUDES_DIR . '/controls.php';
  4. $controls = new NewsletterControls();
  5. $module = NewsletterUsers::instance();
  6. $options = $controls->data;
  7. $options_profile = get_option('newsletter_profile');
  8. $options_main = get_option('newsletter_main');
  9. // Move to base zero
  10. if ($controls->is_action()) {
  11. if ($controls->is_action('reset')) {
  12. $controls->data = array();
  13. } else {
  14. $controls->data['search_page'] = (int) $controls->data['search_page'] - 1;
  15. }
  16. $module->save_options($controls->data, 'search');
  17. } else {
  18. $controls->data = $module->get_options('search');
  19. if (empty($controls->data['search_page']))
  20. $controls->data['search_page'] = 0;
  21. }
  22. if ($controls->is_action('resend')) {
  23. $user = $module->get_user($controls->button_data);
  24. NewsletterSubscription::instance()->send_message('confirmation', $user, true);
  25. $controls->messages = __('Activation email sent.', 'newsletter');
  26. }
  27. if ($controls->is_action('resend_welcome')) {
  28. $user = $module->get_user($controls->button_data);
  29. NewsletterSubscription::instance()->send_message('confirmed', $user, true);
  30. $controls->messages = __('Welcome email sent.', 'newsletter');
  31. }
  32. if ($controls->is_action('remove')) {
  33. $module->delete_user($controls->button_data);
  34. unset($controls->data['subscriber_id']);
  35. }
  36. if ($controls->is_action('delete_selected')) {
  37. $r = Newsletter::instance()->delete_user($_POST['ids']);
  38. $controls->messages .= $r . ' user(s) deleted';
  39. }
  40. // We build the query condition
  41. $where = 'where 1=1';
  42. $query_args = array();
  43. $text = trim($controls->get_value('search_text'));
  44. if ($text) {
  45. $query_args[] = '%' . $text . '%';
  46. $query_args[] = '%' . $text . '%';
  47. $query_args[] = '%' . $text . '%';
  48. $where .= " and (email like %s or name like %s or surname like %s)";
  49. }
  50. if (!empty($controls->data['search_status'])) {
  51. if ($controls->data['search_status'] == 'T') {
  52. $where .= " and test=1";
  53. } else {
  54. $query_args[] = $controls->data['search_status'];
  55. $where .= " and status=%s";
  56. }
  57. }
  58. if (!empty($controls->data['search_list'])) {
  59. $where .= " and list_" . ((int) $controls->data['search_list']) . "=1";
  60. }
  61. $filtered = $where != 'where 1=1';
  62. // Total items, total pages
  63. $items_per_page = 20;
  64. if (!empty($query_args)) {
  65. $where = $wpdb->prepare($where, $query_args);
  66. }
  67. $count = Newsletter::instance()->store->get_count(NEWSLETTER_USERS_TABLE, $where);
  68. $last_page = floor($count / $items_per_page) - ($count % $items_per_page == 0 ? 1 : 0);
  69. if ($last_page < 0)
  70. $last_page = 0;
  71. if ($controls->is_action('last')) {
  72. $controls->data['search_page'] = $last_page;
  73. }
  74. if ($controls->is_action('first')) {
  75. $controls->data['search_page'] = 0;
  76. }
  77. if ($controls->is_action('next')) {
  78. $controls->data['search_page'] = (int) $controls->data['search_page'] + 1;
  79. }
  80. if ($controls->is_action('prev')) {
  81. $controls->data['search_page'] = (int) $controls->data['search_page'] - 1;
  82. }
  83. if ($controls->is_action('search')) {
  84. $controls->data['search_page'] = 0;
  85. }
  86. // Eventually fix the page
  87. if (!isset($controls->data['search_page']) || $controls->data['search_page'] < 0)
  88. $controls->data['search_page'] = 0;
  89. if ($controls->data['search_page'] > $last_page)
  90. $controls->data['search_page'] = $last_page;
  91. $query = "select * from " . NEWSLETTER_USERS_TABLE . ' ' . $where . " order by id desc";
  92. $query .= " limit " . ($controls->data['search_page'] * $items_per_page) . "," . $items_per_page;
  93. $list = $wpdb->get_results($query);
  94. // Move to base 1
  95. $controls->data['search_page'] ++;
  96. ?>
  97. <div class="wrap tnp-users tnp-users-index" id="tnp-wrap">
  98. <?php include NEWSLETTER_DIR . '/tnp-header.php'; ?>
  99. <div id="tnp-heading">
  100. <h2><?php _e('Subscribers', 'newsletter') ?>
  101. <a class="tnp-btn-h1" href="?page=newsletter_users_new"><?php _e('Add a subscriber', 'newsletter') ?></a>
  102. </h2>
  103. </div>
  104. <div id="tnp-body">
  105. <form id="channel" method="post" action="">
  106. <?php $controls->init(); ?>
  107. <div class="tnp-subscribers-search">
  108. <?php $controls->text('search_text', 45, __('Search text', 'newsletter')); ?>
  109. <?php _e('filter by', 'newsletter') ?>:
  110. <?php $controls->select('search_status', array('' => 'Any status', 'T' => 'Test subscribers', 'C' => 'Confirmed', 'S' => 'Not confirmed', 'U' => 'Unsubscribed', 'B' => 'Bounced')); ?>
  111. <?php $controls->lists_select('search_list', '-'); ?>
  112. <?php $controls->button('search', __('Search', 'newsletter')); ?>
  113. <?php if ($where != "where 1=1") { ?>
  114. <?php $controls->button('reset', __('Reset Filters', 'newsletter')); ?>
  115. <?php } ?>
  116. <br>
  117. <?php $controls->checkbox('show_preferences', __('Show lists', 'newsletter')); ?>
  118. </div>
  119. <?php if ($filtered) { ?>
  120. <p><?php _e('The list below is filtered.', 'newsletter') ?></p>
  121. <?php } ?>
  122. <div class="tnp-paginator">
  123. <?php $controls->button('first', '«'); ?>
  124. <?php $controls->button('prev', '‹'); ?>
  125. <?php $controls->text('search_page', 3); ?> of <?php echo $last_page + 1 ?> <?php $controls->button('go', __('Go', 'newsletter')); ?>
  126. <?php $controls->button('next', '›'); ?>
  127. <?php $controls->button('last', '»'); ?>
  128. <?php echo $count ?> <?php _e('subscriber(s) found', 'newsletter') ?>
  129. <?php $controls->button_confirm('delete_selected', __('Delete selected', 'newsletter')); ?>
  130. </div>
  131. <table class="widefat">
  132. <thead>
  133. <tr>
  134. <th><input type="checkbox" onchange="jQuery('input.tnp-selector').prop('checked', this.checked)"</th>
  135. <th>Id</th>
  136. <th>Email</th>
  137. <th><?php _e('Name', 'newsletter') ?></th>
  138. <th><?php _e('Status', 'newsletter') ?></th>
  139. <?php if (isset($options['show_preferences']) && $options['show_preferences'] == 1) { ?>
  140. <th><?php _e('Lists', 'newsletter') ?></th>
  141. <?php } ?>
  142. <th>&nbsp;</th>
  143. <th>&nbsp;</th>
  144. <th>&nbsp;</th>
  145. </tr>
  146. </thead>
  147. <?php $i = 0; ?>
  148. <?php foreach ($list as $s) { ?>
  149. <tr class="<?php echo ($i++ % 2 == 0) ? 'alternate' : ''; ?>">
  150. <td><input class="tnp-selector" type="checkbox" name="ids[]" value="<?php echo $s->id; ?>"/></td>
  151. <td>
  152. <?php echo $s->id; ?>
  153. </td>
  154. <td>
  155. <?php echo esc_html($s->email); ?>
  156. </td>
  157. <td>
  158. <?php echo esc_html($s->name); ?> <?php echo esc_html($s->surname); ?>
  159. </td>
  160. <td>
  161. <small>
  162. <?php echo $module->get_user_status_label($s) ?>
  163. </small>
  164. </td>
  165. <?php if (isset($options['show_preferences']) && $options['show_preferences'] == 1) { ?>
  166. <td>
  167. <small>
  168. <?php
  169. $lists = $module->get_lists();
  170. foreach ($lists as $item) {
  171. $l = 'list_' . $item->id;
  172. if ($s->$l == 1)
  173. echo esc_html($item->name) . '<br>';
  174. }
  175. ?>
  176. </small>
  177. </td>
  178. <?php } ?>
  179. <td>
  180. <a class="button-secondary" href="<?php echo $module->get_admin_page_url('edit'); ?>&amp;id=<?php echo $s->id; ?>"><?php _e('Edit', 'newsletter') ?></a>
  181. </td>
  182. <td>
  183. <?php $controls->button_confirm('remove', __('Remove', 'newsletter'), '', $s->id); ?>
  184. </td>
  185. <td style="text-align: center">
  186. <?php if ($s->status == "C") { ?>
  187. <?php $controls->button_confirm('resend_welcome', __('Resend welcome', 'newsletter'), '', $s->id); ?>
  188. <?php } else { ?>
  189. <?php $controls->button_confirm('resend', __('Resend activation', 'newsletter'), '', $s->id); ?>
  190. <?php } ?>
  191. </td>
  192. </tr>
  193. <?php } ?>
  194. </table>
  195. <div class="tnp-paginator">
  196. <?php $controls->button('first', '«'); ?>
  197. <?php $controls->button('prev', '‹'); ?>
  198. <?php $controls->text('search_page', 3); ?> of <?php echo $last_page + 1 ?> <?php $controls->button('go', __('Go', 'newsletter')); ?>
  199. <?php $controls->button('next', '›'); ?>
  200. <?php $controls->button('last', '»'); ?>
  201. </div>
  202. </form>
  203. </div>
  204. <?php include NEWSLETTER_DIR . '/tnp-footer.php'; ?>
  205. </div>