Skip to content

Commit 8f77c38

Browse files
authored
Update WC plugin matching to use the file name in plugin path: use cases that match plugin by name - part 3 (#15947)
2 parents f0bb4ee + aaf186f commit 8f77c38

File tree

21 files changed

+243
-133
lines changed

21 files changed

+243
-133
lines changed

Modules/Sources/Yosemite/Model/Enums/CardPresentPaymentsPlugin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public enum CardPresentPaymentsPlugin: Equatable, CaseIterable {
1313
}
1414
}
1515

16-
public var fileNameWithoutExtension: String {
16+
public var plugin: Plugin {
1717
switch self {
1818
case .wcPay:
19-
return "woocommerce-payments"
19+
return .wooPayments
2020
case .stripe:
21-
return "woocommerce-gateway-stripe"
21+
return .stripe
2222
}
2323
}
2424

Modules/Sources/Yosemite/Tools/Plugin.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
import Foundation
22

33
public enum Plugin: Equatable, CaseIterable {
4+
case blaze
5+
case jetpack
6+
case googleListingsAndAds
47
case stripe
58
case wooCommerce
9+
case wooCompositeProducts
10+
case wooGiftCards
611
case wooPayments
12+
case wooPayPal
13+
case wooProductBundles
714
case wooSubscriptions
815
case wooShipmentTracking
916
case wooSquare
1017

18+
/// Creates a Plugin from a plugin file name in the plugin path.
19+
/// Returns nil if the file name doesn't match any known Plugin case.
20+
///
21+
/// - Parameter systemPlugin: The SystemPlugin to convert.
22+
public init?(fileNameWithoutExtension: String) {
23+
guard let plugin = Plugin.allCases.first(where: { $0.fileNameWithoutExtension == fileNameWithoutExtension }) else {
24+
return nil
25+
}
26+
self = plugin
27+
}
28+
29+
/// Creates a Plugin from a SystemPlugin by matching the plugin file name in the plugin path.
30+
/// Returns nil if the SystemPlugin doesn't match any known Plugin case.
31+
///
32+
/// - Parameter systemPlugin: The SystemPlugin to convert.
33+
public init?(systemPlugin: SystemPlugin) {
34+
let pluginFileNameWithoutExtension = systemPlugin.fileNameWithoutExtension
35+
self.init(fileNameWithoutExtension: pluginFileNameWithoutExtension)
36+
}
37+
1138
/// File name without extension in the plugin path.
1239
/// Full plugin path is like `woocommerce/woocommerce.php`.
1340
var fileNameWithoutExtension: String {
1441
switch self {
42+
case .blaze:
43+
return "blaze-ads"
44+
case .jetpack:
45+
return "jetpack"
46+
case .googleListingsAndAds:
47+
return "google-listings-and-ads"
1548
case .stripe:
1649
return "woocommerce-gateway-stripe"
1750
case .wooCommerce:
1851
return "woocommerce"
52+
case .wooCompositeProducts:
53+
return "woocommerce-composite-products"
54+
case .wooGiftCards:
55+
return "woocommerce-gift-cards"
1956
case .wooPayments:
2057
return "woocommerce-payments"
58+
case .wooPayPal:
59+
return "woocommerce-paypal-payments"
60+
case .wooProductBundles:
61+
return "woocommerce-product-bundles"
2162
case .wooSubscriptions:
2263
return "woocommerce-subscriptions"
2364
case .wooShipmentTracking:
@@ -27,3 +68,9 @@ public enum Plugin: Equatable, CaseIterable {
2768
}
2869
}
2970
}
71+
72+
private extension SystemPlugin {
73+
var fileNameWithoutExtension: String {
74+
((plugin as NSString).lastPathComponent as NSString).deletingPathExtension
75+
}
76+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import Foundation
2+
import Testing
3+
import Networking
4+
5+
@testable import Yosemite
6+
7+
struct PluginTests {
8+
@Test(arguments: [
9+
("blaze-ads/blaze-ads.php", Plugin.blaze),
10+
("jetpack/jetpack.php", Plugin.jetpack),
11+
("google-listings-and-ads/google-listings-and-ads.php", Plugin.googleListingsAndAds),
12+
("woocommerce-gateway-stripe/woocommerce-gateway-stripe.php", Plugin.stripe),
13+
("woocommerce/woocommerce.php", Plugin.wooCommerce),
14+
("woocommerce-composite-products/woocommerce-composite-products.php", Plugin.wooCompositeProducts),
15+
("woocommerce-gift-cards/woocommerce-gift-cards.php", Plugin.wooGiftCards),
16+
("woocommerce-payments/woocommerce-payments.php", Plugin.wooPayments),
17+
("woocommerce-paypal-payments/woocommerce-paypal-payments.php", Plugin.wooPayPal),
18+
("woocommerce-product-bundles/woocommerce-product-bundles.php", Plugin.wooProductBundles),
19+
("woocommerce-subscriptions/woocommerce-subscriptions.php", Plugin.wooSubscriptions),
20+
("woocommerce-shipment-tracking/woocommerce-shipment-tracking.php", Plugin.wooShipmentTracking),
21+
("woocommerce-square/woocommerce-square.php", Plugin.wooSquare),
22+
// Altered paths
23+
("_woocommerce-subscriptions/woocommerce-subscriptions.php", .wooSubscriptions),
24+
("woocommerce-subscriptions.php", .wooSubscriptions),
25+
("test/woocommerce-dev.php", nil)
26+
])
27+
func init_with_systemPlugin_returns_correct_plugin(pluginPath: String, expectedPlugin: Plugin?) {
28+
// Given
29+
let systemPlugin = SystemPlugin.fake().copy(
30+
siteID: 134,
31+
plugin: pluginPath,
32+
active: true
33+
)
34+
35+
// When
36+
let plugin = Plugin(systemPlugin: systemPlugin)
37+
38+
// Then
39+
#expect(plugin == expectedPlugin)
40+
}
41+
42+
@Test func init_with_systemPlugin_returns_nil_for_unknown_plugin() {
43+
// Given
44+
let systemPlugin = SystemPlugin.fake().copy(
45+
siteID: 134,
46+
plugin: "unknown-plugin/unknown-plugin.php"
47+
)
48+
49+
// When
50+
let plugin = Plugin(systemPlugin: systemPlugin)
51+
52+
// Then
53+
#expect(plugin == nil)
54+
}
55+
56+
@Test func init_with_systemPlugin_returns_nil_for_empty_plugin_path() {
57+
// Given
58+
let systemPlugin = SystemPlugin.fake().copy(
59+
plugin: ""
60+
)
61+
62+
// When
63+
let plugin = Plugin(systemPlugin: systemPlugin)
64+
65+
// Then
66+
#expect(plugin == nil)
67+
}
68+
69+
// MARK: - `init(fileNameWithoutExtension:)`
70+
71+
@Test(arguments: [
72+
("blaze-ads", Plugin.blaze),
73+
("jetpack", Plugin.jetpack),
74+
("google-listings-and-ads", Plugin.googleListingsAndAds),
75+
("woocommerce-gateway-stripe", Plugin.stripe),
76+
("woocommerce", Plugin.wooCommerce),
77+
("woocommerce-composite-products", Plugin.wooCompositeProducts),
78+
("woocommerce-gift-cards", Plugin.wooGiftCards),
79+
("woocommerce-payments", Plugin.wooPayments),
80+
("woocommerce-paypal-payments", Plugin.wooPayPal),
81+
("woocommerce-product-bundles", Plugin.wooProductBundles),
82+
("woocommerce-subscriptions", Plugin.wooSubscriptions),
83+
("woocommerce-shipment-tracking", Plugin.wooShipmentTracking),
84+
("woocommerce-square", Plugin.wooSquare)
85+
])
86+
func init_with_fileNameWithoutExtension_returns_correct_plugin(fileName: String, expectedPlugin: Plugin) {
87+
// When
88+
let plugin = Plugin(fileNameWithoutExtension: fileName)
89+
90+
// Then
91+
#expect(plugin == expectedPlugin)
92+
}
93+
94+
@Test func init_with_fileNameWithoutExtension_returns_nil_for_unknown_filename() {
95+
// When
96+
let plugin = Plugin(fileNameWithoutExtension: "wooooocommerce")
97+
98+
// Then
99+
#expect(plugin == nil)
100+
}
101+
102+
@Test func init_with_fileNameWithoutExtension_returns_nil_for_empty_filename() {
103+
// When
104+
let plugin = Plugin(fileNameWithoutExtension: "")
105+
106+
// Then
107+
#expect(plugin == nil)
108+
}
109+
}

WooCommerce/Classes/Blaze/BlazeEligibilityChecker.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private extension BlazeEligibilityChecker {
7171
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: siteID) { result in
7272
switch result {
7373
case .success(let info):
74-
let plugin = info.systemPlugins.first(where: { $0.plugin == Constants.pluginSlug })
74+
let plugin = info.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .blaze && $0.active })
7575
continuation.resume(returning: plugin)
7676
case .failure:
7777
continuation.resume(returning: nil)
@@ -80,9 +80,3 @@ private extension BlazeEligibilityChecker {
8080
}
8181
}
8282
}
83-
84-
private extension BlazeEligibilityChecker {
85-
enum Constants {
86-
static let pluginSlug = "blaze-ads/blaze-ads.php"
87-
}
88-
}

WooCommerce/Classes/Extensions/SitePlugin+Woo.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ import Yosemite
33
/// Defines the names of the Site Plugins officially supported by the app.
44
///
55
extension SitePlugin {
6-
enum SupportedPlugin {
7-
public static let WCSubscriptions = ["WooCommerce Subscriptions", "Woo Subscriptions"]
8-
public static let WCProductBundles = ["WooCommerce Product Bundles", "Woo Product Bundles"]
9-
public static let WCCompositeProducts = "WooCommerce Composite Products"
10-
public static let WCGiftCards = ["WooCommerce Gift Cards", "Woo Gift Cards"]
11-
public static let GoogleForWooCommerce = ["Google Listings and Ads", "Google for WooCommerce"]
12-
}
13-
146
enum SupportedPluginPath {
157
public static let LegacyWCShip = "woocommerce-services/woocommerce-services.php"
168
public static let WooShipping = "woocommerce-shipping/woocommerce-shipping.php"

WooCommerce/Classes/GoogleAds/GoogleAdsEligibilityChecker.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private extension DefaultGoogleAdsEligibilityChecker {
4949
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: siteID) { result in
5050
switch result {
5151
case .success(let info):
52-
let plugin = info.systemPlugins.first(where: { $0.plugin == Constants.pluginSlug })
52+
let plugin = info.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .googleListingsAndAds && $0.active })
5353
continuation.resume(returning: plugin)
5454
case .failure:
5555
continuation.resume(returning: nil)
@@ -76,8 +76,6 @@ private extension DefaultGoogleAdsEligibilityChecker {
7676
}
7777

7878
enum Constants {
79-
static let pluginSlug = "google-listings-and-ads/google-listings-and-ads.php"
80-
8179
/// Version 2.7.7 is required for an optimized experience of the plugin on the mobile web.
8280
/// Ref: https://github.com/woocommerce/google-listings-and-ads/releases/tag/2.7.7.
8381
/// We can remove this limit once we support native experience.

WooCommerce/Classes/Tools/CardPresentPluginsDataProvider.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ struct CardPresentPluginsDataProvider: CardPresentPluginsDataProviderProtocol {
9595
}
9696
return storageManager.viewStorage
9797
.loadSystemPlugins(siteID: siteID).map { $0.toReadOnly() }
98-
.first(where: { $0.fileNameWithoutExtension == configuration.fileNameWithoutExtension })
99-
}
100-
}
101-
102-
extension Yosemite.SystemPlugin {
103-
var fileNameWithoutExtension: String {
104-
((plugin as NSString).lastPathComponent as NSString).deletingPathExtension
98+
.first(where: { Plugin(systemPlugin: $0) == configuration.plugin })
10599
}
106100
}

WooCommerce/Classes/ViewRelated/Customers/CustomersListViewModel.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ private extension CustomersListViewModel {
329329
}
330330

331331
enum Constants {
332-
static let wcPluginName = "WooCommerce"
333332
static let wcPluginMinimumVersion = "8.0.0-beta.1"
334333
}
335334
}

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/AnalyticsHubViewModel.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ final class AnalyticsHubViewModel: ObservableObject {
164164
case .sessions:
165165
isEligibleForSessionsCard
166166
case .bundles:
167-
isPluginActive(SitePlugin.SupportedPlugin.WCProductBundles)
167+
isPluginActive(.wooProductBundles)
168168
case .giftCards:
169-
isPluginActive(SitePlugin.SupportedPlugin.WCGiftCards)
169+
isPluginActive(.wooGiftCards)
170170
case .googleCampaigns:
171-
isPluginActive(SitePlugin.SupportedPlugin.GoogleForWooCommerce) && googleCampaignsCard.isEligibleForGoogleAds
171+
isPluginActive(.googleListingsAndAds) && googleCampaignsCard.isEligibleForGoogleAds
172172
default:
173173
true
174174
}
@@ -258,17 +258,17 @@ final class AnalyticsHubViewModel: ObservableObject {
258258
///
259259
private var timeRangeSelection: AnalyticsHubTimeRangeSelection
260260

261-
/// Names of the active plugins on the store.
261+
/// A list of known active plugins on the store.
262262
///
263-
private lazy var activePlugins: [String] = {
263+
private lazy var activePlugins: [Plugin] = {
264264
let predicate = NSPredicate(format: "siteID == %lld && active == true", siteID)
265265
let resultsController = ResultsController<StorageSystemPlugin>(storageManager: storage, matching: predicate, sortedBy: [])
266266
do {
267267
try resultsController.performFetch()
268268
} catch {
269269
DDLogError("⛔️ Error fetching active plugins for Analytics Hub")
270270
}
271-
return resultsController.fetchedObjects.map { $0.name }
271+
return resultsController.fetchedObjects.compactMap { Plugin(systemPlugin: $0) }
272272
}()
273273

274274
/// Request stats data from network
@@ -570,11 +570,11 @@ private extension AnalyticsHubViewModel {
570570
}
571571
}
572572

573-
/// Helper function that returns `true` in its callback if the provided plugin name is active on the store.
573+
/// Helper function that returns `true` in its callback if the provided plugin is active on the store.
574574
///
575-
/// - Parameter plugin: A list of names for the plugin (provide all possible names for plugins that have changed names).
576-
private func isPluginActive(_ plugin: [String]) -> Bool {
577-
activePlugins.contains(where: plugin.contains)
575+
/// - Parameter plugin: Plugin to check.
576+
private func isPluginActive(_ plugin: Plugin) -> Bool {
577+
activePlugins.contains(plugin)
578578
}
579579
}
580580

WooCommerce/Classes/ViewRelated/JetpackSetup/JetpackSetupCoordinator.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ private extension JetpackSetupCoordinator {
346346
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: 0) { result in
347347
switch result {
348348
case let .success(systemInformation):
349-
if let plugin = systemInformation.systemPlugins.first(where: { $0.name.lowercased() == Constants.jetpackPluginName.lowercased() }),
350-
plugin.active {
349+
if let plugin = systemInformation.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .jetpack && $0.active }) {
351350
continuation.resume(returning: true)
352351
} else {
353352
continuation.resume(returning: false)
@@ -504,7 +503,6 @@ private extension JetpackSetupCoordinator {
504503

505504
enum Constants {
506505
static let magicLinkUrlHostname = "magic-login"
507-
static let jetpackPluginName = "Jetpack"
508506
}
509507

510508
enum Localization {

0 commit comments

Comments
 (0)