-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRemotePostCreateParameters+Helpers.swift
More file actions
54 lines (52 loc) · 1.88 KB
/
Copy pathRemotePostCreateParameters+Helpers.swift
File metadata and controls
54 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Foundation
import WordPressKit
import WordPressShared
extension RemotePostCreateParameters {
/// Initializes the parameters required to create the given post.
public init(post: AbstractPost) {
self.init(
type: post is Post ? "post" : "page",
status: (post.status ?? .draft).rawValue
)
date = post.dateCreated
// - warning: the currnet Core Data model defaults to `0`
if let authorID = post.authorID?.intValue, authorID > 0 {
self.authorID = authorID
}
title = post.postTitle
content = post.content
password = post.password
excerpt = post.mt_excerpt
slug = post.wp_slug
featuredImageID = post.featuredImage?.mediaID?.intValue
switch post {
case let page as Page:
parentPageID = page.parentID?.intValue
case let post as Post:
format = post.postFormat
isSticky = post.isStickyPost
tags = makeTags(from: post.tags ?? "")
categoryIDs = (post.categories ?? []).compactMap {
$0.categoryID?.intValue
}
metadata = Set(PostHelper.remoteMetadata(for: post).compactMap { value -> RemotePostMetadataItem? in
guard let dictionary = value as? [String: Any] else {
wpAssertionFailure("Unexpected value", userInfo: [
"value": value
])
return nil
}
return PostHelper.mapDictionaryToMetadataItems(dictionary)
})
default:
break
}
}
}
private func makeTags(from tags: String) -> [String] {
tags
.trimmingCharacters(in: .whitespacesAndNewlines)
.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
}