Skip to content

Commit

Permalink
Merge f3b7ea3 into 6b1e0bb
Browse files Browse the repository at this point in the history
  • Loading branch information
rajumsys committed Jul 13, 2017
2 parents 6b1e0bb + f3b7ea3 commit f57c506
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 6 deletions.
23 changes: 18 additions & 5 deletions admin.widget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ function render_tabs($active = '')
'href' => admin_url(add_query_arg(array('page' => 'wpsp-setting-admin', 'tab' => 'test'), 'admin.php')),
'name' => 'Test'
),
'3' => array(
'slug' => 'logs',
'href' => admin_url(add_query_arg(array('page' => 'wpsp-setting-admin', 'tab' => 'logs'), 'admin.php')),
'name' => 'Email Logs'
)
);

$inactive_class = 'nav-tab';
Expand Down Expand Up @@ -195,6 +200,14 @@ protected function render_test_section()
<?php
}

protected function render_logs_section()
{
$logsTable = new SparkPostEmailLogs();
$logsTable->prepare_items();

$logsTable->display();
}

public function wpsp_admin_page()
{
$image_url = $this->asset_url('assets/logo-40.png');
Expand All @@ -205,7 +218,7 @@ public function wpsp_admin_page()
<div class="sp-heading"><img src="<?php echo $image_url ?>" alt="SparkPost"> &nbsp;&nbsp;
<h2>SparkPost</h2>
</div>
<div class="wrap">
<div class="wrap sparkpost">

<?php

Expand All @@ -215,6 +228,8 @@ public function wpsp_admin_page()
$this->render_overrides();
} else if ($active_tab == 'test') {
$this->render_test_section();
} else if ($active_tab == 'logs') {
$this->render_logs_section();
} else {
$this->render_basic_settings();
}
Expand Down Expand Up @@ -254,6 +269,8 @@ public function sanitize_basic($input)

if (!empty($input['template'])) {
$new_input['template'] = sanitize_text_field($input['template']);
} else {
$new_input['template'] = '';
}

if (empty($input['password'])) {
Expand Down Expand Up @@ -308,10 +325,6 @@ public function sanitize_overrides($input)
$new_input['from_name'] = sanitize_text_field($input['from_name']);
}

if (!empty($input['template'])) {
$new_input['template'] = sanitize_text_field($input['template']);
}

if (!empty($input['enable_tracking'])) {
$new_input['enable_tracking'] = true;
} else {
Expand Down
24 changes: 24 additions & 0 deletions assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,27 @@
display: flex;
align-items: center;
}

.sparkpost .column-subject {
width:15% !important;
overflow: hidden;
}

.sparkpost .column-sent_at {
width:10% !important;
overflow: hidden;
}

.sparkpost .column-content {
width:30% !important;
overflow: hidden;
}
.sparkpost .column-response {
width:30% !important;
overflow: hidden;
}

.sparkpost .column-wp_mail_args{
width: 15%
overflow: hidden;
}
107 changes: 107 additions & 0 deletions email-logs.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace WPSparkPost;
// If ABSPATH is defined, we assume WP is calling us.
// Otherwise, this could be an illicit direct request.
if (!defined('ABSPATH')) exit();

