Skip to content

Commit ac3835b

Browse files
author
Patrick Graham
committed
Adds optional logging setting that logs send attempts and their responses from the Postmark API.
1 parent eb853a8 commit ac3835b

10 files changed

+323
-23
lines changed

LICENSE.txt

100644100755
File mode changed.

assets/css/admin.css

100644100755
+30
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,33 @@ input[type=text] {
3636
input[type=text] {
3737
width: 300px;
3838
}
39+
40+
#wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log {
41+
margin-bottom: 0;
42+
margin-top: 3%;
43+
overflow: hidden;
44+
overflow-y: scroll;
45+
display: block;
46+
border-collapse: collapse;
47+
}
48+
49+
#wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log > tbody > tr:nth-child(odd) {
50+
background-color: white;
51+
}
52+
53+
#wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log > tbody > tr > td {
54+
border-bottom: 1px solid #ddd;
55+
padding-bottom: 0.5em;
56+
padding-top: 0.5em;
57+
padding-left: 0.5em
58+
}
59+
60+
#wpbody-content > div.wrap > div.tab-content.tab-log.active > table > thead > tr > th {
61+
font-size: 1.2em;
62+
padding-bottom: 0.7em;
63+
}
64+
65+
.load-more {
66+
display: flex;
67+
justify-content: center;
68+
}

assets/images/logo.png

100644100755
File mode changed.

assets/js/admin.js

