PluginProbe
Mail Queue / 1.3
Mail Queue v1.3
1.6.1 1.6.0 1.5.1 trunk 1.0 1.1 1.2 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5.0
mail-queue / mail-queue-options.php

mail-queue-options.php in Mail Queue 1.3, at mail-queue-options.php

543 lines 31.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /* ***************************************************************
4 Options Page
5 **************************************************************** */
6
7 function wdm_wpma_actionlinks ( $actions ) {
8 $links = array(
9 '<a href="'.admin_url('admin.php?page=wdm_wpma_mail_queue').'">Settings</a>',
10 '<a href="'.admin_url('admin.php?page=wdm_wpma_mail_queue-tab-log').'">Log</a>',
11 '<a href="'.admin_url('admin.php?page=wdm_wpma_mail_queue-tab-queue').'">Queue</a>',
12 '<a href="'.admin_url('admin.php?page=wdm_wpma_mail_queue-tab-faq').'">FAQs</a>',
13 );
14 return array_merge($actions,$links );
15 }
16 add_filter('plugin_action_links_mail-queue/mail-queue.php','wdm_wpma_actionlinks');
17
18 // Options Page
19 function wdm_wpma_settings_page_menuitem() {
20 add_menu_page('Mail Queue','Mail Queue','manage_options','wdm_wpma_mail_queue','wdm_wpma_settings_page','data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTc4IiBoZWlnaHQ9IjE3OCIgdmlld0JveD0iMCAwIDE3OCAxNzgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNNzguNzI0MSA1LjI1MzA5Qzc2LjkwNzQgNC43MDgxIDc0Ljk0MDEgNS4wNTQxMyA3My40MTg0IDYuMTg2MjlDNzEuODk2OCA3LjMxODQ1IDcxIDkuMTAzNDEgNzEgMTEuMDAwMVYxNjdDNzEgMTY4Ljg5NyA3MS44OTY4IDE3MC42ODIgNzMuNDE4NCAxNzEuODE0Qzc0Ljk0MDEgMTcyLjk0NiA3Ni45MDc0IDE3My4yOTIgNzguNzI0MSAxNzIuNzQ3TDE1OC43MjQgMTQ4Ljc0N0MxNjEuMjYyIDE0Ny45ODYgMTYzIDE0NS42NSAxNjMgMTQzVjM1QzE2MyAzMi4zNTA0IDE2MS4yNjIgMzAuMDE0NSAxNTguNzI0IDI5LjI1MzFMNzguNzI0MSA1LjI1MzA5Wk04NS43ODg3IDIyLjM4NDZDODcuNzg1NCAyMS40Mzk0IDkwLjE3MDMgMjIuMjkxOSA5MS4xMTU0IDI0LjI4ODdMMTIyLjg1MiA5MS4zMzc4TDE0My4zMzQgNDQuNDAwMkMxNDQuMjE3IDQyLjM3NTUgMTQ2LjU3NSA0MS40NTAzIDE0OC42IDQyLjMzMzhDMTUwLjYyNSA0My4yMTc0IDE1MS41NSA0NS41NzUgMTUwLjY2NiA0Ny41OTk4TDEyNi42NjYgMTAyLjZDMTI2LjAzOSAxMDQuMDM3IDEyNC42MjkgMTA0Ljk3NiAxMjMuMDYxIDEwNUMxMjEuNDkzIDEwNS4wMjQgMTIwLjA1NiAxMDQuMTI5IDExOS4zODUgMTAyLjcxMUw4My44ODQ2IDI3LjcxMTNDODIuOTM5NCAyNS43MTQ2IDgzLjc5MTkgMjMuMzI5NyA4NS43ODg3IDIyLjM4NDZaIiBmaWxsPSIjYTdhYWFkIi8+CjxwYXRoIGQ9Ik00OSAxM0M1Mi4zMTM3IDEzIDU1IDE1LjY4NjMgNTUgMTlWMTU5QzU1IDE2Mi4zMTQgNTIuMzEzNyAxNjUgNDkgMTY1QzQ1LjY4NjMgMTY1IDQzIDE2Mi4zMTQgNDMgMTU5VjE5QzQzIDE1LjY4NjMgNDUuNjg2MyAxMyA0OSAxM1oiIGZpbGw9IiNhN2FhYWQiLz4KPHBhdGggZD0iTTIxIDIxQzI0LjMxMzcgMjEgMjcgMjMuNjg2MyAyNyAyN1YxNTFDMjcgMTU0LjMxNCAyNC4zMTM3IDE1NyAyMSAxNTdDMTcuNjg2MyAxNTcgMTUgMTU0LjMxNCAxNSAxNTFWMjdDMTUgMjMuNjg2MyAxNy42ODYzIDIxIDIxIDIxWiIgZmlsbD0iI2E3YWFhZCIvPgo8L3N2Zz4=');
21 add_submenu_page('wdm_wpma_mail_queue','Settings','Settings','manage_options','wdm_wpma_mail_queue','wdm_wpma_settings_page');
22 add_submenu_page('wdm_wpma_mail_queue','Log','Log','manage_options','wdm_wpma_mail_queue-tab-log','wdm_wpma_settings_page');
23 add_submenu_page('wdm_wpma_mail_queue','Queue','Queue','manage_options','wdm_wpma_mail_queue-tab-queue','wdm_wpma_settings_page');
24 add_submenu_page('wdm_wpma_mail_queue','FAQ','FAQ','manage_options','wdm_wpma_mail_queue-tab-faq','wdm_wpma_settings_page');
25 if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
26 add_submenu_page('wdm_wpma_mail_queue','Cron Information','Cron Information','manage_options','wdm_wpma_mail_queue-tab-croninfo','wdm_wpma_settings_page');
27 }
28 }
29 add_action('admin_menu','wdm_wpma_settings_page_menuitem');
30
31 function wdm_wpma_settings_page_assets () {
32 $screen = get_current_screen();
33 if ( preg_match( '#wdm_wpma_mail_queue#', $screen->base ) ) {
34 wp_enqueue_style( 'wdm_wpma_style', plugins_url( 'assets/css/admin.css', __FILE__ ) );
35 wp_enqueue_script( 'wdm_wpma_script', plugins_url( 'assets/js/wdm-wpma-admin.js', __FILE__ ), [ 'jquery' ], false, true );
36 }
37 }
38 add_action( 'admin_enqueue_scripts', 'wdm_wpma_settings_page_assets' );
39
40 // Options Page Settings
41 function wdm_wpma_settings_page() {
42
43 // Only Admins
44 if ( ! current_user_can( 'manage_options' ) ) { return; }
45
46 // Settings
47 global $wdm_wpma_options;
48
49 // Get the active tab from the $_GET param
50 $default_tab = null;
51 $tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : $default_tab;
52
53 echo '<div class="wrap">';
54
55 // Options Header
56 echo '<h1 class="wdm-wpma-title"><img class="wdm-wpma-logo" src="'.esc_url(plugins_url('assets/img/mail-queue-logo-wordmark.svg', __FILE__)).'" alt="Mail Queue" width="308" height="56" /></h1>';
57
58 $tab = sanitize_key($_GET['page']);
59 if ($tab != 'wdm_wpma_mail_queue-tab-croninfo' ) {
60 if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
61 echo '<div class="notice notice-warning notice-large">';
62 $url = esc_url(get_option('siteurl').'/wp-cron.php');
63 echo '<p><strong>Please note:</strong><br />Your normal WP Cron is disabled. Please make sure you\'re running the Cron manually by calling <a href="'.$url.'" target="_blank">'.$url.'</a> every couple of minutes.</p>';
64 echo '<p><a href="?page=wdm_wpma_mail_queue-tab-croninfo">More information</a></p>';
65 echo '</div>';
66 }
67 }
68 wdm_wpma_settings_page_navi($tab); // Tabs
69
70 // Options Page Content
71 if ($tab == 'wdm_wpma_mail_queue') {
72 echo '<form action="options.php" method="post">';
73 settings_fields('wdm_wpma_settings');
74 do_settings_sections('wdm_wpma_settings_page');
75 submit_button();
76 echo '</form>';
77 } else if ($tab == 'wdm_wpma_mail_queue-tab-log') {
78 echo '<form method="post">';
79 $logtable = new wdm_wpma_Log_Table();
80 $logtable->prepare_items();
81 $logtable->display();
82 echo '</form>';
83 } else if ($tab == 'wdm_wpma_mail_queue-tab-queue') {
84
85 if (isset($_GET['addtestmail'])) {
86 global $wpdb;
87 $tableName = $wpdb->prefix.$wdm_wpma_options['tableName'];
88 $data = array(
89 'timestamp'=>current_time('mysql',false),
90 'recipient'=>$wdm_wpma_options['email'],
91 'subject'=>'Testmail #'.time(),
92 'message'=>'This is just a test email send by the Mail Queue plugin.',
93 'status' => 'queue'
94 );
95 $wpdb->insert($tableName,$data);
96 }
97
98 $next_cron_timestamp = wp_next_scheduled('wp_mail_queue_hook');
99 if ($next_cron_timestamp) {
100 if ($next_cron_timestamp > time()) {
101 echo '<div class="notice notice-success"><p>Next Sending will be triggered in '.esc_html(human_time_diff($next_cron_timestamp)).' at '.esc_html(wp_date('H:i',$next_cron_timestamp)).'.</p></div>';
102 }
103 } else {
104 echo '<div class="notice notice-error"><p>The Queue is not enabled at the moment. Enable it in the <a href="admin.php?page=wdm_wpma_mail_queue">Settings</a>.</p></div>';
105 }
106
107 echo '<form method="post" action="admin.php?page=wdm_wpma_mail_queue-tab-queue">';
108 $queuetable = new wdm_wpma_Log_Table();
109 $queuetable->prepare_items();
110 $queuetable->display();
111 echo '</form>';
112 } else if ($tab == 'wdm_wpma_mail_queue-tab-faq') {
113 echo '<div class="wdm-wpma-box">';
114 echo '<h3>How does this Plugin work?</h3>';
115 echo '<p>If enabled this plugin intercepts the <a target="_blank" href="https://developer.wordpress.org/reference/functions/wp_mail/"><i>wp_mail()</i></a> function. Instead of sending the mails directly, it stores them in the database and sends them step by step with a delay during the <i>WP Cron</i>.</p>';
116 echo '<p>Current state: ';
117 if ($wdm_wpma_options['enabled'] == '1') {
118 echo '<b class="wdm-wpma-ok">The plugin is enabled</b> All Mails sent through <a target="_blank" href="https://developer.wordpress.org/reference/functions/wp_mail/"><i>wp_mail()</i></a> are delayed by the <a href="admin.php?page=wdm_wpma_mail_queue-tab-queue">Queue</a>.';
119 } else {
120 echo '<b>The plugin is disabled</b>. The plugin has no impact at the moment, no Mails inside the Queue are going to be sent.';
121 }
122 echo '</p>';
123 echo '</div>';
124 echo '<div class="wdm-wpma-box">';
125 echo '<h3>Does this plugin change the way <b>HOW</b> emails are sent?</h3>';
126 echo '<p>No, don\'t worry. This plugin only affects <b>WHEN</b> emails are sent, not how. It delays the sending (by the Queue), nonetheless all emails are sent through the standard <a target="_blank" href="https://developer.wordpress.org/reference/functions/wp_mail/"><i>wp_mail()</i></a> function.</p>';
127 echo '<p>If you use SMTP for sending, or an external service like Mailgun, everything will still work as expected.</p>';
128 echo '</div>';
129 echo '<div class="wdm-wpma-box">';
130 echo '<h3>Does this plugin work, if I have a Caching Plugin installed? E.g. <i>W3 Total Cache</i> or similar?</h3>';
131 echo '<p>If you\'re using a Caching plugin like <i>W3 Total Cache</i>, <i>WP Rocket</i> or any other caching solution which generates static html-files and serves them to visitors, you\'ll have to make sure you\'re calling the <a href="'.esc_url(get_option('siteurl')).'/wp-cron.php" target="_blank">wp-cron file</a> manually every couple of minutes.</p>';
132 echo '<p>Otherwise your normal WP Cron wouldn\'t be called as often as it should be and scheduled messages would be sent with big delays.</p>';
133 echo '</div>';
134 echo '<div class="wdm-wpma-box">';
135 echo '<h3>What about Proxy-Caching, e.g. NGINX?</h3>';
136 echo '<p>Same situation here. Please make sure you\'re calling the <a href="'.esc_url(get_option('siteurl')).'/wp-cron.php" target="_blank">WordPress Cron</a> by an external service or your webhoster every couple of minutes.</p>';
137 echo '</div>';
138 echo '<div class="wdm-wpma-box">';
139 echo '<h3>My form builder supports attachments. What about them?</h3>';
140 echo '<p>You are covered. All attachments are stored temporarily in the queue until they are sent along with their corresponding emails.</p>';
141 echo '</div>';
142 echo '<div class="wdm-wpma-box">';
143 echo '<h3>What are Queue alerts?</h3>';
144 echo '<p>This is a simple and effective way to improve the security of your WordPress installation.</p><p>Imagine: In case your website is sending spam through <a target="_blank" href="https://developer.wordpress.org/reference/functions/wp_mail/"><i>wp_mail()</i></a>, the email Queue would fill up very quickly preventing your website from sending so many spam emails at once. This gives you time and avoids a lot of trouble.</p><p>Queue Alerts warn you, if the Queue is longer than usal. You decide at which point you want to be alerted. So you get the chance to have a look if there might be something wrong on the website.</p>';
145 echo '<p>Current state: ';
146 if ($wdm_wpma_options['alert_enabled'] == '1') {
147 echo '<b class="wdm-wpma-ok">Alerts are enabled</b> If more than '.esc_html($wdm_wpma_options['email_amount']).' emails are waiting in the Queue, WordPress will sent an alert email to <i>'.esc_html($wdm_wpma_options['email']).'</i>.';
148 } else {
149 echo '<b>Alerting is disabled</b>. No alerts will be sent.';
150 }
151 echo '</p>';
152 echo '<p>Please note: This plugin will only send one alert every six hours.</p>';
153 echo '</div>';
154
155 echo '<div class="wdm-wpma-box">';
156 echo '<h3>Can I add emails with a high priority to the queue?</h3>';
157 echo '<p>Yes, you can add the custom <i>`X-Mail-Queue-Prio`</i> header set to <i>`High`</i> to your email. High priority emails will be sent through the standard Mail Queue sending cycle but before all normal emails lacking a priority header in the queue.</p>';
158 echo '<p><b>Example 1 (add priority to Woocommerce emails):</b></p>';
159 echo '<pre><code>add_filter(\'woocommerce_mail_callback_params\',function ( $array ) {
160 $prio_header = \'X-Mail-Queue-Prio: High\';
161 $array[3] .= $prio_header."\\r\\n";
162 return $array;
163 },10,1);</code></pre>';
164 echo '<p><b>Example 2 (add priority to a Contact Form 7 form email):</b></p>';
165 echo '<p>When editing a form in Contact Form 7 just add an additional line to the <i>`Additional Headers`</i> field under the <i>`Mail`</i> tab panel.</p>';
166 echo '<pre><code>X-Mail-Queue-Prio: High</code></pre>';
167 echo '</div>';
168
169 echo '<div class="wdm-wpma-box">';
170 echo '<h3>Can I send emails <i>instantly</i> without going through the queue?</h3>';
171 echo '<p>Yes, this is possible (if you absolutely need to do this).</p>';
172 echo '<p>For this you can add the custom <i>`X-Mail-Queue-Prio`</i> header set to <i>`Instant`</i> to your email. These emails are sent instantly circumventing the mail queue. They still appear in the Mail Queue log flagged as `instant`.</p>';
173 echo '<p>Mind that this is a potential security risk and should be considered carefully. Please use only as an exception.</p>';
174 echo '<p><b>Example 1 (instantly send Woocommerce emails):</b></p>';
175 echo '<pre><code>add_filter(\'woocommerce_mail_callback_params\',function ( $array ) {
176 $prio_header = \'X-Mail-Queue-Prio: Instant\';
177 $array[3] .= $prio_header."\\r\\n";
178 return $array;
179 },10,1);</code></pre>';
180 echo '<p><b>Example 2 (instantly send a Contact Form 7 form email):</b></p>';
181 echo '<p>When editing a form in Contact Form 7 just add an additional line to the <i>`Additional Headers`</i> field under the <i>`Mail`</i> tab panel.</p>';
182 echo '<pre><code>X-Mail-Queue-Prio: Instant</code></pre>';
183 echo '</div>';
184
185 echo '<div class="wdm-wpma-box">';
186 echo '<h3>Want to put a test email into the Queue?</h3>';
187 echo '<p><a class="button" href="admin.php?page=wdm_wpma_mail_queue-tab-queue&addtestmail">Sure! Put a Test Email for '.esc_html($wdm_wpma_options['email']).' into the Queue</a></p>';
188 echo '</div>';
189
190 } else if ($tab == 'wdm_wpma_mail_queue-tab-croninfo') {
191 echo '<div class="wdm-wpma-box">';
192 echo '<h3>Information: Your common WP Cron is disabled</h3>';
193 echo '<p>It look\'s like you deactived the WP Cron by <i>define( \' DISABLE_WP_CRON \' , true )</i>.</p>';
194 $url = esc_url(get_option('siteurl').'/wp-cron.php');
195 echo '<p>In general, this is no problem at all. We just want to remind you to make sure you\'re running the Cron manually by calling <a href="'.$url.'" target="_blank">'.$url.'</a> every couple of minutes.</p>';
196 echo '</div>';
197
198
199 if (function_exists('_get_cron_array')) {
200 $next_tasks = _get_cron_array();
201 if ($next_tasks) {
202 $tasks_in_past = false;
203 $tasks_of_mailqueue_in_past = false;
204 foreach($next_tasks as $key => $val) {
205 if (time() > intval($key) + intval($wdm_wpma_options['queue_interval'])) {
206 if (array_keys($val)[0] == 'wp_mail_queue_hook') { $tasks_of_mailqueue_in_past = intval($key); }
207 $tasks_in_past = true;
208 }
209 }
210 if ($tasks_in_past) {
211 echo '<div class="wdm-wpma-box">';
212 echo '<h3>Attention: It seams that your WP Cron is not running. There are some jobs waiting to be completed.</h3>';
213 if ($tasks_of_mailqueue_in_past) {
214 echo '<p><b>The Queue wasn\'t been able to be executed since '.esc_html(human_time_diff($tasks_of_mailqueue_in_past,time())).'.</b></p>';
215 }
216 echo '</div>';
217 }
218 }
219 }
220
221
222 }
223
224 echo '</div>';
225
226 }
227
228 if(!class_exists('WP_List_Table')){
229 require_once( ABSPATH . 'wp-admin/includes/screen.php' );
230 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
231 }
232
233 class wdm_wpma_Log_Table extends WP_List_Table {
234
235 function get_log() {
236 global $wpdb, $wdm_wpma_options;
237 $tableName = $wpdb->prefix.$wdm_wpma_options['tableName'];
238 return $wpdb->get_results("SELECT * FROM `$tableName` WHERE `status` != 'queue' ORDER BY `timestamp` DESC",'ARRAY_A');
239 }
240
241 function get_queue() {
242 global $wpdb, $wdm_wpma_options;
243 $tableName = $wpdb->prefix.$wdm_wpma_options['tableName'];
244 return $wpdb->get_results("SELECT * FROM `$tableName` WHERE `status` = 'queue' OR `status` = 'high' ORDER BY `status` ASC, `timestamp` DESC",'ARRAY_A');
245 }
246
247 function get_columns() {
248 $columns = array(
249 'id' => '<label><span class="screen-reader-text">Select all</span><input class="wdm-wpma-select-all" type="checkbox"></label>',
250 'timestamp' => 'Time',
251 'status' => 'Status',
252 'recipient' => 'Recipient',
253 'subject' => 'Subject',
254 'message' => 'Message',
255 'headers' => 'Headers',
256 'attachments' => 'Attachments',
257 );
258 return $columns;
259 }
260
261 public function prepare_items() {
262 $type = sanitize_key($_GET['page']);
263 $columns = $this->get_columns();
264 $hidden = array();
265 $sortable = array();
266 $this->_column_headers = array($columns, $hidden, $sortable);
267 $this->process_bulk_action();
268 if ($type == 'wdm_wpma_mail_queue-tab-log') {
269 $data = $this->get_log();
270 } else if ($type == 'wdm_wpma_mail_queue-tab-queue') {
271 $data = $this->get_queue();
272 }
273 if ($data && is_array($data)) {
274 $perPage = 50;
275 $currentPage = $this->get_pagenum();
276 $totalItems = count($data);
277 $this->set_pagination_args( array(
278 'total_items' => $totalItems,
279 'per_page' => $perPage
280 ) );
281 $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
282 }
283 $this->items = $data;
284 }
285
286 public function column_default( $item, $column_name ) {
287 switch( $column_name ) {
288 case 'timestamp':
289 case 'subject':
290 case 'info':
291 return esc_html( maybe_unserialize($item[$column_name]) );
292 break;
293 case 'recipient':
294 case 'headers':
295 $return = maybe_unserialize($item[$column_name]);
296 if (is_array($return)) {
297 return esc_html( implode(',',$return) );
298 } else {
299 return esc_html( $return );
300 }
301 break;
302 case 'attachments':
303 $return = maybe_unserialize($item[$column_name]);
304 //var_dump($return);
305 if (is_array($return)) {
306 $betterreturn = array();
307 foreach($return as $item) {
308 array_push($betterreturn,basename($item));
309 }
310 return esc_html( implode('<br />',$betterreturn) );
311 } else {
312 return esc_html( basename($return) );
313 }
314 break;
315 case 'status':
316 $info = isset( $item[ 'info' ] ) && $item[ 'info' ] ? $item[ 'info' ] : '';
317 if ( $item[$column_name] === 'alert' ) {
318 $alertData = json_decode($info,/*associative*/true);
319 if ( $alertData ) {
320 $info = '<strong>Emails in queue</strong>: '.esc_html($alertData['in_queue']);
321 $info .= '<br /><strong>Queue settings</strong>: Send max '.esc_html($alertData['queue_amount']).' email(s) every '.esc_html($alertData['queue_interval']/60).' minute(s).';
322 $info .= '<br /><strong>Alert settings</strong>: Send alert if more than '.esc_html($alertData['email_amount']).' email(s) in the queue.';
323 } else {
324 $info = '';
325 }
326 }
327 $htmlInfo = $info ? '<span class="wdm-wpma-info">'.$info.'</span>' : '';
328 $cssStatus = $htmlInfo ? ' wdm-wpma-status-has-info' : '';
329 return '<span class="wdm-wpma-status wdm-wpma-status-'.sanitize_title($item[$column_name]).esc_html($cssStatus).'">'.$item[$column_name].$htmlInfo.'</span>';
330 break;
331 case 'message':
332 $message = $item[$column_name];
333
334 if ( $message ) {
335 $messageLen = strlen($message);
336 $message = htmlentities($message);
337 $message = preg_replace('#^(.*&lt;body&gt;)(\r?\n)?#ims', '<details class="wdm-wpma-email-source-meta"><summary>HEADER</summary><span>$1</span></details><!--wdm-wpma-start-->', $message );
338 $message = preg_replace('#(&lt;/body&gt;.*)$#ims', '<!--wdm-wpma-end--><details class="wdm-wpma-email-source-meta"><summary>FOOTER</summary><span>$1</span></details>', $message);
339 $message = preg_replace('#<!--wdm-wpma-start-->(.*)<!--wdm-wpma-end-->#ims', '<span class="wdm-wpma-email-source-body">$1</span>', $message);
340 $message = preg_replace('#\r?\n#', '<br />', $message);
341 $return = '<details>';
342 $return .= '<summary class="wdm-wpma-view-source">View message <i>('.esc_html($messageLen).' bytes)</i></summary>';
343 $return .= '<div class="wdm-wpma-email-source">'.wp_kses_post($message).'</div>';
344 $return .= '</details>';
345 } else {
346 $return = '<em>Empty</em>';
347 }
348 return $return;
349 break;
350 case 'id':
351 return '<input type="checkbox" name="id[]" value="'.esc_attr($item[$column_name]).'" />';
352 break;
353 default:
354 return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
355 }
356 }
357
358 public function get_bulk_actions() {
359 if (isset($_GET['page']) && $_GET['page'] == 'wdm_wpma_mail_queue-tab-queue') {
360 $actions = array(
361 'delete' => __( 'Delete')
362 );
363 } else {
364 $actions = array(
365 'delete' => __( 'Delete'),
366 'resend' => __( 'Resend'),
367 );
368 }
369
370 return $actions;
371 }
372
373 public function process_bulk_action() {
374
375 // security check!
376 if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {
377 $nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
378 $action = 'bulk-' . $this->_args['plural'];
379 if ( ! wp_verify_nonce( $nonce, $action ) )
380 wp_die( 'Nope! Security check failed!' );
381 }
382
383 // get IDs
384 $request_ids = isset( $_REQUEST['id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['id'] ) ) : array();
385 if ( empty( $request_ids ) ) { return; }
386
387 global $wpdb, $wdm_wpma_options;
388 $tableName = $wpdb->prefix.$wdm_wpma_options['tableName'];
389
390 switch ( $this->current_action() ) {
391 case 'delete':
392 foreach($request_ids as $id) {
393 $wpdb->delete($tableName,array('id'=>intval($id)),'%d');
394 }
395 break;
396 case 'resend':
397 foreach($request_ids as $id) {
398 $count_resend = 0;
399 $count_error = 0;
400 $maildata = $wpdb->get_row("SELECT * FROM `$tableName` WHERE `id` = '$id'");
401 if (!$maildata->attachments || $maildata->attachments == '') {
402 $count_resend++;
403 $data = array(
404 'timestamp'=> current_time('mysql',false),
405 'recipient'=> $maildata->recipient,
406 'subject'=> $maildata->subject,
407 'message'=> $maildata->message,
408 'status' => 'queue',
409 'attachments' => '',
410 'headers' => $maildata->headers,
411 );
412 $wpdb->insert($tableName,$data);
413 } else {
414 $count_error++;
415 $notice = '<div class="notice notice-error is-dismissible">';
416 $notice .= '<p><b>Sorry, your email to '.$maildata->recipient.' can\'t be sent again.</b></p>';
417 $notice .= '<p>The email used to have attachments, which are not available anymore. Only emails without attachments can be resend.</p>';
418 $notice .= '</div>';
419 echo $notice;
420 }
421 }
422 if ($count_error == 0 && $count_resend > 0) {
423 wp_redirect('admin.php?page=wdm_wpma_mail_queue-tab-queue');
424 exit;
425 } else if ($count_error > 0 && $count_resend > 0) {
426 $notice = '<div class="notice notice-success is-dismissible">';
427 $notice .= '<p>The other emails have been put again into the <a href="admin.php?page=wdm_wpma_mail_queue-tab-queue">Queue</a>.</p>';
428 $notice .= '</div>';
429 echo $notice;
430 }
431 break;
432 }
433
434 return;
435
436 }
437
438 }
439
440 function wdm_wpma_settings_page_navi($tab) {
441 echo '<nav class="nav-tab-wrapper">';
442 echo '<a href="?page=wdm_wpma_mail_queue" class="nav-tab'; if($tab==='wdm_wpma_mail_queue') { echo ' nav-tab-active'; } echo '">Settings</a>';
443 echo '<a href="?page=wdm_wpma_mail_queue-tab-log" class="nav-tab'; if($tab==='wdm_wpma_mail_queue-tab-log') { echo ' nav-tab-active'; } echo '">Log</a>';
444 echo '<a href="?page=wdm_wpma_mail_queue-tab-queue" class="nav-tab'; if($tab==='wdm_wpma_mail_queue-tab-queue') { echo ' nav-tab-active'; } echo '">Queue</a>';
445 echo '<a href="?page=wdm_wpma_mail_queue-tab-faq" class="nav-tab'; if($tab==='wdm_wpma_mail_queue-tab-faq') { echo ' nav-tab-active'; } echo '">FAQ</a>';
446 if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
447 echo '<a href="?page=wdm_wpma_mail_queue-tab-croninfo" class="nav-tab'; if($tab==='wdm_wpma_mail_queue-tab-croninfo') { echo ' nav-tab-active'; } echo '">Cron Information</a>';
448 }
449 echo '</nav>';
450 }
451
452 function wdm_wpma_settings_init() {
453 global $wdm_wpma_options;
454 register_setting('wdm_wpma_settings','wdm_wpma_settings');
455 add_settings_section('wdm_wpma_settings_section','',null,'wdm_wpma_settings_page');
456 add_settings_field('wdm_wpma_status','Queue + Log enabled','wdm_wpma_render_option_status','wdm_wpma_settings_page','wdm_wpma_settings_section');
457 add_settings_field('wdm_wpma_queue','Queue','wdm_wpma_render_option_queue','wdm_wpma_settings_page','wdm_wpma_settings_section');
458 add_settings_field('wdm_wpma_log','Log','wdm_wpma_render_option_log','wdm_wpma_settings_page','wdm_wpma_settings_section');
459 add_settings_field('wdm_wpma_alert_status','Alert enabled','wdm_wpma_render_option_alert_status','wdm_wpma_settings_page','wdm_wpma_settings_section');
460 add_settings_field('wdm_wpma_sensitivity','Alert Sensitivity','wdm_wpma_render_option_sensitivity','wdm_wpma_settings_page','wdm_wpma_settings_section');
461 }
462 add_action('admin_init','wdm_wpma_settings_init');
463
464 function wdm_wpma_render_option_status() {
465 global $wdm_wpma_options;
466 if ($wdm_wpma_options['enabled'] == '1') {
467 echo '<input type="checkbox" name="wdm_wpma_settings[enabled]" value="1" checked />';
468 } else {
469 echo '<input type="checkbox" name="wdm_wpma_settings[enabled]" value="1" /> &nbsp; &nbsp; <span class="wdm-wpma-warning"> &larr; Check this to enable the Queue. </span> Otherwise this plugin won\'t have any effect on your website.';
470 }
471 }
472
473 function wdm_wpma_render_option_alert_status() {
474 global $wdm_wpma_options;
475 if ($wdm_wpma_options['alert_enabled'] == '1') {
476 echo '<input type="checkbox" name="wdm_wpma_settings[alert_enabled]" value="1" checked />';
477 } else {
478 echo '<input type="checkbox" name="wdm_wpma_settings[alert_enabled]" value="1" />';
479 }
480 }
481
482 function wdm_wpma_render_option_queue() {
483 global $wdm_wpma_options;
484 if ($wdm_wpma_options['queue_interval_unit'] == 'seconds') {
485 $number = intval($wdm_wpma_options['queue_interval']);
486 } else {
487 $number = intval($wdm_wpma_options['queue_interval']) / 60;
488 }
489
490 echo 'Send max. <input name="wdm_wpma_settings[queue_amount]" type="number" min="1" value="'.esc_attr($wdm_wpma_options['queue_amount']).'" /> email(s) every <input name="wdm_wpma_settings[queue_interval]" type="number" min="1" value="'.esc_attr($number).'" />';
491
492 if ($wdm_wpma_options['queue_interval_unit'] == 'seconds') {
493 echo '<select name="wdm_wpma_settings[queue_interval_unit]"><option value="minutes">minute(s)</option><option selected value="seconds">second(s)</option></select>';
494 } else {
495 echo '<select name="wdm_wpma_settings[queue_interval_unit]"><option selected value="minutes">minute(s)</option><option value="seconds">second(s)</option></select>';
496 }
497
498 echo ' by <i><a href="https://developer.wordpress.org/plugins/cron/" target="_blank">WP Cron</a></i>. ';
499
500 }
501
502 function wdm_wpma_render_option_log() {
503 global $wdm_wpma_options;
504 echo 'Delete Log entries older than <input name="wdm_wpma_settings[clear_queue]" type="number" min="1" value="'.esc_attr(intval($wdm_wpma_options['clear_queue']) / 24).'" /> days.';
505 }
506
507 function wdm_wpma_render_option_sensitivity() {
508 global $wdm_wpma_options;
509 echo 'Send alert to <input type="text" name="wdm_wpma_settings[email]" value="'.esc_attr(sanitize_email($wdm_wpma_options['email'])).'" /> if more than <input name="wdm_wpma_settings[email_amount]" type="number" min="1" value="'.esc_attr(intval($wdm_wpma_options['email_amount'])).'" /> email(s) in the <a href="admin.php?page=wdm_wpma_mail_queue-tab-queue">Queue</a>.';
510 }
511
512
513 /* ***************************************************************
514 Alert WordPress User if last email in log could not be sent
515 **************************************************************** */
516 function wdm_wpma_checkLogForErrors() {
517
518 global $wpdb,$wdm_wpma_options;
519 if ($wdm_wpma_options['enabled'] != '1') { return; }
520
521 $tableName = $wpdb->prefix.$wdm_wpma_options['tableName'];
522 $last_mail = $wpdb->get_row("SELECT * FROM `$tableName` WHERE `status` != 'queue' ORDER BY `id` DESC",'ARRAY_A');
523 if (!$last_mail) { return; }
524
525 if ($last_mail['status'] == 'error') {
526 if (current_user_can('manage_options')) {
527 $notice = '<div class="notice notice-error is-dismissible">';
528 $notice .= '<h1>Attention: Your website has problems sending e-mails</h1>';
529 $notice .= '<p>This is an important message from your <i>Mail Queue</i> plugin. Please take a look at your <a href="admin.php?page=wdm_wpma_mail_queue-tab-log">Mail Log</a>. The last email(s) couldn\'t be sent properly.</p>';
530 $notice .= '<p>Last error message was: <b>'.$last_mail['info'].'</b></p>';
531 $notice .= '</div>';
532 echo $notice;
533 } else if (current_user_can('edit_posts')) {
534 $notice = '<div class="notice notice-error is-dismissible">';
535 $notice .= '<h1>Attention: Your website has problems sending e-mails</h1>';
536 $notice .= '<p>Please contact your Administrator. It seems that WordPress is not able to send emails.</p>';
537 $notice .= '<p>Last error message: <b>'.$last_mail['info'].'</b></p>';
538 $notice .= '</div>';
539 echo $notice;
540 }
541 }
542 }
543 add_action('admin_notices', 'wdm_wpma_checkLogForErrors');