Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ local.properties
/android/.gradle
/android/.kotlin
/android/.idea
wp_com_oauth_credentials.json

## Production Build Products
/android/Gutenberg/src/main/assets/assets
Expand Down
12 changes: 11 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class RESTAPIRepository(
private val json = Json { ignoreUnknownKeys = true }

private val apiRoot = configuration.siteApiRoot.trimEnd('/')
private val editorSettingsUrl = "$apiRoot$EDITOR_SETTINGS_PATH"
private val activeThemeUrl = "$apiRoot$ACTIVE_THEME_PATH"
private val siteSettingsUrl = "$apiRoot$SITE_SETTINGS_PATH"
private val postTypesUrl = "$apiRoot$POST_TYPES_PATH"
private val namespace = configuration.siteApiNamespace.firstOrNull()
private val editorSettingsUrl = buildNamespacedUrl(EDITOR_SETTINGS_PATH)
private val activeThemeUrl = buildNamespacedUrl(ACTIVE_THEME_PATH)
private val siteSettingsUrl = buildNamespacedUrl(SITE_SETTINGS_PATH)
private val postTypesUrl = buildNamespacedUrl(POST_TYPES_PATH)

/**
* Cleanup any expired cache entries.
Expand Down Expand Up @@ -72,7 +73,7 @@ class RESTAPIRepository(
}

private fun buildPostUrl(id: Int): String {
return "$apiRoot/wp/v2/posts/$id?context=edit"
return buildNamespacedUrl("/wp/v2/posts/$id?context=edit")
}

// MARK: Editor Settings
Expand Down Expand Up @@ -139,7 +140,7 @@ class RESTAPIRepository(
}

private fun buildPostTypeUrl(type: String): String {
return "$apiRoot/wp/v2/types/$type?context=edit"
return buildNamespacedUrl("/wp/v2/types/$type?context=edit")
}

// MARK: GET Active Theme
Expand Down Expand Up @@ -212,6 +213,26 @@ class RESTAPIRepository(
return urlResponse
}

/**
* Builds a URL from the API root and path, inserting the site API namespace
* after the version segment if one is configured.
*
* For example, with namespace `sites/123/` and path `/wp/v2/types`:
* the result is `$apiRoot/wp/v2/sites/123/types`.
*/
private fun buildNamespacedUrl(path: String): String {
if (namespace == null) {
return "$apiRoot$path"
}

val parts = path.removePrefix("/").split("/", limit = 3)
if (parts.size < 3) {
return "$apiRoot$path"
}

return "$apiRoot/${parts[0]}/${parts[1]}/$namespace${parts[2]}"
}

companion object {
private const val EDITOR_SETTINGS_PATH = "/wp-block-editor/v1/settings"
private const val ACTIVE_THEME_PATH = "/wp/v2/themes?context=edit&status=active"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.encodeToJsonElement
import org.wordpress.gutenberg.encodeForEditor

/**
Expand Down Expand Up @@ -60,7 +61,9 @@ data class GBKitGlobal(
/** The raw editor settings JSON from the WordPress REST API. */
val editorSettings: JsonElement?,
/** Pre-fetched API responses JSON for faster editor initialization. */
val preloadData: JsonElement? = null
val preloadData: JsonElement? = null,
/** Pre-fetched editor assets (scripts, styles, allowed block types) for plugin loading. */
val editorAssets: JsonElement? = null
) {
/**
* The post data passed to the editor.
Expand Down Expand Up @@ -111,7 +114,14 @@ data class GBKitGlobal(
),
enableNetworkLogging = configuration.enableNetworkLogging,
editorSettings = dependencies?.editorSettings?.jsonValue,
preloadData = dependencies?.preloadList?.build()
preloadData = dependencies?.preloadList?.build(),
editorAssets = dependencies?.assetBundle?.let { bundle ->
try {
json.encodeToJsonElement(bundle.getEditorRepresentation())
} catch (_: Exception) {
null
}
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,13 @@ class EditorAssetsLibrary(

private fun editorAssetsUrl(configuration: EditorConfiguration): String {
val baseUrl = configuration.siteApiRoot.trimEnd('/')
return "$baseUrl/wpcom/v2/editor-assets?exclude=core,gutenberg"
val namespace = configuration.siteApiNamespace.firstOrNull()

return if (namespace != null) {
"$baseUrl/wpcom/v2/${namespace}editor-assets?exclude=core,gutenberg"
} else {
"$baseUrl/wpcom/v2/editor-assets?exclude=core,gutenberg"
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ val wpEnvCredentials: Map<String, String> = run {
}
}

// Copy shared OAuth credentials into Android assets so they're available at runtime.
val copyOAuthCredentials by tasks.registering(Copy::class) {
from(rootProject.file("../wp_com_oauth_credentials.json"))
into(layout.buildDirectory.dir("generated/oauth-assets"))
}

android {
namespace = "com.example.gutenbergkit"
compileSdk = 34

sourceSets["main"].assets.srcDir(copyOAuthCredentials.map { it.destinationDir })

defaultConfig {
applicationId = "com.example.gutenbergkit"
minSdk = 24
Expand Down
11 changes: 11 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".GutenbergKitApplication"
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down Expand Up @@ -32,6 +33,16 @@
android:scheme="gutenbergkit" >
</data>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="wpcom-authorized"
android:scheme="gutenbergkit" >
</data>
</intent-filter>
</activity>
<activity
android:name=".EditorActivity"
Expand Down
Loading
Loading