' . esc_html__( 'Readiness score', 'agentrue' ) . '' ); return $links; } public static function url( $tab = 'traffic', $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::SLUG, 'tab' => $tab ), $args ), admin_url( 'admin.php' ) ); } public static function menu() { add_submenu_page( 'woocommerce', 'Agentrue', 'Agentrue', 'manage_woocommerce', self::SLUG, array( __CLASS__, 'render' ) ); } public static function assets( $hook ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $is_order = $screen && ( 'woocommerce_page_wc-orders' === $screen->id || 'shop_order' === $screen->id || 'edit-shop_order' === $screen->id ); if ( false === strpos( (string) $hook, self::SLUG ) && ! $is_order ) { return; } wp_enqueue_style( 'agentrue-admin', AGENTRUE_URL . 'assets/admin.css', array(), AGENTRUE_VERSION ); } public static function handle_scan() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( 'Forbidden', 403 ); } check_admin_referer( 'agentrue_scan' ); Agentrue_Scanner::run(); wp_safe_redirect( self::url( 'readiness', array( 'scanned' => 1 ) ) ); exit; } public static function handle_settings() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( 'Forbidden', 403 ); } check_admin_referer( 'agentrue_settings' ); $values = isset( $_POST['agentrue'] ) ? (array) wp_unslash( $_POST['agentrue'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- sanitized in save(). $clean = array(); foreach ( $values as $k => $v ) { $clean[ sanitize_key( $k ) ] = 'operators' === $k ? sanitize_textarea_field( (string) $v ) : sanitize_text_field( (string) $v ); } Agentrue_Settings::save( $clean ); Agentrue_Feed::add_rewrites(); flush_rewrite_rules(); Agentrue_Scanner::run(); wp_safe_redirect( self::url( 'settings', array( 'saved' => 1 ) ) ); exit; } public static function handle_selftest() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( 'Forbidden', 403 ); } check_admin_referer( 'agentrue_selftest' ); $r = Agentrue_Gate::self_test(); set_transient( 'agentrue_selftest_' . get_current_user_id(), $r, 60 ); wp_safe_redirect( self::url( 'traffic' ) ); exit; } public static function handle_clear_log() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( 'Forbidden', 403 ); } check_admin_referer( 'agentrue_clear_log' ); delete_option( Agentrue_Gate::LOG_OPTION ); wp_safe_redirect( self::url( 'traffic' ) ); exit; } protected static function badge( $status ) { $labels = Agentrue_Gate::status_labels(); $label = isset( $labels[ $status ] ) ? $labels[ $status ] : $status; return '' . esc_html( $label ) . ''; } protected static function post_button( $action, $label, $class = 'secondary' ) { echo '
'; wp_nonce_field( $action ); echo ''; submit_button( $label, $class, 'submit', false ); echo '
'; } protected static function render_traffic() { $test = get_transient( 'agentrue_selftest_' . get_current_user_id() ); if ( $test ) { delete_transient( 'agentrue_selftest_' . get_current_user_id() ); printf( '

%s

', $test['ok'] ? 'success' : 'error', esc_html( $test['message'] ) ); } $mode = Agentrue_Settings::get( 'mode' ); $sum = Agentrue_Gate::summary( 7 ); $st = $sum['by_status']; $get = function ( $k ) use ( $st ) { return isset( $st[ $k ] ) ? (int) $st[ $k ] : 0; }; echo '
'; echo '
' . esc_html__( 'Mode:', 'agentrue' ) . ' '; echo 'enforce' === $mode ? '' . esc_html__( 'Enforce – forged agents are blocked', 'agentrue' ) . '' : '' . esc_html__( 'Monitor – nothing is blocked, everything is logged', 'agentrue' ) . ''; echo '
'; self::post_button( 'agentrue_selftest', __( 'Run verification self-test', 'agentrue' ) ); echo '' . esc_html__( 'Change mode', 'agentrue' ) . ''; echo '
'; $cards = array( array( __( 'Verified agents', 'agentrue' ), $get( 'verified' ), 'verified' ), array( __( 'Forged signatures', 'agentrue' ), $get( 'invalid' ), 'invalid' ), array( __( 'Unsigned impostors', 'agentrue' ), $get( 'impostor_ua' ), 'impostor_ua' ), array( __( 'Blocked requests', 'agentrue' ), (int) $sum['blocked'], 'human' ), ); foreach ( $cards as $c ) { echo '
' . esc_html( $c[0] ) . ' · ' . esc_html__( '7 days', 'agentrue' ) . '' . esc_html( $c[1] ) . '
'; } echo '

' . esc_html__( 'Recent agent requests', 'agentrue' ) . '

