Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline Mode: Add support for metadata #783

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions Sources/WordPressKit/Models/RemotePostParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct RemotePostCreateParameters: Equatable {
public var isSticky = false
public var tags: [String] = []
public var categoryIDs: [Int] = []
public var metadata: Set<RemotePostMetadataItem> = []

public init(type: String, status: String) {
self.type = type
Expand Down Expand Up @@ -51,10 +52,23 @@ public struct RemotePostUpdateParameters: Equatable {
public var isSticky: Bool?
public var tags: [String]?
public var categoryIDs: [Int]?
public var metadata: Set<RemotePostMetadataItem>?

public init() {}
}

public struct RemotePostMetadataItem: Hashable {
public var id: String?
public var key: String?
public var value: String?

public init(id: String?, key: String?, value: String?) {
self.id = id
self.key = key
self.value = value
}
}

// MARK: - Diff

extension RemotePostCreateParameters {
Expand Down Expand Up @@ -104,6 +118,9 @@ extension RemotePostCreateParameters {
if previous.categoryIDs != categoryIDs {
changes.categoryIDs = categoryIDs
}
if previous.metadata != metadata {
changes.metadata = metadata
}
return changes
}

Expand Down Expand Up @@ -151,6 +168,9 @@ extension RemotePostCreateParameters {
if let categoryIDs = changes.categoryIDs {
self.categoryIDs = categoryIDs
}
if let metadata = changes.metadata {
self.metadata = metadata
}
}
}

Expand All @@ -173,6 +193,7 @@ private enum RemotePostWordPressComCodingKeys: String, CodingKey {
case format
case isSticky = "sticky"
case categoryIDs = "categories_by_id"
case metadata

static let postTags = "post_tag"
}
Expand All @@ -192,6 +213,10 @@ struct RemotePostCreateParametersWordPressComEncoder: Encodable {
try container.encodeIfPresent(parameters.excerpt, forKey: .excerpt)
try container.encodeIfPresent(parameters.slug, forKey: .slug)
try container.encodeIfPresent(parameters.featuredImageID, forKey: .featuredImageID)
if !parameters.metadata.isEmpty {
let metadata = parameters.metadata.map(RemotePostUpdateParametersWordPressComMetadata.init)
try container.encode(metadata, forKey: .metadata)
}

// Pages
if let parentPageID = parameters.parentPageID {
Expand All @@ -210,6 +235,50 @@ struct RemotePostCreateParametersWordPressComEncoder: Encodable {
try container.encode(parameters.isSticky, forKey: .isSticky)
}
}

// - warning: fixme
static func encodeMetadata(_ metadata: Set<RemotePostMetadataItem>) -> [[String: Any]] {
metadata.map { item in
var operation = "update"
if item.key == nil {
if item.id != nil && item.value == nil {
operation = "delete"
} else if item.id == nil && item.value != nil {
operation = "add"
}
}
var dictionary: [String: Any] = [:]
if let id = item.id { dictionary["id"] = id }
if let value = item.value { dictionary["value"] = value }
if let key = item.key { dictionary["key"] = key }
dictionary["operation"] = operation
return dictionary
}
}
}

struct RemotePostUpdateParametersWordPressComMetadata: Encodable {
let id: String?
let operation: String
let key: String?
let value: String?

init(_ item: RemotePostMetadataItem) {
if item.key == nil {
if item.id != nil && item.value == nil {
self.operation = "delete"
} else if item.id == nil && item.value != nil {
self.operation = "add"
} else {
self.operation = "update"
}
} else {
self.operation = "update"
}
self.id = item.id
self.key = item.key
self.value = item.value
}
}

struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
Expand All @@ -229,12 +298,16 @@ struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
try container.encodeIfPresent(parameters.slug, forKey: .slug)
if let value = parameters.featuredImageID {
if let featuredImageID = value {
try container.encode(parameters.featuredImageID, forKey: .featuredImageID)
try container.encode(featuredImageID, forKey: .featuredImageID)
} else {
// Passing `null` doesn't work.
try container.encode("", forKey: .featuredImageID)
}
}
if let metadata = parameters.metadata, !metadata.isEmpty {
let metadata = metadata.map(RemotePostUpdateParametersWordPressComMetadata.init)
try container.encode(metadata, forKey: .metadata)
}

// Pages
if let parentPageID = parameters.parentPageID {
Expand Down Expand Up @@ -270,6 +343,7 @@ private enum RemotePostXMLRPCCodingKeys: String, CodingKey {
case format = "wp_post_format"
case isSticky = "sticky"
case categoryIDs = "categories"
case metadata = "custom_fields"

static let postTags = "post_tag"
}
Expand All @@ -289,6 +363,10 @@ struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
try container.encodeIfPresent(parameters.excerpt, forKey: .excerpt)
try container.encodeIfPresent(parameters.slug, forKey: .slug)
try container.encodeIfPresent(parameters.featuredImageID, forKey: .featuredImageID)
if !parameters.metadata.isEmpty {
let metadata = parameters.metadata.map(RemotePostUpdateParametersXMLRPCMetadata.init)
try container.encode(metadata, forKey: .metadata)
}

// Pages
if let parentPageID = parameters.parentPageID {
Expand Down Expand Up @@ -325,12 +403,16 @@ struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
try container.encodeIfPresent(parameters.slug, forKey: .slug)
if let value = parameters.featuredImageID {
if let featuredImageID = value {
try container.encode(parameters.featuredImageID, forKey: .featuredImageID)
try container.encode(featuredImageID, forKey: .featuredImageID)
} else {
// Passing `null` doesn't work.
try container.encode("", forKey: .featuredImageID)
}
}
if let metadata = parameters.metadata, !metadata.isEmpty {
let metadata = metadata.map(RemotePostUpdateParametersXMLRPCMetadata.init)
try container.encode(metadata, forKey: .metadata)
}

// Pages
if let parentPageID = parameters.parentPageID {
Expand All @@ -346,3 +428,15 @@ struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
try container.encodeIfPresent(parameters.isSticky, forKey: .isSticky)
}
}

struct RemotePostUpdateParametersXMLRPCMetadata: Encodable {
let id: String?
let key: String?
let value: String?

init(_ item: RemotePostMetadataItem) {
self.id = item.id
self.key = item.key
self.value = item.value
}
}