Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 58092a4

Browse files
committed
Add startDate, timeZone, and fields parameter to SattsServiceRemoteV2.getData
1 parent 3979cea commit 58092a4

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

Sources/WordPressKit/Models/Stats/StatsSubscribersSummaryData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension StatsSubscribersSummaryData: StatsTimeIntervalData {
7878
}
7979
}
8080

81-
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
81+
public static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
8282
return ["quantity": String(maxCount), "unit": period.stringValue]
8383
}
8484
}

Sources/WordPressKit/Models/Stats/Time Interval/StatsFileDownloadsTimeIntervalData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension StatsFileDownloadsTimeIntervalData: StatsTimeIntervalData {
3535
return "stats/file-downloads"
3636
}
3737

38-
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
38+
public static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
3939
// num = number of periods to include in the query. default: 1.
4040
return ["num": String(maxCount)]
4141
}

Sources/WordPressKit/Models/Stats/Time Interval/StatsSummaryTimeIntervalData.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension StatsSummaryTimeIntervalData: StatsTimeIntervalData {
5959
return "stats/visits"
6060
}
6161

62-
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
62+
public static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
6363
return ["unit": period.stringValue,
6464
"quantity": String(maxCount),
6565
"stat_fields": "views,visitors,comments,likes"]
@@ -234,7 +234,7 @@ extension StatsLikesSummaryTimeIntervalData: StatsTimeIntervalData {
234234
return "stats/visits"
235235
}
236236

237-
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
237+
public static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
238238
return ["unit": period.stringValue,
239239
"quantity": String(maxCount),
240240
"stat_fields": "likes"]

Sources/WordPressKit/Services/StatsServiceRemoteV2.swift

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,53 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
9595
/// - parameters:
9696
/// - period: An enum representing whether either a day, a week, a month or a year worth's of data.
9797
/// - unit: An enum representing whether the data is retuned in a day, a week, a month or a year granularity. Default is `period`.
98-
/// - endingOn: Date on which the `period` for which data you're interested in **is ending**.
98+
/// - endDate: Date on which the `period` for which data you're interested in **is ending**.
9999
/// e.g. if you want data spanning 11-17 Feb 2019, you should pass in a period of `.week` and an
100100
/// ending date of `Feb 17 2019`.
101+
/// - timeZone: The time zone in which the dates are represented.
101102
/// - limit: Limit of how many objects you want returned for your query. Default is `10`. `0` means no limit.
102-
open func getData<TimeStatsType: StatsTimeIntervalData>(for period: StatsPeriodUnit,
103-
unit: StatsPeriodUnit? = nil,
104-
endingOn: Date,
105-
limit: Int = 10,
106-
completion: @escaping ((TimeStatsType?, Error?) -> Void)) {
103+
open func getData<TimeStatsType: StatsTimeIntervalData>(
104+
period: StatsPeriodUnit,
105+
unit: StatsPeriodUnit? = nil,
106+
startDate: Date? = nil,
107+
endDate: Date,
108+
timeZone: TimeZone? = nil,
109+
limit: Int = 10,
110+
fields: [String]? = nil,
111+
completion: @escaping ((TimeStatsType?, Error?) -> Void)
112+
) {
107113
let pathComponent = TimeStatsType.pathComponent
108114
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
109115

110-
let staticProperties = ["period": period.stringValue,
111-
"unit": unit?.stringValue ?? period.stringValue,
112-
"date": periodDataQueryDateFormatter.string(from: endingOn)] as [String: AnyObject]
116+
func formattedDate(_ date: Date) -> String {
117+
guard let timeZone else {
118+
// For backward-compatibility, use the existing periodDataQueryDateFormatter
119+
// with the current time zone.
120+
return periodDataQueryDateFormatter.string(from: date)
121+
}
122+
let formatter = DateFormatter()
123+
formatter.locale = Locale(identifier: "en_US_POSIX")
124+
formatter.dateFormat = "yyyy-MM-dd"
125+
formatter.timeZone = timeZone
126+
return formatter.string(from: date)
127+
}
113128

114-
let classProperties = TimeStatsType.queryProperties(with: endingOn, period: unit ?? period, maxCount: limit) as [String: AnyObject]
129+
var properties = [
130+
"period": period.stringValue,
131+
"unit": unit?.stringValue ?? period.stringValue,
132+
"date": formattedDate(endDate)
133+
] as [String: Any]
115134

116-
let properties = staticProperties.merging(classProperties) { val1, _ in
117-
return val1
135+
for (key, value) in TimeStatsType.queryProperties(period: unit ?? period, maxCount: limit) {
136+
properties[key] = value
137+
}
138+
139+
if let startDate {
140+
properties["period"] = nil
141+
properties["start_date"] = formattedDate(startDate)
142+
}
143+
if let fields {
144+
properties["stat_fields"] = fields.joined(separator: ",")
118145
}
119146

120147
wordPressComRESTAPI.get(path, parameters: properties, success: { [weak self] (response, _) in
@@ -361,7 +388,7 @@ public protocol StatsTimeIntervalData {
361388
init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject])
362389
init?(date: Date, period: StatsPeriodUnit, unit: StatsPeriodUnit?, jsonDictionary: [String: AnyObject])
363390

364-
static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String]
391+
static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String]
365392
}
366393

367394
extension StatsTimeIntervalData {
@@ -370,7 +397,7 @@ extension StatsTimeIntervalData {
370397
return nil
371398
}
372399

373-
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
400+
public static func queryProperties(period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
374401
return ["max": String(maxCount)]
375402
}
376403

0 commit comments

Comments
 (0)