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

Commit 7a19262

Browse files
authored
Add missing parameters to RemotePostCreateParameters (#757)
2 parents bcfb450 + 810b304 commit 7a19262

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

WordPressKit/PostServiceRemoteXMLRPC+Extended.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import wpxmlrpc
33

44
extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
55
public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost {
6-
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters, type: .post))
6+
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters))
77
let parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
88
let response = try await api.call(method: "metaWeblog.newPost", parameters: parameters).get()
9-
guard let postID = (response.body as? NSNumber) else {
9+
guard let postID = (response.body as? NSObject)?.numericValue() else {
1010
throw URLError(.unknown) // Should never happen
1111
}
1212
return try await getPost(withID: postID)
1313
}
1414

1515
public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost {
16-
let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters, type: .post))
16+
let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters))
1717
var parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
1818
if parameters.count > 0 {
1919
parameters[0] = postID as NSNumber

WordPressKit/RemotePostParameters.swift

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Foundation
22

33
/// The parameters required to create a post or a page.
44
public struct RemotePostCreateParameters: Equatable {
5+
public var type: String
6+
57
public var status: String
68
public var date: Date?
79
public var authorID: Int?
@@ -21,7 +23,8 @@ public struct RemotePostCreateParameters: Equatable {
2123
public var tags: [String] = []
2224
public var categoryIDs: [Int] = []
2325

24-
public init(status: String) {
26+
public init(type: String, status: String) {
27+
self.type = type
2528
self.status = status
2629
}
2730
}
@@ -155,6 +158,7 @@ extension RemotePostCreateParameters {
155158

156159
private enum RemotePostWordPressComCodingKeys: String, CodingKey {
157160
case ifNotModifiedSince = "if_not_modified_since"
161+
case type
158162
case status
159163
case date
160164
case authorID = "author"
@@ -178,6 +182,7 @@ struct RemotePostCreateParametersWordPressComEncoder: Encodable {
178182

179183
func encode(to encoder: Encoder) throws {
180184
var container = encoder.container(keyedBy: RemotePostWordPressComCodingKeys.self)
185+
try container.encodeIfPresent(parameters.type, forKey: .type)
181186
try container.encodeIfPresent(parameters.status, forKey: .status)
182187
try container.encodeIfPresent(parameters.date, forKey: .date)
183188
try container.encodeIfPresent(parameters.authorID, forKey: .authorID)
@@ -243,8 +248,8 @@ struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
243248

244249
private enum RemotePostXMLRPCCodingKeys: String, CodingKey {
245250
case ifNotModifiedSince = "if_not_modified_since"
251+
case type = "post_type"
246252
case postStatus = "post_status"
247-
case pageStatus = "page_status"
248253
case date = "date_created_gmt"
249254
case authorID = "wp_author_id"
250255
case title
@@ -262,22 +267,13 @@ private enum RemotePostXMLRPCCodingKeys: String, CodingKey {
262267
static let postTags = "post_tag"
263268
}
264269

265-
enum RemotePostEncodingPostType {
266-
case post, page
267-
}
268-
269270
struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
270271
let parameters: RemotePostCreateParameters
271-
let type: RemotePostEncodingPostType
272272

273273
func encode(to encoder: Encoder) throws {
274274
var container = encoder.container(keyedBy: RemotePostXMLRPCCodingKeys.self)
275-
switch type {
276-
case .post:
277-
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
278-
case .page:
279-
try container.encodeIfPresent(parameters.status, forKey: .pageStatus)
280-
}
275+
try container.encode(parameters.type, forKey: .type)
276+
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
281277
try container.encodeIfPresent(parameters.date, forKey: .date)
282278
try container.encodeIfPresent(parameters.authorID, forKey: .authorID)
283279
try container.encodeIfPresent(parameters.title, forKey: .title)
@@ -308,17 +304,11 @@ struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
308304

309305
struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
310306
let parameters: RemotePostUpdateParameters
311-
let type: RemotePostEncodingPostType
312307

313308
func encode(to encoder: Encoder) throws {
314309
var container = encoder.container(keyedBy: RemotePostXMLRPCCodingKeys.self)
315310
try container.encodeIfPresent(parameters.ifNotModifiedSince, forKey: .ifNotModifiedSince)
316-
switch type {
317-
case .post:
318-
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
319-
case .page:
320-
try container.encodeIfPresent(parameters.status, forKey: .pageStatus)
321-
}
311+
try container.encodeIfPresent(parameters.status, forKey: .postStatus)
322312
try container.encodeIfPresent(parameters.date, forKey: .date)
323313
try container.encodeIfPresent(parameters.authorID, forKey: .authorID)
324314
try container.encodeIfPresent(parameters.title, forKey: .title)

0 commit comments

Comments
 (0)