@@ -127,63 +127,12 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
127127 self . editorSession = PostEditorAnalyticsSession ( editor: . gutenbergKit, post: post)
128128 self . navigationBarManager = navigationBarManager ?? PostEditorNavigationBarManager ( )
129129
130- let selfHostedApiUrl = post. blog. url ( withPath: " wp-json/ " )
131- let isWPComSite = post. blog. isHostedAtWPcom || post. blog. isAtomic ( )
132- let siteApiRoot = post. blog. isAccessibleThroughWPCom ( ) && isWPComSite ? post. blog. wordPressComRestApi? . baseURL. absoluteString : selfHostedApiUrl
133- let siteId = post. blog. dotComID? . stringValue
134- let siteDomain = post. blog. primaryDomainAddress
135- let authToken = post. blog. authToken ?? " "
136- var authHeader = " Bearer \( authToken) "
137-
138- let applicationPassword = try ? post. blog. getApplicationToken ( )
139-
140- if let appPassword = applicationPassword, let username = post. blog. username {
141- let credentials = " \( username) : \( appPassword) "
142- if let credentialsData = credentials. data ( using: . utf8) {
143- let base64Credentials = credentialsData. base64EncodedString ( )
144- authHeader = " Basic \( base64Credentials) "
145- }
146- }
147-
148- // Must provide both namespace forms to detect usages of both forms in third-party code
149- var siteApiNamespace : [ String ] = [ ]
150- if isWPComSite {
151- if let siteId {
152- siteApiNamespace. append ( " sites/ \( siteId) " )
153- }
154- siteApiNamespace. append ( " sites/ \( siteDomain) " )
155- }
156-
157- var conf = EditorConfiguration (
158- title: post. postTitle ?? " " ,
159- content: post. content ?? " "
160- )
130+ var conf = EditorConfiguration ( blog: post. blog)
131+ conf. title = post. postTitle ?? " "
132+ conf. content = post. content ?? " "
161133 conf. postID = post. postID? . intValue != - 1 ? post. postID? . intValue : nil
162134 conf. postType = post is Page ? " page " : " post "
163135
164- conf. siteURL = post. blog. url ?? " "
165- conf. siteApiRoot = siteApiRoot ?? " "
166- conf. siteApiNamespace = siteApiNamespace
167- conf. namespaceExcludedPaths = [ " /wpcom/v2/following/recommendations " , " /wpcom/v2/following/mine " ]
168- conf. authHeader = authHeader
169-
170- conf. themeStyles = FeatureFlag . newGutenbergThemeStyles. enabled
171- // Limited to Simple sites until application password auth is supported
172- conf. plugins = RemoteFeatureFlag . newGutenbergPlugins. enabled ( ) && post. blog. isHostedAtWPcom
173- conf. locale = WordPressComLanguageDatabase ( ) . deviceLanguage. slug
174-
175- if !post. blog. isSelfHosted {
176- let siteType : String = post. blog. isHostedAtWPcom ? " simple " : " atomic "
177- do {
178- conf. webViewGlobals = [
179- try WebViewGlobal ( name: " _currentSiteType " , value: . string( siteType) )
180- ]
181- } catch {
182- wpAssertionFailure ( " Failed to create WebViewGlobal " , userInfo: [ " error " : " \( error) " ] )
183- conf. webViewGlobals = [ ]
184- }
185- }
186-
187136 self . editorViewController = GutenbergKit . EditorViewController ( configuration: conf)
188137
189138 super. init ( nibName: nil , bundle: nil )
@@ -891,6 +840,74 @@ extension NewGutenbergViewController {
891840 }
892841}
893842
843+ extension EditorConfiguration {
844+ init ( blog: Blog ) {
845+ let selfHostedApiUrl = blog. url ( withPath: " wp-json/ " )
846+ let isWPComSite = blog. isHostedAtWPcom || blog. isAtomic ( )
847+ let siteApiRoot = blog. isAccessibleThroughWPCom ( ) && isWPComSite ? blog. wordPressComRestApi? . baseURL. absoluteString : selfHostedApiUrl
848+ let siteId = blog. dotComID? . stringValue
849+ let siteDomain = blog. primaryDomainAddress
850+ let authToken = blog. authToken ?? " "
851+ var authHeader = " Bearer \( authToken) "
852+
853+ let applicationPassword = try ? blog. getApplicationToken ( )
854+
855+ if let appPassword = applicationPassword, let username = blog. username {
856+ let credentials = " \( username) : \( appPassword) "
857+ if let credentialsData = credentials. data ( using: . utf8) {
858+ let base64Credentials = credentialsData. base64EncodedString ( )
859+ authHeader = " Basic \( base64Credentials) "
860+ }
861+ }
862+
863+ // Must provide both namespace forms to detect usages of both forms in third-party code
864+ var siteApiNamespace : [ String ] = [ ]
865+ if isWPComSite {
866+ if let siteId {
867+ siteApiNamespace. append ( " sites/ \( siteId) " )
868+ }
869+ siteApiNamespace. append ( " sites/ \( siteDomain) " )
870+ }
871+
872+ self = EditorConfiguration ( )
873+
874+ self . siteURL = blog. url ?? " "
875+ self . siteApiRoot = siteApiRoot ?? " "
876+ self . siteApiNamespace = siteApiNamespace
877+ self . namespaceExcludedPaths = [ " /wpcom/v2/following/recommendations " , " /wpcom/v2/following/mine " ]
878+ self . authHeader = authHeader
879+
880+ self . themeStyles = FeatureFlag . newGutenbergThemeStyles. enabled
881+ // Limited to Simple sites until application password auth is supported
882+ if RemoteFeatureFlag . newGutenbergPlugins. enabled ( ) && blog. isHostedAtWPcom {
883+ self . plugins = true
884+ if var editorAssetsEndpoint = blog. wordPressComRestApi? . baseURL {
885+ editorAssetsEndpoint. appendPathComponent ( " wpcom/v2/sites " )
886+ if let siteId {
887+ editorAssetsEndpoint. appendPathComponent ( siteId)
888+ } else {
889+ editorAssetsEndpoint. appendPathComponent ( siteDomain)
890+ }
891+ editorAssetsEndpoint. appendPathComponent ( " editor-assets " )
892+ self . editorAssetsEndpoint = editorAssetsEndpoint
893+ }
894+ }
895+ self . locale = WordPressComLanguageDatabase ( ) . deviceLanguage. slug
896+
897+ if !blog. isSelfHosted {
898+ let siteType : String = blog. isHostedAtWPcom ? " simple " : " atomic "
899+ do {
900+ self . webViewGlobals = [
901+ try WebViewGlobal ( name: " _currentSiteType " , value: . string( siteType) )
902+ ]
903+ } catch {
904+ wpAssertionFailure ( " Failed to create WebViewGlobal " , userInfo: [ " error " : " \( error) " ] )
905+ self . webViewGlobals = [ ]
906+ }
907+ }
908+ }
909+ }
910+
894911// Extend Gutenberg JavaScript exception struct to conform the protocol defined in the Crash Logging service
895912extension GutenbergJSException . StacktraceLine : @retroactive AutomatticTracks . JSStacktraceLine { }
896913extension GutenbergJSException : @retroactive AutomatticTracks . JSException { }
0 commit comments