Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit a8539c9

Browse files
committed
Merge branch 'develop' into issue/post-editor-settings
2 parents 8c4928b + 64a002f commit a8539c9

File tree

6 files changed

+80
-31
lines changed

6 files changed

+80
-31
lines changed

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressKit"
3-
s.version = "4.3.0-beta.2"
3+
s.version = "4.3.0-beta.4"
44
s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."
55

66
s.description = <<-DESC

WordPressKit/AccountSettings.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public struct AccountSettings {
99

1010
// MARK: - Account Settings
1111
public let username: String // user_login
12+
public let usernameCanBeChanged: Bool // user_login_can_be_changed
1213
public let email: String // user_email
1314
public let emailPendingAddress: String? // new_user_email
1415
public let emailPendingChange: Bool // user_email_change_pending
@@ -22,6 +23,7 @@ public struct AccountSettings {
2223
displayName: String,
2324
aboutMe: String,
2425
username: String,
26+
usernameCanBeChanged: Bool,
2527
email: String,
2628
emailPendingAddress: String?,
2729
emailPendingChange: Bool,
@@ -34,6 +36,7 @@ public struct AccountSettings {
3436
self.displayName = displayName
3537
self.aboutMe = aboutMe
3638
self.username = username
39+
self.usernameCanBeChanged = usernameCanBeChanged
3740
self.email = email
3841
self.emailPendingAddress = emailPendingAddress
3942
self.emailPendingChange = emailPendingChange

WordPressKit/AccountSettingsRemote.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
9090
})
9191
}
9292

93+
/// Validate the current user's username
94+
///
95+
/// - Parameters:
96+
/// - username: The new username
97+
/// - success: block for success
98+
/// - failure: block for failure
99+
public func validateUsername(to username: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
100+
let endpoint = "me/username/validate/\(username)"
101+
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
102+
103+
wordPressComRestApi.GET(path,
104+
parameters: nil,
105+
success: { responseObject, httpResponse in
106+
// The success block needs to be changed if
107+
// any allowed_actions is required
108+
// by the changeUsername API
109+
success()
110+
},
111+
failure: { error, httpResponse in
112+
failure(error)
113+
})
114+
}
115+
93116
public func suggestUsernames(base: String, finished: @escaping ([String]) -> Void) {
94117
let endpoint = "wpcom/v2/users/username/suggestions"
95118
let parameters = ["name": base]
@@ -131,6 +154,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
131154
let displayName = response["display_name"] as? String,
132155
let aboutMe = response["description"] as? String,
133156
let username = response["user_login"] as? String,
157+
let usernameCanBeChanged = response["user_login_can_be_changed"] as? Bool,
134158
let email = response["user_email"] as? String,
135159
let emailPendingAddress = response["new_user_email"] as? String?,
136160
let emailPendingChange = response["user_email_change_pending"] as? Bool,
@@ -149,6 +173,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
149173
displayName: displayName,
150174
aboutMe: aboutMeText!,
151175
username: username,
176+
usernameCanBeChanged: usernameCanBeChanged,
152177
email: email,
153178
emailPendingAddress: emailPendingAddress,
154179
emailPendingChange: emailPendingChange,

WordPressKit/JetpackServiceRemote.swift

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import Foundation
22

3-
public enum JetpackInstallError: String, Error {
4-
case invalidCredentials = "INVALID_CREDENTIALS"
5-
case forbidden = "FORBIDDEN"
6-
case installFailure = "INSTALL_FAILURE"
7-
case installResponseError = "INSTALL_RESPONSE_ERROR"
8-
case loginFailure = "LOGIN_FAILURE"
9-
case siteIsJetpack = "SITE_IS_JETPACK"
10-
case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE"
11-
case activationResponseError = "ACTIVATION_RESPONSE_ERROR"
12-
case activationFailure = "ACTIVATION_FAILURE"
13-
case unknown
14-
15-
init(error key: String) {
16-
self = JetpackInstallError(rawValue: key) ?? .unknown
3+
public struct JetpackInstallError: LocalizedError, Equatable {
4+
public enum ErrorType: String {
5+
case invalidCredentials = "INVALID_CREDENTIALS"
6+
case forbidden = "FORBIDDEN"
7+
case installFailure = "INSTALL_FAILURE"
8+
case installResponseError = "INSTALL_RESPONSE_ERROR"
9+
case loginFailure = "LOGIN_FAILURE"
10+
case siteIsJetpack = "SITE_IS_JETPACK"
11+
case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE"
12+
case activationResponseError = "ACTIVATION_RESPONSE_ERROR"
13+
case activationFailure = "ACTIVATION_FAILURE"
14+
case unknown
15+
16+
init(error key: String) {
17+
self = ErrorType(rawValue: key) ?? .unknown
18+
}
19+
}
20+
21+
public var title: String?
22+
public var code: Int
23+
public var type: ErrorType
24+
25+
public static var unknown: JetpackInstallError {
26+
return JetpackInstallError(type: .unknown)
27+
}
28+
29+
public init(title: String? = nil, code: Int = 0, key: String? = nil) {
30+
self.init(title: title, code: code, type: ErrorType(error: key ?? ""))
31+
}
32+
33+
public init(title: String? = nil, code: Int = 0, type: ErrorType = .unknown) {
34+
self.title = title
35+
self.code = code
36+
self.type = type
1737
}
1838
}
1939

@@ -61,14 +81,14 @@ public class JetpackServiceRemote: ServiceRemoteWordPressComREST {
6181
let success = response[Constants.status] {
6282
completion(success, nil)
6383
} else {
64-
completion(false, .installResponseError)
84+
completion(false, JetpackInstallError(type: .installResponseError))
6585
}
6686
}) { (error: NSError, httpResponse: HTTPURLResponse?) in
67-
if let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String {
68-
completion(false, JetpackInstallError(error: key))
69-
} else {
70-
completion(false, .unknown)
71-
}
87+
let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String
88+
let jetpackError = JetpackInstallError(title: error.localizedDescription,
89+
code: error.code,
90+
key: key)
91+
completion(false, jetpackError)
7292
}
7393
}
7494

WordPressKitTests/AccountSettingsRemoteTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class AccountSettingsRemoteTests: RemoteTestCase, RESTTestable {
6262
stubRemoteResponse(meSettingsEndpoint, filename: getAccountSettingsSuccessMockFilename, contentType: .ApplicationJSON)
6363
remote.getSettings(success: { settings in
6464
XCTAssertEqual(settings.username, self.username, "The usernames should be equal.")
65+
XCTAssertEqual(settings.usernameCanBeChanged, false, "The usernames can't be changed and its value must be false")
6566
XCTAssertEqual(settings.email, self.email, "The email addresses should be equal.")
6667
XCTAssertEqual(settings.webAddress, self.userURL, "The web addresses should be equal.")
6768
XCTAssertEqual(settings.primarySiteID, self.siteID, "The primary site ID's should be equal.")

WordPressKitTests/JetpackServiceRemoteTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
134134
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInvalidCredentialsMockFilename, contentType: .ApplicationJSON, status: 400)
135135
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
136136
XCTAssertFalse(success, "Success should be false")
137-
XCTAssertEqual(error, .invalidCredentials)
137+
XCTAssertEqual(error?.type, .invalidCredentials)
138138
expect.fulfill()
139139
}
140140

@@ -147,7 +147,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
147147
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorUnknownMockFilename, contentType: .ApplicationJSON, status: 400)
148148
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
149149
XCTAssertFalse(success, "Success should be false")
150-
XCTAssertEqual(error, .unknown)
150+
XCTAssertEqual(error?.type, .unknown)
151151
expect.fulfill()
152152
}
153153

