From 8ecfcca4bd594b306f8a3de8e2f0f15dcfbaad96 Mon Sep 17 00:00:00 2001 From: Tony Li Date: Thu, 8 Dec 2022 21:29:09 +1300 Subject: [PATCH] Re-implement RemoteBlog and RemoteBlogOptionsHelper in Swift --- WordPressKit.xcodeproj/project.pbxproj | 24 ++--- WordPressKit/AccountServiceRemoteREST.m | 2 - WordPressKit/RemoteBlog.h | 102 ------------------ WordPressKit/RemoteBlog.m | 48 --------- WordPressKit/RemoteBlog.swift | 79 ++++++++++++++ WordPressKit/RemoteBlogOptionsHelper.h | 15 --- WordPressKit/RemoteBlogOptionsHelper.m | 77 ------------- WordPressKit/RemoteBlogOptionsHelper.swift | 77 +++++++++++++ WordPressKit/WordPressKit.h | 2 - .../BlogServiceRemoteRESTTests.m | 1 - 10 files changed, 164 insertions(+), 263 deletions(-) delete mode 100644 WordPressKit/RemoteBlog.h delete mode 100644 WordPressKit/RemoteBlog.m create mode 100644 WordPressKit/RemoteBlog.swift delete mode 100644 WordPressKit/RemoteBlogOptionsHelper.h delete mode 100644 WordPressKit/RemoteBlogOptionsHelper.m create mode 100644 WordPressKit/RemoteBlogOptionsHelper.swift diff --git a/WordPressKit.xcodeproj/project.pbxproj b/WordPressKit.xcodeproj/project.pbxproj index 819300f3..69ce1662 100644 --- a/WordPressKit.xcodeproj/project.pbxproj +++ b/WordPressKit.xcodeproj/project.pbxproj @@ -124,6 +124,8 @@ 46ABD0EA262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0E9262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift */; }; 4A1DEF44293051BC00322608 /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF43293051BC00322608 /* LoggingTests.swift */; }; 4A1DEF46293051C600322608 /* LoggingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A1DEF45293051C600322608 /* LoggingTests.m */; }; + 4A68E3CD29404181004AC3DC /* RemoteBlog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */; }; + 4A68E3CF29404289004AC3DC /* RemoteBlogOptionsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CE29404289004AC3DC /* RemoteBlogOptionsHelper.swift */; }; 57BCD3D426209D9500292CB3 /* AppTransportSecuritySettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57BCD3D326209D9500292CB3 /* AppTransportSecuritySettings.swift */; }; 730E869F21E44EFD00753E1A /* WordPressComServiceRemote+SiteVerticals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 730E869E21E44EFD00753E1A /* WordPressComServiceRemote+SiteVerticals.swift */; }; 731BA83621DECD61000FDFCD /* SiteCreationRequestEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 731BA83521DECD61000FDFCD /* SiteCreationRequestEncodingTests.swift */; }; @@ -437,10 +439,6 @@ 93BD27811EE73944002BB00B /* WordPressOrgXMLRPCApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93BD27791EE73944002BB00B /* WordPressOrgXMLRPCApi.swift */; }; 93BD27821EE73944002BB00B /* WordPressOrgXMLRPCValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93BD277A1EE73944002BB00B /* WordPressOrgXMLRPCValidator.swift */; }; 93BD27831EE73944002BB00B /* WordPressRSDParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93BD277B1EE73944002BB00B /* WordPressRSDParser.swift */; }; - 93C674E71EE8345300BFAF05 /* RemoteBlog.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C674E51EE8345300BFAF05 /* RemoteBlog.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 93C674E81EE8345300BFAF05 /* RemoteBlog.m in Sources */ = {isa = PBXBuildFile; fileRef = 93C674E61EE8345300BFAF05 /* RemoteBlog.m */; }; - 93C674EB1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C674E91EE8348F00BFAF05 /* RemoteBlogOptionsHelper.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 93C674EC1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 93C674EA1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.m */; }; 93C674EE1EE834B700BFAF05 /* RemoteBlogSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93C674ED1EE834B700BFAF05 /* RemoteBlogSettings.swift */; }; 93C674F11EE8351E00BFAF05 /* NSMutableDictionary+Helpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 93C674EF1EE8351E00BFAF05 /* NSMutableDictionary+Helpers.h */; }; 93C674F21EE8351E00BFAF05 /* NSMutableDictionary+Helpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 93C674F01EE8351E00BFAF05 /* NSMutableDictionary+Helpers.m */; }; @@ -786,6 +784,8 @@ 46ABD0E9262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppTransportSecuritySettingsTests.swift; sourceTree = ""; }; 4A1DEF43293051BC00322608 /* LoggingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingTests.swift; sourceTree = ""; }; 4A1DEF45293051C600322608 /* LoggingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LoggingTests.m; sourceTree = ""; }; + 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlog.swift; sourceTree = ""; }; + 4A68E3CE29404289004AC3DC /* RemoteBlogOptionsHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlogOptionsHelper.swift; sourceTree = ""; }; 57BCD3D326209D9500292CB3 /* AppTransportSecuritySettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppTransportSecuritySettings.swift; sourceTree = ""; }; 6C2A33D76FD1052D6F30466D /* Pods-WordPressKit.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressKit.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressKit/Pods-WordPressKit.debug.xcconfig"; sourceTree = ""; }; 6F2E0CC4FA01B5475A378DA2 /* Pods-WordPressKitTests.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressKitTests.release-alpha.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressKitTests/Pods-WordPressKitTests.release-alpha.xcconfig"; sourceTree = ""; }; @@ -1105,10 +1105,6 @@ 93BD27791EE73944002BB00B /* WordPressOrgXMLRPCApi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCApi.swift; sourceTree = ""; }; 93BD277A1EE73944002BB00B /* WordPressOrgXMLRPCValidator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCValidator.swift; sourceTree = ""; }; 93BD277B1EE73944002BB00B /* WordPressRSDParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressRSDParser.swift; sourceTree = ""; }; - 93C674E51EE8345300BFAF05 /* RemoteBlog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RemoteBlog.h; sourceTree = ""; }; - 93C674E61EE8345300BFAF05 /* RemoteBlog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RemoteBlog.m; sourceTree = ""; }; - 93C674E91EE8348F00BFAF05 /* RemoteBlogOptionsHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RemoteBlogOptionsHelper.h; sourceTree = ""; }; - 93C674EA1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RemoteBlogOptionsHelper.m; sourceTree = ""; }; 93C674ED1EE834B700BFAF05 /* RemoteBlogSettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlogSettings.swift; sourceTree = ""; }; 93C674EF1EE8351E00BFAF05 /* NSMutableDictionary+Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableDictionary+Helpers.h"; sourceTree = ""; }; 93C674F01EE8351E00BFAF05 /* NSMutableDictionary+Helpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableDictionary+Helpers.m"; sourceTree = ""; }; @@ -1930,13 +1926,11 @@ 826016F21F9FA17B00533B6C /* Activity.swift */, 436D56342118D85800CEAA33 /* WPCountry.swift */, 436D5640211B7F4400CEAA33 /* DomainContactInformation.swift */, - 93C674E51EE8345300BFAF05 /* RemoteBlog.h */, - 93C674E61EE8345300BFAF05 /* RemoteBlog.m */, + 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */, 82FFBF511F45F04100F4573F /* RemoteBlogJetpackMonitorSettings.swift */, 8236EB4C2024B9F8007C7CF9 /* RemoteBlogJetpackModulesSettings.swift */, 82FFBF4F1F45EFD100F4573F /* RemoteBlogJetpackSettings.swift */, - 93C674E91EE8348F00BFAF05 /* RemoteBlogOptionsHelper.h */, - 93C674EA1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.m */, + 4A68E3CE29404289004AC3DC /* RemoteBlogOptionsHelper.swift */, 74585B961F0D54B400E7E667 /* RemoteDomain.swift */, 93C674ED1EE834B700BFAF05 /* RemoteBlogSettings.swift */, 74BA04F71F06DC3900ED5CD8 /* RemoteComment.h */, @@ -2549,7 +2543,6 @@ 74DA56371F06EB0500FE9BF4 /* MediaServiceRemote.h in Headers */, 74DA56321F06EAF000FE9BF4 /* MediaServiceRemoteREST.h in Headers */, 74DA563A1F06EB3000FE9BF4 /* RemoteMedia.h in Headers */, - 93C674E71EE8345300BFAF05 /* RemoteBlog.h in Headers */, 74BA04F91F06DC3900ED5CD8 /* RemoteComment.h in Headers */, 74BA04F21F06DC0A00ED5CD8 /* CommentServiceRemote.h in Headers */, 74BA04F51F06DC0A00ED5CD8 /* CommentServiceRemoteXMLRPC.h in Headers */, @@ -2586,7 +2579,6 @@ 9309995B1F16616A00F006A1 /* RemoteTheme.h in Headers */, 1A4F98672279A87D00D86E8E /* WPKit-Swift.h in Headers */, 93F50A371F226B9300B5BEBA /* WordPressComServiceRemote.h in Headers */, - 93C674EB1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -3147,12 +3139,10 @@ 7E3E7A4C20E443AA0075D159 /* NSMutableParagraphStyle+extensions.swift in Sources */, 40F98809221AC88700B7B369 /* StatsPostingStreakInsight.swift in Sources */, 465F889E263B0C5500F4C950 /* BlockEditorSettingsServiceRemote.swift in Sources */, - 93C674EC1EE8348F00BFAF05 /* RemoteBlogOptionsHelper.m in Sources */, E632D7781F6E047400297F6D /* SocialLogin2FANonceInfo.swift in Sources */, 32FC1D29255C91ED00CD0A7B /* JetpackScanServiceRemote.swift in Sources */, 9F3E0B9B208732B3009CB5BA /* RemoteReaderSiteInfoSubscription.swift in Sources */, 7403A2E41EF06ED500DED7DC /* AccountSettingsRemote.swift in Sources */, - 93C674E81EE8345300BFAF05 /* RemoteBlog.m in Sources */, 3236F77824AE34B40088E8F3 /* ReaderTopicServiceRemote+Interests.swift in Sources */, FE20A6A4282A96C00025E975 /* RemoteBloggingPromptsSettings.swift in Sources */, 93BD27831EE73944002BB00B /* WordPressRSDParser.swift in Sources */, @@ -3208,6 +3198,7 @@ E1D6B558200E473A00325669 /* TimeZoneServiceRemote.swift in Sources */, 1769DEAA24729AFF00F42EFC /* HomepageSettingsServiceRemote.swift in Sources */, 93BD273D1EE73282002BB00B /* AccountServiceRemoteREST.m in Sources */, + 4A68E3CF29404289004AC3DC /* RemoteBlogOptionsHelper.swift in Sources */, 82FFBF501F45EFD100F4573F /* RemoteBlogJetpackSettings.swift in Sources */, 74650F741F0EA1E200188EDB /* RemoteGravatarProfile.swift in Sources */, 40E7FEB4221063480032834E /* StatsTodayInsight.swift in Sources */, @@ -3240,6 +3231,7 @@ 74E2295B1F1E77290085F7F2 /* KeyringConnection.swift in Sources */, 74B5F0DA1EF8299B00B411E7 /* BlogServiceRemoteXMLRPC.m in Sources */, E1EF5D5D1F9F329900B6D53E /* SitePluginCapabilities.swift in Sources */, + 4A68E3CD29404181004AC3DC /* RemoteBlog.swift in Sources */, 7E3E7A4520E443060075D159 /* NSAttributedString+extensions.swift in Sources */, 40E7FEAE220FAEA10032834E /* StatsPublicizeInsight.swift in Sources */, 93F50A411F227C9700B5BEBA /* RemoteProfile.swift in Sources */, diff --git a/WordPressKit/AccountServiceRemoteREST.m b/WordPressKit/AccountServiceRemoteREST.m index 08e91f8a..a4567c5f 100644 --- a/WordPressKit/AccountServiceRemoteREST.m +++ b/WordPressKit/AccountServiceRemoteREST.m @@ -1,6 +1,4 @@ #import "AccountServiceRemoteREST.h" -#import "RemoteBlog.h" -#import "RemoteBlogOptionsHelper.h" #import "WPKit-Swift.h" @import NSObject_SafeExpectations; @import WordPressShared; diff --git a/WordPressKit/RemoteBlog.h b/WordPressKit/RemoteBlog.h deleted file mode 100644 index 5045acdd..00000000 --- a/WordPressKit/RemoteBlog.h +++ /dev/null @@ -1,102 +0,0 @@ -#import - - -/** - * @class RemoteBlog - * @brief This class encapsulates all of the *remote* Blog properties - */ - -@interface RemoteBlog : NSObject - -/** - * @details The ID of the Blog entity. - */ -@property (nonatomic, copy) NSNumber *blogID; - -/** - * @details The organization ID of the Blog entity. - */ -@property (nonatomic, copy) NSNumber *organizationID; - -/** - * @details Represents the Blog Name. - */ -@property (nonatomic, copy) NSString *name; - -/** - * @details Description of the WordPress Blog. - */ -@property (nonatomic, copy) NSString *tagline; - -/** - * @details Represents the Blog Name. - */ -@property (nonatomic, copy) NSString *url; - -/** - * @details Maps to the XMLRPC endpoint. - */ -@property (nonatomic, copy) NSString *xmlrpc; - -/** - * @details Site Icon's URL. - */ -@property (nonatomic, copy) NSString *icon; - -/** - * @details Product ID of the site's current plan, if it has one. - */ -@property (nonatomic, copy) NSNumber *planID; - -/** - * @details Product name of the site's current plan, if it has one. - */ -@property (nonatomic, copy) NSString *planTitle; - -/** - * @details Indicates whether the current's blog plan is paid, or not. - */ -@property (nonatomic, assign) BOOL hasPaidPlan; - -/** - * @details Indicates whether it's a jetpack site, or not. - */ -@property (nonatomic, assign) BOOL jetpack; - -/** - * @details Boolean indicating whether the current user has Admin privileges, or not. - */ -@property (nonatomic, assign) BOOL isAdmin; - -/** - * @details Blog's visibility preferences. - */ -@property (nonatomic, assign) BOOL visible; - -/** - * @details Blog's options preferences. - */ -@property (nonatomic, strong) NSDictionary *options; - -/** - * @details Blog's capabilities: Indicate which actions are allowed / not allowed, for the current user. - */ -@property (nonatomic, strong) NSDictionary *capabilities; - -/** - * @Blog's total disk quota space. - */ -@property (nonatomic, copy) NSNumber *quotaSpaceAllowed; - -/** -* @Blog's total disk quota space used. -*/ -@property (nonatomic, copy) NSNumber *quotaSpaceUsed; - - -/** - * @details Parses details from a JSON dictionary, as returned by the WordPress.com REST API. - */ -- (instancetype)initWithJSONDictionary:(NSDictionary *)json; - -@end diff --git a/WordPressKit/RemoteBlog.m b/WordPressKit/RemoteBlog.m deleted file mode 100644 index 5616e0e1..00000000 --- a/WordPressKit/RemoteBlog.m +++ /dev/null @@ -1,48 +0,0 @@ -#import "RemoteBlog.h" -#import "RemoteBlogOptionsHelper.h" -@import NSObject_SafeExpectations; - -@implementation RemoteBlog - -- (instancetype)initWithJSONDictionary:(NSDictionary *)json -{ - if (self = [super init]) { - _blogID = [json numberForKey:@"ID"]; - _organizationID = [json numberForKey:@"organization_id"]; - _name = [json stringForKey:@"name"]; - _tagline = [json stringForKey:@"description"]; - _url = [json stringForKey:@"URL"]; - _xmlrpc = [json stringForKeyPath:@"meta.links.xmlrpc"]; - _jetpack = [[json numberForKey:@"jetpack"] boolValue]; - _icon = [json stringForKeyPath:@"icon.img"]; - _capabilities = [json dictionaryForKey:@"capabilities"]; - _isAdmin = [[json numberForKeyPath:@"capabilities.manage_options"] boolValue]; - _visible = [[json numberForKey:@"visible"] boolValue]; - _options = [RemoteBlogOptionsHelper mapOptionsFromResponse:json]; - _planID = [json numberForKeyPath:@"plan.product_id"]; - _planTitle = [json stringForKeyPath:@"plan.product_name_short"]; - _hasPaidPlan = ![json numberForKeyPath:@"plan.is_free"].boolValue; - _quotaSpaceAllowed = [[json numberForKeyPath:@"quota.space_allowed"] copy]; - _quotaSpaceUsed = [[json numberForKeyPath:@"quota.space_used"] copy]; - } - - return self; -} - -- (NSString *)description -{ - NSDictionary *properties = @{ - @"blogID" : self.blogID, - @"organizationID": self.organizationID, - @"name" : self.name, - @"url" : self.url, - @"xmlrpc" : self.xmlrpc, - @"jetpack" : self.jetpack ? @"YES" : @"NO", - @"icon" : self.icon ? self.icon : @"", - @"visible" : self.visible ? @"YES" : @"NO", - }; - - return [NSString stringWithFormat:@"<%@: %p> (%@)", NSStringFromClass([self class]), self, properties]; -} - -@end diff --git a/WordPressKit/RemoteBlog.swift b/WordPressKit/RemoteBlog.swift new file mode 100644 index 00000000..bebc67e3 --- /dev/null +++ b/WordPressKit/RemoteBlog.swift @@ -0,0 +1,79 @@ +import Foundation +import NSObject_SafeExpectations + +/// This class encapsulates all of the *remote* Blog properties +@objcMembers public class RemoteBlog: NSObject { + + /// The ID of the Blog entity. + public var blogID: NSNumber? + + /// The organization ID of the Blog entity. + public var organizationID: NSNumber? + + /// Represents the Blog Name. + public var name: String? + + /// Description of the WordPress Blog. + public var tagline: String? + + /// Represents the Blog Name. + public var url: String? + + /// Maps to the XMLRPC endpoint. + public var xmlrpc: String? + + /// Site Icon's URL. + public var icon: String? + + /// Product ID of the site's current plan, if it has one. + public var planID: NSNumber? + + /// Product name of the site's current plan, if it has one. + public var planTitle: String? + + /// Indicates whether the current's blog plan is paid, or not. + public var hasPaidPlan: Bool = false + + /// Indicates whether it's a jetpack site, or not. + public var jetpack: Bool = false + + /// Boolean indicating whether the current user has Admin privileges, or not. + public var isAdmin: Bool = false + + /// Blog's visibility preferences. + public var visible: Bool = false + + /// Blog's options preferences. + public var options: NSDictionary? + + /// Blog's capabilities: Indicate which actions are allowed / not allowed, for the current user. + public var capabilities: NSDictionary? + + /// Blog's total disk quota space. + public var quotaSpaceAllowed: NSNumber? + + /// Blog's total disk quota space used. + public var quotaSpaceUsed: NSNumber? + + /// Parses details from a JSON dictionary, as returned by the WordPress.com REST API. + public init(JSONDictionary json: NSDictionary) { + self.blogID = json.number(forKey: "ID") + self.organizationID = json.number(forKey: "organization_id") + self.name = json.string(forKey: "name") + self.tagline = json.string(forKey: "description") + self.url = json.string(forKey: "URL") + self.xmlrpc = json.string(forKeyPath: "meta.links.xmlrpc") + self.jetpack = json.number(forKey: "jetpack")?.boolValue ?? false + self.icon = json.string(forKeyPath: "icon.img") + self.capabilities = json.object(forKey: "capabilities") as? NSDictionary + self.isAdmin = json.number(forKeyPath: "capabilities.manage_options")?.boolValue ?? false + self.visible = json.number(forKey: "visible")?.boolValue ?? false + self.options = RemoteBlogOptionsHelper.mapOptions(fromResponse: json) + self.planID = json.number(forKeyPath: "plan.product_id") + self.planTitle = json.string(forKeyPath: "plan.product_name_short") + self.hasPaidPlan = json.number(forKeyPath: "plan.is_free")?.boolValue ?? false + self.quotaSpaceAllowed = json.number(forKeyPath: "quota.space_allowed") + self.quotaSpaceUsed = json.number(forKeyPath: "quota.space_used") + } + +} diff --git a/WordPressKit/RemoteBlogOptionsHelper.h b/WordPressKit/RemoteBlogOptionsHelper.h deleted file mode 100644 index 0da0c503..00000000 --- a/WordPressKit/RemoteBlogOptionsHelper.h +++ /dev/null @@ -1,15 +0,0 @@ -#import - -@class RemoteBlogSettings; - -@interface RemoteBlogOptionsHelper : NSObject - -+ (NSDictionary *)mapOptionsFromResponse:(NSDictionary *)response; - -// Helper methods for converting between XMLRPC dictionaries and RemoteBlogSettings -// Currently, we are only ever updating the blog title or tagline through XMLRPC -// Brent - Jan 7, 2017 -+ (NSDictionary *)remoteOptionsForUpdatingBlogTitleAndTagline:(RemoteBlogSettings *)blogSettings; -+ (RemoteBlogSettings *)remoteBlogSettingsFromXMLRPCDictionaryOptions:(NSDictionary *)json; - -@end diff --git a/WordPressKit/RemoteBlogOptionsHelper.m b/WordPressKit/RemoteBlogOptionsHelper.m deleted file mode 100644 index 48aaf291..00000000 --- a/WordPressKit/RemoteBlogOptionsHelper.m +++ /dev/null @@ -1,77 +0,0 @@ -#import "RemoteBlogOptionsHelper.h" -#import "NSMutableDictionary+Helpers.h" -#import "WPKit-Swift.h" -@import NSObject_SafeExpectations; -@import WordPressShared; - -@implementation RemoteBlogOptionsHelper - -// Formats blog options retrieved from REST queries -+ (NSDictionary *)mapOptionsFromResponse:(NSDictionary *)response -{ - NSMutableDictionary *options = [NSMutableDictionary dictionary]; - options[@"home_url"] = response[@"URL"]; - if ([[response numberForKey:@"jetpack"] boolValue]) { - options[@"jetpack_client_id"] = [response numberForKey:@"ID"]; - } - if ( response[@"options"] ) { - options[@"post_thumbnail"] = [response valueForKeyPath:@"options.featured_images_enabled"]; - NSArray *optionsDirectMapKeys = @[ - @"active_modules", - @"admin_url", - @"login_url", - @"unmapped_url", - @"image_default_link_type", - @"software_version", - @"videopress_enabled", - @"timezone", - @"gmt_offset", - @"allowed_file_types", - @"frame_nonce", - @"jetpack_version", - @"is_automated_transfer", - @"blog_public", - @"max_upload_size", - @"is_wpcom_atomic", - @"is_wpforteams_site", - @"show_on_front", - @"page_on_front", - @"page_for_posts", - @"blogging_prompts_settings" - ]; - - for (NSString *key in optionsDirectMapKeys) { - NSString *sourceKeyPath = [NSString stringWithFormat:@"options.%@", key]; - if ([response valueForKeyPath:sourceKeyPath] != nil) { - options[key] = [response valueForKeyPath:sourceKeyPath]; - } - } - } - NSMutableDictionary *valueOptions = [NSMutableDictionary dictionaryWithCapacity:options.count]; - [options enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { - valueOptions[key] = @{@"value": obj}; - }]; - - return [NSDictionary dictionaryWithDictionary:valueOptions ]; -} - -+ (NSDictionary *)remoteOptionsForUpdatingBlogTitleAndTagline:(RemoteBlogSettings *)blogSettings -{ - NSMutableDictionary *options = [NSMutableDictionary dictionary]; - [options setValueIfNotNil:blogSettings.name forKey:@"blog_title"]; - [options setValueIfNotNil:blogSettings.tagline forKey:@"blog_tagline"]; - return options; -} - -+ (RemoteBlogSettings *)remoteBlogSettingsFromXMLRPCDictionaryOptions:(NSDictionary *)options -{ - RemoteBlogSettings *remoteSettings = [RemoteBlogSettings new]; - remoteSettings.name = [[options stringForKeyPath:@"blog_title.value"] stringByDecodingXMLCharacters]; - remoteSettings.tagline = [[options stringForKeyPath:@"blog_tagline.value"] stringByDecodingXMLCharacters]; - if (options[@"blog_public"]) { - remoteSettings.privacy = [options numberForKeyPath:@"blog_public.value"]; - } - return remoteSettings; -} - -@end diff --git a/WordPressKit/RemoteBlogOptionsHelper.swift b/WordPressKit/RemoteBlogOptionsHelper.swift new file mode 100644 index 00000000..dd463cc7 --- /dev/null +++ b/WordPressKit/RemoteBlogOptionsHelper.swift @@ -0,0 +1,77 @@ +import Foundation + +@objcMembers public class RemoteBlogOptionsHelper: NSObject { + + public class func mapOptions(fromResponse response: NSDictionary) -> NSDictionary { + let options = NSMutableDictionary() + options["home_url"] = response["URL"] + if response.number(forKey: "jetpack")?.boolValue == true { + options["jetpack_client_id"] = response.number(forKey: "ID") + } + if response["options"] != nil { + options["post_thumbnail"] = response.value(forKeyPath: "options.featured_images_enabled") + + let optionsDirectMapKeys = [ + "active_modules", + "admin_url", + "login_url", + "unmapped_url", + "image_default_link_type", + "software_version", + "videopress_enabled", + "timezone", + "gmt_offset", + "allowed_file_types", + "frame_nonce", + "jetpack_version", + "is_automated_transfer", + "blog_public", + "max_upload_size", + "is_wpcom_atomic", + "is_wpforteams_site", + "show_on_front", + "page_on_front", + "page_for_posts", + "blogging_prompts_settings" + ] + for key in optionsDirectMapKeys { + if let value = response.value(forKeyPath: "options.\(key)") { + options[key] = value + } + } + } + let valueOptions = NSMutableDictionary(capacity: options.count) + for (key, obj) in options { + valueOptions[key] = [ + "value": obj + ] + } + + return NSDictionary(dictionary: valueOptions) + } + + // Helper methods for converting between XMLRPC dictionaries and RemoteBlogSettings + // Currently, we are only ever updating the blog title or tagline through XMLRPC + // Brent - Jan 7, 2017 + public class func remoteOptionsForUpdatingBlogTitleAndTagline(_ blogSettings: RemoteBlogSettings) -> NSDictionary { + let options = NSMutableDictionary() + if let value = blogSettings.name { + options["blog_title"] = value + } + if let value = blogSettings.tagline { + options["blog_tagline"] = value + } + return options + } + + public class func remoteBlogSettings(fromXMLRPCDictionaryOptions options: NSDictionary) -> RemoteBlogSettings { + let remoteSettings = RemoteBlogSettings() + remoteSettings.name = options.string(forKeyPath: "blog_title.value")?.stringByDecodingXMLCharacters() + remoteSettings.tagline = options.string(forKeyPath: "blog_tagline.value")?.stringByDecodingXMLCharacters() + if options["blog_public"] != nil { + remoteSettings.privacy = options.number(forKeyPath: "blog_public.value") + } + return remoteSettings + } + +} diff --git a/WordPressKit/WordPressKit.h b/WordPressKit/WordPressKit.h index f65dd7c2..fc54a290 100644 --- a/WordPressKit/WordPressKit.h +++ b/WordPressKit/WordPressKit.h @@ -35,8 +35,6 @@ FOUNDATION_EXPORT const unsigned char WordPressKitVersionString[]; #import #import -#import -#import #import #import #import diff --git a/WordPressKitTests/BlogServiceRemoteRESTTests.m b/WordPressKitTests/BlogServiceRemoteRESTTests.m index 69a7e260..fe3444e2 100644 --- a/WordPressKitTests/BlogServiceRemoteRESTTests.m +++ b/WordPressKitTests/BlogServiceRemoteRESTTests.m @@ -1,7 +1,6 @@ #import #import #import -#import "RemoteBlog.h" #import "BlogServiceRemoteREST.h" @import OHHTTPStubs; @import WordPressKit;