ction open_frame( string $ability_name, bool $executing ): void { // Consent is per site, and the site can change mid-request with switch_to_blog(). if ( ! self::is_tracking_allowed() ) { return; } // Only agent entry points. Direct PHP and cron calls are plugin internals and could add a pixel to every page view. if ( ! in_array( $this->get_channel(), self::TRACKED_CHANNELS, true ) ) { return; } // Calls that fail without a closing hook keep their frame until shutdown. Recording the oldest one as an // unknown failure keeps the stack bounded and lets later closing hooks still find their own frame. if ( count( $this->frames ) >= self::MAX_EVENTS_PER_REQUEST ) { $oldest = $this->frames[0]; array_shift( $this->frames ); $this->add_event( $oldest, null, false ); } $this->frames[] = $this->create_frame( $ability_name, $executing ); ++$this->frames_opened; } /** * Create the record of an invocation, with the context it started in. * * @param string $ability_name The ability name. * @param bool $executing Whether execution already started. * @return array{name: string, channel: string, rest_namespace: string, external_client: string, start: float, executing: bool} */ private function create_frame( string $ability_name, bool $executing ): array { $channel = $this->get_channel(); return array( 'name' => $ability_name, 'channel' => $channel, 'rest_namespace' => $this->get_rest_namespace(), // Resolved now: the request metadata it reads is gone once the REST request finishes. 'external_client' => 'mcp' === $channel ? $this->get_external_client() : self::NONE, 'start' => microtime( true ), 'executing' => $executing, ); } /** * Finish tracking the innermost open invocation of an ability. * * Invocations opened after it are calls that failed without a closing hook. On WordPress 7.0, a failed recursive * call of the same ability can't be told apart from the outer call, so their outcomes can swap; counts stay right. * * @param mixed $ability_name The ability name. * @param bool $executing Execution state the invocation must be in; hooks before and after execution share filters with other callers. * @param mixed $result Null for success, a WP_Error for failure, or a short-circuited result. */ private function close_frame( $ability_name, bool $executing, $result ): void { if ( ! self::is_tracked_ability( $ability_name ) ) { return; } $index = $this->find_frame( $ability_name, $executing ); if ( null === $index ) { return; } $frame = $this->frames[ $index ]; foreach ( array_splice( $this->frames, $index ) as $offset => $closed_frame ) { if ( $offset > 0 ) { $this->add_event( $closed_frame, null, false ); } } // Only executed calls report a duration, so it always measures execution. $this->add_event( $frame, is_wp_error( $result ) ? $result : true, $frame['executing'] ); } /** * Find the innermost open invocation of an ability in a given execution state. * * Matching the state keeps a recursive call that failed without a closing hook from hiding its outer call. * * @param string $ability_name The ability name. * @param bool $executing Whether the invocation must have started executing. * @return int|null Index in the frames stack, or null when none is open. */ private function find_frame( string $ability_name, bool $executing ): ?int { for ( $index = count( $this->frames ) - 1; $index >= 0; $index-- ) { if ( $ability_name === $this->frames[ $index ]['name'] && $executing === $this->frames[ $index ]['executing'] ) { return $index; } } return null; } /** * Queue a finished invocation as an ability_called event. * * @param array{name: string, channel: string, rest_namespace: string, external_client: string, start: float, executing: bool} $frame The invocation. * @param true|WP_Error|null $outcome True for success, a WP_Error for a known failure, null for an unknown failure. * @param bool $has_duration Whether the duration is known. */ private function add_event( array $frame, $outcome, bool $has_duration ): void { $error_code = self::NONE; if ( $outcome instanceof WP_Error ) { $error_code = self::get_error_code( $outcome ); } elseif ( null === $outcome ) { $error_code = 'unknown'; } $event = array( 'ability_name' => self::get_event_ability_name( $frame['name'] ), 'outcome' => true === $outcome ? 'success' : 'error', 'error_code' => $error_code, 'channel' => $frame['channel'], 'rest_namespace' => $frame['rest_namespace'], 'external_client' => $frame['external_client'], ); if ( $has_duration ) { $event['duration_ms'] = (int) round( ( microtime( true ) - $frame['start'] ) * 1000 ); } // Unknown failures are recorded later than the call (WordPress 7.0 has no hook for them), so they keep the call's // start time. Site and user are still read now; they differ only if the request switched them since. $this->queue_event( self::EVENT_NAME, $event, null === $outcome ? $frame['start'] : null ); } /** * Record an agent looking up abilities through the MCP Adapter: `get-ability-info` for a tracked ability, or * `discover-abilities`. A separate event, so ability_called stays one row per call. Successful lookups only. * * @param mixed $ability_name The ability that ran. * @param mixed $input Its input. */ private function record_discovery( $ability_name, $input ): void { if ( 'mcp-adapter/discover-abilities' === $ability_name ) { $looked_up = null; } elseif ( 'mcp-adapter/get-ability-info' === $ability_name && is_array( $input ) && self::is_tracked_ability( $input['ability_name'] ?? null ) && wp_has_ability( $input['ability_name'] ) ) { $looked_up = $input['ability_name']; } else { return; } $channel = $this->get_channel(); if ( ! in_array( $channel, self::TRACKED_CHANNELS, true ) ) { return; } $this->queue_event( self::DISCOVERY_EVENT_NAME, array( 'ability_name' => null === $looked_up ? self::NONE : self::get_event_ability_name( $looked_up ), 'discovery' => null === $looked_up ? 'list' : 'info', 'channel' => $channel, 'external_client' => 'mcp' === $channel ? $this->get_external_client() : self::NONE, ) ); } /** * Add the properties every event carries and queue the event for sending. * * Properties are built now, so the user, site and time are those of the call rather than of shutdown. * * @param string $event_name Event name, without the `wcadmin_` prefix. * @param array $event Event-specific properties. * @param float|null $started microtime() to record as the event time, or null for now. */ private function queue_event( string $event_name, array $event, ?float $started = null ): void { // Telemetry must never break the ability call: this runs third-party filters and Jetpack code inside core's hooks. try { if ( count( $this->events ) >= self::MAX_EVENTS_PER_REQUEST ) { if ( ! $this->is_long_running_process() ) { ++$this->events_dropped; return; } $this->flush_events(); $this->send_queued_pixels(); } // The call itself may have turned tracking off. if ( ! self::is_tracking_allowed() ) { return; } // Web requests from logged-out visitors aren't merchant or agent usage, and anyone could trigger them. $auth = self::get_auth_method(); if ( 'none' === $auth && ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! wp_doing_cron() ) { return; } $event['auth'] = $auth; // Many local installs (localhost, .local, .test, DDEV, Lando) still report production. $event['is_test'] = ( 'production' !== wp_get_environment_type() || ( class_exists( Status::class ) && ( new Status() )->is_local_site() ) ) ? 'true' : 'false'; $event['is_a11n'] = self::is_a11n() ? 'true' : 'false'; $properties = \WC_Tracks::get_properties( \WC_Tracks::PREFIX . $event_name, $event ); // The request URL and referrer can carry ability input, e.g. in the query string of GET run requests. unset( $properties['_dl'], $properties['_dr'] ); if ( null !== $started ) { $properties['_ts'] = number_format( round( $started * 1000 ), 0, '', '' ); } $this->events[] = $properties; } catch ( \Throwable $e ) { unset( $e ); } } /** * Ability name as recorded: the `/` becomes `__`, and hyphens stay, so `products-query` and `products_query` stay * different events. * * @param string $ability_name Ability name. * @return string */ private static function get_event_ability_name( string $ability_name ): string { return implode( '__', explode( '/', strtolower( $ability_name ), 2 ) ); } /** * Identify how the ability was invoked. * * @return string */ private function get_channel(): string { if ( $this->is_mcp_request() ) { return 'mcp'; } foreach ( $this->rest_requests as $request ) { if ( 1 === preg_match( self::ABILITIES_RUN_ROUTE_REGEX, $request->get_route() ) ) { return 'rest'; } } // Before WP-CLI: `wp cron event run` defines DOING_CRON, and that is scheduled work, not an agent. if ( wp_doing_cron() ) { return 'cron'; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return 'cli'; } return 'other'; } /** * Whether this is an MCP request. * * MCP Streamable HTTP clients send MCP-Protocol-Version on every request after initialization (2025-06-18 and * later) and Mcp-Session-Id when the server assigned a session, whichever server implementation handles them. * * @return bool */ private function is_mcp_request(): bool { if ( null === $this->is_mcp_request ) { $this->is_mcp_request = isset( $_SERVER['HTTP_MCP_PROTOCOL_VERSION'] ) || isset( $_SERVER['HTTP_MCP_SESSION_ID'] ); } return $this->is_mcp_request || $this->is_json_rpc_tool_call(); } /** * Whether the outermost REST request is a JSON-RPC `tools/call`, as every MCP tool call over HTTP is, including from * 2025-03-26 clients that send no MCP headers. Batches (2025-03-26) are checked message by message. * * @return bool */ private function is_json_rpc_tool_call(): bool { $request = $this->rest_requests[0] ?? null; $body = $request ? $request->get_json_params() : null; if ( ! is_array( $body ) ) { return false; } foreach ( isset( $body['jsonrpc'] ) ? array( $body ) : $body as $message ) { if ( is_array( $message ) && '2.0' === ( $message['jsonrpc'] ?? null ) && 'tools/call' === ( $message['method'] ?? null ) ) { return true; } } return false; } /** * Get the namespace of the outermost REST route being dispatched, such as `wp_abilities` or `mcp`. * * Only the namespace is used: the rest of the route can hold IDs. * * @return string */ private function get_rest_namespace(): string { if ( ! $this->rest_requests ) { return self::NONE; } $segments = explode( '/', ltrim( $this->rest_requests[0]->get_route(), '/' ), 2 ); return self::normalize_value( $segments[0] ); } /** * Get the error code to record for an error. * * Codes are normally fixed slugs, but any filter can return an error whose code was built from input, so codes * that don't look like slugs are recorded as `other`. * * @param WP_Error $error The error. * @return string */ private static function get_error_code( WP_Error $error ): string { $code = (string) $error->get_error_code(); return 1 === preg_match( '/^[A-Za-z0-9_-]{1,64}$/', $code ) ? self::normalize_value( $code ) : 'other'; } /** * Get the error a validation filter result stands for, matching how WP_Ability treats it. * * @param mixed $is_valid The filtered validation result. * @param string $default_code Error code to use when the result is false. * @return WP_Error|null The error, or null when the value is valid. */ private static function get_validation_error( $is_valid, string $default_code ): ?WP_Error { if ( false === $is_valid ) { return new WP_Error( $default_code ); } return is_wp_error( $is_valid ) && $is_valid->has_errors() ? $is_valid : null; } /** * Identify how the caller authenticated, so agents can be told apart from people in wp-admin. * * @return string `application_password`, `jetpack`, `cookie`, `other` (another REST authentication method) or * `none` (logged out, WP-CLI or cron). */ private static function get_auth_method(): string { if ( function_exists( 'rest_get_authenticated_app_password' ) && rest_get_authenticated_app_password() ) { return 'application_password'; } if ( self::is_jetpack_signed_request() ) { return 'jetpack'; } if ( ! is_user_logged_in() || ( defined( 'WP_CLI' ) && WP_CLI ) || wp_doing_cron() ) { return 'none'; } if ( wp_is_serving_rest_request() ) { global $wp_rest_auth_cookie; return true === $wp_rest_auth_cookie ? 'cookie' : 'other'; } return 'cookie'; } /** * Whether Jetpack authenticated this request with a blog or user token signature. * * Jetpack is only asked when the request carries its signature arguments, so its REST authentication isn't * initialized here on other requests. * * @return bool */ private static function is_jetpack_signed_request(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checks for the presence of Jetpack's signature arguments. if ( ! isset( $_GET['token'], $_GET['signature'] ) ) { return false; } $rest_authentication = 'Automattic\Jetpack\Connection\Rest_Authentication'; return is_callable( array( $rest_authentication, 'is_signed_with_blog_token' ) ) && is_callable( array( $rest_authentication, 'is_signed_with_user_token' ) ) && ( $rest_authentication::is_signed_with_blog_token() || $rest_authentication::is_signed_with_user_token() ); } /** * Whether an Automattician made the call: signed in on WordPress.com, or through the Automattic proxy. * * Only detectable on WordPress.com and Atomic; always false elsewhere. * * @return bool */ private static function is_a11n(): bool { if ( function_exists( 'is_automattician' ) && is_automattician() ) { return true; } if ( defined( 'AT_PROXIED_REQUEST' ) && AT_PROXIED_REQUEST ) { return true; } if ( function_exists( 'wpcom_is_proxied_request' ) ) { return (bool) wpcom_is_proxied_request(); } if ( isset( $_SERVER['A8C_PROXIED_REQUEST'] ) && is_string( $_SERVER['A8C_PROXIED_REQUEST'] ) ) { return '1' === sanitize_text_field( wp_unslash( $_SERVER['A8C_PROXIED_REQUEST'] ) ); } return defined( 'A8C_PROXIED_REQUEST' ) && A8C_PROXIED_REQUEST; } /** * Whether the current site allows usage tracking. * * Unlike WC_Site_Tracking::is_tracking_enabled() alone, this requires the stored opt-in: that method also treats a * posted `wc_tracker_checkbox=yes` (the onboarding opt-in) as consent, and any visitor can post to the REST API. * * @return bool */ private static function is_tracking_allowed(): bool { try { return 'yes' === get_option( 'woocommerce_allow_tracking' ) && \WC_Site_Tracking::is_tracking_enabled(); } catch ( \Throwable $e ) { // The consent filters are third-party code; a failure there must not break the ability call. return false; } } /** * Whether an ability is in the tracked namespace. * * @param mixed $ability_name The ability name. * @return bool */ private static function is_tracked_ability( $ability_name ): bool { return is_string( $ability_name ) && 0 === strpos( $ability_name, self::ABILITY_PREFIX ); } /** * Get the MCP client, as one of the known clients, `other` or `none`. * * The client supplies its own name, so only known clients are recorded by name. * * @return string */ private function get_external_client(): string { try { return $this->resolve_external_client(); } catch ( \Throwable $e ) { // The session lookup is MCP Adapter code; a failure there must not break the ability call. return self::NONE; } } /** * Resolve and cache the MCP client. * * @return string */ private function resolve_external_client(): string { if ( null === $this->external_client ) { $name = $this->get_mcp_client_name_from_request() ?? self::get_mcp_client_name_from_session(); if ( null === $name ) { $this->external_client = self::NONE; } else { $this->external_client = 'other'; $tokens = '_' . self::normalize_value( $name ) . '_'; foreach ( self::KNOWN_MCP_CLIENTS as $needle => $client ) { if ( false !== strpos( $tokens, '_' . $needle . '_' ) ) { $this->external_client = $client; break; } } } } return $this->external_client; } /** * Get the MCP client name from the request metadata, which MCP 2026-07-28 sends with every request. * * @return string|null */ private function get_mcp_client_name_from_request(): ?string { $request = $this->rest_requests[0] ?? null; $message = $request ? $request->get_json_params() : null; $name = is_array( $message ) ? ( $message['params']['_meta']['io.modelcontextprotocol/clientInfo']['name'] ?? null ) : null; return is_string( $name ) ? $name : null; } /** * Get the MCP client name from the MCP Adapter session (MCP 2025-11-25 and earlier). * * @return string|null */ private static function get_mcp_client_name_from_session(): ?string { $session_manager = array( 'WP\MCP\Transport\Infrastructure\SessionManager', 'get_session' ); $session_id = isset( $_SERVER['HTTP_MCP_SESSION_ID'] ) && is_string( $_SERVER['HTTP_MCP_SESSION_ID'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_MCP_SESSION_ID'] ) ) : ''; if ( '' === $session_id || ! get_current_user_id() || ! is_callable( $session_manager ) ) { return null; } $session = call_user_func( $session_manager, get_current_user_id(), $session_id ); $name = is_array( $session ) ? ( $session['client_params']['clientInfo']['name'] ?? null ) : null; return is_string( $name ) ? $name : null; } /** * Convert a value to lowercase words separated by underscores, as Tracks property values expect. * * @param string $value The value. * @param int|null $max_length Maximum length, or null for no limit. * @return string The normalized value, or 'none' when nothing is left. */ private static function normalize_value( string $value, ?int $max_length = 64 ): string { $value = trim( (string) preg_replace( '/[^a-z0-9]+/', '_', strtolower( $value ) ), '_' ); if ( '' === $value ) { return self::NONE; } return null === $max_length ? $value : (string) substr( $value, 0, $max_length ); } }