1, 'weekly' => 7, 'monthly' => 30, ); $freq = isset( $days[ $s['frequency'] ] ) ? $s['frequency'] : 'weekly'; $last = (int) get_option( 'aeoora_report_last', 0 ); if ( ! $force && $last && time() - $last < ( $days[ $freq ] * DAY_IN_SECONDS - HOUR_IN_SECONDS ) ) { return false; } $to = '' !== trim( (string) $s['recipient'] ) ? $s['recipient'] : get_option( 'admin_email' ); $o = Aeoora_Stats::overview( $days[ $freq ], 'ai' ); /* translators: 1: site name, 2: number of AI crawler hits. */ $subject = sprintf( __( '[%1$s] aeoora report: %2$s AI crawler visits', 'aeoora' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), number_format_i18n( $o['kpis']['hits'] ) ); $sent = wp_mail( $to, $subject, self::html( $o, $days[ $freq ] ), array( 'Content-Type: text/html; charset=UTF-8' ) ); if ( $sent ) { update_option( 'aeoora_report_last', time(), false ); } return $sent; } /** * Report HTML. * * @param array $o Overview. * @param int $days Days. * @return string */ private static function html( array $o, $days ) { $k = $o['kpis']; $row = static function ( $label, $value ) { return '' . esc_html( $label ) . '' . esc_html( $value ) . ''; }; $html = '
'; $html .= '

aeoora

' . esc_html( sprintf( /* translators: 1: days, 2: site. */ __( 'Last %1$d days on %2$s', 'aeoora' ), $days, home_url() ) ) . '

'; $html .= ''; $html .= $row( __( 'AI crawler visits', 'aeoora' ), number_format_i18n( $k['hits'] ) ); $html .= $row( __( 'Distinct AI crawlers', 'aeoora' ), number_format_i18n( $k['bots'] ) ); $html .= $row( __( 'Pages crawled by AI', 'aeoora' ), number_format_i18n( $k['pages'] ) ); $html .= $row( __( 'Visitors from AI assistants', 'aeoora' ), number_format_i18n( $k['referrals'] ) ); $html .= $row( __( 'Site AEO score', 'aeoora' ), null === $k['site_score'] ? '—' : $k['site_score'] . '/100' ); $html .= $row( __( 'Pending suggestions', 'aeoora' ), number_format_i18n( $k['pending'] ) ); $html .= '
'; if ( $o['top_bots'] ) { $html .= '

' . esc_html__( 'Top AI crawlers', 'aeoora' ) . '

'; foreach ( array_slice( $o['top_bots'], 0, 8 ) as $b ) { $html .= $row( $b['name'] . ( $b['operator'] ? ' (' . $b['operator'] . ')' : '' ), number_format_i18n( $b['hits'] ) ); } $html .= '
'; } if ( $o['referrers'] ) { $html .= '

' . esc_html__( 'AI referrals by assistant', 'aeoora' ) . '

'; foreach ( array_slice( $o['referrers'], 0, 8 ) as $r ) { $html .= $row( $r['label'], number_format_i18n( $r['hits'] ) ); } $html .= '
'; } $html .= '

' . esc_html__( 'Open aeoora dashboard', 'aeoora' ) . '

'; $html .= '

' . esc_html__( 'Sent by aeoora, the free AEO toolkit for WordPress. Change or stop these emails in aeoora → Settings → Email reports.', 'aeoora' ) . '

'; return $html; } /** * WP dashboard widget. */ public static function widget() { if ( current_user_can( 'manage_options' ) ) { wp_add_dashboard_widget( 'aeoora_widget', __( 'aeoora · AI visibility', 'aeoora' ), array( __CLASS__, 'render_widget' ), array( __CLASS__, 'widget_control' ) ); add_action( 'admin_print_styles-index.php', array( __CLASS__, 'widget_styles' ) ); // First in the column, unless the user has arranged their widgets already // (a saved order in user meta always wins over this). global $wp_meta_boxes; if ( isset( $wp_meta_boxes['dashboard']['normal']['core']['aeoora_widget'] ) ) { $ours = array( 'aeoora_widget' => $wp_meta_boxes['dashboard']['normal']['core']['aeoora_widget'] ); $wp_meta_boxes['dashboard']['normal']['core'] = $ours + $wp_meta_boxes['dashboard']['normal']['core']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited } } } /** * Widget stylesheet (only on wp-admin/index.php). */ public static function widget_styles() { wp_enqueue_style( 'aeoora-widget', AEOORA_URL . 'assets/css/widget.css', array(), AEOORA_VERSION ); } /** * Period chosen by the current user. * * @return int */ private static function widget_days() { $days = (int) get_user_meta( get_current_user_id(), 'aeoora_widget_days', true ); return in_array( $days, array( 7, 30, 90 ), true ) ? $days : 7; } /** * "Configure" form for the widget. */ public static function widget_control() { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- core verifies the dashboard-widget nonce before calling this. if ( isset( $_POST['aeoora_widget_days'] ) ) { $days = absint( wp_unslash( $_POST['aeoora_widget_days'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing update_user_meta( get_current_user_id(), 'aeoora_widget_days', in_array( $days, array( 7, 30, 90 ), true ) ? $days : 7 ); } $days = self::widget_days(); echo '

'; } /** * Data for the widget, cached for 10 minutes per period. * * @param int $days Days. * @return array */ private static function widget_data( $days ) { $key = 'aeoora_widget_' . $days; $o = get_transient( $key ); if ( is_array( $o ) && isset( $o['v'] ) && 2 === $o['v'] ) { return $o; } $ov = Aeoora_Stats::overview( $days, 'ai' ); $o = array( 'v' => 2, 'at' => time(), 'kpis' => $ov['kpis'], 'timeline' => array_map( static function ( $d ) { return array( $d['day'], (int) $d['total'], (int) $d['referrals'] ); }, $ov['timeline'] ), 'bots' => array_slice( $ov['top_bots'], 0, 4 ), 'referrers' => array_slice( $ov['referrers'], 0, 4 ), 'violations' => count( Aeoora_Stats::violations( 7 ) ), 'setup' => Aeoora_Setup::status(), ); set_transient( $key, $o, 10 * MINUTE_IN_SECONDS ); return $o; } /** * Print the change against the previous period, as a small badge. * * @param int $now This period. * @param int $prev Previous period. */ private static function print_delta( $now, $prev ) { if ( $prev <= 0 ) { if ( $now > 0 ) { echo '' . esc_html__( 'new', 'aeoora' ) . ''; } return; } $pct = (int) round( ( $now - $prev ) / $prev * 100 ); if ( 0 === $pct ) { echo '' . esc_html( '±0%' ) . ''; return; } $up = $pct > 0; printf( ' %3$s %4$s', esc_attr( $up ? 'is-up' : 'is-down' ), esc_html( $up ? '▲' : '▼' ), esc_html( number_format_i18n( abs( $pct ) ) . '%' ), esc_html( $up ? __( 'more than the previous period', 'aeoora' ) : __( 'less than the previous period', 'aeoora' ) ) ); } /** * Print daily bars (crawls) with a referral line, as inline SVG. * * @param array $timeline [ [ day, crawls, referrals ], … ]. */ private static function print_spark( array $timeline ) { $n = count( $timeline ); if ( ! $n ) { return; } $w = 300; $h = 56; $max_c = max( 1, max( array_column( $timeline, 1 ) ) ); $max_r = max( 1, max( array_column( $timeline, 2 ) ) ); $slot = $w / $n; $bar = max( 1, $slot * 0.62 ); $points = array(); foreach ( $timeline as $i => $d ) { $points[] = round( $i * $slot + $slot / 2, 1 ) . ',' . round( $h - 2 - $d[2] / $max_r * ( $h - 8 ), 1 ); } $crawls = array_sum( array_column( $timeline, 1 ) ); $refs = array_sum( array_column( $timeline, 2 ) ); /* translators: 1: number of days, 2: number of AI crawler visits, 3: number of visitors from AI assistants. */ $label = sprintf( __( 'Last %1$d days: %2$s AI crawler visits and %3$s visitors from AI assistants.', 'aeoora' ), $n, number_format_i18n( $crawls ), number_format_i18n( $refs ) ); printf( '
', esc_attr( '0 0 ' . $w . ' ' . $h ), esc_attr( $label ) ); foreach ( $timeline as $i => $d ) { $bh = max( 1, round( $d[1] / $max_c * ( $h - 4 ), 1 ) ); printf( '', esc_attr( round( $i * $slot + ( $slot - $bar ) / 2, 1 ) ), esc_attr( round( $h - $bh, 1 ) ), esc_attr( round( $bar, 1 ) ), esc_attr( $bh ) ); } echo ''; if ( $refs ) { printf( '', esc_attr( implode( ' ', $points ) ) ); } echo '
' . esc_html__( 'AI crawler visits', 'aeoora' ) . ''; if ( $refs ) { echo '' . esc_html__( 'Visitors from AI', 'aeoora' ) . ''; } echo '' . esc_html( date_i18n( 'M j', strtotime( $timeline[0][0] ) ) . ' – ' . date_i18n( 'M j', strtotime( $timeline[ $n - 1 ][0] ) ) ) . '
'; } /** * Print the score ring. * * @param int|null $score Score. */ private static function print_ring( $score ) { $has = null !== $score; $v = $has ? max( 0, min( 100, (int) $score ) ) : 0; $c = 2 * M_PI * 20; $class = ! $has ? 'is-none' : ( $v >= 80 ? 'is-good' : ( $v >= 55 ? 'is-ok' : 'is-bad' ) ); /* translators: %d: score from 0 to 100. */ $label = $has ? sprintf( __( 'Site AEO score %d out of 100', 'aeoora' ), $v ) : __( 'Site not audited yet', 'aeoora' ); printf( '%4$s', esc_attr( $class ), esc_attr( $label ), esc_attr( round( $v / 100 * $c, 2 ) . ' ' . round( $c, 2 ) ), esc_html( $has ? (string) $v : '–' ) ); } /** * Print a ranked list with logos and proportional bars. * * @param string $title Title. * @param array $rows Rows (label, sub, icon, hits, color). * @param string $more Link. */ private static function print_ranked( $title, array $rows, $more ) { $max = max( 1, max( array_column( $rows, 'hits' ) ) ); printf( '

%2$s

    ', esc_url( $more ), esc_html( $title ) ); foreach ( $rows as $r ) { echo '
  1. '; if ( $r['icon'] ) { printf( '', esc_url( $r['icon'] ) ); } else { echo ''; } printf( '%2$s%3$s
  2. ', esc_attr( $r['sub'] ), esc_html( $r['label'] ), esc_html( number_format_i18n( $r['hits'] ) ), esc_attr( '--w:' . max( 4, round( $r['hits'] / $max * 100 ) ) . '%;--c:' . sanitize_hex_color( $r['color'] ) ) ); } echo '
'; } /** * Render widget. */ public static function render_widget() { $days = self::widget_days(); $o = self::widget_data( $days ); $k = $o['kpis']; $admin = admin_url( 'admin.php?page=' ); $setup = $o['setup']; $empty = 0 === (int) $k['hits'] && 0 === (int) $k['referrals']; echo '
'; if ( empty( $setup['completed'] ) && $setup['done'] < $setup['total'] ) { printf( '%2$s %3$s', esc_url( $setup['url'] ), esc_html__( 'Finish setting up aeoora', 'aeoora' ), /* translators: 1: steps done, 2: total steps. */ esc_html( sprintf( __( '%1$d of %2$d steps done · about 2 minutes', 'aeoora' ), $setup['done'], $setup['total'] ) ), (int) round( $setup['done'] / max( 1, $setup['total'] ) * 100 ) ); } // KPIs. echo '
'; printf( '%2$s%3$s', esc_url( $admin . 'aeoora-crawlers' ), esc_html__( 'AI crawler visits', 'aeoora' ), esc_html( number_format_i18n( $k['hits'] ) ) ); self::print_delta( (int) $k['hits'], (int) $k['hits_prev'] ); echo ''; printf( '%2$s%3$s', esc_url( $admin . 'aeoora-referrals' ), esc_html__( 'Visitors from AI', 'aeoora' ), esc_html( number_format_i18n( $k['referrals'] ) ) ); self::print_delta( (int) $k['referrals'], (int) $k['referrals_prev'] ); echo ''; printf( '', esc_url( $admin . 'aeoora-audit' ) ); self::print_ring( $k['site_score'] ); printf( '%1$s%2$s', esc_html__( 'AEO score', 'aeoora' ), null === $k['content_score'] ? esc_html__( 'Run the audit', 'aeoora' ) : esc_html( /* translators: %d: average content score from 0 to 100. */ sprintf( __( 'Content %d/100', 'aeoora' ), (int) $k['content_score'] ) ) ); echo '
'; if ( $empty ) { echo '

' . esc_html__( 'Tracking is live.', 'aeoora' ) . ' ' . esc_html__( 'AI crawlers usually show up within a few days. Visitors from ChatGPT, Perplexity, Gemini and others appear as soon as an assistant links to you.', 'aeoora' ) . '

'; } else { self::print_spark( $o['timeline'] ); if ( $o['referrers'] || $o['bots'] ) { echo '
'; if ( $o['referrers'] ) { self::print_ranked( __( 'Top AI assistants', 'aeoora' ), array_map( static function ( $r ) { return array( 'label' => $r['label'], 'sub' => $r['vendor'], 'icon' => $r['icon'], 'hits' => (int) $r['hits'], 'color' => $r['color'], ); }, $o['referrers'] ), $admin . 'aeoora-referrals' ); } if ( $o['bots'] ) { self::print_ranked( __( 'Busiest AI crawlers', 'aeoora' ), array_map( static function ( $b ) { return array( 'label' => $b['name'], 'sub' => $b['operator'], 'icon' => $b['icon'], 'hits' => (int) $b['hits'], 'color' => '#635bff', ); }, $o['bots'] ), $admin . 'aeoora-crawlers' ); } echo '
'; } } // What needs attention, most important first. $todo = array(); if ( $o['violations'] ) { $todo[] = array( 'bad', /* translators: %d: number of crawlers. */ sprintf( _n( '%d crawler ignored your robots.txt rules this week', '%d crawlers ignored your robots.txt rules this week', $o['violations'], 'aeoora' ), $o['violations'] ), $admin . 'aeoora-crawlers', __( 'Block them', 'aeoora' ), ); } if ( null === $k['site_score'] ) { $todo[] = array( 'warn', __( 'Your site has not been audited yet', 'aeoora' ), $admin . 'aeoora-audit', __( 'Run audit', 'aeoora' ) ); } elseif ( $k['site_score'] < 70 ) { $todo[] = array( 'warn', __( 'Your site is missing easy AEO wins', 'aeoora' ), $admin . 'aeoora-audit', __( 'See fixes', 'aeoora' ) ); } if ( $k['pending'] ) { $todo[] = array( 'info', /* translators: %d: number of suggestions. */ sprintf( _n( '%d suggestion ready to apply', '%d suggestions ready to apply', $k['pending'], 'aeoora' ), $k['pending'] ), $admin . 'aeoora-suggestions', __( 'Review', 'aeoora' ), ); } if ( 0 === (int) get_option( 'aeoora_loopback_ok', 1 ) ) { $todo[] = array( 'warn', __( 'Your server can’t load its own pages, so page-builder pages are scored from saved content', 'aeoora' ), admin_url( 'site-health.php' ), __( 'Site Health', 'aeoora' ) ); } echo ''; printf( '
%2$sllms.txt %4$s%5$s
', esc_url( $admin . 'aeoora' ), esc_html__( 'Open aeoora', 'aeoora' ), esc_url( home_url( '/llms.txt' ) ), esc_html__( '(opens in a new tab)', 'aeoora' ), /* translators: 1: number of days, 2: relative time, e.g. "5 mins". */ esc_html( sprintf( __( 'Last %1$d days · updated %2$s ago', 'aeoora' ), $days, human_time_diff( (int) $o['at'] ) ) ) ); echo '
'; } }