'; $log = array_slice( Agentrue_Gate::log_entries(), 0, 50 ); if ( empty( $log ) ) { echo '

' . esc_html__( 'No AI agent traffic yet. When ChatGPT agent or another signed agent visits your store, each request is checked against the operator\'s published key and listed here.', 'agentrue' ) . '

'; } else { echo ''; foreach ( $log as $e ) { printf( '', esc_html( sprintf( /* translators: %s time */ __( '%s ago', 'agentrue' ), human_time_diff( $e['t'] ) ) ), self::badge( $e['status'] ), // phpcs:ignore WordPress.Security.EscapeOutput ! empty( $e['blocked'] ) ? ' ' . esc_html__( 'blocked', 'agentrue' ) . '' : '', esc_html( $e['label'] ), esc_html( strtoupper( $e['method'] ) ), esc_html( $e['path'] ), esc_html( $e['reason'] ) ); } echo '
' . esc_html__( 'When', 'agentrue' ) . '' . esc_html__( 'Status', 'agentrue' ) . '' . esc_html__( 'Agent', 'agentrue' ) . '' . esc_html__( 'Request', 'agentrue' ) . '' . esc_html__( 'Detail', 'agentrue' ) . '
%s%s%s%s%s %s%s
'; self::post_button( 'agentrue_clear_log', __( 'Clear log', 'agentrue' ) ); } echo '
'; echo '

' . esc_html__( 'How verification works', 'agentrue' ) . '

    '; echo '
  1. ' . esc_html__( 'AI agents such as ChatGPT agent sign every request with a private key (Web Bot Auth, RFC 9421).', 'agentrue' ) . '
  2. '; echo '
  3. ' . esc_html__( 'Agentrue fetches the operator\'s public key from its published directory and checks the signature on your own server. No third-party service sees your traffic.', 'agentrue' ) . '
  4. '; echo '
  5. ' . esc_html__( 'Orders are labelled Verified agent, Forged signature or Unsigned impostor, so you can fulfil real agent orders with confidence.', 'agentrue' ) . '
  6. '; echo '
  7. ' . esc_html__( 'Developers: call agentrue_is_verified_agent() to skip CAPTCHA or bot challenges for verified agents only.', 'agentrue' ) . '
  8. '; echo '
'; echo '
'; } public static function order_column( $columns ) { $new = array(); foreach ( $columns as $k => $v ) { $new[ $k ] = $v; if ( 'order_status' === $k ) { $new['agentrue'] = __( 'Agent', 'agentrue' ); } } if ( ! isset( $new['agentrue'] ) ) { $new['agentrue'] = __( 'Agent', 'agentrue' ); } return $new; } public static function order_column_value( $column, $order ) { if ( 'agentrue' !== $column || ! $order instanceof WC_Order ) { return; } $status = $order->get_meta( '_agentrue_status' ); if ( $status && 'human' !== $status ) { echo self::badge( $status ); // phpcs:ignore WordPress.Security.EscapeOutput $label = $order->get_meta( '_agentrue_label' ); if ( $label ) { echo '
' . esc_html( $label ) . ''; } } else { echo '–'; } } public static function order_column_value_legacy( $column, $post_id ) { self::order_column_value( $column, wc_get_order( $post_id ) ); } public static function order_meta_box() { $screen = function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; add_meta_box( 'agentrue-agent', __( 'AI agent check', 'agentrue' ), array( __CLASS__, 'render_order_box' ), $screen, 'side', 'high' ); } public static function render_order_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order ) { return; } $status = $order->get_meta( '_agentrue_status' ); if ( ! $status ) { echo '

' . esc_html__( 'Placed before Agentrue was active.', 'agentrue' ) . '

'; return; } echo '

' . self::badge( $status ) . '

'; // phpcs:ignore WordPress.Security.EscapeOutput if ( 'human' !== $status ) { echo '

' . esc_html( $order->get_meta( '_agentrue_label' ) ) . '
' . esc_html( $order->get_meta( '_agentrue_reason' ) ) . '

'; } } /* ------------------------------------------------------------------ */ public static function render() { $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'traffic'; // phpcs:ignore WordPress.Security.NonceVerification $tabs = array( 'traffic' => __( 'Agent traffic', 'agentrue' ), 'readiness' => __( 'Readiness', 'agentrue' ), 'fixes' => __( 'Fix list', 'agentrue' ), 'sales' => __( 'AI sales', 'agentrue' ), 'settings' => __( 'Settings', 'agentrue' ), ); echo '
'; echo '

Agentrue ' . esc_html__( 'Verified AI agents for WooCommerce', 'agentrue' ) . '

