wp-multi-comment-notifications.php aktualisiert

This commit is contained in:
M_Viper 2025-04-09 12:53:28 +00:00
parent e6bfb50207
commit b7ddf09646

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.3
* 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,99 @@
* - Telegram: https://t.me/M_Viper04 * - Telegram: https://t.me/M_Viper04
*/ */
if ( ! defined( 'ABSPATH' ) ) { if (!defined('ABSPATH')) {
exit; exit;
} }
/** define('WPMCN_PLUGIN_VERSION', '1.2');
* Dashboard Widget Update Info für dieses Plugin define('WPMCN_PLUGIN_DIR', plugin_dir_path(__FILE__));
*/
function wp_multi_cmn_dashboard_widget() {
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 // Abhängigkeit prüfen und WP Multi Toolkit installieren/aktivieren
add_filter('cron_schedules', function ($schedules) { function wpmcn_check_dependency() {
$schedules['hourly'] = array( require_once ABSPATH . 'wp-admin/includes/plugin.php';
'interval' => 3600, $required_plugin = 'WP-Multi-Toolkit/wp-multi-toolkit.php';
'display' => __('Stündlich', 'wp-multi-comment-notifications'), $download_url = 'https://git.viper.ipv64.net/M_Viper/wp-multi-toolkit/releases/latest/download/wp-multi-toolkit.zip'; // Direkter Download-Link zur neuesten ZIP
);
return $schedules;
});
// Cron-Event planen // Prüfen, ob WP Multi Toolkit installiert ist
function wp_multi_cmn_schedule_update_check() { if (!file_exists(WP_PLUGIN_DIR . '/' . $required_plugin)) {
if (!wp_next_scheduled('wp_multi_cmn_update_check_event')) { // Plugin ist nicht installiert, versuche es zu installieren
wp_schedule_event(time(), 'hourly', 'wp_multi_cmn_update_check_event'); wpmcn_install_required_plugin($download_url, $required_plugin);
}
}
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); // Prüfen, ob WP Multi Toolkit aktiv ist
$data = json_decode($body, true); if (!is_plugin_active($required_plugin)) {
// Plugin ist installiert, aber nicht aktiv, aktiviere es
if (!is_array($data)) { $result = activate_plugin($required_plugin);
error_log('WP Multi Comment Notifications Ungültige API-Antwort'); if (is_wp_error($result)) {
return false; // Fehlerbehandlung: Admin-Benachrichtigung
} add_action('admin_notices', function () use ($result) {
echo '<div class="notice notice-error"><p>' . sprintf(
foreach ($data as $release) { __('Fehler beim Aktivieren von WP Multi Toolkit: %s', 'wp-multi-comment-notifications'),
if (!$show_prereleases && isset($release['prerelease']) && $release['prerelease']) { esc_html($result->get_error_message())
continue; ) . '</p></div>';
} });
if (!empty($release['tag_name'])) {
return $release;
} }
} }
return null;
} }
// Cron Callback // Funktion zum Installieren des erforderlichen Plugins
function wp_multi_cmn_update_check() { function wpmcn_install_required_plugin($download_url, $plugin_file) {
if (!function_exists('get_plugin_data')) { if (!current_user_can('install_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php'; return; // Nur Admins können Plugins installieren
} }
$plugin_data = get_plugin_data(__FILE__); require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$installed_version = $plugin_data['Version']; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$show_prereleases = get_option('wp_multi_cmn_show_prereleases', false); require_once ABSPATH . 'wp-admin/includes/file.php';
$release = wp_multi_cmn_fetch_latest_release($show_prereleases); // Dateisystemzugriff initialisieren
$access_type = get_filesystem_method();
if ($release) { if ($access_type !== 'direct') {
update_option('wp_multi_cmn_latest_version', $release['tag_name']); add_action('admin_notices', function () {
update_option('wp_multi_cmn_release_notes', $release['body']); echo '<div class="notice notice-error"><p>' . __('Direkter Dateizugriff erforderlich, um WP Multi Toolkit zu installieren. Bitte überprüfe die Dateisystemkonfiguration.', 'wp-multi-comment-notifications') . '</p></div>';
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; return;
} }
$latest_version = $release['tag_name']; $creds = request_filesystem_credentials(admin_url(), '', false, false, null);
$release_notes = isset($release['body']) ? $release['body'] : ''; if (!WP_Filesystem($creds)) {
$is_prerelease = isset($release['prerelease']) && $release['prerelease']; add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>' . __('Dateisystemzugriff fehlgeschlagen. WP Multi Toolkit konnte nicht installiert werden.', 'wp-multi-comment-notifications') . '</p></div>';
});
return;
}
if (version_compare($installed_version, $latest_version, '>=')) { $upgrader = new Plugin_Upgrader();
echo '<p style="color:green;">Du verwendest die aktuelle Version: <strong>' . esc_html($installed_version) . '</strong></p>'; $result = $upgrader->install($download_url);
if (is_wp_error($result)) {
add_action('admin_notices', function () use ($result) {
echo '<div class="notice notice-error"><p>' . sprintf(
__('Fehler beim Installieren von WP Multi Toolkit: %s', 'wp-multi-comment-notifications'),
esc_html($result->get_error_message())
) . '</p></div>';
});
} else { } else {
echo '<p style="color:red;"><strong>Neue Version verfügbar: ' . esc_html($latest_version) . '</strong></p>'; // Installation erfolgreich, aktiviere das Plugin
echo '<p>Installiert: <strong>' . esc_html($installed_version) . '</strong></p>'; $activate = activate_plugin($plugin_file);
if (is_wp_error($activate)) {
if ($is_prerelease && $show_prereleases) { add_action('admin_notices', function () use ($activate) {
echo '<p style="color:blue;">Hinweis: Dies ist ein PreRelease.</p>'; echo '<div class="notice notice-error"><p>' . sprintf(
__('Fehler beim Aktivieren von WP Multi Toolkit nach Installation: %s', 'wp-multi-comment-notifications'),
esc_html($activate->get_error_message())
) . '</p></div>';
});
} else {
add_action('admin_notices', function () {
echo '<div class="notice notice-success"><p>' . __('WP Multi Toolkit wurde erfolgreich installiert und aktiviert.', 'wp-multi-comment-notifications') . '</p></div>';
});
} }
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>';
} }
} }
// Prüfung bei Plugin-Aktivierung und jedem Admin-Ladevorgang
register_activation_hook(__FILE__, 'wpmcn_check_dependency');
add_action('admin_init', 'wpmcn_check_dependency');
// 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() {
@ -162,8 +132,8 @@ function wp_multi_comment_notifications_settings_page() {
<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>
@ -177,7 +147,6 @@ function wp_multi_comment_notifications_settings_page() {
<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> für dynamische Daten.</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> für dynamische Daten.</p>
@ -209,7 +178,7 @@ function wp_multi_comment_notifications_settings_page() {
<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")); ?>" /> <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 --> <!-- 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")); ?>" /> <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") ?: "User $i"); ?>" />
</div> </div>
<?php endfor; ?> <?php endfor; ?>
</div> </div>
@ -275,7 +244,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');
@ -322,7 +290,7 @@ function wp_multi_comment_notifications_on_comment($comment_ID) {
$html .= nl2br(esc_html($email_msg)); $html .= nl2br(esc_html($email_msg));
$headers = [ $headers = [
'Content-Type: text/html; charset=UTF-8', 'Content-Type: text/html; charset=UTF-8',
"From: $site <no-reply@" . $_SERVER['SERVER_NAME'] . '>', 'From' => get_option('admin_email'),
]; ];
wp_mail($email, 'Neuer Kommentar', $html, $headers); wp_mail($email, 'Neuer Kommentar', $html, $headers);
} }
@ -341,7 +309,6 @@ function wp_multi_comment_notifications_on_comment($comment_ID) {
} }
add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1); add_action('wp_insert_comment', 'wp_multi_comment_notifications_on_comment', 10, 1);
/** ------------------------- /** -------------------------
* Testnachrichten senden * Testnachrichten senden
* ------------------------- */ * ------------------------- */
@ -378,7 +345,6 @@ function wp_multi_comment_notifications_send_test_message($user_number) {
} }
} }
/** ------------------------- /** -------------------------
* Telegram-Nachricht mit Button * Telegram-Nachricht mit Button
* ------------------------- */ * ------------------------- */
@ -405,7 +371,6 @@ function wp_multi_comment_notifications_send_telegram($token, $chat_id, $comment
]); ]);
} }
/** ------------------------- /** -------------------------
* Discord-Nachricht senden * Discord-Nachricht senden
* ------------------------- */ * ------------------------- */
@ -417,4 +382,4 @@ function wp_multi_comment_notifications_send_discord($webhook_url, $message) {
'body' => $payload, 'body' => $payload,
'timeout' => 10, 'timeout' => 10,
]); ]);
} }