*/ public function __invoke( string $range, ?string $from = null, ?string $to = null, ?int $limit = null ): array { // NeuronAI passes an explicit null for a property the model omitted, so the // default has to live here rather than in the signature. $limit = $limit ?? 8; $requested_limit = $limit; $check = $this->guard( 'manage_pointics', 'manage_options' ); if ( $check ) { return $check; } $check = $this->pointics_tables_ready(); if ( $check ) { return $check; } try { $request = new \WP_REST_Request( 'GET', '/pointics/v1/analytics/activity' ); $request->set_param( 'range', $range ); $request->set_param( 'limit', max( 1, min( 50, $limit ) ) ); if ( null !== $from ) { $request->set_param( 'from', $from ); } if ( null !== $to ) { $request->set_param( 'to', $to ); } $controller = pointics_container()->make( \Pointics\Api\Controllers\Analytics_Controller::class ); $data = $this->extract_response( $controller->activity( $request ), __( 'Failed to retrieve Pointics activity.', 'aisentic' ), $this->getName() ); if ( $this->is_error( $data ) ) { return $this->steer_analytics_error( $data ); } $result = [ 'range' => $this->pluck( is_array( $data['range'] ?? null ) ? $data['range'] : [], [ 'preset', 'from', 'to', 'previous_from', 'previous_to', 'timezone' ] ), 'items' => $this->shape_rows( is_array( $data['items'] ?? null ) ? $data['items'] : [], function ( array $item ): array { $shaped = $this->pluck( $item, [ 'id', 'type', 'title', 'description', 'points', 'at' ] ); if ( array_key_exists( 'user', $item ) ) { $shaped['user'] = is_array( $item['user'] ) ? $this->pluck( $item['user'], [ 'id', 'name', 'email' ] ) : null; } if ( array_key_exists( 'object', $item ) ) { $shaped['object'] = is_array( $item['object'] ) ? $this->pluck( $item['object'], [ 'type', 'id' ] ) : null; } return $shaped; } ), ]; if ( $requested_limit > 50 ) { /* translators: %d: maximum number of analytics records. */ $result['note'] = sprintf( __( 'The requested limit was clamped to %d to keep the analytics result bounded.', 'aisentic' ), 50 ); } return $result; } catch ( \Throwable $e ) { Log::error( 'Pointics_Get_Activity_Tool error', [ 'error' => $e->getMessage() ] ); return $this->steer( [ 'error' => __( 'Failed to retrieve Pointics activity.', 'aisentic' ), 'code' => 'internal', 'status' => 500, ] ); } } }