'; echo ''; switch ( $tab ) { case 'fixes': self::render_fixes(); break; case 'sales': self::render_sales(); break; case 'settings': self::render_settings(); break; case 'readiness': self::render_overview(); break; default: self::render_traffic(); } echo '
'; } protected static function scan_button( $label = null ) { echo '
'; wp_nonce_field( 'agentrue_scan' ); echo ''; submit_button( $label ? $label : __( 'Run scan now', 'agentrue' ), 'primary', 'submit', false ); echo '
'; } protected static function render_overview() { $scan = Agentrue_Scanner::last(); if ( ! $scan ) { echo '

' . esc_html__( 'Is your store ready for AI shoppers?', 'agentrue' ) . '

'; echo '

' . esc_html__( 'ChatGPT, Gemini and Perplexity now research and buy products for people. Run a scan to see how visible and buyable your store is to these agents.', 'agentrue' ) . '

'; self::scan_button( __( 'Scan my store', 'agentrue' ) ); echo '
'; return; } $score = (int) $scan['score']; $color = $score >= 85 ? '#1a7f37' : ( $score >= 70 ? '#4a7c00' : ( $score >= 50 ? '#b26a00' : '#b42318' ) ); echo '
'; echo '
'; printf( '
%1$d/100
', $score, // phpcs:ignore WordPress.Security.EscapeOutput esc_attr( $color ) ); echo '

' . esc_html( sprintf( /* translators: %s grade */ __( 'Grade %s', 'agentrue' ), $scan['grade'] ) ) . '

'; echo '

' . esc_html( sprintf( /* translators: 1 catalog score 2 store score */ __( 'Catalog data %1$d · Store setup %2$d', 'agentrue' ), $scan['catalog']['score'], $scan['store']['score'] ) ) . '

'; echo '

' . esc_html( sprintf( /* translators: 1 products 2 time */ __( '%1$d products scanned · %2$s ago', 'agentrue' ), $scan['catalog']['product_count'], human_time_diff( $scan['scanned_at'] ) ) ) . '

'; self::scan_button( __( 'Rescan', 'agentrue' ) ); echo '
'; echo '

' . esc_html__( 'Store setup', 'agentrue' ) . '

'; if ( ! empty( $scan['store']['blocked_bots'] ) ) { echo '

' . esc_html__( 'Blocked bots:', 'agentrue' ) . ' ' . esc_html( implode( ', ', $scan['store']['blocked_bots'] ) ) . '

'; } echo '
'; echo '

' . esc_html__( 'Catalog coverage', 'agentrue' ) . '

'; $total = max( 1, (int) $scan['catalog']['product_count'] ); foreach ( Agentrue_Scanner::product_checks() as $key => $def ) { $pct = (int) round( 100 * $scan['catalog']['passed'][ $key ] / $total ); printf( '', esc_html( $def[0] ), $pct, // phpcs:ignore WordPress.Security.EscapeOutput $pct // phpcs:ignore WordPress.Security.EscapeOutput ); } echo '
%s
%d%%
'; echo '

' . esc_html__( 'Top fixes', 'agentrue' ) . '

'; self::fix_list( array_slice( $scan['fixes'], 0, 5 ), false ); echo '

' . esc_html__( 'See all fixes →', 'agentrue' ) . '

'; echo '

' . esc_html__( 'What AI agents see', 'agentrue' ) . '

'; echo '
'; } protected static function fix_list( $fixes, $show_products = true ) { if ( empty( $fixes ) ) { echo '

' . esc_html__( 'Nothing to fix. Your store is agent-ready.', 'agentrue' ) . '

'; return; } echo '
    '; foreach ( $fixes as $fix ) { echo '
  1. '; echo '' . esc_html( ucfirst( $fix['impact'] ) ) . ' '; echo '' . esc_html( $fix['title'] ) . ''; echo '

    ' . esc_html( $fix['help'] ) . '

    '; if ( $show_products && ! empty( $fix['products'] ) ) { echo '

    '; foreach ( $fix['products'] as $pid ) { printf( '%s ', esc_url( get_edit_post_link( $pid ) ), esc_html( get_the_title( $pid ) ) ); } echo '

    '; } echo '
  2. '; } echo '
