= start und created_at < end * (end = Folgetag des letzten Tages, 00:00:00). */ if ( ! defined( 'ABSPATH' ) ) { exit; } class Wp360_NoCookie_Analytics_Stats { /** Aufrufe und eindeutige Besucher pro Tag im Zeitraum. */ public static function daily_series( $start, $end ) { global $wpdb; $table = Wp360_NoCookie_Analytics_Database::table_name(); $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugineigene Tabelle, Abfrage ist vorbereitet; Statistik-Abfragen laufen nur im Dashboard und sollen aktuelle Zahlen liefern, daher kein Cache. $wpdb->prepare( "SELECT DATE(created_at) AS d, COUNT(*) AS views, COUNT(DISTINCT visitor_id) AS visitors FROM %i WHERE created_at >= %s AND created_at < %s GROUP BY DATE(created_at) ORDER BY d ASC", $table, $start, $end ), ARRAY_A ); $by_date = array(); foreach ( $rows as $row ) { $by_date[ $row['d'] ] = array( 'views' => (int) $row['views'], 'visitors' => (int) $row['visitors'], ); } // Luecken (Tage ohne Aufrufe) mit Nullwerten auffuellen. $series = array(); $cursor = new DateTimeImmutable( $start ); $end_date = new DateTimeImmutable( $end ); while ( $cursor < $end_date ) { $day = $cursor->format( 'Y-m-d' ); $series[] = array( 'date' => $day, 'views' => $by_date[ $day ]['views'] ?? 0, 'visitors' => $by_date[ $day ]['visitors'] ?? 0, ); $cursor = $cursor->modify( '+1 day' ); } return $series; } /** Gesamtsummen im Zeitraum. Besucher werden ueber den gesamten Zeitraum dedupliziert (nicht die Summe der Tageswerte). */ public static function totals( $start, $end ) { global $wpdb; $table = Wp360_NoCookie_Analytics_Database::table_name(); $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugineigene Tabelle, Abfrage ist vorbereitet; Statistik-Abfragen laufen nur im Dashboard und sollen aktuelle Zahlen liefern, daher kein Cache. $wpdb->prepare( "SELECT COUNT(*) AS views, COUNT(DISTINCT visitor_id) AS visitors FROM %i WHERE created_at >= %s AND created_at < %s", $table, $start, $end ), ARRAY_A ); return array( 'views' => (int) ( $row['views'] ?? 0 ), 'visitors' => (int) ( $row['visitors'] ?? 0 ), ); } /** Meistbesuchte Seiten im Zeitraum -- getrennt nach post_id (Beitraege/Seiten) und reiner URL (Archive, Suche, ...). */ public static function top_pages( $start, $end, $limit = 25 ) { global $wpdb; $table = Wp360_NoCookie_Analytics_Database::table_name(); $posts = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugineigene Tabelle, Abfrage ist vorbereitet; Statistik-Abfragen laufen nur im Dashboard und sollen aktuelle Zahlen liefern, daher kein Cache. $wpdb->prepare( "SELECT post_id, COUNT(*) AS views, COUNT(DISTINCT visitor_id) AS visitors FROM %i WHERE created_at >= %s AND created_at < %s AND post_id IS NOT NULL GROUP BY post_id ORDER BY views DESC LIMIT %d", $table, $start, $end, $limit ), ARRAY_A ); $urls = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugineigene Tabelle, Abfrage ist vorbereitet; Statistik-Abfragen laufen nur im Dashboard und sollen aktuelle Zahlen liefern, daher kein Cache. $wpdb->prepare( "SELECT url, COUNT(*) AS views, COUNT(DISTINCT visitor_id) AS visitors FROM %i WHERE created_at >= %s AND created_at < %s AND post_id IS NULL GROUP BY url ORDER BY views DESC LIMIT %d", $table, $start, $end, $limit ), ARRAY_A ); $result = array(); foreach ( $posts as $row ) { $post_id = (int) $row['post_id']; $result[] = array( 'post_id' => $post_id, 'title' => get_the_title( $post_id ) ?: ( '#' . $post_id ), 'url' => (string) ( get_permalink( $post_id ) ?: '' ), 'views' => (int) $row['views'], 'visitors' => (int) $row['visitors'], ); } foreach ( $urls as $row ) { $url = (string) $row['url']; // Nur eigene Pfade verlinken -- Eintraege aus aelteren Versionen // koennen noch eine fremde Domain enthalten. $is_local_path = '/' === substr( $url, 0, 1 ) && '//' !== substr( $url, 0, 2 ); $result[] = array( 'post_id' => null, 'title' => $url !== '' ? $url : '(unbekannt)', 'url' => $is_local_path ? $url : '', 'views' => (int) $row['views'], 'visitors' => (int) $row['visitors'], ); } usort( $result, static fn ( $a, $b ) => $b['views'] <=> $a['views'] ); return array_slice( $result, 0, $limit ); } }