prefix . 'wp_multi_user_analytics'); /** * Erstellt die Datenbanktabelle für Benutzer-Analytics. */ function wp_multi_create_analytics_table() { global $wpdb; $table_name = WP_MULTI_ANALYTICS_TABLE; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, action varchar(255) NOT NULL, post_id bigint(20) DEFAULT NULL, timestamp datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), INDEX idx_timestamp (timestamp), INDEX idx_action (action) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); } register_activation_hook(WP_MULTI_FILE, 'wp_multi_create_analytics_table'); /** * Verfolgt Benutzerinteraktionen (Kommentare und Beitragsaufrufe). * * @param int $user_id Benutzer-ID. * @param string $action Aktion (z. B. 'view', 'comment'). * @param int $post_id Beitrag-ID (optional). * @return bool Erfolg der Operation. */ function wp_multi_track_user_activity($user_id, $action, $post_id = null) { global $wpdb; $table_name = WP_MULTI_ANALYTICS_TABLE; $user_id = absint($user_id); $action = sanitize_text_field($action); $post_id = $post_id ? absint($post_id) : null; if ($action === 'view' && is_single()) { $post_id = get_the_ID(); } if (!$user_id || !$action) { return false; } return $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'action' => $action, 'post_id' => $post_id, ), array('%d', '%s', '%d') ); } /** * Verfolgt Kommentar-Aktivitäten. * * @param int $comment_id Kommentar-ID. */ function wp_multi_comment_activity($comment_id) { $comment = get_comment($comment_id); $user_id = absint($comment->user_id); if ($user_id) { wp_multi_track_user_activity($user_id, 'comment', $comment->comment_post_ID); } } add_action('comment_post', 'wp_multi_comment_activity'); /** * Verfolgt Beitragsaufrufe. */ function wp_multi_post_view_activity() { if (is_single() && is_user_logged_in()) { $user_id = get_current_user_id(); $post_id = get_the_ID(); wp_multi_track_user_activity($user_id, 'view', $post_id); } } add_action('wp_head', 'wp_multi_post_view_activity'); /** * Ruft rohe Analytics-Daten aus der Datenbank ab. * * @param string $date_query Datum für die Abfrage. * @return array Rohe Analytics-Daten. */ function wp_multi_fetch_raw_analytics($start_datetime = null, $end_datetime = null) { global $wpdb; if (!$start_datetime) { $start_datetime = gmdate('Y-m-d H:i:s', strtotime('-7 days')); } $where = 'WHERE timestamp >= %s'; $params = [$start_datetime]; if ($end_datetime) { $where .= ' AND timestamp <= %s'; $params[] = $end_datetime; } $sql = "SELECT DATE(timestamp) AS date, action, post_id, COUNT(*) AS count, user_id, timestamp FROM " . WP_MULTI_ANALYTICS_TABLE . " $where GROUP BY date, action, post_id, user_id, timestamp ORDER BY date ASC"; return $wpdb->get_results($wpdb->prepare($sql, $params)); } /** * Verarbeitet rohe Analytics-Daten für Diagramm und Tabelle. * * @param array $results Rohe Analytics-Daten. * @return array Verarbeitete Daten. */ function wp_multi_process_analytics_data($results) { $dates = []; $comment_counts = []; $view_counts = []; foreach ($results as $result) { $date = $result->date; if (!in_array($date, $dates)) { $dates[] = $date; } if ($result->action === 'comment') { $comment_counts[$date] = ($comment_counts[$date] ?? 0) + $result->count; } elseif ($result->action === 'view') { $view_counts[$date] = ($view_counts[$date] ?? 0) + $result->count; } } $all_dates = []; $datasets = ['comments' => [], 'views' => []]; for ($i = 6; $i >= 0; $i--) { $date = date('Y-m-d', strtotime("-$i day")); $all_dates[] = $date; $datasets['comments'][] = $comment_counts[$date] ?? 0; $datasets['views'][] = $view_counts[$date] ?? 0; } return [ 'dates' => array_reverse($all_dates), 'datasets' => [ [ 'label' => __('Kommentare', 'wp-multi'), 'data' => array_reverse($datasets['comments']), 'borderColor' => 'rgba(75, 192, 192, 1)', 'borderWidth' => 1, 'fill' => false, ], [ 'label' => __('Beitragsaufrufe', 'wp-multi'), 'data' => array_reverse($datasets['views']), 'borderColor' => 'rgba(153, 102, 255, 1)', 'borderWidth' => 1, 'fill' => false, ] ], 'data' => $results ]; } /** * Ruft Analytics-Daten mit Caching ab. * * @param string $date_query Datum für die Abfrage. * @return array Analytics-Daten. */ function wp_multi_get_analytics_data($start_datetime = null, $end_datetime = null) { $cache_key = 'wp_multi_analytics_data_' . md5($start_datetime . '|' . $end_datetime); $cached_data = get_transient($cache_key); if ($cached_data !== false) { return $cached_data; } $results = wp_multi_fetch_raw_analytics($start_datetime, $end_datetime); $data = wp_multi_process_analytics_data($results); set_transient($cache_key, $data, HOUR_IN_SECONDS); return $data; } /** * Zeigt die Benutzer-Analytics-Seite im Admin-Bereich an. */ function wp_multi_display_user_analytics() { global $wpdb; if (!$wpdb->get_var("SHOW TABLES LIKE '" . WP_MULTI_ANALYTICS_TABLE . "'")) { echo '
' . esc_html__('Die Analytics-Tabelle existiert nicht. Bitte aktiviere das Plugin erneut.', 'wp-multi') . '
| user_id); ?> | action); ?> | post_id) { $post_title = get_the_title($row->post_id); echo esc_html($post_title ?: __('Kein Titel verfügbar', 'wp-multi')); } else { echo esc_html__('Kein Beitrag', 'wp-multi'); } ?> | post_id ?: '-'); ?> | timestamp); ?> |