'; } protected static function render_fixes() { $scan = Agentrue_Scanner::last(); if ( ! $scan ) { self::scan_button( __( 'Scan my store', 'agentrue' ) ); return; } echo '
'; self::fix_list( $scan['fixes'], true ); echo '
'; } protected static function render_sales() { $r = Agentrue_Analytics::report( 90 ); $cur = $r['currency']; $pct = $r['revenue_total'] > 0 ? round( 100 * $r['revenue_ai'] / $r['revenue_total'], 1 ) : 0; echo '
'; echo '
' . esc_html__( 'AI-driven revenue (90 days)', 'agentrue' ) . '' . wp_kses_post( wc_price( $r['revenue_ai'], array( 'currency' => $cur ) ) ) . '
'; echo '
' . esc_html__( 'AI-driven orders', 'agentrue' ) . '' . esc_html( $r['orders_ai'] ) . '
'; echo '
' . esc_html__( 'Share of revenue', 'agentrue' ) . '' . esc_html( $pct ) . '%
'; echo '

' . esc_html__( 'By assistant', 'agentrue' ) . '

'; if ( empty( $r['by_assistant'] ) ) { echo '

' . esc_html__( 'No AI-driven orders yet. Agentrue reads WooCommerce order attribution, so past orders from ChatGPT, Perplexity, Gemini and others appear here automatically.', 'agentrue' ) . '

'; } else { echo ''; foreach ( $r['by_assistant'] as $name => $row ) { printf( '', esc_html( $name ), (int) $row['orders'], wp_kses_post( wc_price( $row['revenue'], array( 'currency' => $cur ) ) ) ); } echo '
' . esc_html__( 'Assistant', 'agentrue' ) . '' . esc_html__( 'Orders', 'agentrue' ) . '' . esc_html__( 'Revenue', 'agentrue' ) . '
%s%d%s
'; echo '

' . esc_html__( 'Recent AI orders', 'agentrue' ) . '

'; foreach ( $r['recent'] as $o ) { $order = wc_get_order( $o['id'] ); printf( '', esc_url( $order ? $order->get_edit_order_url() : '#' ), (int) $o['id'], esc_html( $o['date'] ), esc_html( $o['assistant'] ), wp_kses_post( wc_price( $o['total'], array( 'currency' => $cur ) ) ) ); } echo '
#%d%s%s%s
'; } echo '
'; } protected static function render_settings() { if ( isset( $_GET['saved'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification echo '

' . esc_html__( 'Settings saved.', 'agentrue' ) . '

'; } $s = Agentrue_Settings::all(); echo '
'; wp_nonce_field( 'agentrue_settings' ); echo ''; echo '

' . esc_html__( 'Agent verification', 'agentrue' ) . '

'; printf( '', esc_html__( 'Mode', 'agentrue' ), checked( 'monitor', $s['mode'], false ), esc_html__( 'Monitor – log and label orders, never block (recommended to start)', 'agentrue' ), checked( 'enforce', $s['mode'], false ), esc_html__( 'Enforce – block requests with forged or expired agent signatures', 'agentrue' ) ); self::checkbox_row( 'block_impostor_checkout', __( 'Impostors at checkout', 'agentrue' ), __( 'In Enforce mode, also block unsigned requests that claim to be an AI agent (for example a fake "ChatGPT-User") when they try to check out.', 'agentrue' ), $s ); $ops = array(); foreach ( Agentrue_Gate::operators() as $origin => $name ) { $ops[] = esc_html( $name . ' (' . $origin . ')' ); } printf( '', esc_html__( 'Trusted agent operators', 'agentrue' ), wp_kses_post( implode( '
', $ops ) ), esc_textarea( $s['operators'] ), esc_html__( 'Add more operators, one per line: https://origin | Display name. Agentrue fetches each operator\'s public keys from its /.well-known/http-message-signatures-directory.', 'agentrue' ) ); self::checkbox_row( 'verify_any', __( 'Verify any operator', 'agentrue' ), __( 'Also verify signed agents from operators not listed above (fetches their key directory on first visit).', 'agentrue' ), $s ); echo '
%s
%s

%s

%s

' . esc_html__( 'Agent-ready data', 'agentrue' ) . '

'; self::checkbox_row( 'feed_enabled', __( 'Agent product feed', 'agentrue' ), __( 'Publish a JSON catalog and a Google Merchant–compatible XML feed for AI agents.', 'agentrue' ), $s ); self::checkbox_row( 'llms_txt_enabled', 'llms.txt', __( 'Publish /llms.txt describing your store and where agents find your data.', 'agentrue' ), $s ); self::checkbox_row( 'allow_ai_bots', __( 'Allow AI shopping bots', 'agentrue' ), __( 'Add explicit Allow rules for AI crawlers and agents to your virtual robots.txt.', 'agentrue' ), $s ); echo '
'; submit_button( __( 'Save settings', 'agentrue' ) ); echo '
'; } protected static function checkbox_row( $key, $label, $desc, $s ) { printf( '%1$s', esc_html( $label ), esc_attr( $key ), checked( 'yes', $s[ $key ], false ), esc_html( $desc ) ); } }