Skip to content

Commit

Permalink
Switch to wp.newPost and wp.editPost
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 16, 2024
1 parent a056604 commit 39a2f3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
39 changes: 21 additions & 18 deletions Sources/WordPressKit/Models/RemotePostParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,23 @@ private enum RemotePostXMLRPCCodingKeys: String, CodingKey {
case ifNotModifiedSince = "if_not_modified_since"
case type = "post_type"
case postStatus = "post_status"
case date = "date_created_gmt"
case authorID = "wp_author_id"
case title
case content = "description"
case password = "wp_password"
case excerpt = "mt_excerpt"
case slug = "wp_slug"
case featuredImageID = "wp_post_thumbnail"
case parentPageID = "wp_page_parent_id"
case tags = "mt_keywords"
case format = "wp_post_format"
case date = "post_date"
case authorID = "post_author"
case title = "post_title"
case content = "post_content"
case password = "post_password"
case excerpt = "post_excerpt"
case slug = "post_name"
case featuredImageID = "post_thumbnail"
case parentPageID = "post_parent"
case terms = "terms"
case termNames = "term_names"
case format = "post_format"
case isSticky = "sticky"
case categoryIDs = "categories"
case metadata = "custom_fields"

static let postTags = "post_tag"
static let taxonomyTag = "post_tag"
static let taxonomyCategory = "category"
}

struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
Expand Down Expand Up @@ -376,10 +377,10 @@ struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
// Posts
try container.encodeIfPresent(parameters.format, forKey: .format)
if !parameters.tags.isEmpty {
try container.encode(parameters.tags, forKey: .tags)
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyTag: parameters.tags], forKey: .termNames)
}
if !parameters.categoryIDs.isEmpty {
try container.encodeIfPresent(parameters.categoryIDs, forKey: .categoryIDs)
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyCategory: parameters.categoryIDs], forKey: .terms)
}
if parameters.isSticky {
try container.encode(parameters.isSticky, forKey: .isSticky)
Expand Down Expand Up @@ -422,14 +423,16 @@ struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
// Posts
try container.encodeStringIfPresent(parameters.format, forKey: .format)
if let tags = parameters.tags {
try container.encode(tags, forKey: .tags)
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyTag: tags], forKey: .termNames)
}
if let categoryIDs = parameters.categoryIDs {
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyCategory: categoryIDs], forKey: .terms)
}
try container.encodeIfPresent(parameters.categoryIDs, forKey: .categoryIDs)
try container.encodeIfPresent(parameters.isSticky, forKey: .isSticky)
}
}

struct RemotePostUpdateParametersXMLRPCMetadata: Encodable {
private struct RemotePostUpdateParametersXMLRPCMetadata: Encodable {
let id: String?
let key: String?
let value: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost {
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters))
let parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
let response = try await api.call(method: "metaWeblog.newPost", parameters: parameters).get()
let response = try await api.call(method: "wp.newPost", parameters: parameters).get()
guard let postID = (response.body as? NSObject)?.numericValue() else {
throw URLError(.unknown) // Should never happen
}
Expand All @@ -28,11 +28,8 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {

public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost {
let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters))
var parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
if parameters.count > 0 {
parameters[0] = postID as NSNumber
}
let result = await api.call(method: "metaWeblog.editPost", parameters: parameters)
let parameters = xmlrpcArguments(withExtraDefaults: [postID as NSNumber], andExtra: dictionary) as [AnyObject]
let result = await api.call(method: "wp.editPost", parameters: parameters)
switch result {
case .success:
return try await post(withID: postID)
Expand Down

0 comments on commit 39a2f3c

Please sign in to comment.