*/ public function __invoke( string $range, ?string $granularity = null, ?string $from = null, ?string $to = null ): array { $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/overview' ); $request->set_param( 'range', $range ); $request->set_param( 'granularity', $granularity ?? 'auto' ); 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->overview( $request ), __( 'Failed to retrieve Pointics analytics overview.', 'aisentic' ), $this->getName() ); if ( $this->is_error( $data ) ) { return $this->steer_analytics_error( $data ); } $card_fields = [ 'value', 'unit', 'previous', 'delta', 'delta_unit' ]; $cards = []; foreach ( [ 'points_issued', 'points_redeemed', 'active_customers', 'redemption_rate', 'referral_count', 'points_expiring_soon' ] as $card_name ) { $fields = 'points_expiring_soon' === $card_name ? [ 'enabled', 'points', 'members', 'window_days' ] : $card_fields; if ( 'active_customers' === $card_name ) { $fields[] = 'new_this_month'; } if ( 'redemption_rate' === $card_name ) { $fields[] = 'scope'; } $cards[ $card_name ] = $this->pluck( is_array( $data['cards'][ $card_name ] ?? null ) ? $data['cards'][ $card_name ] : [], $fields ); if ( 'points_expiring_soon' === $card_name && false === ( $cards[ $card_name ]['enabled'] ?? true ) ) { $cards[ $card_name ]['note'] = __( 'Point expiry tracking is disabled in Pointics settings; no expiry total is reported.', 'aisentic' ); } } return [ 'range' => $this->pluck( is_array( $data['range'] ?? null ) ? $data['range'] : [], [ 'preset', 'from', 'to', 'previous_from', 'previous_to', 'timezone' ] ), 'cards' => $cards, 'granularity' => $data['granularity'] ?? null, 'buckets' => $this->shape_rows( is_array( $data['buckets'] ?? null ) ? $data['buckets'] : [], fn ( array $bucket ): array => $this->pluck( $bucket, [ 'bucket', 'start', 'earned', 'redeemed' ] ) ), ]; } catch ( \Throwable $e ) { Log::error( 'Pointics_Get_Analytics_Overview_Tool error', [ 'error' => $e->getMessage() ] ); return $this->steer( [ 'error' => __( 'Failed to retrieve Pointics analytics overview.', 'aisentic' ), 'code' => 'internal', 'status' => 500, ] ); } } }