-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathBlockEditorSettingsService.swift
234 lines (205 loc) · 9.83 KB
/
BlockEditorSettingsService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import Foundation
import WordPressKit
class BlockEditorSettingsService {
struct SettingsServiceResult {
let hasChanges: Bool
let blockEditorSettings: BlockEditorSettings?
}
enum BlockEditorSettingsServiceError: Int, Error {
case blogNotFound
}
typealias BlockEditorSettingsServiceCompletion = (Swift.Result<SettingsServiceResult, Error>) -> Void
let blog: Blog
let remote: BlockEditorSettingsServiceRemote
let context: NSManagedObjectContext
var cachedSettings: BlockEditorSettings? {
return blog.blockEditorSettings
}
convenience init?(blog: Blog, context: NSManagedObjectContext) {
let remoteAPI: WordPressRestApi
if blog.isAccessibleThroughWPCom(),
blog.dotComID?.intValue != nil,
let restAPI = blog.wordPressComRestApi() {
remoteAPI = restAPI
} else if let orgAPI = blog.wordPressOrgRestApi {
remoteAPI = orgAPI
} else {
// This is should only happen if there is a problem with the blog itsself.
return nil
}
self.init(blog: blog, remoteAPI: remoteAPI, context: context)
}
init(blog: Blog, remoteAPI: WordPressRestApi, context: NSManagedObjectContext) {
self.blog = blog
self.context = context
self.remote = BlockEditorSettingsServiceRemote(remoteAPI: remoteAPI)
}
func fetchSettings(_ completion: @escaping BlockEditorSettingsServiceCompletion) {
if blog.supports(.blockEditorSettings) {
fetchBlockEditorSettings(completion)
} else {
fetchTheme(completion)
}
}
}
// MARK: Editor `theme_supports` support
private extension BlockEditorSettingsService {
func fetchTheme(_ completion: @escaping BlockEditorSettingsServiceCompletion) {
remote.fetchTheme(forSiteID: blog.dotComID?.intValue) { [weak self] (response) in
guard let `self` = self else { return }
switch response {
case .success(let editorTheme):
self.context.perform {
let originalChecksum = self.blog.blockEditorSettings?.checksum ?? ""
self.track(isBlockEditorSettings: false, isFSE: false)
self.updateEditorThemeCache(originalChecksum: originalChecksum, editorTheme: editorTheme, completion: completion)
}
case .failure(let err):
DDLogError("Error loading active theme: \(err)")
completion(.failure(err))
}
}
}
func updateEditorThemeCache(originalChecksum: String, editorTheme: RemoteEditorTheme?, completion: @escaping BlockEditorSettingsServiceCompletion) {
let newChecksum = editorTheme?.checksum ?? ""
guard originalChecksum != newChecksum else {
/// The fetched Editor Theme is the same as the cached one so respond with no new changes.
let result = SettingsServiceResult(hasChanges: false, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
return
}
guard let editorTheme = editorTheme else {
/// The original checksum is different than an empty one so we need to clear the old settings.
clearCoreData(completion: completion)
return
}
/// The fetched Editor Theme is different than the cached one so persist the new one and delete the old one.
context.perform {
self.persistEditorThemeToCoreData(blogID: self.blog.objectID, editorTheme: editorTheme) { callback in
switch callback {
case .success:
self.context.perform {
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
}
case .failure(let err):
completion(.failure(err))
}
}
}
}
func persistEditorThemeToCoreData(blogID: NSManagedObjectID, editorTheme: RemoteEditorTheme, completion: @escaping (Swift.Result<Void, Error>) -> Void) {
let parsingContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
parsingContext.parent = context
parsingContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
parsingContext.perform {
guard let blog = parsingContext.object(with: blogID) as? Blog else {
let err = BlockEditorSettingsServiceError.blogNotFound
completion(.failure(err))
return
}
if let blockEditorSettings = blog.blockEditorSettings {
// Block Editor Settings nullify on delete
parsingContext.delete(blockEditorSettings)
}
blog.blockEditorSettings = BlockEditorSettings(editorTheme: editorTheme, context: parsingContext)
do {
try parsingContext.save()
} catch let err {
completion(.failure(err))
}
completion(.success(()))
}
}
}
// MARK: Editor Global Styles support
private extension BlockEditorSettingsService {
func fetchBlockEditorSettings(_ completion: @escaping BlockEditorSettingsServiceCompletion) {
remote.fetchBlockEditorSettings(forSiteID: blog.dotComID?.intValue) { [weak self] (response) in
guard let `self` = self else { return }
switch response {
case .success(let remoteSettings):
self.context.perform {
let originalChecksum = self.blog.blockEditorSettings?.checksum ?? ""
self.track(isBlockEditorSettings: true, isFSE: remoteSettings?.isFSETheme ?? false)
self.updateBlockEditorSettingsCache(originalChecksum: originalChecksum, remoteSettings: remoteSettings, completion: completion)
}
case .failure(let err):
DDLogError("Error fetching editor settings: \(err)")
// The user may not have the gutenberg plugin installed so try /wp/v2/themes to maintain feature support.
// In WP 5.9 we may be able to skip this attempt.
self.fetchTheme(completion)
}
}
}
func updateBlockEditorSettingsCache(originalChecksum: String, remoteSettings: RemoteBlockEditorSettings?, completion: @escaping BlockEditorSettingsServiceCompletion) {
let newChecksum = remoteSettings?.checksum ?? ""
guard originalChecksum != newChecksum else {
/// The fetched Block Editor Settings is the same as the cached one so respond with no new changes.
let result = SettingsServiceResult(hasChanges: false, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
return
}
guard let remoteSettings = remoteSettings else {
/// The original checksum is different than an empty one so we need to clear the old settings.
clearCoreData(completion: completion)
return
}
/// The fetched Block Editor Settings is different than the cached one so persist the new one and delete the old one.
context.perform {
self.persistBlockEditorSettingsToCoreData(blogID: self.blog.objectID, remoteSettings: remoteSettings) { callback in
switch callback {
case .success:
self.context.perform {
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: self.blog.blockEditorSettings)
completion(.success(result))
}
case .failure(let err):
completion(.failure(err))
}
}
}
}
func persistBlockEditorSettingsToCoreData(blogID: NSManagedObjectID, remoteSettings: RemoteBlockEditorSettings, completion: @escaping (Swift.Result<Void, Error>) -> Void) {
let parsingContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
parsingContext.parent = context
parsingContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
parsingContext.perform {
guard let blog = parsingContext.object(with: blogID) as? Blog else {
let err = BlockEditorSettingsServiceError.blogNotFound
completion(.failure(err))
return
}
if let blockEditorSettings = blog.blockEditorSettings {
// Block Editor Settings nullify on delete
parsingContext.delete(blockEditorSettings)
}
blog.blockEditorSettings = BlockEditorSettings(remoteSettings: remoteSettings, context: parsingContext)
do {
try parsingContext.save()
} catch let err {
completion(.failure(err))
}
completion(.success(()))
}
}
}
// MARK: Shared Events
private extension BlockEditorSettingsService {
func clearCoreData(completion: @escaping BlockEditorSettingsServiceCompletion) {
self.context.perform {
if let blockEditorSettings = self.blog.blockEditorSettings {
// Block Editor Settings nullify on delete
self.context.delete(blockEditorSettings)
}
let result = SettingsServiceResult(hasChanges: true, blockEditorSettings: nil)
completion(.success(result))
}
}
func track(isBlockEditorSettings: Bool, isFSE: Bool) {
let endpoint = isBlockEditorSettings ? "wp-block-editor" : "theme_supports"
let properties: [AnyHashable: Any] = ["endpoint": endpoint,
"full_site_editing": "\(isFSE)"]
WPAnalytics.track(.gutenbergEditorSettingsFetched, properties: properties)
}
}