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

Commit 99ebbc8

Browse files
committed
Add summarize param
1 parent 102dc8d commit 99ebbc8

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let package = Package(
1111
targets: [
1212
.binaryTarget(
1313
name: "WordPressKit",
14-
url: "https://github.com/user-attachments/files/21350975/WordPressKit.zip",
15-
checksum: "dc40a4c09af565c16eca2ca0cd110c57ed4e8638ea4baeb2cf1bd124b07d3f8e"
14+
url: "https://github.com/user-attachments/files/21352107/WordPressKit.zip",
15+
checksum: "bfcb1f6a0c46a142cadecd979ed011cbcca62bb1aba0a25b9fee55c0b2c2f6e0"
1616
),
1717
]
1818
)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ public struct StatsTopPost {
4040
}
4141

4242
public let title: String
43+
public var date: String?
4344
public let postID: Int
4445
public let postURL: URL?
4546
public let viewsCount: Int
4647
public let kind: Kind
4748

4849
public init(title: String,
50+
date: String?,
4951
postID: Int,
5052
postURL: URL?,
5153
viewsCount: Int,
5254
kind: Kind) {
5355
self.title = title
56+
self.date = date
5457
self.postID = postID
5558
self.postURL = postURL
5659
self.viewsCount = viewsCount

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private extension StatsTopPost {
5959
}
6060

6161
self.title = title
62+
self.date = jsonDictionary["date"] as? String
6263
self.postID = postID
6364
self.postURL = URL(string: url)
6465
self.viewsCount = viewsCount

Sources/WordPressKit/Services/StatsServiceRemoteV2.swift

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
106106
/// e.g. if you want data spanning 11-17 Feb 2019, you should pass in a period of `.week` and an
107107
/// ending date of `Feb 17 2019`.
108108
/// - limit: Limit of how many objects you want returned for your query. Default is `10`. `0` means no limit.
109-
open func getData<TimeStatsType: StatsTimeIntervalData>(for period: StatsPeriodUnit,
110-
unit: StatsPeriodUnit? = nil,
111-
startDate: Date? = nil,
112-
endingOn: Date,
113-
limit: Int = 10,
114-
completion: @escaping ((TimeStatsType?, Error?) -> Void)) {
109+
open func getData<TimeStatsType: StatsTimeIntervalData>(
110+
for period: StatsPeriodUnit,
111+
unit: StatsPeriodUnit? = nil,
112+
startDate: Date? = nil,
113+
endingOn: Date,
114+
limit: Int = 10,
115+
summarize: Bool? = nil,
116+
parameters: [String: String]? = nil,
117+
completion: @escaping ((TimeStatsType?, Error?) -> Void)
118+
) {
115119
let pathComponent = TimeStatsType.pathComponent
116120
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
117121

@@ -124,6 +128,14 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
124128
if let startDate {
125129
staticProperties["start_date"] = dateFormatter.string(from: startDate) as AnyObject
126130
}
131+
if let summarize {
132+
staticProperties["summarize"] = summarize.description as NSString
133+
}
134+
if let parameters {
135+
for (key, value) in parameters {
136+
staticProperties[key] = value as NSString
137+
}
138+
}
127139

128140
let classProperties = TimeStatsType.queryProperties(with: endingOn, period: unit ?? period, maxCount: limit) as [String: AnyObject]
129141

@@ -397,14 +409,19 @@ extension StatsTimeIntervalData {
397409
// Most of the responses for time data come in a unwieldy format, that requires awkwkard unwrapping
398410
// at the call-site — unfortunately not _all of them_, which means we can't just do it at the request level.
399411
static func unwrapDaysDictionary(jsonDictionary: [String: AnyObject]) -> [String: AnyObject]? {
400-
guard
401-
let days = jsonDictionary["days"] as? [String: AnyObject],
402-
let firstKey = days.keys.first,
403-
let firstDay = days[firstKey] as? [String: AnyObject]
404-
else {
405-
return nil
412+
if let summary = jsonDictionary["summary"] as? [String: AnyObject] {
413+
return summary
414+
}
415+
if let days = jsonDictionary["days"] as? [String: AnyObject],
416+
let firstKey = days.keys.first,
417+
let firstDay = days[firstKey] as? [String: AnyObject] {
418+
return firstDay
406419
}
407-
return firstDay
420+
/// For empty periods, it sometimes retunrs `null`.
421+
if jsonDictionary["summary"] is NSNull {
422+
return [:]
423+
}
424+
return nil
408425
}
409426

410427
}

0 commit comments

Comments
 (0)