guard_admin(); if ( $check ) { return $check; } $end_date = ! empty( $end_date ) ? $end_date : current_time( 'Y-m-d' ); $start_date = ! empty( $start_date ) ? $start_date : gmdate( 'Y-m-d', strtotime( $end_date . ' -' . self::DEFAULT_DAYS . ' days' ) ); if ( ! $this->is_valid_date( $start_date ) || ! $this->is_valid_date( $end_date ) ) { return [ 'error' => 'start_date and end_date must be in Y-m-d format.' ]; } if ( $start_date > $end_date ) { return [ 'error' => 'start_date must be on or before end_date.' ]; } // Clamp the span: unbounded ranges flood the model with rows. $span_days = (int) floor( ( strtotime( $end_date ) - strtotime( $start_date ) ) / DAY_IN_SECONDS ) + 1; $max_days = true === $include_daily_breakdown ? self::MAX_RANGE_DAYS_WITH_DAILY : self::MAX_RANGE_DAYS; if ( $span_days > $max_days ) { return [ 'error' => sprintf( 'Date range of %d days is too large; the maximum is %d days%s. Narrow start_date/end_date.', $span_days, $max_days, true === $include_daily_breakdown ? ' when include_daily_breakdown is true' : '' ), ]; } try { $range = [ 'start_date' => $start_date, 'end_date' => $end_date, ]; $request = new \WP_REST_Request( 'GET', '/booktics/v1/dashboard' ); $request->set_param( 'total_bookings', $range ); $request->set_param( 'total_revenue', $range ); $request->set_param( 'total_customers', $range ); if ( true === $include_daily_breakdown ) { $request->set_param( 'booking_reports', $range ); } $controller = new \Booktics\Dashboard\Controllers\Dashboard_Controller(); $data = $this->extract_response( $controller->get_dashboard_data( $request ), 'Failed to retrieve dashboard statistics.' ); if ( isset( $data['error'] ) ) { return $data; } $response = [ 'date_range' => $range, 'summary' => $data, ]; // Defence-in-depth on top of the span clamp: cap per-day rows too. if ( isset( $data['booking_reports'] ) && is_array( $data['booking_reports'] ) ) { $reports = array_values( $data['booking_reports'] ); if ( count( $reports ) > self::MAX_DAILY_ROWS ) { $response['summary']['booking_reports'] = array_slice( $reports, 0, self::MAX_DAILY_ROWS ); $response['note'] = sprintf( 'Daily breakdown truncated to the first %d of %d days; narrow the date range to see the rest.', self::MAX_DAILY_ROWS, count( $reports ) ); } } return $response; } catch ( \Throwable $e ) { Log::error( 'Booktics_Get_Dashboard_Summary_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to retrieve dashboard statistics: ' . $e->getMessage() ]; } } }