Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Modules/Sources/WordPressKit/RemotePost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation
import WordPressKitObjC

extension RemotePost {
public static func compare(otherTerms lhs: [String: [String]], withAnother rhs: [String: [String]]) -> Bool {
guard lhs.count == rhs.count else { return false }

return lhs.mapValues { Set($0) } == rhs.mapValues { Set($0) }
}
}
65 changes: 55 additions & 10 deletions Modules/Sources/WordPressKit/RemotePostParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct RemotePostCreateParameters: Equatable {
public var format: String?
public var isSticky = false
public var tags: [String] = []
public var otherTerms: [String: [String]] = [:]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kean I need to add similar code, like updating the new otherTerms variable, assigning its value to another instance, etc. I don't think I missed any places. But can you pay extra attention to reviewing this part? Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) it looks good, but it might be worth adding a convenience terms property/method that would combine tags and other terms to remove some duplication and also simplify encoding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication is across two types: RemotePostUpdateParameters and RemotePostCreateParameters, and different encoders (and keys) for XMLRPC and REST API. I'll leave the code as it is for now, for simplicity.

public var categoryIDs: [Int] = []
public var metadata: Set<RemotePostMetadataItem> = []
public var discussion: RemotePostDiscussionSettings = .default
Expand Down Expand Up @@ -52,6 +53,7 @@ public struct RemotePostUpdateParameters: Equatable {
public var format: String??
public var isSticky: Bool?
public var tags: [String]?
public var otherTerms: [String: [String]]?
public var categoryIDs: [Int]?
public var metadata: Set<RemotePostMetadataItem>?
public var discussion: RemotePostDiscussionSettings?
Expand Down Expand Up @@ -138,6 +140,9 @@ extension RemotePostCreateParameters {
if previous.tags != tags {
changes.tags = tags
}
if !RemotePost.compare(otherTerms: previous.otherTerms, withAnother: otherTerms) {
changes.otherTerms = otherTerms
}
if Set(previous.categoryIDs) != Set(categoryIDs) {
changes.categoryIDs = categoryIDs
}
Expand Down Expand Up @@ -191,6 +196,9 @@ extension RemotePostCreateParameters {
if let tags = changes.tags {
self.tags = tags
}
if let otherTerms = changes.otherTerms {
self.otherTerms = otherTerms
}
if let categoryIDs = changes.categoryIDs {
self.categoryIDs = categoryIDs
}
Expand Down Expand Up @@ -253,9 +261,19 @@ struct RemotePostCreateParametersWordPressComEncoder: Encodable {

// Posts
try container.encodeIfPresent(parameters.format, forKey: .format)

// Terms (including tags)
var terms = [String: [String]]()
if !parameters.tags.isEmpty {
try container.encode([RemotePostWordPressComCodingKeys.postTags: parameters.tags], forKey: .terms)
terms[RemotePostWordPressComCodingKeys.postTags] = parameters.tags
}
if !parameters.otherTerms.isEmpty {
terms.merge(parameters.otherTerms) { old, _ in old }
}
if !terms.isEmpty {
try container.encode(terms, forKey: .terms)
}

if !parameters.categoryIDs.isEmpty {
try container.encodeIfPresent(parameters.categoryIDs, forKey: .categoryIDs)
}
Expand Down Expand Up @@ -330,16 +348,25 @@ struct RemotePostUpdateParametersWordPressComEncoder: Encodable {
try container.encode(metadata, forKey: .metadata)
}

// Terms (including tags)
var terms = [String: [String]]()
if let tags = parameters.tags, !tags.isEmpty {
terms[RemotePostWordPressComCodingKeys.postTags] = tags
}
if let otherTerms = parameters.otherTerms, !otherTerms.isEmpty {
terms.merge(otherTerms) { old, _ in old }
}
if !terms.isEmpty {
try container.encode(terms, forKey: .terms)
}

// Pages
if let parentPageID = parameters.parentPageID {
try container.encodeNullableID(parentPageID, forKey: .parentPageID)
}

// Posts
try container.encodeIfPresent(parameters.format, forKey: .format)
if let tags = parameters.tags {
try container.encode([RemotePostWordPressComCodingKeys.postTags: tags], forKey: .terms)
}
try container.encodeIfPresent(parameters.categoryIDs, forKey: .categoryIDs)
try container.encodeIfPresent(parameters.isSticky, forKey: .isSticky)
if let discussion = parameters.discussion {
Expand Down Expand Up @@ -395,14 +422,23 @@ struct RemotePostCreateParametersXMLRPCEncoder: Encodable {
try container.encode(metadata, forKey: .metadata)
}

// Terms (including tags)
var terms = [String: [String]]()
if !parameters.tags.isEmpty {
terms[RemotePostXMLRPCCodingKeys.taxonomyTag] = parameters.tags
}
if !parameters.otherTerms.isEmpty {
terms.merge(parameters.otherTerms) { old, _ in old }
}
if !terms.isEmpty {
try container.encode(terms, forKey: .termNames)
}

// Pages
try container.encodeIfPresent(parameters.parentPageID, forKey: .parentPageID)

// Posts
try container.encodeIfPresent(parameters.format, forKey: .format)
if !parameters.tags.isEmpty {
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyTag: parameters.tags], forKey: .termNames)
}
if !parameters.categoryIDs.isEmpty {
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyCategory: parameters.categoryIDs], forKey: .terms)
}
Expand Down Expand Up @@ -438,16 +474,25 @@ struct RemotePostUpdateParametersXMLRPCEncoder: Encodable {
try container.encode(metadata, forKey: .metadata)
}

// Terms (including tags)
var terms = [String: [String]]()
if let tags = parameters.tags, !tags.isEmpty {
terms[RemotePostXMLRPCCodingKeys.taxonomyTag] = tags
}
if let otherTerms = parameters.otherTerms, !otherTerms.isEmpty {
terms.merge(otherTerms) { old, _ in old }
}
if !terms.isEmpty {
try container.encode(terms, forKey: .termNames)
}

// Pages
if let parentPageID = parameters.parentPageID {
try container.encodeNullableID(parentPageID, forKey: .parentPageID)
}

// Posts
try container.encodeStringIfPresent(parameters.format, forKey: .format)
if let tags = parameters.tags {
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyTag: tags], forKey: .termNames)
}
if let categoryIDs = parameters.categoryIDs {
try container.encode([RemotePostXMLRPCCodingKeys.taxonomyCategory: categoryIDs], forKey: .terms)
}
Expand Down
38 changes: 34 additions & 4 deletions Modules/Sources/WordPressKitObjC/PostServiceRemoteREST.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "PostServiceRemoteREST.h"
#import "RemotePost.h"
#import "RemotePostCategory.h"
#import "RemotePostTerm.h"
#import "FilePart.h"
#import "WPMapFilterReduce.h"
#import "DisplayableImageHelper.h"
Expand Down Expand Up @@ -453,6 +454,7 @@ + (RemotePost *)remotePostFromJSONDictionary:(NSDictionary *)jsonPost {
post.categories = [self remoteCategoriesFromJSONArray:[categories allValues]];
}
post.tags = [self tagNamesFromJSONDictionary:jsonPost[@"tags"]];
post.otherTerms = [RemotePostTerm simpleMappingRepresentation:[self otherTermsFromJSONDictionary:jsonPost[@"terms"]]];

post.revisions = [jsonPost arrayForKey:@"revisions"];

Expand Down Expand Up @@ -522,11 +524,19 @@ - (NSDictionary *)parametersWithRemotePost:(RemotePost *)post
parameters[@"categories_by_id"] = [post.categories valueForKey:@"categoryID"];
}

if (post.tags) {
NSArray *tags = post.tags;
NSDictionary *postTags = @{@"post_tag":tags};
parameters[@"terms"] = postTags;
{
NSMutableDictionary *terms = [NSMutableDictionary dictionary];
if (post.tags) {
terms[@"post_tag"] = post.tags;
}
[post.otherTerms enumerateKeysAndObjectsUsingBlock:^(NSString *taxonomy, NSArray<NSString *> *theTerms, BOOL *stop) {
terms[taxonomy] = theTerms;
}];
if (terms.count > 0) {
parameters[@"terms"] = terms;
}
}

if (post.format) {
parameters[@"format"] = post.format;
}
Expand Down Expand Up @@ -596,6 +606,26 @@ + (NSArray *)tagNamesFromJSONDictionary:(NSDictionary *)jsonTags {
return [jsonTags allKeys];
}

+ (NSArray<RemotePostTerm *> *)otherTermsFromJSONDictionary:(NSDictionary *)terms {
NSMutableArray<RemotePostTerm *> *otherTerms = [NSMutableArray array];

for (NSString *taxonomySlug in terms) {
if ([taxonomySlug isEqualToString:@"category"] ||
[taxonomySlug isEqualToString:@"post_tag"] ||
[taxonomySlug isEqualToString:@"post_format"]) {
continue;
}

NSDictionary *taxonomyTerms = terms[taxonomySlug];
for (NSString *termKey in taxonomyTerms) {
NSDictionary *termData = taxonomyTerms[termKey];
[otherTerms addObject:[[RemotePostTerm alloc] initWithRESTAPIResponse:termData taxonomySlug:taxonomySlug]];
}
}

return [otherTerms copy];
}

/**
* @brief Returns an array of RemoteLikeUser based on provided JSON
* representation of users.
Expand Down
15 changes: 15 additions & 0 deletions Modules/Sources/WordPressKitObjC/PostServiceRemoteXMLRPC.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "PostServiceRemoteXMLRPC.h"
#import "RemotePost.h"
#import "RemotePostCategory.h"
#import "RemotePostTerm.h"
#import "NSMutableDictionary+Helpers.h"
#import "NSString+Helpers.h"
#import "WPMapFilterReduce.h"
Expand Down Expand Up @@ -333,6 +334,7 @@ + (RemotePost *)remotePostFromXMLRPCDictionary:(NSDictionary *)xmlrpcDictionary
NSArray *terms = [xmlrpcDictionary arrayForKey:@"terms"];
post.tags = [self tagsFromXMLRPCTermsArray:terms];
post.categories = [self remoteCategoriesFromXMLRPCTermsArray:terms];
post.otherTerms = [RemotePostTerm simpleMappingRepresentation:[self otherTermsFromXMLRPCTermsArray:terms]];

post.commentsStatus = [xmlrpcDictionary stringForKey:@"comment_status"];
post.pingsStatus = [xmlrpcDictionary stringForKey:@"ping_status"];
Expand Down Expand Up @@ -373,6 +375,15 @@ + (NSArray *)remoteCategoriesFromXMLRPCTermsArray:(NSArray *)terms {
}];
}

+ (NSArray<RemotePostTerm *> *)otherTermsFromXMLRPCTermsArray:(NSArray *)terms {
return [[terms wpkit_filter:^BOOL(NSDictionary *category) {
return ![[category stringForKey:@"taxonomy"] isEqualToString:@"category"]
&& ![[category stringForKey:@"taxonomy"] isEqualToString:@"post_tag"];
}] wpkit_map:^id(NSDictionary *term) {
return [[RemotePostTerm alloc] initWithXMLRPCResponse:term];
}];
}

+ (RemotePostCategory *)remoteCategoryFromXMLRPCDictionary:(NSDictionary *)xmlrpcCategory {
RemotePostCategory *category = [RemotePostCategory new];
category.categoryID = [xmlrpcCategory numberForKey:@"term_id"];
Expand Down Expand Up @@ -425,6 +436,10 @@ - (NSDictionary *)parametersWithRemotePost:(RemotePost *)post
postParams[@"categories"] = categoryNames;
}

if (post.otherTerms.count > 0) {
postParams[@"terms_names"] = post.otherTerms;
}

if ([post.metadata count] > 0) {
postParams[@"custom_fields"] = post.metadata;
}
Expand Down
74 changes: 74 additions & 0 deletions Modules/Sources/WordPressKitObjC/RemotePostTerm.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#import "RemotePostTerm.h"

@import NSObject_SafeExpectations;

@implementation RemotePostTerm

- (instancetype)initWithXMLRPCResponse:(NSDictionary *)response
{
self = [super init];
if (self) {
self.termID = [response numberForKey:@"term_id"];
self.name = [response stringForKey:@"name"];
self.slug = [response stringForKey:@"slug"];
self.taxonomySlug = [response stringForKey:@"taxonomy"];
self.termDescription = [response stringForKey:@"description"];
self.count = [response numberForKey:@"count"];
}
return self;
}

- (instancetype)initWithRESTAPIResponse:(NSDictionary *)response taxonomySlug:(NSString *)taxonomySlug
{
self = [super init];
if (self) {
self.termID = [response numberForKey:@"ID"];
self.name = [response stringForKey:@"name"];
self.slug = [response stringForKey:@"slug"];
self.taxonomySlug = taxonomySlug;
self.termDescription = [response stringForKey:@"description"];
self.count = [response numberForKey:@"post_count"];
}
return self;
}

- (NSDictionary *)RESTAPIRepresentation
{
return @{
@"ID": self.termID,
@"name": self.name,
@"slug": self.slug,
@"description": self.termDescription ?: @"",
@"post_count": self.count
};
}

+ (NSDictionary<NSString *, NSArray<NSString *> *> *)simpleMappingRepresentation:(NSArray<RemotePostTerm *> *)terms
{
NSMutableDictionary<NSString *, NSMutableArray<NSString *> *> *dict = [NSMutableDictionary dictionary];
for (RemotePostTerm *term in terms) {
NSMutableArray *termNames = dict[term.taxonomySlug];
if (termNames == nil) {
termNames = [NSMutableArray array];
dict[term.taxonomySlug] = termNames;
}
[termNames addObject:term.name];
}
return [dict copy];
}

- (NSString *)debugDescription
{
NSDictionary *properties = @{
@"ID": self.termID,
@"name": self.name
};
return [NSString stringWithFormat:@"<%@: %p> (%@)", NSStringFromClass([self class]), self, properties];
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> %@[%@]", NSStringFromClass([self class]), self, self.name, self.termID];
}

@end
3 changes: 3 additions & 0 deletions Modules/Sources/WordPressKitObjC/include/RemotePost.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import <Foundation/Foundation.h>

@class RemotePostAutosave;
@class RemotePostTerm;

extern NSString * const PostStatusDraft;
extern NSString * const PostStatusPending;
Expand Down Expand Up @@ -54,6 +56,7 @@ extern NSString * const PostStatusDeleted;
@property (nonatomic, strong) NSArray *categories;
@property (nonatomic, strong) NSArray *revisions;
@property (nonatomic, strong) NSArray *tags;
@property (nonatomic, strong) NSDictionary<NSString *, NSArray<NSString *> *> *otherTerms;
@property (nonatomic, strong) NSString *pathForDisplayImage;
@property (nonatomic, assign) NSNumber *isStickyPost;
@property (nonatomic, assign) BOOL isFeaturedImageChanged;
Expand Down
23 changes: 23 additions & 0 deletions Modules/Sources/WordPressKitObjC/include/RemotePostTerm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RemotePostTerm : NSObject

@property (nonatomic, strong) NSNumber *termID;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *slug;
@property (nonatomic, strong) NSString *taxonomySlug;
@property (nonatomic, strong) NSString *termDescription;
@property (nonatomic, strong) NSNumber *count;

- (instancetype)initWithXMLRPCResponse:(NSDictionary *)response;
- (instancetype)initWithRESTAPIResponse:(NSDictionary *)response taxonomySlug:(NSString *)taxonomySlug;

- (NSDictionary *)RESTAPIRepresentation;

+ (NSDictionary<NSString *, NSArray<NSString *> *> *)simpleMappingRepresentation:(NSArray<RemotePostTerm *> *)terms;

@end

NS_ASSUME_NONNULL_END
Loading