wp-multi-comment-notifications/wp-multi-comment-notifications.php

344 lines
13 KiB
PHP

<?php
/*
* 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 & Discord. Ideal für Teams, die schnell informiert werden wollen.
* Version: 1.3
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* License: GPL2
* 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, discord, kommentarüberwachung, teamkommunikation
*
* Support:
* - Microsoft Teams: https://teams.live.com/l/community/FEAzokphpZTJ2u6OgI
* - Telegram: https://t.me/M_Viper04
*/
if (!defined('ABSPATH')) {
exit;
}
// Funktion zur Überprüfung des WP Multi Toolkit Plugins
function wp_multi_comment_notifications_check_dependency() {
if (!function_exists('is_plugin_active')) {
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
// Prüft, ob WP Multi Toolkit installiert und aktiv ist
if (!is_plugin_active('wp-multi-toolkit/wp-multi-toolkit.php')) {
add_action('admin_notices', 'wp_multi_comment_notifications_dependency_notice');
return false;
}
return true;
}
// Fehlermeldung für Admin-Bereich mit Download-Button
function wp_multi_comment_notifications_dependency_notice() {
?>
<div class="notice notice-error">
<p>
<?php _e('Das Plugin "WP Multi Comment Notifications" benötigt "WP Multi Toolkit", um zu funktionieren. Bitte installieren und aktivieren Sie "WP Multi Toolkit".', 'wp-multi-comment-notifications'); ?>
<a href="https://git.viper.ipv64.net/M_Viper/wp-multi-toolkit/releases" target="_blank" class="button button-primary" style="margin-left: 10px;">
<?php _e('WP Multi Toolkit herunterladen', 'wp-multi-comment-notifications'); ?>
</a>
</p>
</div>
<?php
}
// Plugin nur initialisieren, wenn Abhängigkeit erfüllt ist
if (wp_multi_comment_notifications_check_dependency()) {
// Hier würde der restliche Plugin-Code folgen
} else {
// Optional: Plugin komplett deaktivieren, wenn Abhängigkeit fehlt
add_action('admin_init', function() {
deactivate_plugins(plugin_basename(__FILE__));
});
}
define('WPMCN_PLUGIN_VERSION', '1.2');
define('WPMCN_PLUGIN_DIR', plugin_dir_path(__FILE__));
// 1. Plugin-Optionen im Admin-Bereich unter dem Menü "Kommentare" hinzufügen
function wp_multi_comment_notifications_menu() {
add_submenu_page(
'edit-comments.php',
'WP Multi Comment Notifications',
'Kommentar Benachrichtigungen',
'manage_options',
'wp-multi-comment-notifications',
'wp_multi_comment_notifications_settings_page'
);
}
add_action('admin_menu', 'wp_multi_comment_notifications_menu');
function wp_multi_comment_notifications_settings_page() {
?>
<div class="wrap">
<h1>WP Multi Comment Notifications Einstellungen</h1>
<form method="post" action="options.php">
<?php
settings_fields( 'wp_multi_comment_notifications_settings_group' );
do_settings_sections( 'wp-multi-comment-notifications' );
?>
<h3>Telegram Bot Token</h3>
<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>
<h3>Nachricht für Telegram</h3>
<textarea name="wp_telegram_message" rows="5" class="large-text"><?php echo esc_textarea(get_option('wp_telegram_message')); ?></textarea>
<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>
<input type="text" name="wp_backend_button_url" value="<?php echo esc_attr(get_option('wp_backend_button_url')); ?>" class="regular-text" />
<h3>Logo/Banner für E-Mail</h3>
<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" />
<p>Füge hier die vollständige URL zum Logo/Banner ein, das in der E-Mail angezeigt werden soll.</p>
<div class="user-boxes">
<?php for ($i = 1; $i <= 6; $i++) : ?>
<div class="user-box">
<h3>User <?php echo $i; ?></h3>
<label for="wp_user_<?php echo $i; ?>_name">Name</label>
<input type="text" name="wp_user_<?php echo $i; ?>_name" value="<?php echo esc_attr(get_option("wp_user_{$i}_name")); ?>" />
<label for="wp_user_<?php echo $i; ?>_email">E-Mail Adresse</label>
<input type="email" name="wp_user_<?php echo $i; ?>_email" value="<?php echo esc_attr(get_option("wp_user_{$i}_email")); ?>" />
<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>
<?php submit_button(); ?>
</form>
</div>
<style>
.user-boxes {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.user-box {
width: 30%;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.user-box h3 {
margin-top: 0;
}
.user-box label {
display: block;
margin-bottom: 5px;
}
.user-box input,
.user-box textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
.regular-text {
width: 100%;
}
.large-text {
width: 100%;
height: 150px;
}
</style>
<?php
}
function wp_multi_comment_notifications_register_settings() {
for ($i = 1; $i <= 6; $i++) {
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");
}
register_setting('wp_multi_comment_notifications_settings_group', 'wp_telegram_token');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_email_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_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');
/** -------------------------
* Kommentar-Benachrichtigung
* ------------------------- */
function wp_multi_comment_notifications_on_comment($comment_ID) {
$comment = get_comment($comment_ID);
$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++) {
$name = get_option("wp_user_{$i}_name");
$email = get_option("wp_user_{$i}_email");
$chat_id = get_option("wp_user_{$i}_telegram_chat_id");
// Nachricht für E-Mail & Telegram
$replacements = [
'{{COMMENT_AUTHOR}}' => $author,
'{{COMMENT_TEXT}}' => $text,
'{{USER_NAME}}' => $name,
];
$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' => get_option( 'admin_email' ),
];
wp_mail($email, 'Neuer Kommentar', $html, $headers);
}
$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,
$comment_ID,
$tg_msg
);
}
}
}
add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1);
/** -------------------------
* 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');
$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,
]);
}