agent name, in match order. */ function howseen_ai_agents() { return array( 'chatgpt-user' => 'ChatGPT-User', 'oai-searchbot' => 'OAI-SearchBot', 'gptbot' => 'GPTBot', 'claude-user' => 'Claude-User', 'claude-searchbot' => 'Claude-SearchBot', 'claudebot' => 'ClaudeBot', 'perplexity-user' => 'Perplexity-User', 'perplexitybot' => 'PerplexityBot', 'mistralai-user' => 'MistralAI-User', 'meta-externalfetcher' => 'Meta-ExternalFetcher', 'meta-externalagent' => 'Meta-ExternalAgent', 'duckassistbot' => 'DuckAssistBot', 'ccbot' => 'CCBot', ); } /** At most this many day x page x agent counters are kept between two sends. */ define( 'HOWSEEN_READS_MAX_KEYS', 1000 ); // = what one send carries (WP_PLUGIN_READS_MAX): every counter is sent. /** The agent declared by this request's user agent, or ''. */ function howseen_detect_agent( $ua ) { $ua = strtolower( (string) $ua ); if ( '' === $ua ) { return ''; } foreach ( howseen_ai_agents() as $token => $agent ) { if ( false !== strpos( $ua, $token ) ) { return $agent; } } return ''; } /** template_redirect: counts the visit when an AI agent asked for an existing page of the site. */ function howseen_count_ai_visit() { if ( ! howseen_paired() || is_admin() || is_404() || is_preview() ) { return; } $agent = howseen_detect_agent( isset( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- compared only, never stored. if ( '' === $agent ) { return; // A person (or another robot): nothing more happens. } $uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- reduced to a path below. $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); $path = '/' . ltrim( preg_replace( '/[^\x21-\x7E]/', '', $path ), '/' ); $path = substr( $path, 0, 290 ); $key = gmdate( 'Y-m-d' ) . '|' . $path . '|' . $agent; $all = get_option( 'howseen_reads', array() ); if ( ! is_array( $all ) ) { $all = array(); } if ( ! isset( $all[ $key ] ) && count( $all ) >= HOWSEEN_READS_MAX_KEYS ) { return; } $all[ $key ] = isset( $all[ $key ] ) ? (int) $all[ $key ] + 1 : 1; update_option( 'howseen_reads', $all, false ); } /** WP-Cron, hourly: sends the running totals of the last 7 days (what Howseen accepts), newest first; once Howseen * has them, days before yesterday are dropped. */ function howseen_send_reads() { $token = howseen_token(); $all = get_option( 'howseen_reads', array() ); if ( ! howseen_paired() || ! howseen_token_valid_format( $token ) || ! is_array( $all ) || ! $all ) { return; } $since = gmdate( 'Y-m-d', time() - 6 * DAY_IN_SECONDS ); $rows = array(); foreach ( $all as $key => $hits ) { $parts = explode( '|', (string) $key, 3 ); if ( 3 === count( $parts ) && $parts[0] >= $since ) { $rows[] = array( 'day' => $parts[0], 'path' => $parts[1], 'agent' => $parts[2], 'hits' => (int) $hits, ); } } usort( $rows, function ( $a, $b ) { return strcmp( $b['day'], $a['day'] ); } ); $keep_from = gmdate( 'Y-m-d', time() - DAY_IN_SECONDS ); $sent = true; if ( $rows ) { $body = wp_json_encode( array( 'token_id' => substr( $token, 0, strrpos( $token, '.' ) ), 'reads' => array_slice( $rows, 0, 1000 ), ) ); $ts = (string) time(); $nonce = wp_generate_password( 32, false, false ); $res = wp_remote_post( HOWSEEN_APP_URL . HOWSEEN_READS_ROUTE, array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json', 'X-Howseen-Timestamp' => $ts, 'X-Howseen-Nonce' => $nonce, 'X-Howseen-Signature' => howseen_signature( $token, $ts, $nonce, 'POST', HOWSEEN_READS_ROUTE, $body ), ), 'body' => $body, ) ); $sent = ! is_wp_error( $res ) && 200 === (int) wp_remote_retrieve_response_code( $res ); update_option( 'howseen_reads_last', array( 'at' => time(), 'ok' => $sent, 'code' => is_wp_error( $res ) ? $res->get_error_message() : (int) wp_remote_retrieve_response_code( $res ), ), false ); } // Today and yesterday stay (their totals keep growing). Older days go once sent, and after 7 days in any case. $drop_before = $sent ? $keep_from : gmdate( 'Y-m-d', time() - 7 * DAY_IN_SECONDS ); $fresh = get_option( 'howseen_reads', array() ); // re-read: visits counted while we were sending stay. if ( is_array( $fresh ) ) { foreach ( array_keys( $fresh ) as $key ) { if ( substr( (string) $key, 0, 10 ) < $drop_before ) { unset( $fresh[ $key ] ); } } update_option( 'howseen_reads', $fresh, false ); } } /** The hourly send is scheduled while a token is set (activation, or a site connected before this version). */ function howseen_schedule_reads() { if ( howseen_paired() && ! wp_next_scheduled( 'howseen_send_reads' ) ) { wp_schedule_event( time() + 300, 'hourly', 'howseen_send_reads' ); } }