Skip to content

Conversation

@crazytonyli
Copy link
Contributor

There are two issues in loading custom posts from the editor

  1. A request to wp/v2/post/<id> is sent out when preparing editor. This URL is hard-coded.
  2. A JS exception is thrown when editing a custom post. The JS code contains a hard-coded list of "post entities", which appears to be the root cause.

This PR adds restBase and restNamespace to EditorConfiguration. The values will be used to fix the hard-coded wp/v2/post request. They are passed to the JS library and are added to the "post entities" list.

The second commit of this PR updates the Demo project to browse custom posts and allows opening them in the GBK editor. You can checkout this commit to reproduce the issue locally.

Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tested well for me. I noted the correct post type is provided to the WebView configuration. I was unable to verify the correct post type is loaded due to the issue with the Preload Editor button noted in an inline comment.

We might should address the inline comments before merging.

var body: some View {
Group {
if viewModel.isLoading && viewModel.posts.isEmpty {
ProgressView("Loading Posts...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May not be worth the effort given this is a demo app, but we might changes this and other static "Posts" references to be "\(viewModel.postTypeDetails.name) entries" or simply "entries".

}
self.posts = loadedPosts
} catch {
self.error = error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a WP.com WoW/Atomic site, I was unable to load posts/pages and encountered the following error.

Post loading error screenshot Image

Comment on lines +306 to +307
restBase: post?.restBase || 'posts',
restNamespace: post?.restNamespace || 'wp/v2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the JavaScript tests to address the current failures. Something like the following.

Example diff
diff --git a/src/utils/bridge.test.js b/src/utils/bridge.test.js
index 6928581..5c2175b 100644
--- a/src/utils/bridge.test.js
+++ b/src/utils/bridge.test.js
@@ -236,6 +236,8 @@ describe( 'getPost', () => {
 			expect( result ).toEqual( {
 				id: 123,
 				type: 'page',
+				restBase: 'posts',
+				restNamespace: 'wp/v2',
 				status: 'draft',
 				title: { raw: 'Host Title' },
 				content: { raw: 'Host Content' },
@@ -299,6 +301,8 @@ describe( 'getPost', () => {
 			expect( result ).toEqual( {
 				id: 789,
 				type: 'post',
+				restBase: 'posts',
+				restNamespace: 'wp/v2',
 				status: 'draft',
 				title: { raw: 'GBKit Title' },
 				content: { raw: 'GBKit Content' },
@@ -322,6 +326,8 @@ describe( 'getPost', () => {
 			expect( result ).toEqual( {
 				id: 101,
 				type: 'page',
+				restBase: 'posts',
+				restNamespace: 'wp/v2',
 				status: 'publish',
 				title: { raw: 'Fallback Title' },
 				content: { raw: 'Fallback Content' },
@@ -338,6 +344,8 @@ describe( 'getPost', () => {
 			expect( result ).toEqual( {
 				id: -1,
 				type: 'post',
+				restBase: 'posts',
+				restNamespace: 'wp/v2',
 				status: 'draft',
 				title: { raw: '' },
 				content: { raw: '' },
@@ -364,6 +372,8 @@ describe( 'getPost', () => {
 			expect( result ).toEqual( {
 				id: -1,
 				type: 'post',
+				restBase: 'posts',
+				restNamespace: 'wp/v2',
 				status: 'draft',
 				title: { raw: 'Title' },
 				content: { raw: 'Content' },

content: String = "",
postID: Int? = nil,
postType: String,
postType: PostTypeDetails,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are Swift test failures that seem to relate to this change and the one in EditorPreloadList. We need to update the automated tests.

Example diff
diff --git a/ios/Tests/GutenbergKitTests/Model/GBKitGlobalTests.swift b/ios/Tests/GutenbergKitTests/Model/GBKitGlobalTests.swift
index e1fcd1a..e32a2d3 100644
--- a/ios/Tests/GutenbergKitTests/Model/GBKitGlobalTests.swift
+++ b/ios/Tests/GutenbergKitTests/Model/GBKitGlobalTests.swift
@@ -20,7 +20,7 @@ struct GBKitGlobalTests: MakesTestFixtures {
 
   private func makePreloadList() -> EditorPreloadList {
     EditorPreloadList(
-      postType: "post",
+      postType: .post,
       postTypeData: EditorURLResponse(data: Data(), responseHeaders: [:]),
       postTypesData: EditorURLResponse(data: Data(), responseHeaders: [:]),
       activeThemeData: EditorURLResponse(data: Data(), responseHeaders: [:]),
@@ -97,8 +97,8 @@ struct GBKitGlobalTests: MakesTestFixtures {
 
   @Test("maps postType to post.type")
   func mapsPostType() throws {
-    let postConfig = makeConfiguration(postType: "post")
-    let pageConfig = makeConfiguration(postType: "page")
+    let postConfig = makeConfiguration(postType: .post)
+    let pageConfig = makeConfiguration(postType: .page)
 
     let postGlobal = try GBKitGlobal(configuration: postConfig, dependencies: makeDependencies())
     let pageGlobal = try GBKitGlobal(configuration: pageConfig, dependencies: makeDependencies())
diff --git a/ios/Tests/GutenbergKitTests/Stores/EditorAssetLibraryTests.swift b/ios/Tests/GutenbergKitTests/Stores/EditorAssetLibraryTests.swift
index 0e354a4..eec81ee 100644
--- a/ios/Tests/GutenbergKitTests/Stores/EditorAssetLibraryTests.swift
+++ b/ios/Tests/GutenbergKitTests/Stores/EditorAssetLibraryTests.swift
@@ -9,7 +9,7 @@ struct EditorAssetLibraryTests {
 
     static var testConfiguration: EditorConfiguration {
         EditorConfigurationBuilder(
-            postType: "post",
+            postType: .post,
             siteURL: URL(string: "https://example.com")!,
             siteApiRoot: URL(string: "https://example.com/wp-json")!,
         )
@@ -20,7 +20,7 @@ struct EditorAssetLibraryTests {
 
     static var minimalConfiguration: EditorConfiguration {
         EditorConfigurationBuilder(
-            postType: "post",
+            postType: .post,
             siteURL: URL(string: "https://example.com")!,
             siteApiRoot: URL(string: "https://example.com/wp-json")!
         )

Comment on lines +61 to +65
Picker("Post Type", selection: $viewModel.selectedPostTypeDetails) {
ForEach(viewModel.postTypes, id: \.self) { postType in
Text(postType.name).tag(postType)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be a pre-existing issue, but the Prepare Editor button does not take this selection into consideration. So, it always preloads the post type rather than the selected type.

We might should fix this so that we can further validate the correct data is preloaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants