Skip to content

Commit 00f95c0

Browse files
committed
Implement init with JSON logic in RemoteBlogSettings in Swift
1 parent 23c2b34 commit 00f95c0

File tree

4 files changed

+113
-72
lines changed

4 files changed

+113
-72
lines changed

Sources/WordPressKit/Models/RemoteBlogOptionsHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import Foundation
6767
}
6868

6969
public class func remoteBlogSettings(fromXMLRPCDictionaryOptions options: NSDictionary) -> RemoteBlogSettings {
70-
let remoteSettings = RemoteBlogSettings()
70+
let remoteSettings = RemoteBlogSettings(jsonDictionary: [:])
7171
remoteSettings.name = options.string(forKeyPath: "blog_title.value")?.stringByDecodingXMLCharacters()
7272
remoteSettings.tagline = options.string(forKeyPath: "blog_tagline.value")?.stringByDecodingXMLCharacters()
7373
if options["blog_public"] != nil {

Sources/WordPressKit/Models/RemoteBlogSettings.swift

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,114 @@ public class RemoteBlogSettings: NSObject {
186186
///
187187
@objc public var sharingDisabledReblogs: NSNumber?
188188

189+
// Defined as CodingKey-conforming already to simplify Codable support in the future.
190+
enum CodingKeys: String, CodingKey {
191+
case name = "name"
192+
case tagline = "description"
193+
case privacy = "blog_public"
194+
case languageID = "lang_id"
195+
case iconMediaID = "site_icon"
196+
case gmtOffset = "gmt_offset"
197+
case timezoneString = "timezone_string"
198+
case settings = "settings"
199+
case defaultCategory = "default_category"
200+
case defaultPostFormat = "default_post_format"
201+
case dateFormat = "date_format"
202+
case timeFormat = "time_format"
203+
case startOfWeek = "start_of_week"
204+
case postsPerPage = "posts_per_page"
205+
case commentsAllowed = "default_comment_status"
206+
case commentsBlocklistKeys = "blacklist_keys"
207+
case commentsCloseAutomatically = "close_comments_for_old_posts"
208+
case commentsCloseAutomaticallyAfterDays = "close_comments_days_old"
209+
case commentsKnownUsersAllowlist = "comment_whitelist"
210+
case commentsMaxLinks = "comment_max_links"
211+
case commentsModerationKeys = "moderation_keys"
212+
case commentsPagingEnabled = "page_comments"
213+
case commentsPageSize = "comments_per_page"
214+
case commentsRequireModeration = "comment_moderation"
215+
case commentsRequireNameAndEmail = "require_name_email"
216+
case commentsRequireRegistration = "comment_registration"
217+
case commentsSortOrder = "comment_order"
218+
case commentsThreadingEnabled = "thread_comments"
219+
case commentsThreadingDepth = "thread_comments_depth"
220+
case pingbackOutbound = "default_pingback_flag"
221+
case pingbackInbound = "default_ping_status"
222+
case relatedPostsAllowed = "jetpack_relatedposts_allowed"
223+
case relatedPostsEnabled = "jetpack_relatedposts_enabled"
224+
case relatedPostsShowHeadline = "jetpack_relatedposts_show_headline"
225+
case relatedPostsShowThumbnails = "jetpack_relatedposts_show_thumbnails"
226+
case ampSupported = "amp_is_supported"
227+
case ampEnabled = "amp_is_enabled"
228+
229+
case sharingButtonStyle = "sharing_button_style"
230+
case sharingLabel = "sharing_label"
231+
case sharingTwitterName = "twitter_via"
232+
case sharingCommentLikesEnabled = "jetpack_comment_likes_enabled"
233+
case sharingDisabledLikes = "disabled_likes"
234+
case sharingDisabledReblogs = "disabled_reblogs"
235+
}
236+
237+
/// Parses details from a JSON dictionary, as returned by the WordPress.com REST API.
238+
@objc
239+
public init(jsonDictionary json: NSDictionary) {
240+
let rawSettings = json.object(forKey: CodingKeys.settings.rawValue) as? NSDictionary ?? [:]
241+
242+
name = json.string(forKey: CodingKeys.name.rawValue)
243+
tagline = json.string(forKey: CodingKeys.tagline.rawValue)
244+
privacy = rawSettings.number(forKey: CodingKeys.privacy.rawValue)
245+
languageID = rawSettings.number(forKey: CodingKeys.languageID.rawValue)
246+
iconMediaID = rawSettings.number(forKey: CodingKeys.iconMediaID.rawValue)
247+
gmtOffset = rawSettings.number(forKey: CodingKeys.gmtOffset.rawValue)
248+
timezoneString = rawSettings.string(forKey: CodingKeys.timezoneString.rawValue)
249+
250+
defaultCategoryID = rawSettings.number(forKey: CodingKeys.defaultCategory.rawValue) ?? 1
251+
let defaultPostFormatValue = rawSettings.object(forKey: CodingKeys.defaultPostFormat.rawValue)
252+
if let defaultPostFormatNumber = defaultPostFormatValue as? NSNumber, defaultPostFormatNumber == 0 ||
253+
defaultPostFormatValue as? String == "0" {
254+
defaultPostFormat = "standard"
255+
} else {
256+
defaultPostFormat = rawSettings.string(forKey: CodingKeys.defaultPostFormat.rawValue)
257+
}
258+
dateFormat = rawSettings.string(forKey: CodingKeys.dateFormat.rawValue)
259+
timeFormat = rawSettings.string(forKey: CodingKeys.timeFormat.rawValue)
260+
startOfWeek = rawSettings.string(forKey: CodingKeys.startOfWeek.rawValue)
261+
postsPerPage = rawSettings.number(forKey: CodingKeys.postsPerPage.rawValue)
262+
263+
commentsAllowed = rawSettings.number(forKey: CodingKeys.commentsAllowed.rawValue)
264+
commentsBlocklistKeys = rawSettings.string(forKey: CodingKeys.commentsBlocklistKeys.rawValue)
265+
commentsCloseAutomatically = rawSettings.number(forKey: CodingKeys.commentsCloseAutomatically.rawValue)
266+
commentsCloseAutomaticallyAfterDays = rawSettings.number(forKey: CodingKeys.commentsCloseAutomaticallyAfterDays.rawValue)
267+
commentsFromKnownUsersAllowlisted = rawSettings.number(forKey: CodingKeys.commentsKnownUsersAllowlist.rawValue)
268+
commentsMaximumLinks = rawSettings.number(forKey: CodingKeys.commentsMaxLinks.rawValue)
269+
commentsModerationKeys = rawSettings.string(forKey: CodingKeys.commentsModerationKeys.rawValue)
270+
commentsPagingEnabled = rawSettings.number(forKey: CodingKeys.commentsPagingEnabled.rawValue)
271+
commentsPageSize = rawSettings.number(forKey: CodingKeys.commentsPageSize.rawValue)
272+
commentsRequireManualModeration = rawSettings.number(forKey: CodingKeys.commentsRequireModeration.rawValue)
273+
commentsRequireNameAndEmail = rawSettings.number(forKey: CodingKeys.commentsRequireNameAndEmail.rawValue)
274+
commentsRequireRegistration = rawSettings.number(forKey: CodingKeys.commentsRequireRegistration.rawValue)
275+
commentsSortOrder = rawSettings.string(forKey: CodingKeys.commentsSortOrder.rawValue)
276+
commentsThreadingEnabled = rawSettings.number(forKey: CodingKeys.commentsThreadingEnabled.rawValue)
277+
commentsThreadingDepth = rawSettings.number(forKey: CodingKeys.commentsThreadingDepth.rawValue)
278+
pingbackOutboundEnabled = rawSettings.number(forKey: CodingKeys.pingbackOutbound.rawValue)
279+
pingbackInboundEnabled = rawSettings.number(forKey: CodingKeys.pingbackInbound.rawValue)
280+
281+
relatedPostsAllowed = rawSettings.number(forKey: CodingKeys.relatedPostsAllowed.rawValue)
282+
relatedPostsEnabled = rawSettings.number(forKey: CodingKeys.relatedPostsEnabled.rawValue)
283+
relatedPostsShowHeadline = rawSettings.number(forKey: CodingKeys.relatedPostsShowHeadline.rawValue)
284+
relatedPostsShowThumbnails = rawSettings.number(forKey: CodingKeys.relatedPostsShowThumbnails.rawValue)
285+
286+
ampSupported = rawSettings.number(forKey: CodingKeys.ampSupported.rawValue)
287+
ampEnabled = rawSettings.number(forKey: CodingKeys.ampEnabled.rawValue)
288+
289+
sharingButtonStyle = rawSettings.string(forKey: CodingKeys.sharingButtonStyle.rawValue)
290+
sharingLabel = rawSettings.string(forKey: CodingKeys.sharingLabel.rawValue)
291+
sharingTwitterName = rawSettings.string(forKey: CodingKeys.sharingTwitterName.rawValue)
292+
sharingCommentLikesEnabled = rawSettings.number(forKey: CodingKeys.sharingCommentLikesEnabled.rawValue)
293+
sharingDisabledLikes = rawSettings.number(forKey: CodingKeys.sharingDisabledLikes.rawValue)
294+
sharingDisabledReblogs = rawSettings.number(forKey: CodingKeys.sharingDisabledReblogs.rawValue)
295+
}
296+
189297
// MARK: - Helpers
190298

191299
/// Computed property, meant to help conversion from Remote / String-Based values, into their Integer counterparts

Sources/WordPressKit/Services/BlogServiceRemoteREST.m

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -376,74 +376,7 @@ - (RemotePostType *)remotePostTypeWithDictionary:(NSDictionary *)json
376376

377377
- (RemoteBlogSettings *)remoteBlogSettingFromJSONDictionary:(NSDictionary *)json
378378
{
379-
NSAssert([json isKindOfClass:[NSDictionary class]], @"Invalid Settings Kind");
380-
381-
RemoteBlogSettings *settings = [RemoteBlogSettings new];
382-
NSDictionary *rawSettings = [json dictionaryForKey:RemoteBlogSettingsKey];
383-
384-
// General
385-
settings.name = [json stringForKey:RemoteBlogNameKey];
386-
settings.tagline = [json stringForKey:RemoteBlogTaglineKey];
387-
settings.privacy = [rawSettings numberForKey:RemoteBlogPrivacyKey];
388-
settings.languageID = [rawSettings numberForKey:RemoteBlogLanguageKey];
389-
settings.iconMediaID = [rawSettings numberForKey:RemoteBlogIconKey];
390-
settings.gmtOffset = [rawSettings numberForKey:RemoteBlogGMTOffsetKey];
391-
settings.timezoneString = [rawSettings stringForKey:RemoteBlogTimezoneStringKey];
392-
393-
// Writing
394-
settings.defaultCategoryID = [rawSettings numberForKey:RemoteBlogDefaultCategoryKey] ?: @(RemoteBlogUncategorizedCategory);
395-
396-
// Note: the backend might send '0' as a number, OR a string value. Ref. Issue #4187
397-
if ([[rawSettings numberForKey:RemoteBlogDefaultPostFormatKey] isEqualToNumber:@(0)] ||
398-
[[rawSettings stringForKey:RemoteBlogDefaultPostFormatKey] isEqualToString:@"0"])
399-
{
400-
settings.defaultPostFormat = RemoteBlogDefaultPostFormat;
401-
} else {
402-
settings.defaultPostFormat = [rawSettings stringForKey:RemoteBlogDefaultPostFormatKey];
403-
}
404-
settings.dateFormat = [rawSettings stringForKey:RemoteBlogDateFormatKey];
405-
settings.timeFormat = [rawSettings stringForKey:RemoteBlogTimeFormatKey];
406-
settings.startOfWeek = [rawSettings stringForKey:RemoteBlogStartOfWeekKey];
407-
settings.postsPerPage = [rawSettings numberForKey:RemoteBlogPostsPerPageKey];
408-
409-
// Discussion
410-
settings.commentsAllowed = [rawSettings numberForKey:RemoteBlogCommentsAllowedKey];
411-
settings.commentsBlocklistKeys = [rawSettings stringForKey:RemoteBlogCommentsBlocklistKeys];
412-
settings.commentsCloseAutomatically = [rawSettings numberForKey:RemoteBlogCommentsCloseAutomaticallyKey];
413-
settings.commentsCloseAutomaticallyAfterDays = [rawSettings numberForKey:RemoteBlogCommentsCloseAutomaticallyAfterDaysKey];
414-
settings.commentsFromKnownUsersAllowlisted = [rawSettings numberForKey:RemoteBlogCommentsKnownUsersAllowlistKey];
415-
settings.commentsMaximumLinks = [rawSettings numberForKey:RemoteBlogCommentsMaxLinksKey];
416-
settings.commentsModerationKeys = [rawSettings stringForKey:RemoteBlogCommentsModerationKeys];
417-
settings.commentsPagingEnabled = [rawSettings numberForKey:RemoteBlogCommentsPagingEnabledKey];
418-
settings.commentsPageSize = [rawSettings numberForKey:RemoteBlogCommentsPageSizeKey];
419-
settings.commentsRequireManualModeration = [rawSettings numberForKey:RemoteBlogCommentsRequireModerationKey];
420-
settings.commentsRequireNameAndEmail = [rawSettings numberForKey:RemoteBlogCommentsRequireNameAndEmailKey];
421-
settings.commentsRequireRegistration = [rawSettings numberForKey:RemoteBlogCommentsRequireRegistrationKey];
422-
settings.commentsSortOrder = [rawSettings stringForKey:RemoteBlogCommentsSortOrderKey];
423-
settings.commentsThreadingEnabled = [rawSettings numberForKey:RemoteBlogCommentsThreadingEnabledKey];
424-
settings.commentsThreadingDepth = [rawSettings numberForKey:RemoteBlogCommentsThreadingDepthKey];
425-
settings.pingbackOutboundEnabled = [rawSettings numberForKey:RemoteBlogCommentsPingbackOutboundKey];
426-
settings.pingbackInboundEnabled = [rawSettings numberForKey:RemoteBlogCommentsPingbackInboundKey];
427-
428-
// Related Posts
429-
settings.relatedPostsAllowed = [rawSettings numberForKey:RemoteBlogRelatedPostsAllowedKey];
430-
settings.relatedPostsEnabled = [rawSettings numberForKey:RemoteBlogRelatedPostsEnabledKey];
431-
settings.relatedPostsShowHeadline = [rawSettings numberForKey:RemoteBlogRelatedPostsShowHeadlineKey];
432-
settings.relatedPostsShowThumbnails = [rawSettings numberForKey:RemoteBlogRelatedPostsShowThumbnailsKey];
433-
434-
// AMP
435-
settings.ampSupported = [rawSettings numberForKey:RemoteBlogAmpSupportedKey];
436-
settings.ampEnabled = [rawSettings numberForKey:RemoteBlogAmpEnabledKey];
437-
438-
// Sharing
439-
settings.sharingButtonStyle = [rawSettings stringForKey:RemoteBlogSharingButtonStyle];
440-
settings.sharingLabel = [rawSettings stringForKey:RemoteBlogSharingLabel];
441-
settings.sharingTwitterName = [rawSettings stringForKey:RemoteBlogSharingTwitterName];
442-
settings.sharingCommentLikesEnabled = [rawSettings numberForKey:RemoteBlogSharingCommentLikesEnabled];
443-
settings.sharingDisabledLikes = [rawSettings numberForKey:RemoteBlogSharingDisabledLikes];
444-
settings.sharingDisabledReblogs = [rawSettings numberForKey:RemoteBlogSharingDisabledReblogs];
445-
446-
return settings;
379+
return [[RemoteBlogSettings alloc] initWithJsonDictionary:json];
447380
}
448381

449382
- (NSDictionary *)remoteSettingsToDictionary:(RemoteBlogSettings *)settings

Tests/WordPressKitTests/Tests/RemoteBlogSettingsTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import XCTest
33

44
// The aim of these tests is not to be comprehensive, but to help with migrating the decoding and encoding logic from Objective-C to Swift without regression.
5+
// (The logic was originally part of BlogServiceRemoteREST).
6+
// They remain in the suite after the conversion to Swift to prevent future regression and provide a ready made harness where to add more tests.
57
final class RemoteBlogSettingsTests: XCTestCase {
68

79
func testInitWithJSON() throws {
810
let json = try loadJSONSettings()
9-
// FIXME: The init logic is currently in BlogServiceRemoteREST. We'll test it from there first, then update the test with the new location.
10-
let blogService = BlogServiceRemoteREST(wordPressComRestApi: MockWordPressComRestApi(), siteID: 0)
11-
let settings = try XCTUnwrap(blogService.remoteBlogSetting(fromJSONDictionary: json))
11+
let settings = RemoteBlogSettings(jsonDictionary: json as NSDictionary)
1212

1313
// Root properties
1414
XCTAssertEqual(settings.name, "My Epic Blog")

0 commit comments

Comments
 (0)