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

@ -1,359 +1,420 @@
<?php <?php
/* /*
* 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
* Tested up to: 6.7.2 * Tested up to: 6.7.2
* License: GPL2 * License: GPL2
* 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
* - Telegram: https://t.me/M_Viper04 * - Telegram: https://t.me/M_Viper04
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
/** /**
* Dashboard Widget Update Info für dieses Plugin * Dashboard Widget Update Info für dieses Plugin
*/ */
function wp_multi_cmn_dashboard_widget() { function wp_multi_cmn_dashboard_widget() {
wp_add_dashboard_widget( wp_add_dashboard_widget(
'wp_multi_cmn_update_widget', 'wp_multi_cmn_update_widget',
__('WP Multi Comment Notifications Update Info', 'wp-multi-comment-notifications'), __('WP Multi Comment Notifications Update Info', 'wp-multi-comment-notifications'),
'wp_multi_cmn_update_dashboard_widget_content' 'wp_multi_cmn_update_dashboard_widget_content'
); );
} }
add_action('wp_dashboard_setup', 'wp_multi_cmn_dashboard_widget'); add_action('wp_dashboard_setup', 'wp_multi_cmn_dashboard_widget');
// Cron-Intervall „stündlich“ definieren // Cron-Intervall „stündlich“ definieren
add_filter('cron_schedules', function ($schedules) { add_filter('cron_schedules', function ($schedules) {
$schedules['hourly'] = array( $schedules['hourly'] = array(
'interval' => 3600, 'interval' => 3600,
'display' => __('Stündlich', 'wp-multi-comment-notifications'), 'display' => __('Stündlich', 'wp-multi-comment-notifications'),
); );
return $schedules; return $schedules;
}); });
// Cron-Event planen // Cron-Event planen
function wp_multi_cmn_schedule_update_check() { function wp_multi_cmn_schedule_update_check() {
if (!wp_next_scheduled('wp_multi_cmn_update_check_event')) { if (!wp_next_scheduled('wp_multi_cmn_update_check_event')) {
wp_schedule_event(time(), 'hourly', 'wp_multi_cmn_update_check_event'); wp_schedule_event(time(), 'hourly', 'wp_multi_cmn_update_check_event');
} }
} }
add_action('wp', 'wp_multi_cmn_schedule_update_check'); add_action('wp', 'wp_multi_cmn_schedule_update_check');
// Gitea API aufrufen // Gitea API aufrufen
function wp_multi_cmn_fetch_latest_release($show_prereleases = false) { function wp_multi_cmn_fetch_latest_release($show_prereleases = false) {
$api_url = 'https://git.viper.ipv64.net/api/v1/repos/M_Viper/wp-multi-comment-notifications/releases'; $api_url = 'https://git.viper.ipv64.net/api/v1/repos/M_Viper/wp-multi-comment-notifications/releases';
$response = wp_remote_get($api_url, array('timeout' => 10)); $response = wp_remote_get($api_url, array('timeout' => 10));
if (is_wp_error($response)) { if (is_wp_error($response)) {
error_log('WP Multi Comment Notifications Update Fehler: ' . $response->get_error_message()); error_log('WP Multi Comment Notifications Update Fehler: ' . $response->get_error_message());
return false; return false;
} }
$body = wp_remote_retrieve_body($response); $body = wp_remote_retrieve_body($response);
$data = json_decode($body, true); $data = json_decode($body, true);
if (!is_array($data)) { if (!is_array($data)) {
error_log('WP Multi Comment Notifications Ungültige API-Antwort'); error_log('WP Multi Comment Notifications Ungültige API-Antwort');
return false; return false;
} }
foreach ($data as $release) { foreach ($data as $release) {
if (!$show_prereleases && isset($release['prerelease']) && $release['prerelease']) { if (!$show_prereleases && isset($release['prerelease']) && $release['prerelease']) {
continue; continue;
} }
if (!empty($release['tag_name'])) { if (!empty($release['tag_name'])) {
return $release; return $release;
} }
} }
return null; return null;
} }
// Cron Callback // Cron Callback
function wp_multi_cmn_update_check() { function wp_multi_cmn_update_check() {
if (!function_exists('get_plugin_data')) { if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
} }
$plugin_data = get_plugin_data(__FILE__); $plugin_data = get_plugin_data(__FILE__);
$installed_version = $plugin_data['Version']; $installed_version = $plugin_data['Version'];
$show_prereleases = get_option('wp_multi_cmn_show_prereleases', false); $show_prereleases = get_option('wp_multi_cmn_show_prereleases', false);
$release = wp_multi_cmn_fetch_latest_release($show_prereleases); $release = wp_multi_cmn_fetch_latest_release($show_prereleases);
if ($release) { if ($release) {
update_option('wp_multi_cmn_latest_version', $release['tag_name']); update_option('wp_multi_cmn_latest_version', $release['tag_name']);
update_option('wp_multi_cmn_release_notes', $release['body']); update_option('wp_multi_cmn_release_notes', $release['body']);
update_option('wp_multi_cmn_is_prerelease', $release['prerelease']); update_option('wp_multi_cmn_is_prerelease', $release['prerelease']);
} }
} }
add_action('wp_multi_cmn_update_check_event', 'wp_multi_cmn_update_check'); add_action('wp_multi_cmn_update_check_event', 'wp_multi_cmn_update_check');
// Widget Callback // Widget Callback
function wp_multi_cmn_update_dashboard_widget_content() { function wp_multi_cmn_update_dashboard_widget_content() {
if (!function_exists('get_plugin_data')) { if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
} }
$plugin_data = get_plugin_data(__FILE__); $plugin_data = get_plugin_data(__FILE__);
$installed_version = $plugin_data['Version']; $installed_version = $plugin_data['Version'];
$show_prereleases = get_option('wp_multi_cmn_show_prereleases', false); $show_prereleases = get_option('wp_multi_cmn_show_prereleases', false);
$release = wp_multi_cmn_fetch_latest_release($show_prereleases); $release = wp_multi_cmn_fetch_latest_release($show_prereleases);
if ($release === false) { if ($release === false) {
echo '<p style="color:red;">Fehler beim Abrufen der Update-Informationen.</p>'; echo '<p style="color:red;">Fehler beim Abrufen der Update-Informationen.</p>';
return; return;
} }
$latest_version = $release['tag_name']; $latest_version = $release['tag_name'];
$release_notes = isset($release['body']) ? $release['body'] : ''; $release_notes = isset($release['body']) ? $release['body'] : '';
$is_prerelease = isset($release['prerelease']) && $release['prerelease']; $is_prerelease = isset($release['prerelease']) && $release['prerelease'];
if (version_compare($installed_version, $latest_version, '>=')) { if (version_compare($installed_version, $latest_version, '>=')) {
echo '<p style="color:green;">Du verwendest die aktuelle Version: <strong>' . esc_html($installed_version) . '</strong></p>'; echo '<p style="color:green;">Du verwendest die aktuelle Version: <strong>' . esc_html($installed_version) . '</strong></p>';
} else { } else {
echo '<p style="color:red;"><strong>Neue Version verfügbar: ' . esc_html($latest_version) . '</strong></p>'; echo '<p style="color:red;"><strong>Neue Version verfügbar: ' . esc_html($latest_version) . '</strong></p>';
echo '<p>Installiert: <strong>' . esc_html($installed_version) . '</strong></p>'; echo '<p>Installiert: <strong>' . esc_html($installed_version) . '</strong></p>';
if ($is_prerelease && $show_prereleases) { if ($is_prerelease && $show_prereleases) {
echo '<p style="color:blue;">Hinweis: Dies ist ein PreRelease.</p>'; echo '<p style="color:blue;">Hinweis: Dies ist ein PreRelease.</p>';
} }
if (!empty($release_notes)) { if (!empty($release_notes)) {
echo '<p><strong>Release Notes:</strong><br>' . nl2br(esc_html($release_notes)) . '</p>'; echo '<p><strong>Release Notes:</strong><br>' . nl2br(esc_html($release_notes)) . '</p>';
} }
$download_url = isset($release['assets'][0]['browser_download_url']) ? $release['assets'][0]['browser_download_url'] : '#'; $download_url = isset($release['assets'][0]['browser_download_url']) ? $release['assets'][0]['browser_download_url'] : '#';
echo '<p><a href="' . esc_url($download_url) . '" class="button button-primary">Update herunterladen</a></p>'; echo '<p><a href="' . esc_url($download_url) . '" class="button button-primary">Update herunterladen</a></p>';
} }
} }
// 1. Plugin-Optionen im Admin-Bereich unter dem Menü "Kommentare" hinzufügen // 1. Plugin-Optionen im Admin-Bereich unter dem Menü "Kommentare" hinzufügen
function wp_multi_comment_notifications_menu() { function wp_multi_comment_notifications_menu() {
add_submenu_page( add_submenu_page(
'edit-comments.php', 'edit-comments.php',
'WP Multi Comment Notifications', 'WP Multi Comment Notifications',
'Kommentar Benachrichtigungen', 'Kommentar Benachrichtigungen',
'manage_options', 'manage_options',
'wp-multi-comment-notifications', 'wp-multi-comment-notifications',
'wp_multi_comment_notifications_settings_page' 'wp_multi_comment_notifications_settings_page'
); );
} }
add_action('admin_menu', 'wp_multi_comment_notifications_menu'); add_action('admin_menu', 'wp_multi_comment_notifications_menu');
function wp_multi_comment_notifications_settings_page() { function wp_multi_comment_notifications_settings_page() {
?> ?>
<div class="wrap"> <div class="wrap">
<h1>WP Multi Comment Notifications Einstellungen</h1> <h1>WP Multi Comment Notifications Einstellungen</h1>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php <?php
settings_fields( 'wp_multi_comment_notifications_settings_group' ); settings_fields( 'wp_multi_comment_notifications_settings_group' );
do_settings_sections( 'wp-multi-comment-notifications' ); do_settings_sections( 'wp-multi-comment-notifications' );
?> ?>
<h3>Telegram Bot Token</h3> <h3>Telegram Bot Token</h3>
<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>Nachricht für E-Mail</h3> <h3>Discord Webhook URL</h3>
<textarea name="wp_email_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_email_message')); ?></textarea> <label for="wp_discord_webhook_url">Webhook URL</label>
<p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p> <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 Telegram</h3> <h3>Nachricht für Discord</h3>
<textarea name="wp_telegram_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_telegram_message')); ?></textarea> <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> <p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p>
<h3>Backend-Button URL</h3>
<label for="wp_backend_button_url">URL für Backend Button</label> <h3>Nachricht für E-Mail</h3>
<input type="text" name="wp_backend_button_url" value="<?php echo esc_attr(get_option('wp_backend_button_url')); ?>" class="regular-text" /> <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>
<h3>Logo/Banner für E-Mail</h3>
<label for="wp_email_logo_url">Logo/Banner URL</label> <h3>Nachricht für Telegram</h3>
<input type="text" name="wp_email_logo_url" value="<?php echo esc_attr(get_option('wp_email_logo_url')); ?>" class="regular-text" /> <textarea name="wp_telegram_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_telegram_message')); ?></textarea>
<p>Füge hier die vollständige URL zum Logo/Banner ein, das in der E-Mail angezeigt werden soll.</p> <p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code> für dynamische Daten.</p>
<div class="user-boxes"> <h3>Backend-Button URL</h3>
<?php for ($i = 1; $i <= 6; $i++) : ?> <label for="wp_backend_button_url">URL für Backend Button</label>
<div class="user-box"> <input type="text" name="wp_backend_button_url" value="<?php echo esc_attr(get_option('wp_backend_button_url')); ?>" class="regular-text" />
<h3>User <?php echo $i; ?></h3>
<label for="wp_user_<?php echo $i; ?>_name">Name</label> <h3>Logo/Banner für E-Mail</h3>
<input type="text" name="wp_user_<?php echo $i; ?>_name" value="<?php echo esc_attr(get_option("wp_user_{$i}_name")); ?>" /> <label for="wp_email_logo_url">Logo/Banner URL</label>
<input type="text" name="wp_email_logo_url" value="<?php echo esc_attr(get_option('wp_email_logo_url')); ?>" class="regular-text" />
<label for="wp_user_<?php echo $i; ?>_email">E-Mail Adresse</label> <p>Füge hier die vollständige URL zum Logo/Banner ein, das in der E-Mail angezeigt werden soll.</p>
<input type="email" name="wp_user_<?php echo $i; ?>_email" value="<?php echo esc_attr(get_option("wp_user_{$i}_email")); ?>" />
<div class="user-boxes">
<label for="wp_user_<?php echo $i; ?>_telegram_chat_id">Telegram Chat ID</label> <?php for ($i = 1; $i <= 6; $i++) : ?>
<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")); ?>" /> <div class="user-box">
</div> <h3>User <?php echo $i; ?></h3>
<?php endfor; ?> <label for="wp_user_<?php echo $i; ?>_name">Name</label>
</div> <input type="text" name="wp_user_<?php echo $i; ?>_name" value="<?php echo esc_attr(get_option("wp_user_{$i}_name")); ?>" />
<?php submit_button(); ?>
</form> <label for="wp_user_<?php echo $i; ?>_email">E-Mail Adresse</label>
</div> <input type="email" name="wp_user_<?php echo $i; ?>_email" value="<?php echo esc_attr(get_option("wp_user_{$i}_email")); ?>" />
<style> <label for="wp_user_<?php echo $i; ?>_telegram_chat_id">Telegram Chat ID</label>
.user-boxes { <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")); ?>" />
display: flex;
flex-wrap: wrap; <!-- Button für individuelle Testnachricht -->
gap: 20px; <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>
<?php endfor; ?>
.user-box { </div>
width: 30%; <?php submit_button(); ?>
padding: 15px; </form>
border: 1px solid #ddd; </div>
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); <style>
} .user-boxes {
display: flex;
.user-box h3 { flex-wrap: wrap;
margin-top: 0; gap: 20px;
} }
.user-box label { .user-box {
display: block; width: 30%;
margin-bottom: 5px; padding: 15px;
} border: 1px solid #ddd;
border-radius: 5px;
.user-box input, box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
.user-box textarea { }
width: 100%;
padding: 8px; .user-box h3 {
margin-bottom: 10px; margin-top: 0;
border: 1px solid #ddd; }
border-radius: 5px;
} .user-box label {
display: block;
.regular-text { margin-bottom: 5px;
width: 100%; }
}
.user-box input,
.large-text { .user-box textarea {
width: 100%; width: 100%;
height: 150px; padding: 8px;
} margin-bottom: 10px;
</style> border: 1px solid #ddd;
<?php border-radius: 5px;
} }
function wp_multi_comment_notifications_register_settings() { .regular-text {
for ($i = 1; $i <= 6; $i++) { width: 100%;
register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_name"); }
register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_email");
register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_telegram_chat_id"); .large-text {
} width: 100%;
register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_token'); height: 150px;
register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_message'); }
register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_message'); </style>
register_setting('wp_multi_comment_notifications_settings_group', 'wp_backend_button_url'); <?php
register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_logo_url'); }
}
add_action('admin_init', 'wp_multi_comment_notifications_register_settings'); function wp_multi_comment_notifications_register_settings() {
for ($i = 1; $i <= 6; $i++) {
// 2. Kommentar-Benachrichtigung bei neuem Kommentar register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_name");
function wp_multi_comment_notifications_on_comment($comment_ID) { register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_email");
$comment = get_comment($comment_ID); register_setting('wp_multi_comment_notifications_settings_group', "wp_user_{$i}_telegram_chat_id");
$comment_author = $comment->comment_author; }
$comment_text = $comment->comment_content; register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_token');
$site_name = get_bloginfo('name'); register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_message');
$backend_url = get_option('wp_backend_button_url'); register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_message');
$logo_url = esc_url(get_option('wp_email_logo_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');
for ($i = 1; $i <= 6; $i++) { register_setting('wp_multi_comment_notifications_settings_group', 'wp_discord_webhook_url');
$user_name = get_option("wp_user_{$i}_name"); register_setting('wp_multi_comment_notifications_settings_group', 'wp_discord_message');
$user_email = get_option("wp_user_{$i}_email");
$telegram_chat_id = get_option("wp_user_{$i}_telegram_chat_id"); }
add_action('admin_init', 'wp_multi_comment_notifications_register_settings');
$email_message = str_replace(
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'], /** -------------------------
[$comment_author, $comment_text, $user_name], * Kommentar-Benachrichtigung
get_option('wp_email_message') * ------------------------- */
); function wp_multi_comment_notifications_on_comment($comment_ID) {
$comment = get_comment($comment_ID);
if (!empty($backend_url)) { $author = $comment->comment_author;
$email_message .= "\n\nWeitere Details hier: " . $backend_url; $text = $comment->comment_content;
} $site = get_bloginfo('name');
$url = get_option('wp_backend_button_url');
if ($user_email) { $logo = esc_url(get_option('wp_email_logo_url'));
$html_message = ''; $discord_webhook = get_option('wp_discord_webhook_url');
if (!empty($logo_url)) { $discord_template = get_option('wp_discord_message');
$html_message .= '<img src="' . $logo_url . '" alt="Logo" style="max-width: 100%; height: auto; margin-bottom: 20px;"><br>';
} // Nachricht für Discord nur einmal senden
if ($discord_webhook && $discord_template) {
$html_message .= nl2br(esc_html($email_message)); $discord_msg = strtr($discord_template, [
'{{COMMENT_AUTHOR}}' => $author,
$headers = [ '{{COMMENT_TEXT}}' => $text,
'Content-Type: text/html; charset=UTF-8', ]);
'From: ' . $site_name . ' <no-reply@' . $_SERVER['SERVER_NAME'] . '>' wp_multi_comment_notifications_send_discord($discord_webhook, $discord_msg);
]; }
wp_mail(trim($user_email), 'Neuer Kommentar', $html_message, $headers); // E-Mail & Telegram Benachrichtigungen pro Benutzer
} for ($i = 1; $i <= 6; $i++) {
$name = get_option("wp_user_{$i}_name");
$telegram_message = str_replace( $email = get_option("wp_user_{$i}_email");
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'], $chat_id = get_option("wp_user_{$i}_telegram_chat_id");
[$comment_author, $comment_text, $user_name],
get_option('wp_telegram_message') // Nachricht für E-Mail & Telegram
); $replacements = [
'{{COMMENT_AUTHOR}}' => $author,
if ($telegram_chat_id) { '{{COMMENT_TEXT}}' => $text,
wp_multi_comment_notifications_send_telegram(get_option('wp_telegram_token'), $telegram_chat_id, $comment_ID, $telegram_message); '{{USER_NAME}}' => $name,
} ];
}
} $email_msg = strtr(get_option('wp_email_message'), $replacements);
add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1); if ($url) $email_msg .= "\n\nWeitere Details hier: $url";
// 3. Telegram Nachricht senden (mit Inline-Button) if ($email) {
function wp_multi_comment_notifications_send_telegram($token, $chat_id, $comment_ID, $message) { $html = $logo ? "<img src='$logo' style='max-width:100%;margin-bottom:20px;'><br>" : '';
$url = "https://api.telegram.org/bot$token/sendMessage"; $html .= nl2br(esc_html($email_msg));
$button_url = get_option('wp_backend_button_url'); $headers = [
$inline_button = []; 'Content-Type: text/html; charset=UTF-8',
"From: $site <no-reply@" . $_SERVER['SERVER_NAME'] . '>',
if (!empty($button_url)) { ];
$inline_button = [ wp_mail($email, 'Neuer Kommentar', $html, $headers);
'inline_keyboard' => [ }
[
['text' => 'Zum Backend', 'url' => $button_url] $tg_msg = strtr(get_option('wp_telegram_message'), $replacements);
]
] if ($chat_id) {
]; wp_multi_comment_notifications_send_telegram(
} get_option('wp_telegram_token'),
$chat_id,
$data = [ $comment_ID,
'chat_id' => $chat_id, $tg_msg
'text' => $message, );
]; }
}
if (!empty($inline_button)) { }
$data['reply_markup'] = json_encode($inline_button); add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1);
}
$options = [ /** -------------------------
'http' => [ * Testnachrichten senden
'method' => 'POST', * ------------------------- */
'content' => http_build_query($data), add_action('admin_init', function () {
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" 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);
$context = stream_context_create($options); }
$response = file_get_contents($url, false, $context); }
}
if ($response === FALSE) { });
error_log("Fehler bei Telegram-Nachricht: $url");
} 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) {
$url = "https://api.telegram.org/bot$token/sendMessage";
$button_url = get_option('wp_backend_button_url');
$data = [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'HTML'
];
if ($button_url) {
$data['reply_markup'] = json_encode([
'inline_keyboard' => [[['text' => 'Zum Backend', 'url' => $button_url]]]
]);
}
wp_remote_post($url, [
'body' => $data,
'timeout' => 10,
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
]);
}
/** -------------------------
* Discord-Nachricht senden
* ------------------------- */
function wp_multi_comment_notifications_send_discord($webhook_url, $message) {
$payload = json_encode(['content' => $message]);
wp_remote_post($webhook_url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => $payload,
'timeout' => 10,
]);
}