This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSelfHostedPluginManagementClient.swift
More file actions
162 lines (140 loc) · 6.18 KB
/
Copy pathSelfHostedPluginManagementClient.swift
File metadata and controls
162 lines (140 loc) · 6.18 KB
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
public class SelfHostedPluginManagementClient: PluginManagementClient {
private let remote: WordPressOrgRestApi
public required init?(with remote: WordPressOrgRestApi) {
self.remote = remote
}
// MARK: - Get
public func getPlugins(success: @escaping (SitePlugins) -> Void, failure: @escaping (Error) -> Void) {
Task { @MainActor in
await remote.get(path: path(), type: [PluginStateResponse].self)
.mapError { error -> Error in
if case let .unparsableResponse(_, _, underlyingError) = error, underlyingError is DecodingError {
return PluginServiceRemote.ResponseError.decodingFailure
}
return error
}
.map {
SitePlugins(
plugins: $0.compactMap { self.pluginState(with: $0) },
capabilities: SitePluginCapabilities(modify: true, autoupdate: false)
)
}
.execute(onSuccess: success, onFailure: failure)
}
}
// MARK: - Activate / Deactivate
public func activatePlugin(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = ["status": "active"]
let path = self.path(with: pluginID)
Task { @MainActor in
await remote.perform(.put, path: path, parameters: parameters, type: AnyResponse.self)
.map { _ in }
.execute(onSuccess: success, onFailure: failure)
}
}
public func deactivatePlugin(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = ["status": "inactive"]
let path = self.path(with: pluginID)
Task { @MainActor in
await remote.perform(.put, path: path, parameters: parameters, type: AnyResponse.self)
.map { _ in }
.execute(onSuccess: success, onFailure: failure)
}
}
// MARK: - Install / Uninstall
public func install(pluginSlug: String, success: @escaping (PluginState) -> Void, failure: @escaping (Error) -> Void) {
let parameters = ["slug": pluginSlug]
Task { @MainActor in
await remote.post(path: path(), parameters: parameters, type: PluginStateResponse.self)
.mapError { error -> Error in
if case let .unparsableResponse(_, _, underlyingError) = error, underlyingError is DecodingError {
return PluginServiceRemote.ResponseError.decodingFailure
}
return error
}
.flatMap {
guard let state = self.pluginState(with: $0) else {
return .failure(PluginServiceRemote.ResponseError.decodingFailure)
}
return .success(state)
}
.execute(onSuccess: success, onFailure: failure)
}
}
public func remove(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let path = self.path(with: pluginID)
Task { @MainActor in
await remote.perform(.delete, path: path, type: AnyResponse.self)
.map { _ in }
.execute(onSuccess: success, onFailure: failure)
}
}
// MARK: - Private: Helpers
private func path(with slug: String? = nil) -> String {
var returnPath = "wp/v2/plugins/"
if let slug = slug {
returnPath = returnPath.appending(slug)
}
return returnPath
}
private func pluginState(with response: PluginStateResponse) -> PluginState? {
guard
// The slugs returned are in the form of XXX/YYY
// The PluginStore uses slugs that are just XXX
// Extract that information out
let slug = response.plugin.components(separatedBy: "/").first
else {
return nil
}
let isActive = response.status == "active"
return PluginState(id: response.plugin,
slug: slug,
active: isActive,
name: response.name,
author: response.author,
version: response.version,
updateState: .updated, // API Doesn't support this yet
autoupdate: false, // API Doesn't support this yet
automanaged: false, // API Doesn't support this yet
// TODO: Return nil instead of an empty URL when 'plugin_uri' is nil?
url: URL(string: response.pluginURI ?? ""),
settingsURL: nil)
}
// MARK: - Unsupported
public func updatePlugin(pluginID: String, success: @escaping (PluginState) -> Void, failure: @escaping (Error) -> Void) {
// NOOP - Not supported by the WP.org REST API
}
public func enableAutoupdates(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
// NOOP - Not supported by the WP.org REST API
success()
}
public func disableAutoupdates(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
// NOOP - Not supported by the WP.org REST API
success()
}
public func activateAndEnableAutoupdates(pluginID: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
// Just activate since API does not support autoupdates yet
activatePlugin(pluginID: pluginID, success: success, failure: failure)
}
}
private struct PluginStateResponse: Decodable {
enum CodingKeys: String, CodingKey {
case plugin = "plugin"
case status = "status"
case name = "name"
case author = "author"
case version = "version"
case pluginURI = "plugin_uri"
}
var plugin: String
var status: String
var name: String
var author: String
var version: String
var pluginURI: String?
}
private struct AnyResponse: Decodable {
init(from decoder: Decoder) throws {
// Do nothing
}
}