-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Basic support of custom post types #25157
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ca8f552
Create WordPressClientFactory
crazytonyli cb99793
Instantiate WordPressApiCache and service in WordPressClient
crazytonyli a8ed0cb
Show custom post types
crazytonyli 17c55d2
Extract `CustomPostCollectionView`
crazytonyli 8109fe7
Support searching custom posts
crazytonyli b0d13ce
Re-implement showing loading more states
crazytonyli 01af67d
Adopt new GBK API
crazytonyli 0908d90
Simple post conflict detection
crazytonyli ad5df26
Add a Filter type
crazytonyli 7a54dcb
Update wordpress-rs
crazytonyli 0341dc8
Fix a compilation issue in unit tests
crazytonyli 9b44e33
Add a missing delegate function
crazytonyli bcf17ed
Improve excerpt rendering
crazytonyli 3e3d4a3
Add a TODO
crazytonyli 6a8d9a2
Use Picker to present the status filter
crazytonyli bef22c0
Use DDLog instead of NSLog
crazytonyli 5215f23
Extract data loading code to a view model type
crazytonyli ca016e6
Add a localizable string
crazytonyli 7bdff39
Merge branch 'trunk' into prototype-custom-post-types
crazytonyli 4093ab4
Fix compilation issues after merge
crazytonyli 96fa4a9
Merge branch 'trunk' into prototype-custom-post-types
crazytonyli 2d0aec9
Fix a typo
crazytonyli 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
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,72 @@ | ||
| import Foundation | ||
| import WordPressAPI | ||
| import WordPressAPIInternal | ||
| import WordPressApiCache | ||
|
|
||
| extension WordPressApiCache { | ||
| static func bootstrap() -> WordPressApiCache? { | ||
| let instance: WordPressApiCache? = .onDiskCache() ?? .memoryCache() | ||
| instance?.startListeningForUpdates() | ||
| return instance | ||
| } | ||
|
|
||
| private static func onDiskCache() -> WordPressApiCache? { | ||
| let cacheURL: URL | ||
| do { | ||
| cacheURL = try FileManager.default | ||
| .url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true) | ||
| .appending(path: "app.sqlite") | ||
| } catch { | ||
| NSLog("Failed to create api cache file: \(error)") | ||
| return nil | ||
| } | ||
|
|
||
| if let cache = WordPressApiCache.onDiskCache(file: cacheURL) { | ||
| return cache | ||
| } | ||
|
|
||
| if FileManager.default.fileExists(at: cacheURL) { | ||
| do { | ||
| try FileManager.default.removeItem(at: cacheURL) | ||
|
|
||
| if let cache = WordPressApiCache.onDiskCache(file: cacheURL) { | ||
| return cache | ||
| } | ||
| } catch { | ||
| NSLog("Failed to delete sqlite database: \(error)") | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private static func onDiskCache(file: URL) -> WordPressApiCache? { | ||
| let cache: WordPressApiCache | ||
| do { | ||
| cache = try WordPressApiCache(url: file) | ||
| } catch { | ||
| NSLog("Failed to create an instance: \(error)") | ||
| return nil | ||
| } | ||
|
|
||
| do { | ||
| _ = try cache.performMigrations() | ||
| } catch { | ||
| NSLog("Failed to migrate database: \(error)") | ||
| return nil | ||
| } | ||
|
|
||
| return cache | ||
| } | ||
|
|
||
| private static func memoryCache() -> WordPressApiCache? { | ||
| do { | ||
| let cache = try WordPressApiCache() | ||
| _ = try cache.performMigrations() | ||
| return cache | ||
| } catch { | ||
| NSLog("Failed to create memory cache: \(error)") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import Foundation | ||
| import WordPressAPI | ||
| import WordPressAPIInternal | ||
| import WordPressApiCache | ||
|
|
||
| /// Protocol defining the WordPress API methods that WordPressClient needs. | ||
| /// This abstraction allows for mocking in tests using the `NoHandle` constructors | ||
|
|
@@ -15,6 +16,9 @@ public protocol WordPressClientAPI: Sendable { | |
| var taxonomies: TaxonomiesRequestExecutor { get } | ||
| var terms: TermsRequestExecutor { get } | ||
| var applicationPasswords: ApplicationPasswordsRequestExecutor { get } | ||
| var posts: PostsRequestExecutor { get } | ||
|
|
||
| func createSelfHostedService(cache: WordPressApiCache) throws -> WpSelfHostedService | ||
|
|
||
| func uploadMedia( | ||
| params: MediaCreateParams, | ||
|
|
@@ -70,11 +74,34 @@ public actor WordPressClient { | |
| case noActiveTheme | ||
| } | ||
|
|
||
| public let siteURL: URL | ||
|
|
||
| /// The underlying API executor used for making network requests. | ||
| public let api: any WordPressClientAPI | ||
|
|
||
| /// The root URL of the WordPress site this client is connected to. | ||
| public let rootUrl: String | ||
| private var _cache: WordPressApiCache? | ||
| public var cache: WordPressApiCache? { | ||
| get { | ||
| if _cache == nil { | ||
| _cache = WordPressApiCache.bootstrap() | ||
| } | ||
| return _cache | ||
| } | ||
| } | ||
|
|
||
| private var _service: WpSelfHostedService? | ||
| public var service: WpSelfHostedService? { | ||
| get { | ||
| if _service == nil, let cache { | ||
| do { | ||
| _service = try api.createSelfHostedService(cache: cache) | ||
| } catch { | ||
| NSLog("Failed to create service: \(error)") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we at least log this to Sentry? I don't love dropping this error. |
||
| } | ||
| } | ||
| return _service | ||
| } | ||
| } | ||
|
|
||
| /// The cached task for fetching site API details. | ||
| private var loadSiteInfoTask: Task<WpApiDetails, Error> | ||
|
|
@@ -93,10 +120,10 @@ public actor WordPressClient { | |
| /// | ||
| /// - Parameters: | ||
| /// - api: The API executor to use for network requests. | ||
| /// - rootUrl: The parsed root URL of the WordPress site. | ||
| public init(api: any WordPressClientAPI, rootUrl: ParsedUrl) { | ||
| /// - siteURL: The parsed root URL of the WordPress site. | ||
| public init(api: WordPressClientAPI, siteURL: URL) { | ||
| self.api = api | ||
| self.rootUrl = rootUrl.url() | ||
| self.siteURL = siteURL | ||
|
|
||
| // These tasks need to be manually restated here because we can't use the task constructors | ||
| self.loadSiteInfoTask = Task { try await api.apiRoot.get().data } | ||
|
|
||
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
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
Oops, something went wrong.
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.
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.
The on-disk handling could probably be a bit simpler?