Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Global Styles] Implement Global Styles endpoint #394

Merged
merged 19 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion WordPressKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressKit"
s.version = "4.37.0-beta.3"
s.version = "4.37.0-beta.4"

s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."
s.description = <<-DESC
Expand Down
52 changes: 52 additions & 0 deletions WordPressKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@
4625BAEB253E118400C04AAD /* PageLayoutServiceRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4625BAEA253E118400C04AAD /* PageLayoutServiceRemoteTests.swift */; };
4625BAF1253E12C000C04AAD /* page-layout-blog-layouts-success.json in Resources */ = {isa = PBXBuildFile; fileRef = 4625BAF0253E12C000C04AAD /* page-layout-blog-layouts-success.json */; };
4625BAF7253E130900C04AAD /* page-layout-blog-layouts-malformed.json in Resources */ = {isa = PBXBuildFile; fileRef = 4625BAF6253E130800C04AAD /* page-layout-blog-layouts-malformed.json */; };
464BAB0B262F6736006AEED5 /* RemoteBlockEditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 464BAB0A262F6736006AEED5 /* RemoteBlockEditorSettings.swift */; };
465F8894263B09AE00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json in Resources */ = {isa = PBXBuildFile; fileRef = 465F8893263B09AE00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json */; };
465F889A263B09BF00F4C950 /* wp-block-editor-v1-settings-success-ThemeJSON.json in Resources */ = {isa = PBXBuildFile; fileRef = 465F8899263B09BF00F4C950 /* wp-block-editor-v1-settings-success-ThemeJSON.json */; };
465F889E263B0C5500F4C950 /* BlockEditorSettingsServiceRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465F889D263B0C5500F4C950 /* BlockEditorSettingsServiceRemote.swift */; };
465F88A2263B325C00F4C950 /* ChecksumUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465F88A1263B325C00F4C950 /* ChecksumUtil.swift */; };
465F88A7263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465F88A6263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift */; };
465F88B1263B3E8A00F4C950 /* get_wp_v2_themes_twentytwentyone.json in Resources */ = {isa = PBXBuildFile; fileRef = 465F88B0263B3E8A00F4C950 /* get_wp_v2_themes_twentytwentyone.json */; };
465F88B7263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json in Resources */ = {isa = PBXBuildFile; fileRef = 465F88B6263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json */; };
465F88BF263B54EE00F4C950 /* ChecksumUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465F88BE263B54EE00F4C950 /* ChecksumUtilTests.swift */; };
467C20692626243D00DB5A38 /* WordPressRestApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 467C20682626243D00DB5A38 /* WordPressRestApi.swift */; };
46ABD0E0262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0DF262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift */; };
46ABD0E6262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46ABD0E5262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift */; };
Expand Down Expand Up @@ -704,6 +713,15 @@
4625BAEA253E118400C04AAD /* PageLayoutServiceRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageLayoutServiceRemoteTests.swift; sourceTree = "<group>"; };
4625BAF0253E12C000C04AAD /* page-layout-blog-layouts-success.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "page-layout-blog-layouts-success.json"; sourceTree = "<group>"; };
4625BAF6253E130800C04AAD /* page-layout-blog-layouts-malformed.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "page-layout-blog-layouts-malformed.json"; sourceTree = "<group>"; };
464BAB0A262F6736006AEED5 /* RemoteBlockEditorSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteBlockEditorSettings.swift; sourceTree = "<group>"; };
465F8893263B09AE00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wp-block-editor-v1-settings-success-NotThemeJSON.json"; sourceTree = "<group>"; };
465F8899263B09BF00F4C950 /* wp-block-editor-v1-settings-success-ThemeJSON.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "wp-block-editor-v1-settings-success-ThemeJSON.json"; sourceTree = "<group>"; };
465F889D263B0C5500F4C950 /* BlockEditorSettingsServiceRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockEditorSettingsServiceRemote.swift; sourceTree = "<group>"; };
465F88A1263B325C00F4C950 /* ChecksumUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChecksumUtil.swift; sourceTree = "<group>"; };
465F88A6263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockEditorSettingsServiceRemoteTests.swift; sourceTree = "<group>"; };
465F88B0263B3E8A00F4C950 /* get_wp_v2_themes_twentytwentyone.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = get_wp_v2_themes_twentytwentyone.json; sourceTree = "<group>"; };
465F88B6263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "get_wp_v2_themes_twentytwentyone-no-colors.json"; sourceTree = "<group>"; };
465F88BE263B54EE00F4C950 /* ChecksumUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChecksumUtilTests.swift; sourceTree = "<group>"; };
467C20682626243D00DB5A38 /* WordPressRestApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressRestApi.swift; sourceTree = "<group>"; };
46ABD0DF262EED3D00C7FF24 /* WordPressOrgXMLRPCValidatorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WordPressOrgXMLRPCValidatorTests.swift; sourceTree = "<group>"; };
46ABD0E5262EEDAB00C7FF24 /* FakeInfoDictionaryObjectProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeInfoDictionaryObjectProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1327,6 +1345,25 @@
name = StarterPageLayouts;
sourceTree = "<group>";
};
465F8892263B094900F4C950 /* BlockEditorSettings */ = {
isa = PBXGroup;
children = (
465F8893263B09AE00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json */,
465F8899263B09BF00F4C950 /* wp-block-editor-v1-settings-success-ThemeJSON.json */,
465F88B0263B3E8A00F4C950 /* get_wp_v2_themes_twentytwentyone.json */,
465F88B6263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json */,
);
path = BlockEditorSettings;
sourceTree = "<group>";
};
465F88A5263B370300F4C950 /* BlockEditorSettings */ = {
isa = PBXGroup;
children = (
465F88A6263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift */,
);
name = BlockEditorSettings;
sourceTree = "<group>";
};
57A38E502624F7D000472480 /* WordPressAPI */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1536,6 +1573,7 @@
826016FE1F9FD59400533B6C /* Activity */,
FAD1345425909DF500A8FEB1 /* Backup */,
74B5F0DF1EF82AAB00B411E7 /* Blog */,
465F88A5263B370300F4C950 /* BlockEditorSettings */,
ABD95B7D25DD6C2400735BEE /* Comment */,
74585B911F0D520700E7E667 /* Domains */,
7EC60EBC22DC4F5A00FB0336 /* Editor */,
Expand Down Expand Up @@ -1730,6 +1768,7 @@
F9E56DF724EB125600916770 /* FeatureFlagRemote.swift */,
4625B96B253A357500C04AAD /* PageLayoutServiceRemote.swift */,
462422282548B98A002B8A12 /* SiteDesignServiceRemote.swift */,
465F889D263B0C5500F4C950 /* BlockEditorSettingsServiceRemote.swift */,
);
name = Services;
sourceTree = "<group>";
Expand Down Expand Up @@ -1822,6 +1861,7 @@
F9E56DF524EB11EF00916770 /* FeatureFlag.swift */,
4625B964253A343900C04AAD /* RemotePageLayouts.swift */,
4624222C2548BA0F002B8A12 /* RemoteSiteDesign.swift */,
464BAB0A262F6736006AEED5 /* RemoteBlockEditorSettings.swift */,
);
name = Models;
sourceTree = "<group>";
Expand All @@ -1841,6 +1881,7 @@
93BD27421EE73384002BB00B /* Mock Data */ = {
isa = PBXGroup;
children = (
465F8892263B094900F4C950 /* BlockEditorSettings */,
C92EFF7225E7444400E0308D /* common-starter-site-designs-empty-designs.json */,
C92EFF6C25E741E900E0308D /* common-starter-site-designs-malformed.json */,
C92EFF6825E7403F00E0308D /* common-starter-site-designs-success.json */,
Expand Down Expand Up @@ -2121,6 +2162,7 @@
93C674F01EE8351E00BFAF05 /* NSMutableDictionary+Helpers.m */,
9F4E51FF2088E38200424676 /* ObjectValidation.swift */,
57BCD3D326209D9500292CB3 /* AppTransportSecuritySettings.swift */,
465F88A1263B325C00F4C950 /* ChecksumUtil.swift */,
);
name = Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -2235,6 +2277,7 @@
46ABD0E9262EEE0400C7FF24 /* AppTransportSecuritySettingsTests.swift */,
F9E56DFA24EB18C300916770 /* FeatureFlagRemoteTests.swift */,
24ADA24D24F9B32D001B5DAE /* FeatureFlagSerializationTest.swift */,
465F88BE263B54EE00F4C950 /* ChecksumUtilTests.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -2448,6 +2491,7 @@
93BD27591EE73442002BB00B /* is-available-email-failure.json in Resources */,
74D67F171F15C2D70010C5ED /* site-users-update-role-success.json in Resources */,
74C473B11EF31A19009918F2 /* site-delete-success.json in Resources */,
465F8894263B09AE00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json in Resources */,
E14694031F344F71004052C8 /* site-plugins-error.json in Resources */,
829BA4311FACF187003ADEEA /* activity-rewind-status-success.json in Resources */,
93BD27571EE73442002BB00B /* auth-send-login-email-no-user-failure.json in Resources */,
Expand Down Expand Up @@ -2481,8 +2525,10 @@
BA5AEF6B24923DAC007D8E49 /* plugin-state-jetpack.json in Resources */,
436D564C211CCCB900CEAA33 /* domain-contact-information-response-success.json in Resources */,
FFA4D4AD2423B1FE00BF5180 /* wp-admin-post-new.html in Resources */,
465F889A263B09BF00F4C950 /* wp-block-editor-v1-settings-success-ThemeJSON.json in Resources */,
BA9A7F7F24C6895600925E81 /* plugin-directory-jetpack-beta.json in Resources */,
E6B0461425E5B6F500DF6F4F /* sites-invites-links-generate.json in Resources */,
465F88B1263B3E8A00F4C950 /* get_wp_v2_themes_twentytwentyone.json in Resources */,
93BD275A1EE73442002BB00B /* is-available-email-success.json in Resources */,
17BF9A6C20C7DC3300BF57D2 /* reader-site-search-success.json in Resources */,
FACBDD4C25ECB76C0026705B /* reader-post-related-posts-success.json in Resources */,
Expand Down Expand Up @@ -2547,6 +2593,7 @@
FFE247AF20C891E6002DF3A2 /* WordPressComOAuthWrongPasswordFail.json in Resources */,
F194E1252417EE7E00874408 /* atomic-get-auth-cookie-success.json in Resources */,
731BA83A21DED358000FDFCD /* site-creation-success.json in Resources */,
465F88B7263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json in Resources */,
9A2D0B2F225E1245009E585F /* jetpack-service-check-site-success.json in Resources */,
829BA4301FACF187003ADEEA /* activity-rewind-status-restore-failure.json in Resources */,
8B16CE962525045F007BE5A9 /* reader-posts-success.json in Resources */,
Expand Down Expand Up @@ -2743,6 +2790,7 @@
files = (
32E1DD23236AA09A008914B0 /* RemotePostAutosave.swift in Sources */,
FF807252241FB90E00809AF5 /* Authenticator.swift in Sources */,
464BAB0B262F6736006AEED5 /* RemoteBlockEditorSettings.swift in Sources */,
7430C9B41F1927C50051B8E6 /* RemoteReaderSite.m in Sources */,
74585B8E1F0D51A100E7E667 /* DomainsServiceRemote.swift in Sources */,
4625B965253A343900C04AAD /* RemotePageLayouts.swift in Sources */,
Expand Down Expand Up @@ -2830,6 +2878,7 @@
74BA04FA1F06DC3900ED5CD8 /* RemoteComment.m in Sources */,
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 */,
Expand Down Expand Up @@ -2868,6 +2917,7 @@
93188D1F1F2262BF0028ED4D /* RemotePostTag.m in Sources */,
74D67F081F15BEB70010C5ED /* RemotePerson.swift in Sources */,
984E34EB22EF7209005C3F92 /* StatsFileDownloadsTimeIntervalData.swift in Sources */,
465F88A2263B325C00F4C950 /* ChecksumUtil.swift in Sources */,
40AB1ADA200FED25009B533D /* PluginDirectoryFeedPage.swift in Sources */,
436D56352118D85800CEAA33 /* WPCountry.swift in Sources */,
74A44DCB1F13C533006CD8F4 /* NotificationSettingsServiceRemote.swift in Sources */,
Expand Down Expand Up @@ -2962,6 +3012,7 @@
8B2F4BE924ABC9DC0056C08A /* ReaderPostServiceRemote+CardsTests.swift in Sources */,
40F9880C221ACEEE00B7B369 /* StatsRemoteV2Tests.swift in Sources */,
4625BAEB253E118400C04AAD /* PageLayoutServiceRemoteTests.swift in Sources */,
465F88BF263B54EE00F4C950 /* ChecksumUtilTests.swift in Sources */,
826017001F9FD60A00533B6C /* ActivityServiceRemoteTests.swift in Sources */,
93F50A3A1F226BB600B5BEBA /* WordPressComServiceRemoteRestTests.swift in Sources */,
E13EE14C1F332C4400C15787 /* PluginServiceRemoteTests.swift in Sources */,
Expand Down Expand Up @@ -2992,6 +3043,7 @@
FFE247A720C891D1002DF3A2 /* WordPressComOAuthTests.swift in Sources */,
93AB06041EE8838400EF8764 /* RemoteTestCase.swift in Sources */,
BA2A78FA24A486D300BB6F53 /* SitePluginTests.swift in Sources */,
465F88A7263B371D00F4C950 /* BlockEditorSettingsServiceRemoteTests.swift in Sources */,
93BD27411EE73311002BB00B /* AccountServiceRemoteRESTTests.swift in Sources */,
93F50A441F227CFB00B5BEBA /* UsersServiceRemoteXMLRPCTests.swift in Sources */,
7430C9BC1F192C0F0051B8E6 /* ReaderSiteServiceRemoteTests.swift in Sources */,
Expand Down
70 changes: 70 additions & 0 deletions WordPressKit/BlockEditorSettingsServiceRemote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Foundation

public class BlockEditorSettingsServiceRemote {
let remoteAPI: WordPressRestApi
public init(remoteAPI: WordPressRestApi) {
self.remoteAPI = remoteAPI
}
}

// MARK: Editor `theme_supports` support
public extension BlockEditorSettingsServiceRemote {
typealias EditorThemeCompletionHandler = (Swift.Result<RemoteEditorTheme?, Error>) -> Void

func fetchTheme(forSiteID siteID: Int?, _ completion: @escaping EditorThemeCompletionHandler) {
let requestPath = "/wp/v2/themes"
let parameters: [String: AnyObject] = ["status": "active" as AnyObject]
let modifiedPath = remoteAPI.requestPath(fromOrgPath: requestPath, with: siteID)
remoteAPI.GET(modifiedPath, parameters: parameters) { [weak self] (result, _) in
guard let `self` = self else { return }
switch result {
case .success(let response):
self.processEditorThemeResponse(response) { editorTheme in
completion(.success(editorTheme))
}
case .failure(let error):
completion(.failure(error))
}
}
}

private func processEditorThemeResponse(_ response: Any, completion: (_ editorTheme: RemoteEditorTheme?) -> Void) {
guard let responseData = try? JSONSerialization.data(withJSONObject: response, options: []),
let editorThemes = try? JSONDecoder().decode([RemoteEditorTheme].self, from: responseData) else {
completion(nil)
return
}
completion(editorThemes.first)
}

}

// MARK: Editor Global Styles support
public extension BlockEditorSettingsServiceRemote {
typealias BlockEditorSettingsCompletionHandler = (Swift.Result<RemoteBlockEditorSettings?, Error>) -> Void

func fetchBlockEditorSettings(_ completion: @escaping BlockEditorSettingsCompletionHandler) {
let requestPath = "/__experimental/wp-block-editor/v1/settings"
let parameters: [String: AnyObject] = ["context": "mobile" as AnyObject]
remoteAPI.GET(requestPath, parameters: parameters) { [weak self] (result, _) in
guard let `self` = self else { return }
switch result {
case .success(let response):
self.processBlockEditorSettingsResponse(response) { blockEditorSettings in
completion(.success(blockEditorSettings))
}
case .failure(let error):
completion(.failure(error))
}
}
}

private func processBlockEditorSettingsResponse(_ response: Any, completion: (_ editorTheme: RemoteBlockEditorSettings?) -> Void) {
guard let responseData = try? JSONSerialization.data(withJSONObject: response, options: []),
let blockEditorSettings = try? JSONDecoder().decode(RemoteBlockEditorSettings.self, from: responseData) else {
completion(nil)
return
}
completion(blockEditorSettings)
}
}
18 changes: 18 additions & 0 deletions WordPressKit/ChecksumUtil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

public class ChecksumUtil {

/// Generates a checksum based on the encoded keys.
static func checksum<T>(from codable: T) -> String where T: Encodable {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let result: String
do {
let data = try encoder.encode(codable)
result = String(data: data, encoding: .utf8) ?? ""
} catch {
result = ""
}
return result.md5()
}
}
Loading