' . esc_html__( 'Insights settings saved.', 'ai-search' ) . '

'; } if ( isset( $_POST['ai_search_clear_logs'] ) ) { check_admin_referer( 'ai_search_clear_logs' ); $wpdb->query( "TRUNCATE TABLE {$ai_log_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery echo '

' . esc_html__( 'Search log cleared.', 'ai-search' ) . '

'; } $logging_enabled = (bool) get_option( 'ai_search_logging_enabled', true ); $retention_days = (int) get_option( 'ai_search_log_retention_days', 90 ); $digest_enabled = (bool) get_option( 'ai_search_weekly_digest_enabled', true ); $digest_last = (int) get_option( 'ai_search_weekly_digest_last_sent', 0 ); // Does the table exist yet? (Sites that updated rather than activated get // it on the next load; be graceful in the meantime.) $table_ready = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $ai_log_table ) ) === $ai_log_table ); if ( ! $table_ready ) { echo '

' . esc_html__( 'The search log is being set up. Reload this page in a moment.', 'ai-search' ) . '

'; return; } $total_logged = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$ai_log_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery ?>

' . esc_html( number_format_i18n( $total_logged ) ) . '' ); ?>

0 ) : ?> get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT query, COUNT(*) AS hits, ROUND( AVG( results_total ) ) AS avg_results, MAX( created_at ) AS last_seen FROM {$ai_log_table} GROUP BY query ORDER BY hits DESC, last_seen DESC LIMIT 20" ); ?>

query ); ?> hits ) ); ?> avg_results ) ); ?> last_seen ), current_time( 'timestamp' ) ) ) ); ?>
get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT query, COUNT(*) AS hits, MAX(created_at) AS last_seen FROM {$ai_log_table} WHERE results_total = 0 GROUP BY query ORDER BY hits DESC, last_seen DESC LIMIT 20" ); ?>

query ); ?> hits ) ); ?> last_seen ), current_time( 'timestamp' ) ) ) ); ?>
get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT query, results_total, tier_exact, tier_lexical, tier_ai, top_ids, created_at FROM {$ai_log_table} ORDER BY id DESC LIMIT 25" ); // Resolve every referenced post in one query rather than per row. $all_ids = []; foreach ( $recent as $row ) { $all_ids = array_merge( $all_ids, AI_Search::parse_top_ids( $row->top_ids ) ); } $titles = []; if ( ! empty( $all_ids ) ) { foreach ( get_posts( [ 'post_type' => 'any', 'post__in' => array_unique( $all_ids ), 'posts_per_page' => -1, 'suppress_filters' => true, ] ) as $post ) { $titles[ $post->ID ] = $post->post_title; } } ?>

query ); ?> top_ids ) as $pid ) { if ( isset( $titles[ $pid ] ) ) { $shown[] = '' . esc_html( $titles[ $pid ] ) . ''; } else { $shown[] = '' . esc_html__( '(removed)', 'ai-search' ) . ''; } } echo $shown ? wp_kses_post( implode( ', ', $shown ) ) : '—'; ?> tier_exact ) { /* translators: %d: number of exact matches */ $parts[] = sprintf( esc_html__( '%d exact', 'ai-search' ), (int) $row->tier_exact ); } if ( $row->tier_lexical ) { /* translators: %d: number of keyword matches */ $parts[] = sprintf( esc_html__( '%d keyword', 'ai-search' ), (int) $row->tier_lexical ); } if ( $row->tier_ai ) { /* translators: %d: number of AI matches */ $parts[] = sprintf( esc_html__( '%d AI', 'ai-search' ), (int) $row->tier_ai ); } echo esc_html( implode( ' · ', $parts ) ); ?> created_at ), current_time( 'timestamp' ) ) ) ); ?>

0 ) : ?>

' . esc_html( get_option( 'admin_email' ) ) . '' ); ?>