wp-multi-comment-notifications.php aktualisiert

This commit is contained in:
M_Viper 2025-04-07 17:11:33 +00:00
parent baa8f3be1e
commit 7071d3a062

View File

@ -2,8 +2,8 @@
/* /*
* Plugin Name: WP Multi Comment Notifications * Plugin Name: WP Multi Comment Notifications
* Plugin URI: https://git.viper.ipv64.net/M_Viper/wp-multi-comment-notifications * Plugin URI: https://git.viper.ipv64.net/M_Viper/wp-multi-comment-notifications
* Description: Benachrichtigt bei neuen Kommentaren per E-Mail und optional über Telegram. Ideal für Teams, die schnell informiert werden wollen. * Description: Benachrichtigt bei neuen Kommentaren per E-Mail und optional über Telegram & Discord. Ideal für Teams, die schnell informiert werden wollen.
* Version: 1.0 * Version: 1.1
* Author: M_Viper * Author: M_Viper
* Author URI: https://m-viper.de * Author URI: https://m-viper.de
* Requires at least: 6.7.2 * Requires at least: 6.7.2
@ -12,7 +12,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.html * License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-multi-comment-notifications * Text Domain: wp-multi-comment-notifications
* Domain Path: /languages * Domain Path: /languages
* Tags: kommentare, benachrichtigung, e-mail, telegram, kommentarüberwachung, teamkommunikation * Tags: kommentare, benachrichtigung, e-mail, telegram, discord, kommentarüberwachung, teamkommunikation
* *
* Support: * Support:
* - Microsoft Teams: https://teams.live.com/l/community/FEAzokphpZTJ2u6OgI * - Microsoft Teams: https://teams.live.com/l/community/FEAzokphpZTJ2u6OgI
@ -169,6 +169,15 @@ function wp_multi_comment_notifications_settings_page() {
<label for="wp_telegram_token">Telegram Bot Token</label> <label for="wp_telegram_token">Telegram Bot Token</label>
<input type="text" name="wp_telegram_token" value="<?php echo esc_attr(get_option('wp_telegram_token')); ?>" class="regular-text" /> <input type="text" name="wp_telegram_token" value="<?php echo esc_attr(get_option('wp_telegram_token')); ?>" class="regular-text" />
<h3>Discord Webhook URL</h3>
<label for="wp_discord_webhook_url">Webhook URL</label>
<input type="text" name="wp_discord_webhook_url" value="<?php echo esc_attr(get_option('wp_discord_webhook_url')); ?>" class="regular-text" />
<h3>Nachricht für Discord</h3>
<textarea name="wp_discord_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_discord_message')); ?></textarea>
<p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p>
<h3>Nachricht für E-Mail</h3> <h3>Nachricht für E-Mail</h3>
<textarea name="wp_email_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_email_message')); ?></textarea> <textarea name="wp_email_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_email_message')); ?></textarea>
<p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p> <p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p>
@ -198,6 +207,9 @@ function wp_multi_comment_notifications_settings_page() {
<label for="wp_user_<?php echo $i; ?>_telegram_chat_id">Telegram Chat ID</label> <label for="wp_user_<?php echo $i; ?>_telegram_chat_id">Telegram Chat ID</label>
<input type="text" name="wp_user_<?php echo $i; ?>_telegram_chat_id" value="<?php echo esc_attr(get_option("wp_user_{$i}_telegram_chat_id")); ?>" /> <input type="text" name="wp_user_<?php echo $i; ?>_telegram_chat_id" value="<?php echo esc_attr(get_option("wp_user_{$i}_telegram_chat_id")); ?>" />
<!-- Button für individuelle Testnachricht -->
<input type="submit" name="send_test_message_user_<?php echo $i; ?>" class="button button-secondary" value="Testnachricht an <?php echo esc_attr(get_option("wp_user_{$i}_name")); ?>" />
</div> </div>
<?php endfor; ?> <?php endfor; ?>
</div> </div>
@ -261,99 +273,148 @@ function wp_multi_comment_notifications_register_settings() {
register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_message'); register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_message');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_backend_button_url'); register_setting('wp_multi_comment_notifications_settings_group', 'wp_backend_button_url');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_logo_url'); register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_logo_url');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_discord_webhook_url');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_discord_message');
} }
add_action('admin_init', 'wp_multi_comment_notifications_register_settings'); add_action('admin_init', 'wp_multi_comment_notifications_register_settings');
// 2. Kommentar-Benachrichtigung bei neuem Kommentar /** -------------------------
* Kommentar-Benachrichtigung
* ------------------------- */
function wp_multi_comment_notifications_on_comment($comment_ID) { function wp_multi_comment_notifications_on_comment($comment_ID) {
$comment = get_comment($comment_ID); $comment = get_comment($comment_ID);
$comment_author = $comment->comment_author; $author = $comment->comment_author;
$comment_text = $comment->comment_content; $text = $comment->comment_content;
$site_name = get_bloginfo('name'); $site = get_bloginfo('name');
$backend_url = get_option('wp_backend_button_url'); $url = get_option('wp_backend_button_url');
$logo_url = esc_url(get_option('wp_email_logo_url')); $logo = esc_url(get_option('wp_email_logo_url'));
$discord_webhook = get_option('wp_discord_webhook_url');
$discord_template = get_option('wp_discord_message');
// Nachricht für Discord nur einmal senden
if ($discord_webhook && $discord_template) {
$discord_msg = strtr($discord_template, [
'{{COMMENT_AUTHOR}}' => $author,
'{{COMMENT_TEXT}}' => $text,
]);
wp_multi_comment_notifications_send_discord($discord_webhook, $discord_msg);
}
// E-Mail & Telegram Benachrichtigungen pro Benutzer
for ($i = 1; $i <= 6; $i++) { for ($i = 1; $i <= 6; $i++) {
$user_name = get_option("wp_user_{$i}_name"); $name = get_option("wp_user_{$i}_name");
$user_email = get_option("wp_user_{$i}_email"); $email = get_option("wp_user_{$i}_email");
$telegram_chat_id = get_option("wp_user_{$i}_telegram_chat_id"); $chat_id = get_option("wp_user_{$i}_telegram_chat_id");
$email_message = str_replace( // Nachricht für E-Mail & Telegram
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'], $replacements = [
[$comment_author, $comment_text, $user_name], '{{COMMENT_AUTHOR}}' => $author,
get_option('wp_email_message') '{{COMMENT_TEXT}}' => $text,
); '{{USER_NAME}}' => $name,
if (!empty($backend_url)) {
$email_message .= "\n\nWeitere Details hier: " . $backend_url;
}
if ($user_email) {
$html_message = '';
if (!empty($logo_url)) {
$html_message .= '<img src="' . $logo_url . '" alt="Logo" style="max-width: 100%; height: auto; margin-bottom: 20px;"><br>';
}
$html_message .= nl2br(esc_html($email_message));
$headers = [
'Content-Type: text/html; charset=UTF-8',
'From: ' . $site_name . ' <no-reply@' . $_SERVER['SERVER_NAME'] . '>'
]; ];
wp_mail(trim($user_email), 'Neuer Kommentar', $html_message, $headers); $email_msg = strtr(get_option('wp_email_message'), $replacements);
if ($url) $email_msg .= "\n\nWeitere Details hier: $url";
if ($email) {
$html = $logo ? "<img src='$logo' style='max-width:100%;margin-bottom:20px;'><br>" : '';
$html .= nl2br(esc_html($email_msg));
$headers = [
'Content-Type: text/html; charset=UTF-8',
"From: $site <no-reply@" . $_SERVER['SERVER_NAME'] . '>',
];
wp_mail($email, 'Neuer Kommentar', $html, $headers);
} }
$telegram_message = str_replace( $tg_msg = strtr(get_option('wp_telegram_message'), $replacements);
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'],
[$comment_author, $comment_text, $user_name],
get_option('wp_telegram_message')
);
if ($telegram_chat_id) { if ($chat_id) {
wp_multi_comment_notifications_send_telegram(get_option('wp_telegram_token'), $telegram_chat_id, $comment_ID, $telegram_message); wp_multi_comment_notifications_send_telegram(
get_option('wp_telegram_token'),
$chat_id,
$comment_ID,
$tg_msg
);
} }
} }
} }
add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1); add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1);
// 3. Telegram Nachricht senden (mit Inline-Button)
/** -------------------------
* Testnachrichten senden
* ------------------------- */
add_action('admin_init', function () {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
for ($i = 1; $i <= 6; $i++) {
if (isset($_POST["send_test_message_user_$i"])) {
wp_multi_comment_notifications_send_test_message($i);
}
}
}
});
function wp_multi_comment_notifications_send_test_message($user_number) {
$name = get_option("wp_user_{$user_number}_name");
$email = get_option("wp_user_{$user_number}_email");
$chat_id = get_option("wp_user_{$user_number}_telegram_chat_id");
$discord_webhook = get_option('wp_discord_webhook_url');
$token = get_option('wp_telegram_token');
$button_url = get_option('wp_backend_button_url');
$message = "Dies ist eine Testnachricht für $name.";
if ($email) {
wp_mail($email, 'Testnachricht', $message);
}
if ($chat_id) {
wp_multi_comment_notifications_send_telegram($token, $chat_id, 0, $message);
}
if ($discord_webhook) {
wp_multi_comment_notifications_send_discord($discord_webhook, $message);
}
}
/** -------------------------
* Telegram-Nachricht mit Button
* ------------------------- */
function wp_multi_comment_notifications_send_telegram($token, $chat_id, $comment_ID, $message) { function wp_multi_comment_notifications_send_telegram($token, $chat_id, $comment_ID, $message) {
$url = "https://api.telegram.org/bot$token/sendMessage"; $url = "https://api.telegram.org/bot$token/sendMessage";
$button_url = get_option('wp_backend_button_url'); $button_url = get_option('wp_backend_button_url');
$inline_button = [];
if (!empty($button_url)) {
$inline_button = [
'inline_keyboard' => [
[
['text' => 'Zum Backend', 'url' => $button_url]
]
]
];
}
$data = [ $data = [
'chat_id' => $chat_id, 'chat_id' => $chat_id,
'text' => $message, 'text' => $message,
'parse_mode' => 'HTML'
]; ];
if (!empty($inline_button)) { if ($button_url) {
$data['reply_markup'] = json_encode($inline_button); $data['reply_markup'] = json_encode([
'inline_keyboard' => [[['text' => 'Zum Backend', 'url' => $button_url]]]
]);
} }
$options = [ wp_remote_post($url, [
'http' => [ 'body' => $data,
'method' => 'POST', 'timeout' => 10,
'content' => http_build_query($data), 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" ]);
] }
];
$context = stream_context_create($options); /** -------------------------
$response = file_get_contents($url, false, $context); * Discord-Nachricht senden
* ------------------------- */
if ($response === FALSE) { function wp_multi_comment_notifications_send_discord($webhook_url, $message) {
error_log("Fehler bei Telegram-Nachricht: $url"); $payload = json_encode(['content' => $message]);
}
wp_remote_post($webhook_url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => $payload,
'timeout' => 10,
]);
} }