-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: Use GutenbergKit configuration builder #24662
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
Draft
dcalhoun
wants to merge
1
commit into
trunk
Choose a base branch
from
refactor/use-gutenberg-kit-configuration-builder
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
WordPress/Classes/Utility/Editor/EditorConfigurationBuilder+Blog.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Foundation | ||
import GutenbergKit | ||
import WordPressData | ||
import WordPressShared | ||
|
||
extension EditorConfigurationBuilder { | ||
init(blog: Blog) { | ||
let selfHostedApiUrl = blog.url(withPath: "wp-json/") | ||
let isWPComSite = blog.isHostedAtWPcom || blog.isAtomic() | ||
let siteApiRoot = blog.isAccessibleThroughWPCom() && isWPComSite ? blog.wordPressComRestApi?.baseURL.absoluteString : selfHostedApiUrl | ||
let siteId = blog.dotComID?.stringValue | ||
let siteDomain = blog.primaryDomainAddress | ||
let authToken = blog.authToken ?? "" | ||
var authHeader = "Bearer \(authToken)" | ||
|
||
let applicationPassword = try? blog.getApplicationToken() | ||
|
||
if let appPassword = applicationPassword, let username = blog.username { | ||
let credentials = "\(username):\(appPassword)" | ||
if let credentialsData = credentials.data(using: .utf8) { | ||
let base64Credentials = credentialsData.base64EncodedString() | ||
authHeader = "Basic \(base64Credentials)" | ||
} | ||
} | ||
|
||
// Must provide both namespace forms to detect usages of both forms in third-party code | ||
var siteApiNamespace: [String] = [] | ||
if isWPComSite { | ||
if let siteId { | ||
siteApiNamespace.append("sites/\(siteId)") | ||
} | ||
siteApiNamespace.append("sites/\(siteDomain)") | ||
} | ||
|
||
self = EditorConfigurationBuilder() | ||
.setSiteUrl(blog.url ?? "") | ||
.setSiteApiRoot(siteApiRoot ?? "") | ||
.setSiteApiNamespace(siteApiNamespace) | ||
.setNamespaceExcludedPaths(["/wpcom/v2/following/recommendations", "/wpcom/v2/following/mine"]) | ||
.setAuthHeader(authHeader) | ||
.setShouldUseThemeStyles(FeatureFlag.newGutenbergThemeStyles.enabled) | ||
|
||
// Limited to Simple sites until application password auth is supported | ||
if RemoteFeatureFlag.newGutenbergPlugins.enabled() && blog.isHostedAtWPcom { | ||
self = self.setShouldUsePlugins(true) | ||
if var editorAssetsEndpoint = blog.wordPressComRestApi?.baseURL { | ||
editorAssetsEndpoint.appendPathComponent("wpcom/v2/sites") | ||
if let siteId { | ||
editorAssetsEndpoint.appendPathComponent(siteId) | ||
} else { | ||
editorAssetsEndpoint.appendPathComponent(siteDomain) | ||
} | ||
editorAssetsEndpoint.appendPathComponent("editor-assets") | ||
self = self.setEditorAssetsEndpoint(editorAssetsEndpoint) | ||
} | ||
} | ||
self = self.setLocale(WordPressComLanguageDatabase().deviceLanguage.slug) | ||
|
||
if !blog.isSelfHosted { | ||
let siteType: String = blog.isHostedAtWPcom ? "simple" : "atomic" | ||
do { | ||
self = self.setWebViewGlobals([ | ||
try WebViewGlobal(name: "_currentSiteType", value: .string(siteType)) | ||
]) | ||
} catch { | ||
wpAssertionFailure("Failed to create WebViewGlobal", userInfo: ["error": "\(error)"]) | ||
self = self.setWebViewGlobals([]) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crazytonyli would you please help me determine a reasonable approach for managing type safety of fetching editor settings from the API endpoint and passing it to GutenbergKit?
GutenbergKit was updated in wordpress-mobile/GutenbergKit#146 to now expect
[String: Encodable]
rather than[String: Any]
. This seems like a sound change, but my lack of Swift experience inhibits me from determining an appropriate solution for the logic here in WP-iOS.Please feel free to push directly to this branch if that is easiest. Thank you!