prefix . "email_log";
// TODO - Should find some way to get away with these global variables.
global $smel_db_version;
$smel_db_version = "0.1";
class EmailLog {
private $table_name ; /* Database table name */
private $db_version ; /* Database version */
private $admin_page;
private $admin_screen;
/**
* Initalize the plugin by registering the hooks
*/
function __construct() {
global $wpdb;
global $smel_table_name;
global $smel_db_version;
// Load localization domain
load_plugin_textdomain( 'email-log', false, dirname(plugin_basename(__FILE__)) . '/languages' );
// Register hooks
add_action( 'admin_menu', array(&$this, 'register_settings_page') );
// Register Filter
add_filter('wp_mail', array(&$this, 'log_email'));
add_filter('set-screen-option', array(&$this, 'save_screen_options'), 10, 3);
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", array(&$this, 'add_action_links'));
// Initialize Variables
$this->table_name = $smel_table_name;
$this->db_version = $smel_db_version;
}
/**
* Register the settings page
*/
function register_settings_page() {
//Save the handle to your admin page - you'll need it to create a WP_Screen object
$this->admin_page = add_submenu_page( 'tools.php', __('Email Log', 'email-log'), __('Email Log', 'email-log'), 'manage_options', 'email-log', array(&$this, 'settings_page') );
add_action("load-{$this->admin_page}",array(&$this,'create_settings_panel'));
}
/**
* Add settings Panel
*/
function create_settings_panel() {
/**
* Create the WP_Screen object against your admin page handle
* This ensures we're working with the right admin page
*/
$this->admin_screen = WP_Screen::get($this->admin_page);
/**
* Content specified inline
*/
$this->admin_screen->add_help_tab(
array(
'title' => __('About Plugin', 'email-log'),
'id' => 'about_tab',
'content' => '
' . __('Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log') . '
',
'callback' => false
)
);
// Add help sidebar
$this->admin_screen->set_help_sidebar(
'' . __('More information', 'email-log') . '
' .
'' . __('Plugin Homepage/support', 'email-log') . '
' .
'' . __("Plugin author's blog", 'email-log') . '
' .
'' . __("Other Plugin's by Author", 'email-log') . '
'
);
// Add screen options
$this->admin_screen->add_option(
'per_page',
array(
'label' => __('Entries per page', 'email-log'),
'default' => 20,
'option' => 'per_page'
)
);
}
/**
* Save Screen option
*/
function save_screen_options($status, $option, $value) {
if ( 'per_page' == $option ) return $value;
}
/**
* Get the per page option
*/
private function get_per_page() {
$screen = get_current_screen();
$option = $screen->get_option('per_page', 'option');
$per_page = get_user_meta(get_current_user_id(), $option, TRUE);
if ( empty ( $per_page) || $per_page < 1 ) {
$per_page = $screen->get_option( 'per_page', 'default' );
}
return $per_page;
}
/**
* hook to add action links
*
* @param $links
* @return
*/
function add_action_links( $links ) {
// Add a link to this plugin's settings page
$settings_link = '' . __("Settings", 'email-log') . '';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
*/
function add_footer_links() {
$plugin_data = get_plugin_data( __FILE__ );
printf('%1$s ' . __("plugin", 'email-log') .' | ' . __("Version", 'email-log') . ' %2$s | '. __('by', 'email-log') . ' %3$s
', $plugin_data['Title'], $plugin_data['Version'], $plugin_data['Author']);
}
/**
* Dipslay the Settings page
*
* Some parts of this function is based on the wp-rattings Plugin http://wordpress.org/extend/plugins/email-log/
*/
function settings_page() {
global $wpdb;
global $text_direction;
$base_name = plugin_basename('email-log');
$base_page = 'tools.php?page='.$base_name;
$email_log_page = intval($this->array_get($_GET, 'emaillog_page'));
$emaillogs_filterid = trim(addslashes($this->array_get($_GET, 'id')));
$emaillogs_filter_to_email = trim(addslashes($this->array_get($_GET, 'to_email')));
$emaillogs_filter_subject = trim(addslashes($this->array_get($_GET, 'subject')));
$emaillog_sort_by = trim($this->array_get($_GET, 'by'));
$emaillog_sortby_text = '';
$emaillog_sortorder = trim($this->array_get($_GET, 'order'));
$emaillog_sortorder_text = '';
$email_log_perpage = intval($this->get_per_page());
$emaillog_sort_url = '';
### Form Processing
if(!empty($_POST['do'])) {
// Decide What To Do
switch($_POST['do']) {
case __('Delete Logs', 'email-log'):
$delete_datalog = intval($_POST['delete_datalog']);
switch($delete_datalog) {
case 1:
// delete selected entries
$selected_ids = implode(',', $_POST['selected_ids']);
$delete_logs = $wpdb->query("DELETE FROM $this->table_name where id IN ($selected_ids)");
if($delete_logs) {
$text = '' . __('The selected Email Logs have been deleted.', 'email-log') . '';
} else {
$text = '' . __('An error has occurred while deleting the selected Email logs', 'email-log') . '';
}
break;
case 2:
// Delete based on condition
$to_email = trim(addslashes( $_POST['delete_to_email']));
if ('' != $to_email) {
$delete_logs = $wpdb->query("DELETE FROM $this->table_name where to_email = '$to_email'");
if($delete_logs) {
$text = ''.sprintf(__('All Email Logs for email id "%s" have been deleted.', 'email-log'), $to_email).'';
} else {
$text = ''.sprintf(__('An error has occurred while deleting all Email Logs for email id "%s".', 'email-log'), $to_email).'';
}
}
$subject = trim(addslashes( $_POST['delete_subject']));
if ('' != $subject) {
$delete_logs = $wpdb->query("DELETE FROM $this->table_name where subject = '$subject'");
if($delete_logs) {
$text .= ''.sprintf(__('All Email Logs with subject "%s" have been deleted.', 'email-log'), $subject).'';
} else {
$text .= ''.sprintf(__('An error has occurred while deleting all Email Logs with subject "%s".', 'email-log'), $subject).'';
}
}
break;
case 3:
// Delete all
$delete_logs = $wpdb->query("DELETE FROM $this->table_name ");
if ($delete_logs) {
$text = ''.__('All Email Logs were deleted.', 'email-log').'
';
} else {
$text = ''.__('An error has occurred while deleting all Email Logs', 'email-log').'';
}
break;
}
break;
}
}
### Form Sorting URL
if(!empty($emaillogs_filterid)) {
$emaillogs_filterid = intval($emaillogs_filterid);
$emaillog_sort_url .= '&id='.$emaillogs_filterid;
}
if(!empty($emaillogs_filter_to_email)) {
$emaillog_sort_url .= '&to_email='.$emaillogs_filter_to_email;
}
if(!empty($emaillogs_filter_subject)) {
$emaillog_sort_url .= '&subject='.$emaillogs_filter_subject;
}
if(!empty($emaillog_sort_by)) {
$emaillog_sort_url .= '&by='.$emaillog_sort_by;
}
if(!empty($emaillog_sortorder)) {
$emaillog_sort_url .= '&order='.$emaillog_sortorder;
}
### Get Order By
switch($emaillog_sort_by) {
case 'id':
$emaillog_sort_by = 'id';
$emaillog_sortby_text = __('ID', 'email-log');
break;
case 'to_email':
$emaillog_sort_by = 'to_email';
$emaillog_sortby_text = __('To Email', 'email-log');
break;
case 'subject':
$emaillog_sort_by = 'subject';
$emaillog_sortby_text = __('Subject', 'email-log');
break;
case 'date':
default:
$emaillog_sort_by = 'sent_date';
$emaillog_sortby_text = __('Date', 'email-log');
}
### Get Sort Order
switch($emaillog_sortorder) {
case 'asc':
$emaillog_sortorder = 'ASC';
$emaillog_sortorder_text = __('Ascending', 'email-log');
break;
case 'desc':
default:
$emaillog_sortorder = 'DESC';
$emaillog_sortorder_text = __('Descending', 'email-log');
}
// Where
$emaillog_where = '';
if(!empty($emaillogs_filterid)) {
$emaillog_where = "AND id =$emaillogs_filterid";
}
if(!empty($emaillogs_filter_to_email)) {
$emaillog_where .= " AND to_email like '%$emaillogs_filter_to_email%'";
}
if(!empty($emaillogs_filter_subject)) {
$emaillog_where .= " AND subject like '%$emaillogs_filter_subject%'";
}
// Get email Logs Data
$total_logs = $wpdb->get_var("SELECT COUNT(id) FROM $this->table_name WHERE 1=1 $emaillog_where");
// Checking $postratings_page and $offset
if(empty($email_log_page) || $email_log_page == 0) { $email_log_page = 1; }
if(empty($offset)) { $offset = 0; }
// Determin $offset
$offset = ($email_log_page-1) * $email_log_perpage;
// Determine Max Number Of Logs To Display On Page
if(($offset + $email_log_perpage) > $total_logs) {
$max_on_page = $total_logs;
} else {
$max_on_page = ($offset + $email_log_perpage);
}
// Determine Number Of Logs To Display On Page
if (($offset + 1) > ($total_logs)) {
$display_on_page = $total_logs;
} else {
$display_on_page = ($offset + 1);
}
// Determing Total Amount Of Pages
$total_pages = ceil($total_logs / $email_log_perpage);
// Get The Logs
$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");
// TODO: Should move this to a seperate js file
?>
'; } ?>
%s to %s of %s Email log entries.', 'email-log'), number_format_i18n($display_on_page), number_format_i18n($max_on_page), number_format_i18n($total_logs)); ?>
%s in %s order.', 'email-log'), $emaillog_sortby_text, $emaillog_sortorder_text); ?>
0) ? "true" : "false";
// Log into the database
$wpdb->insert($this->table_name, array(
'to_email' => $mail_info['to'],
'subject' => $mail_info['subject'],
'message' => $mail_info['message'],
'headers' => $mail_info['headers'],
'attachments' => $attachment_present
));
// return unmodifiyed array
return $mail_info;
}
/**
* Check whether a key is present. If present returns the value, else returns the default value
*
* @param $array - Array whose key has to be checked
* @param $key - key that has to be checked
* @param $default - the default value that has to be used, if the key is not found (optional)
*
* @return If present returns the value, else returns the default value
* @author Sudar
*/
private function array_get($array, $key, $default = NULL) {
return isset($array[$key]) ? $array[$key] : $default;
}
// PHP4 compatibility
function EmailLog() {
$this->__construct();
}
}
/**
* Create database table when the Plugin is installed for the first time
*
* @global object $wpdb
* @global string $smel_table_name Table Name
* @global string $smel_db_version DB Version
*/
function smel_on_install() {
global $wpdb;
global $smel_table_name;
global $smel_db_version;
if($wpdb->get_var("show tables like '{$smel_table_name}'") != $smel_table_name) {
$sql = "CREATE TABLE " . $smel_table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
to_email VARCHAR(100) NOT NULL,
subject VARCHAR(250) NOT NULL,
message TEXT NOT NULL,
headers TEXT NOT NULL,
attachments TEXT NOT NULL,
sent_date timestamp NOT NULL,
PRIMARY KEY (id)
);";
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
dbDelta($sql);
add_option("email-log-db", $smel_db_version);
}
}
// When installed
register_activation_hook(__FILE__, 'smel_on_install');
// Start this plugin once all other plugins are fully loaded
add_action( 'init', 'EmailLog' ); function EmailLog() { global $EmailLog; $EmailLog = new EmailLog(); }
?>