@@ -160,7 +160,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
160160
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorForbiddenMockFilename, contentType: .ApplicationJSON, status: 400)
161161
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
162162
XCTAssertFalse(success, "Success should be false")
163-
XCTAssertEqual(error, .forbidden)
163+
XCTAssertEqual(error?.type, .forbidden)
164164
expect.fulfill()
165165
}
166166

@@ -173,7 +173,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
173173
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallFailureMockFilename, contentType: .ApplicationJSON, status: 400)
174174
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
175175
XCTAssertFalse(success, "Success should be false")
176-
XCTAssertEqual(error, .installFailure)
176+
XCTAssertEqual(error?.type, .installFailure)
177177
expect.fulfill()
178178
}
179179

@@ -186,7 +186,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
186186
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallResponseMockFilename, contentType: .ApplicationJSON, status: 400)
187187
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
188188
XCTAssertFalse(success, "Success should be false")
189-
XCTAssertEqual(error, .installResponseError)
189+
XCTAssertEqual(error?.type, .installResponseError)
190190
expect.fulfill()
191191
}
192192

@@ -199,7 +199,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
199199
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorLoginFailureMockFilename, contentType: .ApplicationJSON, status: 400)
200200
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
201201
XCTAssertFalse(success, "Success should be false")
202-
XCTAssertEqual(error, .loginFailure)
202+
XCTAssertEqual(error?.type, .loginFailure)
203203
expect.fulfill()
204204
}
205205

@@ -212,7 +212,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
212212
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorSiteIsJetpackMockFilename, contentType: .ApplicationJSON, status: 400)
213213
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
214214
XCTAssertFalse(success, "Success should be false")
215-
XCTAssertEqual(error, .siteIsJetpack)
215+
XCTAssertEqual(error?.type, .siteIsJetpack)
216216
expect.fulfill()
217217
}
218218

@@ -225,7 +225,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
225225
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationInstallMockFilename, contentType: .ApplicationJSON, status: 400)
226226
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
227227
XCTAssertFalse(success, "Success should be false")
228-
XCTAssertEqual(error, .activationOnInstallFailure)
228+
XCTAssertEqual(error?.type, .activationOnInstallFailure)
229229
expect.fulfill()
230230
}
231231

@@ -238,7 +238,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
238238
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationResponseMockFilename, contentType: .ApplicationJSON, status: 400)
239239
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
240240
XCTAssertFalse(success, "Success should be false")
241-
XCTAssertEqual(error, .activationResponseError)
241+
XCTAssertEqual(error?.type, .activationResponseError)
242242
expect.fulfill()
243243
}
244244

@@ -251,7 +251,7 @@ class JetpackServiceRemoteTests: RemoteTestCase, RESTTestable {
251251
stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationFailureMockFilename, contentType: .ApplicationJSON, status: 400)
252252
remote.installJetpack(url: url, username: username, password: password) { (success, error) in
253253
XCTAssertFalse(success, "Success should be false")
254-
XCTAssertEqual(error, .activationFailure)
254+
XCTAssertEqual(error?.type, .activationFailure)
255255
expect.fulfill()
256256
}
257257

0 commit comments

Comments
 (0)