@@ -50,6 +50,7 @@ public function __construct() {
5050 $ this ->plugin_name = TNCTOOLBOX_NAME ;
5151 add_action ('init ' , array ($ this , 'init_settings ' ));
5252 add_action ('admin_enqueue_scripts ' , array ($ this , 'enqueue_admin_styles ' ));
53+ add_action ('wp_ajax_tnc_test_slack_webhook ' , array ($ this , 'ajax_test_slack_webhook ' ));
5354 }
5455
5556 // Enqueue admin styles for settings page
@@ -126,11 +127,15 @@ private function save_settings() {
126127 $ api_key = sanitize_text_field ($ _POST ['tnc_toolbox_api_key ' ] ?? '' );
127128 $ username = sanitize_text_field ($ _POST ['tnc_toolbox_username ' ] ?? '' );
128129 $ hostname = sanitize_text_field ($ _POST ['tnc_toolbox_server_hostname ' ] ?? '' );
130+ $ slack_webhook = sanitize_url ($ _POST ['tnc_toolbox_slack_webhook ' ] ?? '' );
129131
130132 // Handle selective purge setting - use the POST value directly to avoid race conditions
131133 $ selective_purge_enabled = isset ($ _POST ['tnc_selective_purge ' ]);
132134 TNC_Cache_Purge::set_enabled ($ selective_purge_enabled );
133135
136+ // Save Slack webhook URL
137+ TNC_Slack_Alerts::store_webhook_url ($ slack_webhook );
138+
134139 // Try to save the configuration even if empty to ensure options exist
135140 TNC_cPanel_UAPI::store_config ($ username , $ api_key , $ hostname );
136141
@@ -229,6 +234,21 @@ class="regular-text" />
229234 </label>
230235 </td>
231236 </tr>
237+ <tr>
238+ <th scope="row">
239+ <label for="tnc_toolbox_slack_webhook">Slack Webhook URL</label>
240+ <p class="description">For mail failure alerts.<br><a href="https://api.slack.com/messaging/webhooks" target="_blank">Create Webhook</a>.</p>
241+ </th>
242+ <td>
243+ <input type="url" id="tnc_toolbox_slack_webhook" name="tnc_toolbox_slack_webhook"
244+ value="<?php echo esc_attr (TNC_Slack_Alerts::get_webhook_url ()); ?> "
245+ placeholder="https://hooks.slack.com/services/..."
246+ class="regular-text" />
247+ <button type="button" id="tnc_test_slack_webhook" class="button button-secondary">Test Webhook</button>
248+ <span id="tnc_slack_test_result" style="margin-left: 10px;"></span>
249+ <p class="description">Receive alerts when WordPress fails to send emails.</p>
250+ </td>
251+ </tr>
232252 </table>
233253
234254 <p class="submit">
@@ -254,6 +274,72 @@ class="regular-text" />
254274 <?php endif ; ?>
255275 </div>
256276 </div>
277+ <script type="text/javascript">
278+ jQuery(document).ready(function($) {
279+ $('#tnc_test_slack_webhook').on('click', function() {
280+ var $button = $(this);
281+ var $result = $('#tnc_slack_test_result');
282+ var webhookUrl = $('#tnc_toolbox_slack_webhook').val();
283+
284+ if (!webhookUrl) {
285+ $result.html('<span style="color: #dc3232;">Please enter a webhook URL first.</span>');
286+ return;
287+ }
288+
289+ $button.prop('disabled', true).text('Testing...');
290+ $result.html('');
291+
292+ $.post(ajaxurl, {
293+ action: 'tnc_test_slack_webhook',
294+ webhook_url: webhookUrl,
295+ nonce: '<?php echo wp_create_nonce ('tnc_test_slack_webhook ' ); ?> '
296+ }, function(response) {
297+ if (response.success) {
298+ $result.html('<span style="color: #46b450;">✓ ' + response.data.message + '</span>');
299+ } else {
300+ $result.html('<span style="color: #dc3232;">✗ ' + response.data.message + '</span>');
301+ }
302+ }).fail(function() {
303+ $result.html('<span style="color: #dc3232;">✗ Request failed</span>');
304+ }).always(function() {
305+ $button.prop('disabled', false).text('Test Webhook');
306+ });
307+ });
308+ });
309+ </script>
257310 <?php
258311 }
312+
313+ /**
314+ * AJAX handler for testing Slack webhook
315+ */
316+ public function ajax_test_slack_webhook () {
317+ // Verify nonce
318+ if (!wp_verify_nonce ($ _POST ['nonce ' ] ?? '' , 'tnc_test_slack_webhook ' )) {
319+ wp_send_json_error (array ('message ' => 'Invalid nonce ' ));
320+ }
321+
322+ // Check permissions
323+ if (!current_user_can ('manage_options ' )) {
324+ wp_send_json_error (array ('message ' => 'Permission denied ' ));
325+ }
326+
327+ // Get and validate webhook URL
328+ $ webhook_url = sanitize_url ($ _POST ['webhook_url ' ] ?? '' );
329+ if (empty ($ webhook_url )) {
330+ wp_send_json_error (array ('message ' => 'No webhook URL provided ' ));
331+ }
332+
333+ // Temporarily store the URL for testing
334+ TNC_Slack_Alerts::store_webhook_url ($ webhook_url );
335+
336+ // Run the test
337+ $ result = TNC_Slack_Alerts::test_webhook ();
338+
339+ if ($ result ['success ' ]) {
340+ wp_send_json_success (array ('message ' => $ result ['message ' ]));
341+ } else {
342+ wp_send_json_error (array ('message ' => $ result ['message ' ]));
343+ }
344+ }
259345}
0 commit comments