diff --git a/wp-multi-comment-notifications.php b/wp-multi-comment-notifications.php index 1d1d4fe..b0e5a32 100644 --- a/wp-multi-comment-notifications.php +++ b/wp-multi-comment-notifications.php @@ -1,359 +1,420 @@ - 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 -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'; - $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; - } - - $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 -function wp_multi_cmn_update_check() { - 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) { - 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 '

Fehler beim Abrufen der Update-Informationen.

'; - 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 '

Du verwendest die aktuelle Version: ' . esc_html($installed_version) . '

'; - } else { - echo '

Neue Version verfügbar: ' . esc_html($latest_version) . '

'; - echo '

Installiert: ' . esc_html($installed_version) . '

'; - - if ($is_prerelease && $show_prereleases) { - echo '

Hinweis: Dies ist ein PreRelease.

'; - } - - if (!empty($release_notes)) { - echo '

Release Notes:
' . nl2br(esc_html($release_notes)) . '

'; - } - - $download_url = isset($release['assets'][0]['browser_download_url']) ? $release['assets'][0]['browser_download_url'] : '#'; - echo '

Update herunterladen

'; - } -} - - -// 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() { - ?> -
-

WP Multi Comment Notifications Einstellungen

-
- -

Telegram Bot Token

- - - -

Nachricht für E-Mail

- -

Verwende Platzhalter wie {{COMMENT_AUTHOR}}, {{COMMENT_TEXT}}, {{USER_NAME}} für dynamische Daten.

- -

Nachricht für Telegram

- -

Verwende Platzhalter wie {{COMMENT_AUTHOR}}, {{COMMENT_TEXT}}, {{USER_NAME}} für dynamische Daten.

- -

Backend-Button URL

- - - -

Logo/Banner für E-Mail

- - -

Füge hier die vollständige URL zum Logo/Banner ein, das in der E-Mail angezeigt werden soll.

- -
- -
-

User

- - " /> - - - " /> - - - " /> -
- -
- -
-
- - - 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')); - - 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"); - - $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 .= 'Logo
'; - } - - $html_message .= nl2br(esc_html($email_message)); - - $headers = [ - 'Content-Type: text/html; charset=UTF-8', - 'From: ' . $site_name . ' ' - ]; - - wp_mail(trim($user_email), 'Neuer Kommentar', $html_message, $headers); - } - - $telegram_message = str_replace( - ['{{COMMENT_AUTHOR}}', '{{COMMENT_TEXT}}', '{{USER_NAME}}'], - [$comment_author, $comment_text, $user_name], - get_option('wp_telegram_message') - ); - - if ($telegram_chat_id) { - wp_multi_comment_notifications_send_telegram(get_option('wp_telegram_token'), $telegram_chat_id, $comment_ID, $telegram_message); - } - } -} -add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1); - -// 3. Telegram Nachricht senden (mit Inline-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, - ]; - - if (!empty($inline_button)) { - $data['reply_markup'] = json_encode($inline_button); - } - - $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"); - } -} + 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 +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'; + $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; + } + + $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 +function wp_multi_cmn_update_check() { + 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) { + 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 '

Fehler beim Abrufen der Update-Informationen.

'; + 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 '

Du verwendest die aktuelle Version: ' . esc_html($installed_version) . '

'; + } else { + echo '

Neue Version verfügbar: ' . esc_html($latest_version) . '

'; + echo '

Installiert: ' . esc_html($installed_version) . '

'; + + if ($is_prerelease && $show_prereleases) { + echo '

Hinweis: Dies ist ein PreRelease.

'; + } + + if (!empty($release_notes)) { + echo '

Release Notes:
' . nl2br(esc_html($release_notes)) . '

'; + } + + $download_url = isset($release['assets'][0]['browser_download_url']) ? $release['assets'][0]['browser_download_url'] : '#'; + echo '

Update herunterladen

'; + } +} + + +// 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() { + ?> +
+

WP Multi Comment Notifications Einstellungen

+
+ +

Telegram Bot Token

+ + + +

Discord Webhook URL

+ + + +

Nachricht für Discord

+ +

Verwende Platzhalter wie {{COMMENT_AUTHOR}}, {{COMMENT_TEXT}}, {{USER_NAME}} für dynamische Daten.

+ + +

Nachricht für E-Mail

+ +

Verwende Platzhalter wie {{COMMENT_AUTHOR}}, {{COMMENT_TEXT}}, {{USER_NAME}} für dynamische Daten.

+ +

Nachricht für Telegram

+ +

Verwende Platzhalter wie {{COMMENT_AUTHOR}}, {{COMMENT_TEXT}}, {{USER_NAME}} für dynamische Daten.

+ +

Backend-Button URL

+ + + +

Logo/Banner für E-Mail

+ + +

Füge hier die vollständige URL zum Logo/Banner ein, das in der E-Mail angezeigt werden soll.

+ +
+ +
+

User

+ + " /> + + + " /> + + + " /> + + + " /> +
+ +
+ +
+
+ + + 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 ? "
" : ''; + $html .= nl2br(esc_html($email_msg)); + $headers = [ + 'Content-Type: text/html; charset=UTF-8', + "From: $site ', + ]; + 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, + ]); +}