if(!class_exists('WP_List_Table')){
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

class SparkPostEmailLogs extends \WP_List_Table {

function __construct(){
global $status, $page;
//Set parent defaults
parent::__construct( array(
'singular' => 'log',
'plural' => 'logs',
'ajax' => false
) );
}

public static function record_count() {
global $wpdb;

$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}sp_email_logs";

return $wpdb->get_var( $sql );
}

function get_logs($per_page = 10, $page = 1) {
global $wpdb;

$wpdb->show_errors();

$sql = "SELECT * FROM {$wpdb->prefix}sp_email_logs ORDER BY `created_at` DESC LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page - 1 ) * $per_page;

return $wpdb->get_results( $sql, 'ARRAY_A' );
}

function get_columns() {
$columns = [
'subject' => 'Subject',
'wp_mail_args' => 'wp_mail Arguments',
'sent_at' => 'Generated At',
'content' => 'Request',
'response' => 'SparkPost Response'
];

return $columns;
}

function column_subject($item){
return $item['subject'];
}

function column_sent_at($item) {
return $item['created_at'];
}

function column_content($item) {
return '<textarea style="width: 100%" rows="5">' . $item['content']. '</textarea>';
}

function column_wp_mail_args($item) {
return '<textarea style="width: 100%" rows="5">' . $item['wp_mail_args'] . '</textarea>';
}

function column_response($item) {
return '<textarea style="width: 100%" rows="5">' . $item['response'] . '</textarea>';
}


// function column_default($item, $column_name) {
// switch ( $column_name ) {
// case 'subject':
// return $item[ $column_name ];
// default:
// // return print_r( $item, true ); //Show the whole array for troubleshooting purposes
// }
// }

function column_name( $item ) {

return $item['name'];
}

function prepare_items() {
global $wpdb;

$per_page = 10;
$page = $this->get_pagenum();
$columns = $this->get_columns();
$this->_column_headers = array($columns);

$this->items = $this->get_logs($per_page, $page);
$total_items = $this->record_count();

$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages
) );

}
}
2 changes: 2 additions & 0 deletions mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class SparkPostHTTPMailer extends \PHPMailer
{
public $endpoint = 'https://api.sparkpost.com/api/v1/transmissions';
public $wp_mail_args;
private $settings;

/**
Expand Down Expand Up @@ -484,6 +485,7 @@ public function request($endpoint, $data) {
$result = $http->request($endpoint, $data);
do_action('wpsp_after_send', $result);
$this->debug('Response received');
SparkPost::add_log($this->wp_mail_args, $data['body'], $result);
return $result;
}
}
64 changes: 63 additions & 1 deletion sparkpost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,69 @@ class SparkPost
'transactional' => false
);

var $settings;
var $settings, $db_version;

public function __construct()
{
register_activation_hook(WPSP_PLUGIN_PATH, array($this, 'sp_activate'));
register_deactivation_hook(WPSP_PLUGIN_PATH, array($this, 'sp_deactivate'));

add_filter('plugin_action_links_' . plugin_basename(WPSP_PLUGIN_PATH), array($this, 'add_settings_link'));
add_action( 'plugins_loaded', array($this, 'db_update_check' ));


$this->settings = self::get_settings();

if (self::get_setting('enable_sparkpost')) { //no need to register this hooks if plugin is disabled
add_filter('wp_mail_from', array($this, 'set_from_email'));
add_filter('wp_mail_from_name', array($this, 'set_from_name'));
}

$this->db_version = '1.0.0';
}

public function sp_activate()
{
$settings = self::$settings_default;
$settings['transactional'] = true; // setting it here to apply this default value to new installation only as this is breaking change
update_option('sp_settings', $settings);
$this->install_db();
}

protected function install_email_log_table(){
global $wpdb;
$table_name = $wpdb->prefix . 'sp_email_logs';

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
id SERIAL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
wp_mail_args text NOT NULL,
subject varchar(255) NOT NULL,
content text NOT NULL,
response text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

$result = dbDelta($sql);
update_option('sp_db_version', $this->db_version);

return $result;
}

protected function install_db(){
return $this->install_email_log_table();
}


function db_update_check() {
if ( get_site_option( 'sp_db_version' ) != $this->db_version ) {
return $this->install_db();
}
return false;
}

public function sp_deactivate()
Expand Down Expand Up @@ -123,6 +164,7 @@ public function init_sp_http_mailer($args)
if (!$phpmailer instanceof SparkPostHTTPMailer) {
$phpmailer = new SparkPostHTTPMailer();
}
$phpmailer->wp_mail_args = $args;
return $args;
}

Expand All @@ -131,4 +173,24 @@ static function is_sandbox($email)
$email_splitted = array_slice(explode('@', $email), -1);
return $email_splitted[0] === 'sparkpostbox.com';
}

static function add_log($wp_mail_args, $content, $response) {
global $wpdb;
$wpdb->show_errors();
$content = json_decode($content);

//get subject
if(property_exists($content->content, 'subject')) {
$subject = $content->content->subject;
} else {
$subject = $content->substitution_data->subject;
}

return $wpdb->insert($wpdb->prefix . 'sp_email_logs', array(
'subject' => $subject,
'content' => json_encode($content),
'response' => json_encode($response),
'wp_mail_args' => json_encode($wp_mail_args)
));
}
}
1 change: 1 addition & 0 deletions wordpress-sparkpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

if (is_admin()) {
require_once(WPSP_PLUGIN_DIR . 'admin.widget.class.php');
require_once(WPSP_PLUGIN_DIR . 'email-logs.class.php');
new SparkPostAdmin();
}
$sp = new SparkPost();
Expand Down

0 comments on commit f57c506

Please sign in to comment.