modify( '-' . ( $days - 1 ) . ' days' ); $utc = clone $from; $utc->setTimezone( new DateTimeZone( 'UTC' ) ); return array( 'days' => $days, 'from_day' => $from->format( 'Y-m-d' ), 'to_day' => $to->format( 'Y-m-d' ), 'from_ts' => $utc->format( 'Y-m-d H:i:s' ), ); } /** * Values for the category filter every scoped query carries, as three * placeholders: `AND ( %s = 'all' OR ( %s = 'ai' AND category IN (…AI * categories…) ) OR category = %s )`. * * @param string $scope ai|all|. * @return string[] */ private static function scope_args( $scope ) { $scope = sanitize_key( (string) $scope ); $scope = '' === $scope ? 'all' : $scope; return array( $scope, $scope, $scope ); } /** * Dashboard overview. * * @param int $days Days. * @param string $scope ai|all|category. * @return array */ public static function overview( $days = 30, $scope = 'ai', $fresh = false ) { global $wpdb; $r = self::range( $days ); $scope = self::normalize_scope( $scope ); $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $sa = self::scope_args( $scope ); // Previous period for deltas. $prev_from = gmdate( 'Y-m-d', strtotime( $r['from_day'] ) - $r['days'] * DAY_IN_SECONDS ); // Totals and timelines read the small daily rollup table: always live. $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(hits),0) FROM %i WHERE kind='bot' AND day >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s )", $daily, $r['from_day'], $sa[0], $sa[1], $sa[2] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $prev = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(hits),0) FROM %i WHERE kind='bot' AND day >= %s AND day < %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s )", $daily, $prev_from, $r['from_day'], $sa[0], $sa[1], $sa[2] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $bots = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT item) FROM %i WHERE kind='bot' AND day >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s )", $daily, $r['from_day'], $sa[0], $sa[1], $sa[2] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $ref_total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(hits),0) FROM %i WHERE kind='ref' AND day >= %s", $daily, $r['from_day'] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $ref_prev = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(hits),0) FROM %i WHERE kind='ref' AND day >= %s AND day < %s", $daily, $prev_from, $r['from_day'] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. // Page-level figures scan the detailed log (millions of rows on a busy // site): computed at most every ten minutes per period and scope. $heavy = self::cached( 'heavy|' . $r['days'] . '|' . $scope, static function () use ( $wpdb, $hits, $sa, $r, $days, $scope ) { return array( 'pages' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT path) FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s )", $hits, $r['from_ts'], $sa[0], $sa[1], $sa[2] ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. 'md_hits' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ts >= %s AND resource IN ('md','llms','llms-full') AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s )", $hits, $r['from_ts'], $sa[0], $sa[1], $sa[2] ) ), // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. 'top_pages' => self::top_pages( $days, $scope, 10 ), 'ref_pages' => self::referral_pages( $days, 8 ), 'resources' => self::resources( $r, $scope ), ); }, $fresh ); return array( 'range' => $r, 'kpis' => array( 'hits' => $total, 'hits_prev' => $prev, 'bots' => $bots, 'pages' => $heavy['pages'], 'referrals' => $ref_total, 'referrals_prev' => $ref_prev, 'ai_files' => $heavy['md_hits'], 'site_score' => self::site_score(), 'content_score' => Aeoora_Audit::average_score(), 'pending' => Aeoora_Suggestions::pending_count(), ), 'timeline' => self::timeline( $r, $scope ), 'categories' => self::categories( $r ), 'top_bots' => self::top_bots( $days, $scope, 12 ), 'top_pages' => $heavy['top_pages'], 'referrers' => self::referrers( $days ), 'ref_pages' => $heavy['ref_pages'], 'resources' => $heavy['resources'], 'recent' => self::recent( 12, $scope ), ); } /** * Only known scopes (all, ai, a category), so cache keys stay bounded. * * @param string $scope Scope. * @return string */ private static function normalize_scope( $scope ) { $scope = sanitize_key( (string) $scope ); if ( 'all' === $scope || 'ai' === $scope || isset( Aeoora_Bots::categories()[ $scope ] ) ) { return $scope; } return 'ai'; } /** * Cache generation: bumped when data is purged, so cached figures go too. * * @return int */ public static function generation() { return (int) get_option( 'aeoora_stats_gen', 0 ); } /** * Forget every cached figure. */ public static function flush() { update_option( 'aeoora_stats_gen', self::generation() + 1, false ); } /** * Compute a value at most every ten minutes. * * @param string $key Key. * @param callable $callback Computes the value. * @param bool $fresh Recompute now. * @return mixed */ private static function cached( $key, $callback, $fresh = false ) { $tkey = 'aeoora_st_' . md5( $key . '|' . self::generation() ); if ( ! $fresh ) { $value = get_transient( $tkey ); if ( false !== $value ) { return $value; } } $value = call_user_func( $callback ); set_transient( $tkey, $value, 10 * MINUTE_IN_SECONDS ); return $value; } /** * Cached site score. * * @return int|null */ private static function site_score() { $a = get_option( 'aeoora_site_audit' ); return is_array( $a ) && isset( $a['score'] ) ? (int) $a['score'] : null; } /** * Daily timeline by category (bots) and referrals. * * @param array $r Range. * @param string $scope Scope. * @return array */ private static function timeline( array $r, $scope ) { global $wpdb; $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $sa = self::scope_args( $scope ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT day, category, SUM(hits) h FROM %i WHERE kind='bot' AND day >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) GROUP BY day, category", $daily, $r['from_day'], $sa[0], $sa[1], $sa[2] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $refs = $wpdb->get_results( $wpdb->prepare( "SELECT day, SUM(hits) h FROM %i WHERE kind='ref' AND day >= %s GROUP BY day", $daily, $r['from_day'] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $days = array(); $cur = new DateTime( $r['from_day'] ); for ( $i = 0; $i < $r['days']; $i++ ) { $days[ $cur->format( 'Y-m-d' ) ] = array( 'day' => $cur->format( 'Y-m-d' ), 'total' => 0, 'referrals' => 0, 'cats' => array(), ); $cur->modify( '+1 day' ); } foreach ( $rows as $row ) { if ( isset( $days[ $row['day'] ] ) ) { $days[ $row['day'] ]['cats'][ $row['category'] ] = (int) $row['h']; $days[ $row['day'] ]['total'] += (int) $row['h']; } } foreach ( $refs as $row ) { if ( isset( $days[ $row['day'] ] ) ) { $days[ $row['day'] ]['referrals'] = (int) $row['h']; } } return array_values( $days ); } /** * Hits by category. * * @param array $r Range. * @return array */ private static function categories( array $r ) { global $wpdb; $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT category, SUM(hits) h FROM %i WHERE kind='bot' AND day >= %s GROUP BY category ORDER BY h DESC", $daily, $r['from_day'] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $cats = Aeoora_Bots::categories(); $out = array(); foreach ( $rows as $row ) { $c = $row['category']; $out[] = array( 'id' => $c, 'label' => isset( $cats[ $c ] ) ? $cats[ $c ]['label'] : $c, 'color' => isset( $cats[ $c ] ) ? $cats[ $c ]['color'] : '#94a3b8', 'hits' => (int) $row['h'], 'ai' => Aeoora_Bots::is_ai_category( $c ), ); } return $out; } /** * Top bots. * * @param int $days Days. * @param string $scope Scope. * @param int $limit Limit. * @return array */ public static function top_bots( $days = 30, $scope = 'ai', $limit = 20 ) { global $wpdb; $r = self::range( $days ); $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $sa = self::scope_args( $scope ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT item, category, SUM(hits) h FROM %i WHERE kind='bot' AND day >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) GROUP BY item, category ORDER BY h DESC LIMIT %d", $daily, $r['from_day'], $sa[0], $sa[1], $sa[2], (int) $limit ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $out = array(); foreach ( $rows as $row ) { $bot = Aeoora_Bots::get( $row['item'] ); $last = $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(ts) FROM %i WHERE bot = %s', $hits, $row['item'] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $out[] = array( 'id' => $row['item'], 'name' => $bot ? $bot['name'] : ( 'unknown-bot' === $row['item'] ? __( 'Unidentified bot', 'aeoora' ) : $row['item'] ), 'operator' => $bot ? $bot['operator'] : '', 'category' => $row['category'], 'icon' => $bot ? Aeoora_Favicons::url( $bot['domain'] ) : '', 'hits' => (int) $row['h'], 'last' => $last ? get_date_from_gmt( $last, 'c' ) : null, 'policy' => Aeoora_Bots::policy( $row['item'] ), 'token' => $bot ? $bot['token'] : null, ); } return $out; } /** * Top crawled pages. * * @param int $days Days. * @param string $scope Scope. * @param int $limit Limit. * @return array */ public static function top_pages( $days = 30, $scope = 'ai', $limit = 20 ) { global $wpdb; $r = self::range( $days ); $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $sa = self::scope_args( $scope ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT path, MAX(post_id) post_id, COUNT(*) h, COUNT(DISTINCT bot) bots FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) GROUP BY path ORDER BY h DESC LIMIT %d", $hits, $r['from_ts'], $sa[0], $sa[1], $sa[2], (int) $limit ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. foreach ( $rows as &$row ) { $row['h'] = (int) $row['h']; $row['bots'] = (int) $row['bots']; $row['title'] = $row['post_id'] ? get_the_title( (int) $row['post_id'] ) : ''; $row['score'] = $row['post_id'] ? get_post_meta( (int) $row['post_id'], '_aeoora_score', true ) : ''; } return $rows; } /** * AI referrals by source (model). * * @param int $days Days. * @return array */ public static function referrers( $days = 30 ) { global $wpdb; $r = self::range( $days ); $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT item, SUM(hits) h FROM %i WHERE kind='ref' AND day >= %s GROUP BY item ORDER BY h DESC", $daily, $r['from_day'] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $src = Aeoora_Referrals::sources(); $out = array(); foreach ( $rows as $row ) { $s = isset( $src[ $row['item'] ] ) ? $src[ $row['item'] ] : null; $out[] = array( 'id' => $row['item'], 'label' => $s ? $s['label'] : $row['item'], 'vendor' => $s ? $s['vendor'] : '', 'color' => $s ? $s['color'] : '#94a3b8', 'icon' => $s ? Aeoora_Favicons::url( $s['domain'] ) : '', 'hits' => (int) $row['h'], ); } return $out; } /** * Landing pages receiving AI referrals. * * @param int $days Days. * @param int $limit Limit. * @return array */ public static function referral_pages( $days = 30, $limit = 20 ) { global $wpdb; $r = self::range( $days ); $refs = Aeoora_Install::table( Aeoora_Install::TABLE_REFERRALS ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT path, MAX(post_id) post_id, COUNT(*) h, GROUP_CONCAT(DISTINCT source) sources FROM %i WHERE ts >= %s GROUP BY path ORDER BY h DESC LIMIT %d', $refs, $r['from_ts'], (int) $limit ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $src = Aeoora_Referrals::sources(); foreach ( $rows as &$row ) { $row['h'] = (int) $row['h']; $row['title'] = $row['post_id'] ? get_the_title( (int) $row['post_id'] ) : ''; $row['sources'] = array_values( array_filter( explode( ',', (string) $row['sources'] ) ) ); // Display names ("ChatGPT"), next to the ids ("chatgpt"). $row['source_labels'] = array_map( static function ( $id ) use ( $src ) { return isset( $src[ $id ]['label'] ) ? $src[ $id ]['label'] : $id; }, $row['sources'] ); } unset( $row ); return $rows; } /** * Referral log. * * @param array $args days, source, page, per_page. * @return array */ public static function referral_log( array $args ) { global $wpdb; $r = self::range( isset( $args['days'] ) ? $args['days'] : 30 ); $refs = Aeoora_Install::table( Aeoora_Install::TABLE_REFERRALS ); $source = ! empty( $args['source'] ) ? sanitize_key( $args['source'] ) : ''; $per = isset( $args['per_page'] ) ? max( 1, min( 200, (int) $args['per_page'] ) ) : 50; $page = isset( $args['page'] ) ? max( 1, (int) $args['page'] ) : 1; $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ts >= %s AND ( %s = '' OR source = %s )", $refs, $r['from_ts'], $source, $source ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE ts >= %s AND ( %s = '' OR source = %s ) ORDER BY ts DESC LIMIT %d OFFSET %d", $refs, $r['from_ts'], $source, $source, $per, ( $page - 1 ) * $per ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $src = Aeoora_Referrals::sources(); foreach ( $rows as &$row ) { $row['time'] = get_date_from_gmt( $row['ts'], 'c' ); $row['label'] = isset( $src[ $row['source'] ] ) ? $src[ $row['source'] ]['label'] : $row['source']; } return array( 'total' => $total, 'pages' => (int) ceil( $total / $per ), 'rows' => $rows, ); } /** * Hits by resource type (html, md, llms, robots, ...). * * @param array $r Range. * @param string $scope Scope. * @return array */ private static function resources( array $r, $scope ) { global $wpdb; $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $sa = self::scope_args( $scope ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT resource, COUNT(*) h FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) GROUP BY resource ORDER BY h DESC", $hits, $r['from_ts'], $sa[0], $sa[1], $sa[2] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $out = array(); foreach ( $rows as $row ) { $out[ $row['resource'] ] = (int) $row['h']; } return $out; } /** * Recent hits. * * @param int $limit Limit. * @param string $scope Scope. * @return array */ public static function recent( $limit = 20, $scope = 'ai' ) { $log = self::log( array( 'per_page' => $limit, 'scope' => $scope, 'days' => 30, ), false ); return $log['rows']; } /** * Filterable hit log. * * @param array $args days, scope, bot, resource, status, search, page, per_page. * @param bool $count Count the matching rows (for paging). * @return array */ public static function log( array $args, $count = true ) { global $wpdb; $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $vals = self::log_args( $args ); $per = isset( $args['per_page'] ) ? max( 1, min( 500, (int) $args['per_page'] ) ) : 50; $page = isset( $args['page'] ) ? max( 1, (int) $args['page'] ) : 1; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, ts, bot, category, path, post_id, resource, status, ip, ua FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) AND ( %s = '' OR bot = %s ) AND ( %s = '' OR resource = %s ) AND ( %d = 0 OR ( %d < 10 AND status >= %d AND status < %d ) OR ( %d >= 10 AND status = %d ) ) AND ( %s = '' OR path LIKE %s ) ORDER BY ts DESC, id DESC LIMIT %d OFFSET %d", $hits, $vals[0], $vals[1], $vals[2], $vals[3], $vals[4], $vals[5], $vals[6], $vals[7], $vals[8], $vals[9], $vals[10], $vals[11], $vals[12], $vals[13], $vals[14], $vals[15], $per, ( $page - 1 ) * $per ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $total = $count ? (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) AND ( %s = '' OR bot = %s ) AND ( %s = '' OR resource = %s ) AND ( %d = 0 OR ( %d < 10 AND status >= %d AND status < %d ) OR ( %d >= 10 AND status = %d ) ) AND ( %s = '' OR path LIKE %s )", $hits, $vals[0], $vals[1], $vals[2], $vals[3], $vals[4], $vals[5], $vals[6], $vals[7], $vals[8], $vals[9], $vals[10], $vals[11], $vals[12], $vals[13], $vals[14], $vals[15] ) ) : count( $rows ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. return array( 'total' => $total, 'pages' => (int) ceil( $total / $per ), 'rows' => self::decorate( (array) $rows ), ); } /** * Log rows older than a row id, newest first (keyset paging for exports: * no OFFSET scans and no COUNT per page). * * @param array $args Filters (as log()). * @param int $before Only rows with a lower id (0 = from the newest). * @param int $limit Rows. * @return array */ public static function log_before( array $args, $before, $limit = 1000 ) { global $wpdb; $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $before = max( 0, (int) $before ); $vals = self::log_args( $args ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, ts, bot, category, path, post_id, resource, status, ip, ua FROM %i WHERE ts >= %s AND ( %s = 'all' OR ( %s = 'ai' AND category IN ('ai_training','ai_search','ai_assistant','ai_agent') ) OR category = %s ) AND ( %s = '' OR bot = %s ) AND ( %s = '' OR resource = %s ) AND ( %d = 0 OR ( %d < 10 AND status >= %d AND status < %d ) OR ( %d >= 10 AND status = %d ) ) AND ( %s = '' OR path LIKE %s ) AND ( %d = 0 OR id < %d ) ORDER BY id DESC LIMIT %d", $hits, $vals[0], $vals[1], $vals[2], $vals[3], $vals[4], $vals[5], $vals[6], $vals[7], $vals[8], $vals[9], $vals[10], $vals[11], $vals[12], $vals[13], $vals[14], $vals[15], $before, $before, max( 1, min( 5000, (int) $limit ) ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. return self::decorate( (array) $rows ); } /** * Names, icons, local times and titles for log rows. * * @param array $rows Rows. * @return array */ private static function decorate( array $rows ) { foreach ( $rows as &$row ) { $bot = Aeoora_Bots::get( $row['bot'] ); $row['name'] = $bot ? $bot['name'] : ( 'unknown-bot' === $row['bot'] ? __( 'Unidentified bot', 'aeoora' ) : $row['bot'] ); $row['icon'] = $bot ? Aeoora_Favicons::url( $bot['domain'] ) : ''; $row['time'] = get_date_from_gmt( $row['ts'], 'c' ); $row['title'] = $row['post_id'] ? get_the_title( (int) $row['post_id'] ) : ''; } unset( $row ); return $rows; } /** * Values for the hit log's fixed WHERE clause, in placeholder order: * from, scope ×3, bot ×2, resource ×2, status ×6 (a class like 4 for * 4xx, or an exact code), search ×2. An empty filter matches everything. * * @param array $args Filters. * @return array */ private static function log_args( array $args ) { global $wpdb; $r = self::range( isset( $args['days'] ) ? $args['days'] : 30 ); $bot = ! empty( $args['bot'] ) ? sanitize_text_field( $args['bot'] ) : ''; $resource = ! empty( $args['resource'] ) ? sanitize_key( $args['resource'] ) : ''; $status = ! empty( $args['status'] ) ? max( 0, (int) $args['status'] ) : 0; $search = ! empty( $args['search'] ) && is_scalar( $args['search'] ) ? sanitize_text_field( (string) $args['search'] ) : ''; return array_merge( array( $r['from_ts'] ), self::scope_args( isset( $args['scope'] ) ? $args['scope'] : 'all' ), array( $bot, $bot, $resource, $resource ), array( $status, $status, $status * 100, ( $status + 1 ) * 100, $status, $status ), array( $search, '%' . $wpdb->esc_like( $search ) . '%' ) ); } /** * Per-bot totals and last-seen for the crawler directory (one query). * * @param int $days Days. * @return array id => { hits, last } */ public static function bot_totals( $days = 30 ) { global $wpdb; $r = self::range( $days ); $daily = Aeoora_Install::table( Aeoora_Install::TABLE_DAILY ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT item, SUM(hits) h, MAX(day) last FROM %i WHERE kind='bot' AND day >= %s GROUP BY item", $daily, $r['from_day'] ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. $out = array(); foreach ( $rows as $row ) { $out[ $row['item'] ] = array( 'hits' => (int) $row['h'], 'last' => $row['last'], ); } return $out; } /** * Bots that requested pages they are disallowed from (compliance check). * * @param int $days Days. * @return array */ public static function violations( $days = 7 ) { global $wpdb; $r = self::range( $days ); $hits = Aeoora_Install::table( Aeoora_Install::TABLE_HITS ); $changed = (array) get_option( 'aeoora_policy_changed', array() ); $watch = array(); // Bot id => counted since. foreach ( Aeoora_Bots::all() as $id => $bot ) { $policy = Aeoora_Bots::policy( $id ); if ( 'allow' === $policy || empty( $bot['token'] ) ) { continue; } // Crawlers may cache robots.txt for up to 24 hours after a change. $since = $r['from_ts']; if ( isset( $changed[ $id ] ) ) { $since = max( $since, gmdate( 'Y-m-d H:i:s', (int) $changed[ $id ] + DAY_IN_SECONDS ) ); } $watch[ $id ] = $since; } if ( ! $watch ) { return array(); } $counts = self::cached( 'violations|' . wp_json_encode( $watch ), static function () use ( $wpdb, $hits, $watch, $r ) { $counts = array(); // One grouped query for every bot counted over the whole period… $same = array_keys( $watch, $r['from_ts'], true ); if ( $same ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT bot, COUNT(*) n FROM %i WHERE ts >= %s AND bot IN (" . implode( ',', array_fill( 0, count( $same ), '%s' ) ) . ") AND resource NOT IN ('robots','blocked') GROUP BY bot", ...array_merge( array( $hits, $r['from_ts'] ), $same ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. foreach ( (array) $rows as $row ) { $counts[ $row['bot'] ] = (int) $row['n']; } } // …and one each for the few whose policy changed during it. foreach ( $watch as $id => $since ) { if ( $since !== $r['from_ts'] ) { $counts[ $id ] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE bot = %s AND ts >= %s AND resource NOT IN ('robots','blocked')", $hits, $id, $since ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- custom tables, no core API. } } return $counts; } ); $out = array(); $robots = Aeoora_Robots::preview(); foreach ( (array) $counts as $id => $count ) { $bot = Aeoora_Bots::get( $id ); if ( $count && $bot && ! Aeoora_Robots::is_allowed( $robots, $bot['token'], '/' ) ) { $out[] = array( 'id' => $id, 'name' => $bot['name'], 'hits' => (int) $count, 'policy' => Aeoora_Bots::policy( $id ), ); } } return $out; } }