100644100755
+44-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
(function ($) {
22
$(function() {
33
var settings = postmark.settings;
4+
var logs_offset = 10;
45
$('.pm-enabled').prop('checked', settings.enabled);
56
$('.pm-api-key').val(settings.api_key);
67
$('.pm-sender-address').val(settings.sender_address);
78
$('.pm-force-html').prop('checked', settings.force_html);
89
$('.pm-track-opens').prop('checked', settings.track_opens);
10+
$('.pm-enable-logs').prop('checked', settings.enable_logs);
911

1012
// save
1113
$(document).on('click', '.save-settings', function() {
@@ -14,7 +16,8 @@
1416
'api_key': $('.pm-api-key').val(),
1517
'sender_address': $('.pm-sender-address').val(),
1618
'force_html': $('.pm-force-html').is(':checked') ? 1 : 0,
17-
'track_opens': $('.pm-track-opens').is(':checked') ? 1 : 0
19+
'track_opens': $('.pm-track-opens').is(':checked') ? 1 : 0,
20+
'enable_logs': $('.pm-enable-logs').is(':checked') ? 1 : 0
1821
};
1922

2023
$.post(ajaxurl, {
@@ -24,6 +27,13 @@
2427
}, function(response) {
2528
$('.pm-notice').html('<p>' + response + '</p>');
2629
$('.pm-notice').removeClass('hidden');
30+
31+
// Immediately hides logs tab if logs were disabled.
32+
if (data.enable_logs == 1) {
33+
$('#pm-log-nav-tab').show();
34+
} else {
35+
$('#pm-log-nav-tab').hide();
36+
}
2737
});
2838
});
2939

@@ -50,6 +60,39 @@
5060
$('.tab-' + which).addClass('active');
5161
});
5262

63+
// Loads more logs when 'Load More' button is clicked.
64+
$(document).on('click', '.load-more', function() {
65+
66+
var data = {
67+
action: 'postmark_load_more_logs',
68+
offset: logs_offset,
69+
_wpnonce : $('#_wpnonce').val()
70+
};
71+
72+
// Prepares for next 'Load More' by increasing the offset amount for next query.
73+
logs_offset += 10;
74+
75+
$.post( ajaxurl, data, function( response ) {
76+
77+
// Parses response string into JSON.
78+
response = JSON.parse(response);
79+
80+
// Adds new logs to logs table.
81+
$('#pm-log-table tr:last').after(response.html);
82+
83+
// Tracks the number of total logs present in logs table.
84+
var total_logs_count = response.total_count;
85+
86+
// Tracks the number of logs currently displayed.
87+
var displayed_log_count = $('#pm-log-table tr').length - 1;
88+
89+
// Hides the 'Load More' button if no additional logs to display.
90+
if (displayed_log_count == total_logs_count) {
91+
$('.load-more').hide();
92+
}
93+
});
94+
});
95+
5396
// force html if track opens is enabled
5497
$(document).on('click', '.pm-track-opens', function() {
5598
if ($(this).is(':checked')) {

docker-compose-non-dev.yml

100644100755
File mode changed.

docker-compose.yml

100644100755
File mode changed.

page-settings.php

100644100755
+87-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
<script>
2-
var postmark = postmark || {};
3-
postmark.settings = <?php echo json_encode( $this->settings ); ?>;
2+
var postmark = postmark || {};
3+
postmark.settings = <?php echo json_encode( $this->settings ); ?>;
44
</script>
5-
<script src="<?php echo POSTMARK_URL; ?>/assets/js/admin.js"></script>
6-
<link href="<?php echo POSTMARK_URL; ?>/assets/css/admin.css" rel="stylesheet">
7-
<?php wp_nonce_field( 'postmark_nonce' ); ?>
5+
<?php
6+
7+
// Registers script for JS.
8+
wp_register_script('pm-js',
9+
plugins_url( 'assets/js/admin.js', __FILE__ )
10+
);
11+
12+
// Enqueues script for JS.
13+
wp_enqueue_script( 'pm-js' );
14+
15+
// Registers script for CSS.
16+
wp_register_style( 'pm-styles',
17+
plugins_url( 'assets/css/admin.css', __FILE__ )
18+
);
19+
20+
// Enqueues script for CSS.
21+
wp_enqueue_style( 'pm-styles' );
22+
23+
wp_nonce_field( 'postmark_nonce' );
24+
25+
?>
826
<div class="wrap">
927
<div class="logo-bar">
1028
<a href="https://postmarkapp.com/" target="_blank"><img src="<?php echo POSTMARK_URL; ?>/assets/images/logo.png" width="130" height="21" alt="" /></a>
@@ -14,16 +32,22 @@
1432
<a class="nav-tab" rel="general">General</a>
1533
<a class="nav-tab" rel="test">Send Test Email</a>
1634
<a class="nav-tab" rel="overrides">Overrides</a>
35+
<!-- Only show Log tab if logging is enabled -->
36+
<?php if ( $this->settings['enable_logs']) : ?>
37+
<a class="nav-tab" rel="log" id="pm-log-nav-tab">Logs</a>
38+
<?php else : ?>
39+
<a class="nav-tab hidden" rel="log" id="pm-log-nav-tab">Logs</a>
40+
<?php endif; ?>
1741

1842
<?php if ( isset($_ENV['POSTMARK_PLUGIN_TESTING']) && 'POSTMARK_PLUGIN_TESTING' == $_ENV['POSTMARK_PLUGIN_TESTING'] ) : ?>
19-
<a class="nav-tab" rel="plugin-testing">Plugin Testing</a>
20-
<?php endif; ?>
43+
<a class="nav-tab" rel="plugin-testing">Plugin Testing</a>
44+
<?php endif; ?>
2145
</h1>
2246

2347
<div class="updated notice pm-notice hidden"></div>
2448

2549
<div class="tab-content tab-general">
26-
<table class="form-table" style="max-width:740px;">
50+
<table class="form-table">
2751
<tr>
2852
<th><label>Enabled?</label></th>
2953
<td>
@@ -59,6 +83,13 @@
5983
<span class="footnote">Track email opens (<code>Force HTML</code> is required).</span>
6084
</td>
6185
</tr>
86+
<tr>
87+
<th><label>Enable Logs</label></th>
88+
<td>
89+
<input type="checkbox" class="pm-enable-logs" value="1" />
90+
<span class="footnote">Log send attempts for historical/troubleshooting purposes (Recommended).</span>
91+
</td>
92+
</tr>
6293
</table>
6394

6495
<div class="submit">
@@ -106,6 +137,54 @@
106137
</pre>
107138
To learn more about <code>wp_mail</code>, see the <a href="https://developer.wordpress.org/reference/functions/wp_mail/">WordPress Codex page.</a>
108139
</div>
140+
141+
<!-- Sending logs tab -->
142+
<div class="tab-content tab-log">
143+
144+
<?php
145+
global $wpdb;
146+
147+
$table = $wpdb->prefix . "postmark_log";
148+
149+
// Checks how many logs are in the logs table.
150+
$count = $wpdb->get_var("SELECT COUNT(*) FROM " . $table );
151+
152+
// Only shows some logs if some logs are stored.
153+
if ($count > 0) {
154+
155+
// Pulls sending logs from db to display in UI. prepare() used to prevent SQL injections
156+
$result = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table ORDER BY time DESC LIMIT %d", 10));
157+
158+
// Logs table header HTML.
159+
echo "<table class=\"pm-log\" id=\"pm-log-table\">
160+
<thead>
161+
<th>Date</th>
162+
<th>From</th>
163+
<th>To</th>
164+
<th>Subject</th>
165+
<th>Postmark API Response</th>
166+
</thead><tbody>";
167+
168+
// Builds HTML for each log to show as a row in the logs table.
169+
foreach($result as $row)
170+
{
171+
echo "<tr><td align=\"center\">" . get_date_from_gmt($row->time, get_option('date_format') . ' - ' . get_option('time_format') ) . "</td><td align=\"center\"> " . $row->fromaddress . "</td><td align=\"center\"> " . $row->toaddress . "</td><td align=\"center\"> " . $row->subject . "</td><td align=\"center\"> " . $row->response . "</td></tr>";
172+
}
173+
174+
echo "</tbody></table>";
175+
176+
// Shows a 'Load More' button if more than 10 logs in logs table.
177+
if ($count > 10) {
178+
echo '<div class="submit load-more">
179+
<input type="submit" class="button-primary" value="Load More" /></div>';
180+
}
181+
} else {
182+
echo '<h2 align="center">No Logs</h2>';
183+
}
184+
?>
185+
186+
</div>
187+
109188
<?php if ( isset($_ENV['POSTMARK_PLUGIN_TESTING']) &&'POSTMARK_PLUGIN_TESTING' == $_ENV['POSTMARK_PLUGIN_TESTING'] ) : ?>
110189
<div class="tab-content tab-plugin-testing">
111190
<table class="form-table" style="max-width:740px;">

0 commit comments

Comments
 (0)