@@ -248,6 +248,97 @@ private const val VIEW_PAGER_OFFSCREEN_PAGE_LIMIT = 4
248248
249249private const val MEDIA_ID_NO_FEATURED_IMAGE_SET = 0
250250
251+ object GutenbergKitSettingsBuilder {
252+ data class SiteConfig (
253+ val url : String ,
254+ val siteId : Long ,
255+ val isWPCom : Boolean ,
256+ val isWPComAtomic : Boolean ,
257+ val isJetpackConnected : Boolean ,
258+ val isUsingWpComRestApi : Boolean ,
259+ val wpApiRestUrl : String? ,
260+ val apiRestUsernamePlain : String? ,
261+ val apiRestPasswordPlain : String?
262+ )
263+
264+ data class PostConfig (
265+ val remotePostId : Long? ,
266+ val isPage : Boolean ,
267+ val title : String? ,
268+ val content : String?
269+ )
270+
271+ data class FeatureConfig (
272+ val isPluginsFeatureEnabled : Boolean ,
273+ val isThemeStylesFeatureEnabled : Boolean
274+ )
275+
276+ data class AppConfig (
277+ val accessToken : String? ,
278+ val locale : String ,
279+ val cookies : Any?
280+ )
281+
282+ fun buildSettings (
283+ siteConfig : SiteConfig ,
284+ postConfig : PostConfig ,
285+ appConfig : AppConfig ,
286+ featureConfig : FeatureConfig
287+ ): MutableMap <String , Any ?> {
288+ val applicationPassword = siteConfig.apiRestPasswordPlain
289+ val shouldUseWPComRestApi = applicationPassword.isNullOrEmpty() && siteConfig.isUsingWpComRestApi
290+
291+ val siteApiRoot = if (shouldUseWPComRestApi) " https://public-api.wordpress.com/"
292+ else siteConfig.wpApiRestUrl ? : " ${siteConfig.url} /wp-json/"
293+
294+ val authHeader = if (shouldUseWPComRestApi) " Bearer ${appConfig.accessToken ? : " " } "
295+ else " Basic ${Base64 .encodeToString(
296+ " ${siteConfig.apiRestUsernamePlain} :$applicationPassword " .toByteArray(),
297+ Base64 .NO_WRAP
298+ )} "
299+
300+ val siteApiNamespace = if (shouldUseWPComRestApi)
301+ arrayOf(" sites/${siteConfig.siteId} /" , " sites/${UrlUtils .removeScheme(siteConfig.url)} /" )
302+ else arrayOf()
303+
304+ val wpcomLocaleSlug = appConfig.locale.replace(" _" , " -" ).lowercase()
305+
306+ return mutableMapOf (
307+ " postId" to postConfig.remotePostId?.toInt(),
308+ " postType" to if (postConfig.isPage) " page" else " post" ,
309+ " postTitle" to postConfig.title,
310+ " postContent" to postConfig.content,
311+ " siteURL" to siteConfig.url,
312+ " siteApiRoot" to siteApiRoot,
313+ " namespaceExcludedPaths" to arrayOf(" /wpcom/v2/following/recommendations" , " /wpcom/v2/following/mine" ),
314+ " authHeader" to authHeader,
315+ " siteApiNamespace" to siteApiNamespace,
316+ " themeStyles" to featureConfig.isThemeStylesFeatureEnabled,
317+ " plugins" to shouldUsePlugins(
318+ isFeatureEnabled = featureConfig.isPluginsFeatureEnabled,
319+ isWPComSite = siteConfig.isWPCom,
320+ isJetpackConnected = siteConfig.isJetpackConnected,
321+ applicationPassword = applicationPassword
322+ ),
323+ " locale" to wpcomLocaleSlug,
324+ " cookies" to appConfig.cookies
325+ )
326+ }
327+
328+ private fun shouldUsePlugins (
329+ isFeatureEnabled : Boolean ,
330+ isWPComSite : Boolean ,
331+ isJetpackConnected : Boolean ,
332+ applicationPassword : String?
333+ ): Boolean {
334+ // Enable plugins for:
335+ // 1. WP.com Simple sites (when feature is enabled)
336+ // 2. Jetpack-connected sites with application passwords (when feature is enabled)
337+ return isFeatureEnabled &&
338+ (isWPComSite || (isJetpackConnected && ! applicationPassword.isNullOrEmpty()))
339+ }
340+ }
341+
251342@Suppress(" LargeClass" )
252343class GutenbergKitActivity : BaseAppCompatActivity (), EditorFragmentActivity, EditorImageSettingsListener,
253344 EditorImagePreviewListener , EditorEditMediaListener , EditorFragmentListener ,
@@ -2305,52 +2396,44 @@ class GutenbergKitActivity : BaseAppCompatActivity(), EditorFragmentActivity, Ed
23052396 }
23062397
23072398 private fun createGutenbergKitSettings (): MutableMap <String , Any ?> {
2308- val postType = if (editPostRepository.isPage) " page" else " post"
2309- val siteURL = siteModel.url
2310- val applicationPassword = siteModel.apiRestPasswordPlain
2311- val shouldUseWPComRestApi = applicationPassword.isNullOrEmpty() && siteModel.isUsingWpComRestApi
2312-
2313- val siteApiRoot = if (shouldUseWPComRestApi) " https://public-api.wordpress.com/"
2314- else siteModel.wpApiRestUrl ? : " $siteURL /wp-json/"
2315- val authHeader = if
2316- (shouldUseWPComRestApi) " Bearer ${accountStore.accessToken} "
2317- else {
2318- val credentials = " ${site.apiRestUsernamePlain} :${applicationPassword} "
2319- " Basic ${Base64 .encodeToString(credentials.toByteArray(), Base64 .NO_WRAP )} "
2320- }
2321- val siteApiNamespace = if (shouldUseWPComRestApi)
2322- arrayOf(" sites/${site.siteId} /" , " sites/${UrlUtils .removeScheme(siteURL)} /" )
2323- else arrayOf()
2324-
2325- val languageString = perAppLocaleManager.getCurrentLocaleLanguageCode()
2326- val wpcomLocaleSlug = languageString.replace(" _" , " -" ).lowercase()
2327-
2328- return mutableMapOf (
2329- " postId" to editPostRepository.getPost()?.remotePostId?.toInt(),
2330- " postType" to postType,
2331- " postTitle" to editPostRepository.getPost()?.title,
2332- " postContent" to editPostRepository.getPost()?.content,
2333- " siteURL" to siteURL,
2334- " siteApiRoot" to siteApiRoot,
2335- " namespaceExcludedPaths" to arrayOf(" /wpcom/v2/following/recommendations" , " /wpcom/v2/following/mine" ),
2336- " authHeader" to authHeader,
2337- " siteApiNamespace" to siteApiNamespace,
2338- " themeStyles" to experimentalFeatures.isEnabled(Feature .EXPERIMENTAL_BLOCK_EDITOR_THEME_STYLES ),
2339- " plugins" to shouldUsePlugins(applicationPassword),
2340- " locale" to wpcomLocaleSlug,
2341- " cookies" to editPostAuthViewModel.getCookiesForPrivateSites(site, privateAtomicCookie),
2399+ val siteConfig = GutenbergKitSettingsBuilder .SiteConfig (
2400+ url = siteModel.url,
2401+ siteId = site.siteId,
2402+ isWPCom = site.isWPCom,
2403+ isWPComAtomic = siteModel.isWPComAtomic,
2404+ isJetpackConnected = site.isJetpackConnected,
2405+ isUsingWpComRestApi = siteModel.isUsingWpComRestApi,
2406+ wpApiRestUrl = siteModel.wpApiRestUrl,
2407+ apiRestUsernamePlain = site.apiRestUsernamePlain,
2408+ apiRestPasswordPlain = siteModel.apiRestPasswordPlain
2409+ )
2410+
2411+ val postConfig = GutenbergKitSettingsBuilder .PostConfig (
2412+ remotePostId = editPostRepository.getPost()?.remotePostId,
2413+ isPage = editPostRepository.isPage,
2414+ title = editPostRepository.getPost()?.title,
2415+ content = editPostRepository.getPost()?.content
2416+ )
2417+
2418+ val featureConfig = GutenbergKitSettingsBuilder .FeatureConfig (
2419+ isPluginsFeatureEnabled = gutenbergKitPluginsFeature.isEnabled(),
2420+ isThemeStylesFeatureEnabled = experimentalFeatures.isEnabled(
2421+ Feature .EXPERIMENTAL_BLOCK_EDITOR_THEME_STYLES
2422+ )
2423+ )
2424+
2425+ val appConfig = GutenbergKitSettingsBuilder .AppConfig (
2426+ accessToken = accountStore.accessToken,
2427+ locale = perAppLocaleManager.getCurrentLocaleLanguageCode(),
2428+ cookies = editPostAuthViewModel.getCookiesForPrivateSites(site, privateAtomicCookie)
2429+ )
2430+
2431+ return GutenbergKitSettingsBuilder .buildSettings(
2432+ siteConfig = siteConfig,
2433+ postConfig = postConfig,
2434+ appConfig = appConfig,
2435+ featureConfig = featureConfig
23422436 )
2343- }
2344-
2345- // Returns true if the plugins should be enabled for the given blog.
2346- // This is used to determine if the editor should load third-party
2347- // plugins providing blocks.
2348- private fun shouldUsePlugins (applicationPassword : String? ): Boolean {
2349- // Requires a Jetpack until editor assets endpoint is available in WordPress core.
2350- // Requires a WP.com Simple site or an application password to authenticate all REST
2351- // API requests, including those originating from non-core blocks.
2352- return gutenbergKitPluginsFeature.isEnabled() &&
2353- (site.isWPCom || (site.isJetpackConnected && ! applicationPassword.isNullOrEmpty()))
23542437 }
23552438
23562439 override fun instantiateItem (container : ViewGroup , position : Int ): Any {
0 commit comments