Advertisement
samuelaguilera

email-log.php submenu page moved to Tools

Jan 7th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.12 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Email Log
  4. Plugin URI: http://sudarmuthu.com/wordpress/email-log
  5. Description: Logs every email sent through WordPress. Compatiable with WPMU too.
  6. Donate Link: http://sudarmuthu.com/if-you-wanna-thank-me
  7. Author: Sudar
  8. Version: 0.8.1
  9. Author URI: http://sudarmuthu.com/
  10. Text Domain: email-log
  11.  
  12. === RELEASE NOTES ===
  13. 2009-10-08 - v0.1 - Initial Release
  14. 2009-10-15 - v0.2 - Added compatability for MySQL 4
  15. 2009-10-19 - v0.3 - Added compatability for MySQL 4 (Thanks Frank)
  16. 2010-01-02 - v0.4 - Added german translation (Thanks Frank)
  17. 2012-01-01 - v0.5 - Fixed a deprecation notice
  18. 2012-04-29 - v0.6 - (Dev time: 2 hours)
  19.                   - Added option to delete individual email logs
  20.                   - Moved pages per screen option to Screen options panel
  21.                   - Added information to the screen help tab                  
  22.                   - Added Lithuanian translations
  23. 2012-06-23 - v0.7 - (Dev time: 1 hour)
  24.                   - Changed Timestamp(n) MySQL datatype to Timestamp (now compatible with MySQL 5.5+)
  25.                   - Added the ability to bulk delete checkboxes
  26. 2012-07-12 - v0.8 - (Dev time: 1 hour)
  27.                   - Fixed undefined notices - http://wordpress.org/support/topic/plugin-email-log-notices-undefined-indices
  28.                   - Added Dutch translations
  29. 2012-07-23 - v0.8.1 - (Dev time: 0.5 hour)
  30.                   - Reworded most error messages and fixed lot of typos
  31.  
  32. */
  33. /*  Copyright 2009  Sudar Muthu  (email : sudar@sudarmuthu.com)
  34.  
  35.     This program is free software; you can redistribute it and/or modify
  36.     it under the terms of the GNU General Public License, version 2, as
  37.     published by the Free Software Foundation.
  38.  
  39.     This program is distributed in the hope that it will be useful,
  40.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.     GNU General Public License for more details.
  43.  
  44.     You should have received a copy of the GNU General Public License
  45.     along with this program; if not, write to the Free Software
  46.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  47. */
  48.  
  49. global $wpdb;
  50. global $smel_table_name;
  51. $smel_table_name = $wpdb->prefix . "email_log";
  52.  
  53. // TODO - Should find some way to get away with these global variables.
  54. global $smel_db_version;
  55. $smel_db_version = "0.1";
  56.  
  57. class EmailLog {
  58.  
  59.     private $table_name ;    /* Database table name */
  60.     private $db_version ;    /* Database version */
  61.     private $admin_page;
  62.     private $admin_screen;
  63.  
  64.     /**
  65.      * Initalize the plugin by registering the hooks
  66.      */
  67.     function __construct() {
  68.  
  69.         global $wpdb;
  70.         global $smel_table_name;
  71.         global $smel_db_version;
  72.  
  73.         // Load localization domain
  74.         load_plugin_textdomain( 'email-log', false, dirname(plugin_basename(__FILE__)) . '/languages' );
  75.  
  76.         // Register hooks
  77.         add_action( 'admin_menu', array(&$this, 'register_settings_page') );
  78.  
  79.         // Register Filter
  80.         add_filter('wp_mail', array(&$this, 'log_email'));
  81.         add_filter('set-screen-option', array(&$this, 'save_screen_options'), 10, 3);
  82.  
  83.         $plugin = plugin_basename(__FILE__);
  84.         add_filter("plugin_action_links_$plugin", array(&$this, 'add_action_links'));
  85.  
  86.         // Initialize Variables
  87.         $this->table_name = $smel_table_name;
  88.         $this->db_version = $smel_db_version;
  89.     }
  90.  
  91.     /**
  92.      * Register the settings page
  93.      */
  94.     function register_settings_page() {
  95.         //Save the handle to your admin page - you'll need it to create a WP_Screen object
  96.      
  97.         $this->admin_page = add_submenu_page( 'tools.php', __('Email Log', 'email-log'), __('Email Log', 'email-log'), 'manage_options', 'email-log', array(&$this, 'settings_page') );
  98.  
  99.         add_action("load-{$this->admin_page}",array(&$this,'create_settings_panel'));
  100.     }
  101.  
  102.     /**
  103.      * Add settings Panel
  104.      */
  105.     function create_settings_panel() {
  106.  
  107.         /**
  108.          * Create the WP_Screen object against your admin page handle
  109.          * This ensures we're working with the right admin page
  110.          */
  111.         $this->admin_screen = WP_Screen::get($this->admin_page);
  112.  
  113.         /**
  114.          * Content specified inline
  115.          */
  116.         $this->admin_screen->add_help_tab(
  117.             array(
  118.                 'title'    => __('About Plugin', 'email-log'),
  119.                 'id'       => 'about_tab',
  120.                 'content'  => '<p>' . __('Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log') . '</p>',
  121.                 'callback' => false
  122.             )
  123.         );
  124.  
  125.         // Add help sidebar
  126.         $this->admin_screen->set_help_sidebar(
  127.             '<p><strong>' . __('More information', 'email-log') . '</strong></p>' .
  128.             '<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __('Plugin Homepage/support', 'email-log') . '</a></p>' .
  129.             '<p><a href = "http://sudarmuthu.com/blog">' . __("Plugin author's blog", 'email-log') . '</a></p>' .
  130.             '<p><a href = "http://sudarmuthu.com/wordpress/">' . __("Other Plugin's by Author", 'email-log') . '</a></p>'
  131.         );
  132.  
  133.         // Add screen options
  134.         $this->admin_screen->add_option(
  135.             'per_page',
  136.             array(
  137.                 'label' => __('Entries per page', 'email-log'),
  138.                 'default' => 20,
  139.                 'option' => 'per_page'
  140.             )
  141.         );
  142.     }
  143.  
  144.     /**
  145.      * Save Screen option
  146.      */
  147.     function save_screen_options($status, $option, $value) {
  148.         if ( 'per_page' == $option ) return $value;
  149.     }
  150.  
  151.     /**
  152.      * Get the per page option
  153.      */
  154.     private function get_per_page() {
  155.         $screen = get_current_screen();
  156.         $option = $screen->get_option('per_page', 'option');
  157.        
  158.         $per_page = get_user_meta(get_current_user_id(), $option, TRUE);
  159.        
  160.         if ( empty ( $per_page) || $per_page < 1 ) {
  161.             $per_page = $screen->get_option( 'per_page', 'default' );
  162.         }
  163.  
  164.         return $per_page;
  165.     }
  166.        
  167.     /**
  168.      * hook to add action links
  169.      *
  170.      * @param <type> $links
  171.      * @return <type>
  172.      */
  173.     function add_action_links( $links ) {
  174.         // Add a link to this plugin's settings page
  175.         $settings_link = '<a href="options-general.php?page=email-log">' . __("Settings", 'email-log') . '</a>';
  176.         array_unshift( $links, $settings_link );
  177.         return $links;
  178.     }
  179.  
  180.     /**
  181.      * Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
  182.      */
  183.     function add_footer_links() {
  184.         $plugin_data = get_plugin_data( __FILE__ );
  185.         printf('%1$s ' . __("plugin", 'email-log') .' | ' . __("Version", 'email-log') . ' %2$s | '. __('by', 'email-log') . ' %3$s<br />', $plugin_data['Title'], $plugin_data['Version'], $plugin_data['Author']);
  186.     }
  187.  
  188.     /**
  189.      * Dipslay the Settings page
  190.      *
  191.      * Some parts of this function is based on the wp-rattings Plugin http://wordpress.org/extend/plugins/email-log/
  192.      */
  193.     function settings_page() {
  194.         global $wpdb;
  195.         global $text_direction;
  196.  
  197.         $base_name = plugin_basename('email-log');
  198.         $base_page = 'tools.php?page='.$base_name;
  199.  
  200.         $email_log_page            = intval($this->array_get($_GET, 'emaillog_page'));
  201.         $emaillogs_filterid        = trim(addslashes($this->array_get($_GET, 'id')));
  202.         $emaillogs_filter_to_email = trim(addslashes($this->array_get($_GET, 'to_email')));
  203.         $emaillogs_filter_subject  = trim(addslashes($this->array_get($_GET, 'subject')));
  204.         $emaillog_sort_by          = trim($this->array_get($_GET, 'by'));
  205.         $emaillog_sortby_text      = '';
  206.         $emaillog_sortorder        = trim($this->array_get($_GET, 'order'));
  207.         $emaillog_sortorder_text   = '';
  208.         $email_log_perpage         = intval($this->get_per_page());
  209.         $emaillog_sort_url         = '';
  210.  
  211.         ### Form Processing
  212.        if(!empty($_POST['do'])) {
  213.             // Decide What To Do
  214.             switch($_POST['do']) {
  215.                 case __('Delete Logs', 'email-log'):
  216.                     $delete_datalog = intval($_POST['delete_datalog']);
  217.                     switch($delete_datalog) {
  218.                         case 1:
  219.                             // delete selected entries
  220.                             $selected_ids = implode(',', $_POST['selected_ids']);
  221.                             $delete_logs = $wpdb->query("DELETE FROM $this->table_name where id IN ($selected_ids)");
  222.  
  223.                             if($delete_logs) {
  224.                                 $text = '<font color="green">' . __('The selected Email Logs have been deleted.', 'email-log') . '</font>';
  225.                             } else {
  226.                                 $text = '<font color="red">' . __('An error has occurred while deleting the selected Email logs', 'email-log') . '</font>';
  227.                             }
  228.                             break;
  229.                         case 2:
  230.                             // Delete based on condition
  231.                             $to_email = trim(addslashes( $_POST['delete_to_email']));
  232.                             if ('' != $to_email) {
  233.                                 $delete_logs = $wpdb->query("DELETE FROM $this->table_name where to_email = '$to_email'");
  234.                                 if($delete_logs) {
  235.                                     $text = '<font color="green">'.sprintf(__('All Email Logs for email id "%s" have been deleted.', 'email-log'), $to_email).'</font>';
  236.                                 } else {
  237.                                     $text = '<font color="red">'.sprintf(__('An error has occurred while deleting all Email Logs for email id "%s".', 'email-log'), $to_email).'</font>';
  238.                                 }
  239.                             }
  240.  
  241.                             $subject = trim(addslashes( $_POST['delete_subject']));
  242.                             if ('' != $subject) {
  243.                                 $delete_logs = $wpdb->query("DELETE FROM $this->table_name where subject = '$subject'");
  244.                                 if($delete_logs) {
  245.                                     $text .= '<font color="green">'.sprintf(__('All Email Logs with subject "%s" have been deleted.', 'email-log'), $subject).'</font>';
  246.                                 } else {
  247.                                     $text .= '<font color="red">'.sprintf(__('An error has occurred while deleting all Email Logs with subject "%s".', 'email-log'), $subject).'</font>';
  248.                                 }
  249.                             }
  250.                             break;
  251.                         case 3:
  252.                             // Delete all
  253.                             $delete_logs = $wpdb->query("DELETE FROM $this->table_name ");
  254.                             if ($delete_logs) {
  255.                                 $text = '<font color="green">'.__('All Email Logs were deleted.', 'email-log').'</font><br />';
  256.                             } else {
  257.                                 $text = '<font color="red">'.__('An error has occurred while deleting all Email Logs', 'email-log').'</font>';
  258.                             }
  259.                             break;
  260.                     }
  261.                 break;
  262.             }
  263.         }
  264.  
  265.         ### Form Sorting URL
  266.        if(!empty($emaillogs_filterid)) {
  267.             $emaillogs_filterid = intval($emaillogs_filterid);
  268.             $emaillog_sort_url .= '&amp;id='.$emaillogs_filterid;
  269.         }
  270.         if(!empty($emaillogs_filter_to_email)) {
  271.             $emaillog_sort_url .= '&amp;to_email='.$emaillogs_filter_to_email;
  272.         }
  273.         if(!empty($emaillogs_filter_subject)) {
  274.             $emaillog_sort_url .= '&amp;subject='.$emaillogs_filter_subject;
  275.         }
  276.         if(!empty($emaillog_sort_by)) {
  277.             $emaillog_sort_url .= '&amp;by='.$emaillog_sort_by;
  278.         }
  279.         if(!empty($emaillog_sortorder)) {
  280.             $emaillog_sort_url .= '&amp;order='.$emaillog_sortorder;
  281.         }
  282.  
  283.         ### Get Order By
  284.        switch($emaillog_sort_by) {
  285.             case 'id':
  286.                 $emaillog_sort_by = 'id';
  287.                 $emaillog_sortby_text = __('ID', 'email-log');
  288.                 break;
  289.             case 'to_email':
  290.                 $emaillog_sort_by = 'to_email';
  291.                 $emaillog_sortby_text = __('To Email', 'email-log');
  292.                 break;
  293.             case 'subject':
  294.                 $emaillog_sort_by = 'subject';
  295.                 $emaillog_sortby_text = __('Subject', 'email-log');
  296.                 break;
  297.             case 'date':
  298.             default:
  299.                 $emaillog_sort_by = 'sent_date';
  300.                 $emaillog_sortby_text = __('Date', 'email-log');
  301.         }
  302.  
  303.         ### Get Sort Order
  304.        switch($emaillog_sortorder) {
  305.             case 'asc':
  306.                 $emaillog_sortorder = 'ASC';
  307.                 $emaillog_sortorder_text = __('Ascending', 'email-log');
  308.                 break;
  309.             case 'desc':
  310.             default:
  311.                 $emaillog_sortorder = 'DESC';
  312.                 $emaillog_sortorder_text = __('Descending', 'email-log');
  313.         }
  314.  
  315.         // Where
  316.         $emaillog_where = '';
  317.         if(!empty($emaillogs_filterid)) {
  318.             $emaillog_where = "AND id =$emaillogs_filterid";
  319.         }
  320.         if(!empty($emaillogs_filter_to_email)) {
  321.             $emaillog_where .= " AND to_email like '%$emaillogs_filter_to_email%'";
  322.         }
  323.         if(!empty($emaillogs_filter_subject)) {
  324.             $emaillog_where .= " AND subject like '%$emaillogs_filter_subject%'";
  325.         }
  326.  
  327.         // Get email Logs Data
  328.         $total_logs = $wpdb->get_var("SELECT COUNT(id) FROM $this->table_name WHERE 1=1 $emaillog_where");
  329.  
  330.         // Checking $postratings_page and $offset
  331.         if(empty($email_log_page) || $email_log_page == 0) { $email_log_page = 1; }
  332.         if(empty($offset)) { $offset = 0; }
  333.  
  334.         // Determin $offset
  335.         $offset = ($email_log_page-1) * $email_log_perpage;
  336.  
  337.         // Determine Max Number Of Logs To Display On Page
  338.         if(($offset + $email_log_perpage) > $total_logs) {
  339.             $max_on_page = $total_logs;
  340.         } else {
  341.             $max_on_page = ($offset + $email_log_perpage);
  342.         }
  343.  
  344.         // Determine Number Of Logs To Display On Page
  345.         if (($offset + 1) > ($total_logs)) {
  346.             $display_on_page = $total_logs;
  347.         } else {
  348.             $display_on_page = ($offset + 1);
  349.         }
  350.  
  351.         // Determing Total Amount Of Pages
  352.         $total_pages = ceil($total_logs / $email_log_perpage);
  353.  
  354.         // Get The Logs
  355.         $email_logs = $wpdb->get_results("SELECT * FROM $this->table_name WHERE 1=1 $emaillog_where ORDER BY $emaillog_sort_by $emaillog_sortorder LIMIT $offset, $email_log_perpage");
  356.  
  357.         // TODO: Should move this to a seperate js file
  358. ?>
  359. <script type = "text/javascript">
  360. jQuery('document').ready(function() {
  361.     jQuery('.selectall').click(function (e) {
  362.         if (jQuery(e.target).is(':checked')) {
  363.             jQuery('.select_box').attr('checked', 'checked');
  364.         } else {
  365.             jQuery('.select_box').removeAttr('checked');
  366.         }        
  367.     });
  368. });
  369. </script>
  370.         <?php if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade"><p>'.$text.'</p></div>'; } ?>
  371.         <div class="wrap">
  372.             <?php screen_icon(); ?>
  373.             <h2><?php _e( 'Email Log Settings', 'email-log' ); ?></h2>
  374.  
  375.             <p>&nbsp;</p>
  376.  
  377.     <form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="get">
  378.         <input type="hidden" name="page" value="<?php echo $base_name; ?>" />
  379.         <table class="widefat">
  380.             <tr>
  381.                 <th><?php _e('Filter Options:', 'email-log'); ?></th>
  382.                 <td>
  383.                     <?php _e('ID:', 'email-log'); ?>&nbsp;<input type="text" name="id" value="<?php echo $emaillogs_filterid; ?>" size="5" maxlength="5" />
  384.                     &nbsp;&nbsp;&nbsp;
  385.                     <?php _e('To Email:', 'email-log'); ?>&nbsp;<input type="text" name="to_email" value="<?php echo $emaillogs_filter_to_email; ?>" size="40" maxlength="50" />
  386.                     &nbsp;&nbsp;&nbsp;
  387.                     <?php _e('Subject:', 'email-log'); ?>&nbsp;<input type="text" name="subject" value="<?php echo $emaillogs_filter_subject; ?>" size="40" maxlength="50" />
  388.                     &nbsp;&nbsp;&nbsp;
  389.                 </td>
  390.             </tr>
  391.             <tr class="alternate">
  392.                 <th><?php _e('Sort Options:', 'email-log'); ?></th>
  393.                 <td>
  394.                     <select name="by" size="1">
  395.                         <option value="id"<?php if($emaillog_sort_by == 'id') { echo ' selected="selected"'; }?>><?php _e('ID', 'email-log'); ?></option>
  396.                         <option value="to_email"<?php if($emaillog_sort_by == 'to_email') { echo ' selected="selected"'; }?>><?php _e('To Email', 'email-log'); ?></option>
  397.                         <option value="subject"<?php if($emaillog_sort_by == 'subject') { echo ' selected="selected"'; }?>><?php _e('Subject', 'email-log'); ?></option>
  398.                         <option value="sent_date"<?php if($emaillog_sort_by == 'sent_date') { echo ' selected="selected"'; }?>><?php _e('Date', 'email-log'); ?></option>
  399.                     </select>
  400.                     &nbsp;&nbsp;&nbsp;
  401.                     <select name="order" size="1">
  402.                         <option value="asc"<?php if($emaillog_sortorder == 'ASC') { echo ' selected="selected"'; }?>><?php _e('Ascending', 'email-log'); ?></option>
  403.                         <option value="desc"<?php if($emaillog_sortorder == 'DESC') { echo ' selected="selected"'; } ?>><?php _e('Descending', 'email-log'); ?></option>
  404.                     </select>
  405.                 </td>
  406.             </tr>
  407.             <tr>
  408.                 <td colspan="2" align="center"><input type="submit" value="<?php _e('Filter', 'email-log'); ?>" class="button" /></td>
  409.             </tr>
  410.         </table>
  411.     </form>
  412.  
  413.             <p><?php printf(__('Displaying <strong>%s</strong> to <strong>%s</strong> of <strong>%s</strong> Email log entries.', 'email-log'), number_format_i18n($display_on_page), number_format_i18n($max_on_page), number_format_i18n($total_logs)); ?></p>
  414.             <p><?php printf(__('Sorted by <strong>%s</strong> in <strong>%s</strong> order.', 'email-log'), $emaillog_sortby_text, $emaillog_sortorder_text); ?></p>
  415.  
  416.         <form method="post" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>?page=<?php echo $base_name; ?>">
  417. <?php
  418.             if($total_pages > 1) {
  419. ?>
  420.         <br />
  421.         <table class="widefat">
  422.             <tr>
  423.                 <td align="<?php echo ('rtl' == $text_direction) ? 'right' : 'left'; ?>" width="40%">
  424.                     <?php
  425.                         if($email_log_page > 1 && ((($email_log_page*$email_log_perpage)-($email_log_perpage-1)) <= $total_logs)) {
  426.                             echo '<strong>&laquo;</strong> <a href="'.$base_page.'&amp;emaillog_page='.($email_log_page-1).$emaillog_sort_url.'" title="&laquo; '.__('Previous Page', 'email-log').'">'.__('Previous Page', 'email-log').'</a>';
  427.                         } else {
  428.                             echo '&nbsp;';
  429.                         }
  430.                     ?>
  431.                 </td>
  432.                 <td align="center" width="20%">
  433.                     <?php printf(__('Pages (%s): ', 'email-log'), number_format_i18n($total_pages)); ?>
  434.                     <?php
  435.                         if ($email_log_page >= 4) {
  436.                             echo '<strong><a href="'.$base_page.'&amp;emaillog_page=1'.$emaillog_sort_url.$emaillog_sort_url.'" title="'.__('Go to First Page', 'email-log').'">&laquo; '.__('First', 'email-log').'</a></strong> ... ';
  437.                         }
  438.                         if($email_log_page > 1) {
  439.                             echo ' <strong><a href="'.$base_page.'&amp;emaillog_page='.($email_log_page-1).$emaillog_sort_url.'" title="&laquo; '.__('Go to Page', 'email-log').' '.number_format_i18n($email_log_page-1).'">&laquo;</a></strong> ';
  440.                         }
  441.                         for($i = $email_log_page - 2 ; $i  <= $email_log_page +2; $i++) {
  442.                             if ($i >= 1 && $i <= $total_pages) {
  443.                                 if($i == $email_log_page) {
  444.                                     echo '<strong>['.number_format_i18n($i).']</strong> ';
  445.                                 } else {
  446.                                     echo '<a href="'.$base_page.'&amp;emaillog_page='.($i).$emaillog_sort_url.'" title="'.__('Page', 'email-log').' '.number_format_i18n($i).'">'.number_format_i18n($i).'</a> ';
  447.                                 }
  448.                             }
  449.                         }
  450.                         if($email_log_page < $total_pages) {
  451.                             echo ' <strong><a href="'.$base_page.'&amp;emaillog_page='.($email_log_page+1).$emaillog_sort_url.'" title="'.__('Go to Page', 'email-log').' '.number_format_i18n($email_log_page+1).' &raquo;">&raquo;</a></strong> ';
  452.                         }
  453.                         if (($email_log_page+2) < $total_pages) {
  454.                             echo ' ... <strong><a href="'.$base_page.'&amp;emaillog_page='.($total_pages).$emaillog_sort_url.'" title="'.__('Go to Last Page', 'email-log').'">'.__('Last', 'email-log').' &raquo;</a></strong>';
  455.                         }
  456.                     ?>
  457.                 </td>
  458.                 <td align="<?php echo ('rtl' == $text_direction) ? 'left' : 'right'; ?>" width="40%">
  459.                     <?php
  460.                         if($email_log_page >= 1 && ((($email_log_page*$email_log_perpage)+1) <=  $total_logs)) {
  461.                             echo '<a href="'.$base_page.'&amp;emaillog_page='.($email_log_page+1).$emaillog_sort_url.'" title="'.__('Next Page', 'email-log').' &raquo;">'.__('Next Page', 'email-log').'</a> <strong>&raquo;</strong>';
  462.                         } else {
  463.                             echo '&nbsp;';
  464.                         }
  465.                     ?>
  466.                 </td>
  467.             </tr>
  468.         </table>
  469.         <!-- </Paging> -->
  470.         <?php
  471.             }
  472.         ?>
  473.             <table class="widefat">
  474.                 <thead>
  475.                     <tr>
  476.                         <td width="5%"><input type = "checkbox" name = "selectall" class = "selectall" ></td>
  477.                         <th width="5%"><?php _e('ID', 'email-log'); ?></th>
  478.                         <th width="20%"><?php _e('Date / Time', 'email-log'); ?></th>
  479.                         <th width="30%"><?php _e('To', 'email-log'); ?></th>
  480.                         <th width="40%"><?php _e('Subject', 'email-log'); ?></th>
  481.                     </tr>
  482.                 </thead>
  483.                 <tbody>
  484. <?php
  485.                 if($email_logs) {
  486.                     $i = 0;
  487.                     foreach($email_logs as $email_log) {
  488.                         if($i%2 == 0) {
  489.                             $style = 'class="alternate"';
  490.                         }  else {
  491.                             $style = '';
  492.                         }
  493.                         $email_id = intval($email_log->id);
  494.                         $email_date = mysql2date(sprintf(__('%s @ %s', 'email-log'), get_option('date_format'), get_option('time_format')), $email_log->sent_date);
  495.                         $email_to = stripslashes($email_log->to_email);
  496.                         $email_subject = stripslashes($email_log->subject);
  497.                         echo "<tr $style>\n";
  498.                         echo '<td><input type = "checkbox" class = "select_box" name = "selected_ids[]" value = "' . $email_id . '"></td>'."\n";
  499.                         echo '<td>'.$email_id.'</td>'."\n";
  500.                         echo "<td>$email_date</td>\n";
  501.                         echo "<td>$email_to</td>\n";
  502.                         echo "<td>$email_subject</td>\n";
  503.                         echo '</tr>';
  504.                         $i++;
  505.                     }
  506.                 } else {
  507.                     echo '<tr><td colspan="7" align="center"><strong>'.__('No Email Logs were found', 'email-log').'</strong></td></tr>';
  508.                 }
  509. ?>
  510.                 </tbody>
  511.                 <tfoot>
  512.                     <tr>
  513.                         <td width="5%"><input type = "checkbox" name = "selectall" class = "selectall" ></td>
  514.                         <th width="5%"><?php _e('ID', 'email-log'); ?></th>
  515.                         <th width="20%"><?php _e('Date / Time', 'email-log'); ?></th>
  516.                         <th width="30%"><?php _e('To', 'email-log'); ?></th>
  517.                         <th width="40%"><?php _e('Subject', 'email-log'); ?></th>
  518.                     </tr>
  519.                 </tfoot>
  520.             </table>
  521. <?php
  522.             if($total_pages > 1) {
  523. ?>
  524.         <table class="widefat">
  525.             <tr>
  526.                 <td align="<?php echo ('rtl' == $text_direction) ? 'right' : 'left'; ?>" width="40%">
  527.                     <?php
  528.                         if($email_log_page > 1 && ((($email_log_page*$email_log_perpage)-($email_log_perpage-1)) <= $total_logs)) {
  529.                             echo '<strong>&laquo;</strong> <a href="'.$base_page.'&amp;emaillog_page='.($email_log_page-1).$emaillog_sort_url.'" title="&laquo; '.__('Previous Page', 'email-log').'">'.__('Previous Page', 'email-log').'</a>';
  530.                         } else {
  531.                             echo '&nbsp;';
  532.                         }
  533.                     ?>
  534.                 </td>
  535.                 <td align="center" width="20%">
  536.                     <?php printf(__('Pages (%s): ', 'email-log'), number_format_i18n($total_pages)); ?>
  537.                     <?php
  538.                         if ($email_log_page >= 4) {
  539.                             echo '<strong><a href="'.$base_page.'&amp;emaillog_page=1'.$emaillog_sort_url.$emaillog_sort_url.'" title="'.__('Go to First Page', 'email-log').'">&laquo; '.__('First', 'email-log').'</a></strong> ... ';
  540.                         }
  541.                         if($email_log_page > 1) {
  542.                             echo ' <strong><a href="'.$base_page.'&amp;emaillog_page='.($email_log_page-1).$emaillog_sort_url.'" title="&laquo; '.__('Go to Page', 'email-log').' '.number_format_i18n($email_log_page-1).'">&laquo;</a></strong> ';
  543.                         }
  544.                         for($i = $email_log_page - 2 ; $i  <= $email_log_page +2; $i++) {
  545.                             if ($i >= 1 && $i <= $total_pages) {
  546.                                 if($i == $email_log_page) {
  547.                                     echo '<strong>['.number_format_i18n($i).']</strong> ';
  548.                                 } else {
  549.                                     echo '<a href="'.$base_page.'&amp;emaillog_page='.($i).$emaillog_sort_url.'" title="'.__('Page', 'email-log').' '.number_format_i18n($i).'">'.number_format_i18n($i).'</a> ';
  550.                                 }
  551.                             }
  552.                         }
  553.                         if($email_log_page < $total_pages) {
  554.                             echo ' <strong><a href="'.$base_page.'&amp;emaillog_page='.($email_log_page+1).$emaillog_sort_url.'" title="'.__('Go to Page', 'email-log').' '.number_format_i18n($email_log_page+1).' &raquo;">&raquo;</a></strong> ';
  555.                         }
  556.                         if (($email_log_page+2) < $total_pages) {
  557.                             echo ' ... <strong><a href="'.$base_page.'&amp;emaillog_page='.($total_pages).$emaillog_sort_url.'" title="'.__('Go to Last Page', 'email-log').'">'.__('Last', 'email-log').' &raquo;</a></strong>';
  558.                         }
  559.                     ?>
  560.                 </td>
  561.                 <td align="<?php echo ('rtl' == $text_direction) ? 'left' : 'right'; ?>" width="40%">
  562.                     <?php
  563.                         if($email_log_page >= 1 && ((($email_log_page*$email_log_perpage)+1) <=  $total_logs)) {
  564.                             echo '<a href="'.$base_page.'&amp;emaillog_page='.($email_log_page+1).$emaillog_sort_url.'" title="'.__('Next Page', 'email-log').' &raquo;">'.__('Next Page', 'email-log').'</a> <strong>&raquo;</strong>';
  565.                         } else {
  566.                             echo '&nbsp;';
  567.                         }
  568.                     ?>
  569.                 </td>
  570.             </tr>
  571.             <tr class="alternate">
  572.             </tr>
  573.         </table>
  574.         <!-- </Paging> -->
  575.         <?php
  576.             }
  577.         ?>
  578.  
  579. <!-- Delete Email Logs -->
  580.     <h3><?php _e('Delete Logs', 'email-log'); ?></h3>
  581.     <div align="center">
  582.         <table class="widefat">
  583.             <tr>
  584.                 <td valign="top"><b><?php _e('Delete Type : ', 'email-log'); ?></b></td>
  585.                 <td valign="top">
  586.                     <select size="1" name="delete_datalog">
  587.                         <option value="1"><?php _e('Selected entries', 'email-log'); ?></option>
  588.                         <option value="2"><?php _e('Based on', 'email-log'); ?></option>
  589.                         <option value="3"><?php _e('All Logs', 'email-log'); ?></option>
  590.                     </select>
  591.                 </td>
  592.             </tr>
  593.             <tr>
  594.                 <td valign="top"><b><?php _e('Condition:', 'email-log'); ?></b></td>
  595.                 <td valign="top">
  596.                     <label for ="delete_to_email"><?php _e('To Email', 'email-log');?> <input type="text" name="delete_to_email" size="20" dir="ltr" /></label>
  597.                     <?php _e('or', 'email-log');?>
  598.                     <label for ="delete_subject"><?php _e('Subject', 'email-log');?> <input type="text" name="delete_subject" size="20" dir="ltr" /></label>
  599.                 </td>
  600.             </tr>
  601.             <tr>
  602.                 <td colspan="2" align="center">
  603.                     <input type="submit" name="do" value="<?php _e('Delete Logs', 'email-log'); ?>" class="button" onclick="return confirm('<?php _e('You Are About To Delete Email Logs.\nThis Action Is Not Reversible.\n\n Choose \\\'Cancel\\\' to stop, \\\'OK\\\' to delete.', 'email-log'); ?>')" />
  604.                 </td>
  605.             </tr>
  606.         </table>
  607.     </div>
  608.         </form>
  609. </div>
  610. <?php
  611.         // Display credits in Footer
  612.         add_action( 'in_admin_footer', array(&$this, 'add_footer_links'));
  613.     }
  614.  
  615.     /**
  616.      * Log all email to database
  617.      *
  618.      * @global object $wpdb
  619.      * @param array $mail_info Information about email
  620.      * @return array Information about email
  621.      */
  622.     function log_email($mail_info) {
  623.  
  624.         global $wpdb;
  625.  
  626.         $attachment_present = (count ($mail_info['attachments']) > 0) ? "true" : "false";
  627.  
  628.         // Log into the database
  629.         $wpdb->insert($this->table_name, array(
  630.                 'to_email' => $mail_info['to'],
  631.                 'subject' => $mail_info['subject'],
  632.                 'message' => $mail_info['message'],
  633.                 'headers' => $mail_info['headers'],
  634.                 'attachments' => $attachment_present
  635.         ));
  636.  
  637.         // return unmodifiyed array
  638.         return $mail_info;
  639.     }
  640.  
  641.     /**
  642.     * Check whether a key is present. If present returns the value, else returns the default value
  643.     *
  644.     * @param <array> $array - Array whose key has to be checked
  645.     * @param <string> $key - key that has to be checked
  646.     * @param <string> $default - the default value that has to be used, if the key is not found (optional)
  647.     *
  648.     * @return <mixed> If present returns the value, else returns the default value
  649.     * @author Sudar
  650.     */
  651.     private function array_get($array, $key, $default = NULL) {
  652.         return isset($array[$key]) ? $array[$key] : $default;
  653.     }
  654.  
  655.     // PHP4 compatibility
  656.     function EmailLog() {
  657.         $this->__construct();
  658.     }
  659. }
  660.  
  661. /**
  662.  * Create database table when the Plugin is installed for the first time
  663.  *
  664.  * @global object $wpdb
  665.  * @global string $smel_table_name Table Name
  666.  * @global string $smel_db_version DB Version
  667.  */
  668. function smel_on_install() {
  669.  
  670.    global $wpdb;
  671.    global $smel_table_name;
  672.    global $smel_db_version;
  673.  
  674.    if($wpdb->get_var("show tables like '{$smel_table_name}'") != $smel_table_name) {
  675.  
  676.       $sql = "CREATE TABLE " . $smel_table_name . " (
  677.          id mediumint(9) NOT NULL AUTO_INCREMENT,
  678.          to_email VARCHAR(100) NOT NULL,
  679.          subject VARCHAR(250) NOT NULL,
  680.          message TEXT NOT NULL,
  681.          headers TEXT NOT NULL,
  682.          attachments TEXT NOT NULL,
  683.          sent_date timestamp NOT NULL,
  684.          PRIMARY KEY  (id)
  685.        );";
  686.  
  687.       require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
  688.       dbDelta($sql);
  689.  
  690.       add_option("email-log-db", $smel_db_version);
  691.    }
  692. }
  693.  
  694. // When installed
  695. register_activation_hook(__FILE__, 'smel_on_install');
  696.  
  697. // Start this plugin once all other plugins are fully loaded
  698. add_action( 'init', 'EmailLog' ); function EmailLog() { global $EmailLog; $EmailLog = new EmailLog(); }
  699.  
  700. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement