Compare commits

..

6 Commits
1.1 ... main

View File

@ -3,7 +3,7 @@
* 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 & Discord. 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.1 * Version: 1.4
* 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
@ -19,129 +19,51 @@
* - Telegram: https://t.me/M_Viper04 * - Telegram: https://t.me/M_Viper04
*/ */
if ( ! defined( 'ABSPATH' ) ) { if (!defined('ABSPATH')) {
exit; exit;
} }
/** // Funktion zur Überprüfung des WP Multi Toolkit Plugins
* Dashboard Widget Update Info für dieses Plugin function wp_multi_comment_notifications_check_dependency() {
*/ if (!function_exists('is_plugin_active')) {
function wp_multi_cmn_dashboard_widget() { include_once(ABSPATH . 'wp-admin/includes/plugin.php');
wp_add_dashboard_widget(
'wp_multi_cmn_update_widget',
__('WP Multi Comment Notifications Update Info', 'wp-multi-comment-notifications'),
'wp_multi_cmn_update_dashboard_widget_content'
);
}
add_action('wp_dashboard_setup', 'wp_multi_cmn_dashboard_widget');
// Cron-Intervall „stündlich“ definieren
add_filter('cron_schedules', function ($schedules) {
$schedules['hourly'] = array(
'interval' => 3600,
'display' => __('Stündlich', 'wp-multi-comment-notifications'),
);
return $schedules;
});
// Cron-Event planen
function wp_multi_cmn_schedule_update_check() {
if (!wp_next_scheduled('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');
// Gitea API aufrufen // Prüft, ob WP Multi Toolkit installiert und aktiv ist
function wp_multi_cmn_fetch_latest_release($show_prereleases = false) { if (!is_plugin_active('wp-multi-toolkit/wp-multi-toolkit.php')) {
$api_url = 'https://git.viper.ipv64.net/api/v1/repos/M_Viper/wp-multi-comment-notifications/releases'; add_action('admin_notices', 'wp_multi_comment_notifications_dependency_notice');
$response = wp_remote_get($api_url, array('timeout' => 10));
if (is_wp_error($response)) {
error_log('WP Multi Comment Notifications Update Fehler: ' . $response->get_error_message());
return false; return false;
} }
return true;
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!is_array($data)) {
error_log('WP Multi Comment Notifications Ungültige API-Antwort');
return false;
}
foreach ($data as $release) {
if (!$show_prereleases && isset($release['prerelease']) && $release['prerelease']) {
continue;
}
if (!empty($release['tag_name'])) {
return $release;
}
}
return null;
} }
// Cron Callback // Fehlermeldung für Admin-Bereich mit Download-Button
function wp_multi_cmn_update_check() { function wp_multi_comment_notifications_dependency_notice() {
if (!function_exists('get_plugin_data')) { ?>
require_once ABSPATH . 'wp-admin/includes/plugin.php'; <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'); ?>
$plugin_data = get_plugin_data(__FILE__); <a href="https://git.viper.ipv64.net/M_Viper/wp-multi-toolkit/releases" target="_blank" class="button button-primary" style="margin-left: 10px;">
$installed_version = $plugin_data['Version']; <?php _e('WP Multi Toolkit herunterladen', 'wp-multi-comment-notifications'); ?>
$show_prereleases = get_option('wp_multi_cmn_show_prereleases', false); </a>
</p>
$release = wp_multi_cmn_fetch_latest_release($show_prereleases); </div>
<?php
if ($release) {
update_option('wp_multi_cmn_latest_version', $release['tag_name']);
update_option('wp_multi_cmn_release_notes', $release['body']);
update_option('wp_multi_cmn_is_prerelease', $release['prerelease']);
}
}
add_action('wp_multi_cmn_update_check_event', 'wp_multi_cmn_update_check');
// Widget Callback
function wp_multi_cmn_update_dashboard_widget_content() {
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data(__FILE__);
$installed_version = $plugin_data['Version'];
$show_prereleases = get_option('wp_multi_cmn_show_prereleases', false);
$release = wp_multi_cmn_fetch_latest_release($show_prereleases);
if ($release === false) {
echo '<p style="color:red;">Fehler beim Abrufen der Update-Informationen.</p>';
return;
}
$latest_version = $release['tag_name'];
$release_notes = isset($release['body']) ? $release['body'] : '';
$is_prerelease = isset($release['prerelease']) && $release['prerelease'];
if (version_compare($installed_version, $latest_version, '>=')) {
echo '<p style="color:green;">Du verwendest die aktuelle Version: <strong>' . esc_html($installed_version) . '</strong></p>';
} else {
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>';
if ($is_prerelease && $show_prereleases) {
echo '<p style="color:blue;">Hinweis: Dies ist ein PreRelease.</p>';
}
if (!empty($release_notes)) {
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'] : '#';
echo '<p><a href="' . esc_url($download_url) . '" class="button button-primary">Update herunterladen</a></p>';
}
} }
// 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 // 1. Plugin-Optionen im Admin-Bereich unter dem Menü "Kommentare" hinzufügen
function wp_multi_comment_notifications_menu() { function wp_multi_comment_notifications_menu() {
@ -175,16 +97,15 @@ function wp_multi_comment_notifications_settings_page() {
<h3>Nachricht für Discord</h3> <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> <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>, <code>{{POST_TITLE}}</code>, <code>{{SITE_NAME}}</code>, <code>{{POST_URL}}</code>, <code>{{COMMENT_DATE}}</code>, <code>{{COMMENT_URL}}</code> </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>, <code>{{POST_TITLE}}</code>, <code>{{SITE_NAME}}</code>, <code>{{POST_URL}}</code>, <code>{{COMMENT_DATE}}</code>, <code>{{COMMENT_URL}}</code> </p>
<h3>Nachricht für Telegram</h3> <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> <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> <p>Verwende Platzhalter wie <code>{{COMMENT_AUTHOR}}</code>, <code>{{COMMENT_TEXT}}</code>, <code>{{USER_NAME}}</code>, <code>{{POST_TITLE}}</code>, <code>{{SITE_NAME}}</code>, <code>{{POST_URL}}</code>, <code>{{COMMENT_DATE}}</code>, <code>{{COMMENT_URL}}</code> </p>
<h3>Backend-Button URL</h3> <h3>Backend-Button URL</h3>
<label for="wp_backend_button_url">URL für Backend Button</label> <label for="wp_backend_button_url">URL für Backend Button</label>
@ -275,7 +196,6 @@ function wp_multi_comment_notifications_register_settings() {
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_webhook_url');
register_setting('wp_multi_comment_notifications_settings_group', 'wp_discord_message'); 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');
@ -286,61 +206,68 @@ function wp_multi_comment_notifications_on_comment($comment_ID) {
$comment = get_comment($comment_ID); $comment = get_comment($comment_ID);
$author = $comment->comment_author; $author = $comment->comment_author;
$text = $comment->comment_content; $text = $comment->comment_content;
$post_id = $comment->comment_post_ID;
$post_title = get_the_title($post_id); // Titel des Beitrags
$site = get_bloginfo('name'); $site = get_bloginfo('name');
$url = get_option('wp_backend_button_url'); $url = get_option('wp_backend_button_url');
$logo = 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_webhook = get_option('wp_discord_webhook_url');
$discord_template = get_option('wp_discord_message'); $discord_template = get_option('wp_discord_message');
// Nachricht für Discord nur einmal senden $post_url = get_permalink($post_id);
if ($discord_webhook && $discord_template) { $comment_date = get_comment_date('H:i - d.m.Y', $comment_ID); // Zeit und Datum im gewünschten Format
$discord_msg = strtr($discord_template, [ $comment_url = get_comment_link($comment_ID);
// Hole den Benutzernamen aus den Plugin-Einstellungen
$user_name = get_option("wp_user_1_name"); // Beispiel: Benutzer 1
// Nachricht für Discord
$discord_msg = strtr($discord_template, [
'{{COMMENT_AUTHOR}}' => $author, '{{COMMENT_AUTHOR}}' => $author,
'{{COMMENT_TEXT}}' => $text, '{{COMMENT_TEXT}}' => $text,
'{{POST_TITLE}}' => $post_title,
'{{SITE_NAME}}' => $site,
'{{POST_URL}}' => $post_url,
'{{COMMENT_DATE}}' => $comment_date,
'{{COMMENT_URL}}' => $comment_url,
'{{USER_NAME}}' => $user_name, // Benutzername aus den Optionen
]);
// Nachricht per E-Mail verschicken
$email_message = get_option('wp_email_message');
$email_message = strtr($email_message, [
'{{COMMENT_AUTHOR}}' => $author,
'{{COMMENT_TEXT}}' => $text,
'{{USER_NAME}}' => $user_name, // Benutzername aus den Optionen
'{{POST_TITLE}}' => $post_title,
'{{SITE_NAME}}' => $site,
'{{POST_URL}}' => $post_url,
'{{COMMENT_DATE}}' => $comment_date,
'{{COMMENT_URL}}' => $comment_url
]);
// Telegram-Nachricht
$telegram_message = get_option('wp_telegram_message');
$telegram_message = strtr($telegram_message, [
'{{COMMENT_AUTHOR}}' => $author,
'{{COMMENT_TEXT}}' => $text,
'{{USER_NAME}}' => $user_name, // Benutzername aus den Optionen
'{{POST_TITLE}}' => $post_title,
'{{SITE_NAME}}' => $site,
'{{POST_URL}}' => $post_url,
'{{COMMENT_DATE}}' => $comment_date,
'{{COMMENT_URL}}' => $comment_url
]);
$telegram_chat_id = get_option('wp_user_1_telegram_chat_id'); // Beispiel für den ersten Benutzer
wp_remote_post("https://api.telegram.org/bot" . get_option('wp_telegram_token') . "/sendMessage", [
'body' => [
'chat_id' => $telegram_chat_id,
'text' => $telegram_message
]
]); ]);
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: $site <no-reply@" . $_SERVER['SERVER_NAME'] . '>',
];
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);
add_action('comment_post', 'wp_multi_comment_notifications_on_comment');
/** ------------------------- /** -------------------------
* Testnachrichten senden * Testnachrichten senden
@ -378,7 +305,6 @@ function wp_multi_comment_notifications_send_test_message($user_number) {
} }
} }
/** ------------------------- /** -------------------------
* Telegram-Nachricht mit Button * Telegram-Nachricht mit Button
* ------------------------- */ * ------------------------- */
@ -405,7 +331,6 @@ function wp_multi_comment_notifications_send_telegram($token, $chat_id, $comment
]); ]);
} }
/** ------------------------- /** -------------------------
* Discord-Nachricht senden * Discord-Nachricht senden
* ------------------------- */ * ------------------------- */