Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. <?php
  2. global $wpdb;
  3.  
  4. $default_from = '2017-12-14';
  5. $default_to = current_time('Y-m-d');
  6. if (isset($_GET['reset'])) {
  7. $_SESSION['reports']['contacts']['from'] = $default_from;
  8. $_SESSION['reports']['contacts']['to'] = $default_to;
  9. wp_redirect(admin_url('admin.php?page=reports'));
  10. exit;
  11. }
  12. $user = isset($_GET['user']) ? intval($_GET['user']) : 0;
  13.  
  14. if (!empty($_POST['date_from']) && !empty($_POST['date_to'])) {
  15. $_SESSION['reports']['contacts']['from'] = date('Y-m-d', strtotime($_POST['date_from'], current_time('timestamp')));
  16. $_SESSION['reports']['contacts']['to'] = date('Y-m-d', strtotime($_POST['date_to'], current_time('timestamp')));
  17. } else if (empty($_SESSION['reports']['contacts']['from']) || empty($_SESSION['reports']['contacts']['to'])) {
  18. $_SESSION['reports']['contacts']['from'] = $default_from;
  19. $_SESSION['reports']['contacts']['to'] = $default_to;
  20. }
  21.  
  22. // get counts by author
  23. $sql = "SELECT COUNT(*) AS contact_count, display_name, post_author FROM {$wpdb->posts}
  24. LEFT JOIN {$wpdb->users} ON {$wpdb->users}.ID = post_author
  25. WHERE post_type = 'contact' AND post_status = 'publish'
  26. AND post_date BETWEEN '{$_SESSION['reports']['contacts']['from']} 00:00:00' AND '{$_SESSION['reports']['contacts']['to']} 23:59:59'
  27. GROUP BY post_author ORDER BY display_name";
  28. $users = $wpdb->get_results($sql);
  29. $total_users = count($users);
  30. $total_count = array_reduce($users, function($carry, $item) {
  31. return $carry + $item->contact_count;
  32. }, 0);
  33. $user_filter = $user ? 'AND post_author = ' . $user : '';
  34.  
  35. // get contacts by author
  36. $sql = "SELECT {$wpdb->posts}.*, display_name FROM {$wpdb->posts}
  37. LEFT JOIN {$wpdb->users} ON {$wpdb->users}.ID = post_author
  38. WHERE post_type = 'contact' AND post_status = 'publish' $user_filter
  39. AND post_date BETWEEN '{$_SESSION['reports']['contacts']['from']} 00:00:00' AND '{$_SESSION['reports']['contacts']['to']} 23:59:59'
  40. ORDER BY display_name, post_date DESC, post_title";
  41. $results = $wpdb->get_results($sql);
  42. $total = count($results);
  43. $table_nav = '
  44. <div class="alignleft actions">
  45. <form action="" method="post">
  46. From: <input type="text" name="date_from" class="datepicker" value="' . date('m/d/Y', strtotime($_SESSION['reports']['contacts']['from'], current_time('timestamp'))) . '">
  47. To: <input type="text" name="date_to" class="datepicker" value="' . date('m/d/Y', strtotime($_SESSION['reports']['contacts']['to'], current_time('timestamp'))) . '">
  48. <input type="submit" class="button" value="Go">
  49. ' . (
  50. $_SESSION['reports']['contacts']['from'] != $default_from || $_SESSION['reports']['contacts']['to'] != $default_to
  51. ? '<a href="' . add_query_arg('reset', true) . '">Reset</a>'
  52. : ''
  53. ) . '
  54. </form>
  55. </div>
  56. <div class="tablenav-pages"><span class="displaying-num">' . $total . ' contacts</span></div>
  57. <br class="clear">';
  58. $table_headers = '
  59. <tr>
  60. <th class="manage-column column-cb index-column">&nbsp;</th>
  61. <th scope="col" class="manage-column column-primary">Contact</th>
  62. <th scope="col" class="manage-column column-author">Date Created</th>
  63. <th scope="col" class="manage-column column-author">Author</th>
  64. </tr>';
  65. ?>
  66. <div class="wrap">
  67. <h1>Contacts Report</h1>
  68. <ul class="subsubsub">
  69. <li><a href="<?php echo remove_query_arg('user'); ?>" class="<?php if (!$user) echo 'current'; ?>">All <span class="count">(<?php echo $total_count; ?>)</span></a> |</li>
  70. <?php foreach ($users as $i => $row) : ?>
  71. <li><a href="<?php echo add_query_arg('user', $row->post_author); ?>" class="<?php if ($user == $row->post_author) echo 'current'; ?>"><?php echo $row->display_name; ?> <span class="count">(<?php echo $row->contact_count; ?>)</span></a><?php if ($i < $total_users - 1) echo ' |'; ?></li>
  72. <?php endforeach; ?>
  73. </ul>
  74. <div class="tablenav top">
  75. <?php echo $table_nav; ?>
  76. </div>
  77. <table class="wp-list-table widefat fixed striped posts">
  78. <thead>
  79. <?php echo $table_headers; ?>
  80. </thead>
  81. <tbody id="the-list">
  82. <?php if ($total) : ?>
  83. <?php foreach ($results as $i => $row) :
  84. ?>
  85. <tr class="iedit author-self level-0 type-page status-publish hentry">
  86. <th scope="row" class="index-column" valign="top"><?php echo $i + 1; ?>.</th>
  87. <td data-colname="Contact" class="title column-title has-row-actions column-primary page-title">
  88. <strong><a href="<?php echo admin_url('post.php?action=edit&post=' . $row->ID); ?>"><?php echo $row->post_title; ?></a></strong>
  89. <button type="button" class="toggle-row"><span class="screen-reader-text">Show more details</span></button>
  90. </td>
  91. <td data-colname="Date Created"><?php echo get_the_date('m/d/Y', $row->ID); ?></td>
  92. <td data-colname="Author"><?php echo $row->display_name; ?></td>
  93. </tr>
  94. <?php endforeach; ?>
  95. <?php else : ?>
  96. <tr class="no-items"><td class="colspanchange" colspan="4">No contacts found.</td></tr>
  97. <?php endif; ?>
  98. </tbody>
  99. <tfoot>
  100. <?php echo $table_headers; ?>
  101. </tfoot>
  102. </table>
  103. <div class="tablenav bottom">
  104. <?php echo $table_nav; ?>
  105. </div>
  106. </div>
  107. <script>
  108. jQuery(document).ready(function($) {
  109. $('.datepicker').datepicker({
  110. maxDate: new Date(),
  111. dateFormat: 'mm/dd/yy',
  112. });
  113. });
  114. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement