wp-multi-comment-notifications.php aktualisiert
This commit is contained in:
parent
baa8f3be1e
commit
7071d3a062
@ -2,8 +2,8 @@
|
||||
/*
|
||||
* Plugin Name: 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.
|
||||
* Version: 1.0
|
||||
* Description: Benachrichtigt bei neuen Kommentaren per E-Mail und optional über Telegram & Discord. Ideal für Teams, die schnell informiert werden wollen.
|
||||
* Version: 1.1
|
||||
* Author: M_Viper
|
||||
* Author URI: https://m-viper.de
|
||||
* Requires at least: 6.7.2
|
||||
@ -12,7 +12,7 @@
|
||||
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
||||
* Text Domain: wp-multi-comment-notifications
|
||||
* Domain Path: /languages
|
||||
* Tags: kommentare, benachrichtigung, e-mail, telegram, kommentarüberwachung, teamkommunikation
|
||||
* Tags: kommentare, benachrichtigung, e-mail, telegram, discord, kommentarüberwachung, teamkommunikation
|
||||
*
|
||||
* Support:
|
||||
* - 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>
|
||||
<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>
|
||||
<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>
|
||||
@ -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>
|
||||
<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>
|
||||
<?php endfor; ?>
|
||||
</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_backend_button_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');
|
||||
|
||||
// 2. Kommentar-Benachrichtigung bei neuem Kommentar
|
||||
/** -------------------------
|
||||
* Kommentar-Benachrichtigung
|
||||
* ------------------------- */
|
||||
function wp_multi_comment_notifications_on_comment($comment_ID) {
|
||||
$comment = get_comment($comment_ID);
|
||||
$comment_author = $comment->comment_author;
|
||||
$comment_text = $comment->comment_content;
|
||||
$site_name = get_bloginfo('name');
|
||||
$backend_url = get_option('wp_backend_button_url');
|
||||
$logo_url = esc_url(get_option('wp_email_logo_url'));
|
||||
$author = $comment->comment_author;
|
||||
$text = $comment->comment_content;
|
||||
$site = get_bloginfo('name');
|
||||
$url = get_option('wp_backend_button_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++) {
|
||||
$user_name = get_option("wp_user_{$i}_name");
|
||||
$user_email = get_option("wp_user_{$i}_email");
|
||||
$telegram_chat_id = get_option("wp_user_{$i}_telegram_chat_id");
|
||||
$name = get_option("wp_user_{$i}_name");
|
||||
$email = get_option("wp_user_{$i}_email");
|
||||
$chat_id = get_option("wp_user_{$i}_telegram_chat_id");
|
||||
|
||||
$email_message = str_replace(
|
||||
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'],
|
||||
[$comment_author, $comment_text, $user_name],
|
||||
get_option('wp_email_message')
|
||||
);
|
||||
|
||||
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'] . '>'
|
||||
// Nachricht für E-Mail & Telegram
|
||||
$replacements = [
|
||||
'{{COMMENT_AUTHOR}}' => $author,
|
||||
'{{COMMENT_TEXT}}' => $text,
|
||||
'{{USER_NAME}}' => $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(
|
||||
['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'],
|
||||
[$comment_author, $comment_text, $user_name],
|
||||
get_option('wp_telegram_message')
|
||||
);
|
||||
$tg_msg = strtr(get_option('wp_telegram_message'), $replacements);
|
||||
|
||||
if ($telegram_chat_id) {
|
||||
wp_multi_comment_notifications_send_telegram(get_option('wp_telegram_token'), $telegram_chat_id, $comment_ID, $telegram_message);
|
||||
if ($chat_id) {
|
||||
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);
|
||||
|
||||
// 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) {
|
||||
$url = "https://api.telegram.org/bot$token/sendMessage";
|
||||
$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 = [
|
||||
'chat_id' => $chat_id,
|
||||
'text' => $message,
|
||||
'parse_mode' => 'HTML'
|
||||
];
|
||||
|
||||
if (!empty($inline_button)) {
|
||||
$data['reply_markup'] = json_encode($inline_button);
|
||||
if ($button_url) {
|
||||
$data['reply_markup'] = json_encode([
|
||||
'inline_keyboard' => [[['text' => 'Zum Backend', 'url' => $button_url]]]
|
||||
]);
|
||||
}
|
||||
|
||||
$options = [
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'content' => http_build_query($data),
|
||||
'header' => "Content-Type: application/x-www-form-urlencoded\r\n"
|
||||
]
|
||||
];
|
||||
|
||||
$context = stream_context_create($options);
|
||||
$response = file_get_contents($url, false, $context);
|
||||
|
||||
if ($response === FALSE) {
|
||||
error_log("Fehler bei Telegram-Nachricht: $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,
|
||||
]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user