edit.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. defined('ABSPATH') || exit;
  3. require_once NEWSLETTER_INCLUDES_DIR . '/controls.php';
  4. $controls = new NewsletterControls();
  5. $module = NewsletterUsers::instance();
  6. $id = (int) $_GET['id'];
  7. $user = $module->get_user($id);
  8. if ($controls->is_action('save')) {
  9. $email = $module->normalize_email($controls->data['email']);
  10. if (empty($email)) {
  11. $controls->errors = __('Wrong email address', 'newsletter');
  12. } else {
  13. $controls->data['email'] = $email;
  14. }
  15. if (empty($controls->errors)) {
  16. $u = $module->get_user($controls->data['email']);
  17. if ($u && $u->id != $id) {
  18. $controls->errors = __('The email address is already in use', 'newsletter');
  19. }
  20. }
  21. if (empty($controls->errors)) {
  22. // For unselected preferences, force the zero value
  23. for ($i = 1; $i <= NEWSLETTER_LIST_MAX; $i++) {
  24. if (!isset($controls->data['list_' . $i])) {
  25. $controls->data['list_' . $i] = 0;
  26. }
  27. }
  28. if (empty($controls->data['token'])) {
  29. $controls->data['token'] = $module->get_token();
  30. }
  31. $controls->data['id'] = $id;
  32. $user = $module->save_user($controls->data);
  33. $module->add_user_log($user, 'edit');
  34. if ($user === false) {
  35. $controls->errors = __('Error. Check the log files.', 'newsletter');
  36. } else {
  37. $controls->add_message_saved();
  38. $controls->data = (array) $user;
  39. }
  40. }
  41. }
  42. if ($controls->is_action('delete')) {
  43. $module->delete_user($id);
  44. $controls->js_redirect($module->get_admin_page_url('index'));
  45. return;
  46. }
  47. if (!$controls->is_action()) {
  48. $controls->data = (array) $user;
  49. }
  50. $options_profile = NewsletterSubscription::instance()->get_options('profile');
  51. function percent($value, $total) {
  52. if ($total == 0) {
  53. return '-';
  54. }
  55. return sprintf("%.2f", $value / $total * 100) . '%';
  56. }
  57. function percentValue($value, $total) {
  58. if ($total == 0) {
  59. return 0;
  60. }
  61. return round($value / $total * 100);
  62. }
  63. ?>
  64. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  65. <script type="text/javascript">
  66. google.charts.load('current', {'packages': ['corechart', 'geomap']});
  67. </script>
  68. <div class="wrap tnp-users tnp-users-edit" id="tnp-wrap">
  69. <?php include NEWSLETTER_DIR . '/tnp-header.php'; ?>
  70. <div id="tnp-heading">
  71. <h2><?php _e('Editing', 'newsletter') ?> <?php echo esc_html($controls->data['email']) ?></h2>
  72. </div>
  73. <div id="tnp-body">
  74. <form method="post" action="">
  75. <p>
  76. <?php $controls->button_back('?page=newsletter_users_index'); ?>
  77. <?php $controls->button_save(); ?>
  78. </p>
  79. <?php $controls->init(); ?>
  80. <div id="tabs">
  81. <ul>
  82. <li><a href="#tabs-general"><?php _e('General', 'newsletter') ?></a></li>
  83. <li><a href="#tabs-preferences"><?php _e('Lists', 'newsletter') ?></a></li>
  84. <li><a href="#tabs-profile"><?php _e('Extra fields', 'newsletter') ?></a></li>
  85. <li><a href="#tabs-other"><?php _e('Other', 'newsletter') ?></a></li>
  86. <li><a href="#tabs-newsletters"><?php _e('Newsletters', 'newsletter') ?></a></li>
  87. <li><a href="#tabs-history"><?php _e('Logs', 'newsletter') ?></a></li>
  88. </ul>
  89. <div id="tabs-general" class="tnp-tab">
  90. <?php do_action('newsletter_users_edit_general', $id, $controls) ?>
  91. <table class="form-table">
  92. <tr>
  93. <th><?php _e('Email', 'newsletter'); ?></th>
  94. <td>
  95. <?php $controls->text_email('email', 60); ?>
  96. </td>
  97. </tr>
  98. <tr>
  99. <th><?php _e('First name', 'newsletter'); ?></th>
  100. <td>
  101. <?php $controls->text('name', 50); ?>
  102. </td>
  103. </tr>
  104. <tr>
  105. <th><?php _e('Last name', 'newsletter'); ?></th>
  106. <td>
  107. <?php $controls->text('surname', 50); ?>
  108. </td>
  109. </tr>
  110. <tr>
  111. <th><?php _e('Gender', 'newsletter'); ?></th>
  112. <td>
  113. <?php $controls->select('sex', array('n' => 'Not specified', 'f' => 'female', 'm' => 'male')); ?>
  114. </td>
  115. </tr>
  116. <tr>
  117. <th><?php _e('Status', 'newsletter'); ?></th>
  118. <td>
  119. <?php $controls->select('status', array('C' => 'Confirmed', 'S' => 'Not confirmed', 'U' => 'Unsubscribed', 'B' => 'Bounced')); ?>
  120. </td>
  121. </tr>
  122. <tr>
  123. <th><?php _e('Language', 'newsletter'); ?></th>
  124. <td>
  125. <?php $controls->language(); ?>
  126. </td>
  127. </tr>
  128. <tr>
  129. <th><?php _e('Test subscriber', 'newsletter'); ?>
  130. <br><?php $controls->help('https://www.thenewsletterplugin.com/documentation/subscribers#test-subscribers') ?></th>
  131. <td>
  132. <?php $controls->yesno('test'); ?>
  133. </td>
  134. </tr>
  135. <?php do_action('newsletter_user_edit_extra', $controls); ?>
  136. <tr>
  137. <th>Feed by mail</th>
  138. <td>
  139. <?php $controls->yesno('feed'); ?>
  140. </td>
  141. </tr>
  142. </table>
  143. </div>
  144. <div id="tabs-preferences" class="tnp-tab">
  145. <table class="form-table">
  146. <tr>
  147. <th><?php _e('Lists', 'newsletter') ?><br><?php echo $controls->help('https://www.thenewsletterplugin.com/plugins/newsletter/newsletter-preferences') ?></th>
  148. <td>
  149. <?php $controls->preferences('list'); ?>
  150. </td>
  151. </tr>
  152. </table>
  153. </div>
  154. <div id="tabs-profile" class="tnp-tab">
  155. <table class="widefat">
  156. <thead>
  157. <tr>
  158. <th>#</th>
  159. <th><?php _e('Name', 'newsletter'); ?></th>
  160. <th><?php _e('Value', 'newsletter'); ?></th>
  161. </tr>
  162. </thead>
  163. <tbody>
  164. <?php
  165. for ($i = 1; $i <= NEWSLETTER_PROFILE_MAX; $i++) {
  166. echo '<tr><td>';
  167. echo $i;
  168. echo '</td><td>';
  169. echo esc_html($options_profile['profile_' . $i]);
  170. echo '</td><td>';
  171. $controls->text('profile_' . $i, 70);
  172. echo '</td></tr>';
  173. }
  174. ?>
  175. </tbody>
  176. </table>
  177. </div>
  178. <div id="tabs-other" class="tnp-tab">
  179. <table class="form-table">
  180. <tr>
  181. <th>ID</th>
  182. <td>
  183. <?php $controls->value('id'); ?>
  184. </td>
  185. </tr>
  186. <tr>
  187. <th><?php _e('Created', 'newsletter') ?></th>
  188. <td>
  189. <?php echo $controls->print_date(strtotime($controls->data['created'])); ?>
  190. </td>
  191. </tr>
  192. <tr>
  193. <th><?php _e('Last activity', 'newsletter') ?></th>
  194. <td>
  195. <?php echo $controls->print_date($controls->data['last_activity']); ?>
  196. </td>
  197. </tr>
  198. <tr>
  199. <th><?php _e('WP user ID', 'newsletter') ?></th>
  200. <td>
  201. <?php $controls->text('wp_user_id'); ?>
  202. </td>
  203. </tr>
  204. <tr>
  205. <th><?php _e('IP address', 'newsletter'); ?></th>
  206. <td>
  207. <?php $controls->value('ip'); ?>
  208. </td>
  209. </tr>
  210. <tr>
  211. <th><?php _e('Secret token', 'newsletter'); ?></th>
  212. <td>
  213. <?php $controls->text('token', 50); ?>
  214. </td>
  215. </tr>
  216. <tr>
  217. <th><?php _e('Profile URL', 'newsletter'); ?></th>
  218. <td>
  219. <?php $profile_url = NewsletterProfile::instance()->get_profile_url($user) ?>
  220. <a href='<?php echo $profile_url ?>' target="_blank"><?php echo $profile_url ?></a>
  221. </td>
  222. </tr>
  223. </table>
  224. </div>
  225. <div id="tabs-newsletters" class="tnp-tab">
  226. <?php if (!has_action('newsletter_user_newsletters_tab') && !has_action('newsletter_users_edit_newsletters')) { ?>
  227. <div class="tnp-tab-notice">
  228. This panel requires the <a href="https://www.thenewsletterplugin.com/plugins/newsletter/reports-module" target="_blank">Reports Extension 4+</a>.
  229. </div>
  230. <?php
  231. } else {
  232. do_action('newsletter_user_newsletters_tab', $id);
  233. do_action('newsletter_users_edit_newsletters', $id);
  234. }
  235. ?>
  236. </div>
  237. <div id="tabs-history" class="tnp-tab">
  238. <?php
  239. $logs = $wpdb->get_results($wpdb->prepare("select * from {$wpdb->prefix}newsletter_user_logs where user_id=%d order by id desc", $id));
  240. ?>
  241. <?php if (empty($logs)) { ?>
  242. <p>No logs available</p>
  243. <?php } else { ?>
  244. <p>Only public lists are recorded.</p>
  245. <table class="widefat" style="width: auto">
  246. <thead>
  247. <tr>
  248. <th>Date</th>
  249. <th>Source</th>
  250. <th>IP</th>
  251. <th>Lists</th>
  252. </tr>
  253. <tbody>
  254. <?php foreach ($logs as $log) { ?>
  255. <?php
  256. $data = json_decode($log->data, ARRAY_A);
  257. if (isset($data['new']))
  258. $data = $data['new'];
  259. ?>
  260. <tr>
  261. <td><?php echo $controls->print_date($log->created) ?></td>
  262. <td><?php echo esc_html($log->source) ?></td>
  263. <td><?php echo esc_html($log->ip) ?></td>
  264. <td>
  265. <?php
  266. if (is_array($data)) {
  267. foreach ($data as $key => $value) {
  268. echo esc_html(str_replace('_', ' ', $key)), ': ', esc_html($value) . '<br>';
  269. }
  270. }
  271. ?>
  272. </td>
  273. </tr>
  274. <?php } ?>
  275. </tbody>
  276. </table>
  277. <?php } ?>
  278. </div>
  279. </div>
  280. <p>
  281. <?php $controls->button_save(); ?>
  282. <?php $controls->button_delete(); ?>
  283. </p>
  284. </form>
  285. </div>
  286. <?php include NEWSLETTER_DIR . '/tnp-footer.php'; ?>
  287. </div>