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 pathPluginState.swift
More file actions
121 lines (104 loc) · 4 KB
/
Copy pathPluginState.swift
File metadata and controls
121 lines (104 loc) · 4 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
import Foundation
public struct PluginState: Equatable, Codable {
@frozen public enum UpdateState: Equatable, Codable {
public static func ==(lhs: PluginState.UpdateState, rhs: PluginState.UpdateState) -> Bool {
switch (lhs, rhs) {
case (.updated, .updated):
return true
case (.available(let lhsValue), .available(let rhsValue)):
return lhsValue == rhsValue
case (.updating(let lhsValue), .updating(let rhsValue)):
return lhsValue == rhsValue
default:
return false
}
}
private enum CodingKeys: String, CodingKey {
case updated
case available
case updating
}
case updated
case available(String)
case updating(String)
public func encode(to encoder: Encoder) throws {
var encoder = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .updated:
try encoder.encode(true, forKey: .updated)
case .available(let value):
try encoder.encode(value, forKey: .available)
case .updating(let value):
try encoder.encode(value, forKey: .updating)
}
}
public init(from decoder: Decoder) throws {
let decoder = try decoder.container(keyedBy: CodingKeys.self)
if let _ = try decoder.decodeIfPresent(Bool.self, forKey: .updated) {
self = .updated
return
}
if let value = try decoder.decodeIfPresent(String.self, forKey: .available) {
self = .available(value)
return
}
if let value = try decoder.decodeIfPresent(String.self, forKey: .updating) {
self = .updating(value)
return
}
self = .updated
}
}
public let id: String
public let slug: String
public var active: Bool
public let name: String
public let author: String
public let version: String?
public var updateState: UpdateState
public var autoupdate: Bool
public var automanaged: Bool
public let url: URL?
public let settingsURL: URL?
public static func ==(lhs: PluginState, rhs: PluginState) -> Bool {
return lhs.id == rhs.id
&& lhs.slug == rhs.slug
&& lhs.active == rhs.active
&& lhs.name == rhs.name
&& lhs.version == rhs.version
&& lhs.updateState == rhs.updateState
&& lhs.autoupdate == rhs.autoupdate
&& lhs.automanaged == rhs.automanaged
&& lhs.url == rhs.url
}
}
public extension PluginState {
var stateDescription: String {
if automanaged {
return NSLocalizedString("Auto-managed on this site", comment: "The plugin can not be manually updated or deactivated")
}
switch (active, autoupdate) {
case (false, false):
return NSLocalizedString("Inactive, Autoupdates off", comment: "The plugin is not active on the site and has not enabled automatic updates")
case (false, true):
return NSLocalizedString("Inactive, Autoupdates on", comment: "The plugin is not active on the site and has enabled automatic updates")
case (true, false):
return NSLocalizedString("Active, Autoupdates off", comment: "The plugin is active on the site and has not enabled automatic updates")
case (true, true):
return NSLocalizedString("Active, Autoupdates on", comment: "The plugin is active on the site and has enabled automatic updates")
}
}
var homeURL: URL? {
return url
}
var directoryURL: URL? {
return URL(string: "https://wordpress.org/plugins/\(slug)")
}
var deactivateAllowed: Bool {
return !isJetpack && !automanaged
}
var isJetpack: Bool {
return slug == "jetpack"
|| slug == "jetpack-dev"
}
}