Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public enum CardPresentPaymentsPlugin: Equatable, CaseIterable {
}
}

public var fileNameWithoutExtension: String {
public var plugin: Plugin {
switch self {
case .wcPay:
return "woocommerce-payments"
return .wooPayments
case .stripe:
return "woocommerce-gateway-stripe"
return .stripe
}
}

Expand Down
47 changes: 47 additions & 0 deletions Modules/Sources/Yosemite/Tools/Plugin.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,64 @@
import Foundation

public enum Plugin: Equatable, CaseIterable {
case blaze
case jetpack
case googleListingsAndAds
case stripe
case wooCommerce
case wooCompositeProducts
case wooGiftCards
case wooPayments
case wooPayPal
case wooProductBundles
case wooSubscriptions
case wooShipmentTracking
case wooSquare

/// Creates a Plugin from a plugin file name in the plugin path.
/// Returns nil if the file name doesn't match any known Plugin case.
///
/// - Parameter systemPlugin: The SystemPlugin to convert.
public init?(fileNameWithoutExtension: String) {
guard let plugin = Plugin.allCases.first(where: { $0.fileNameWithoutExtension == fileNameWithoutExtension }) else {
return nil
}
self = plugin
}

/// Creates a Plugin from a SystemPlugin by matching the plugin file name in the plugin path.
/// Returns nil if the SystemPlugin doesn't match any known Plugin case.
///
/// - Parameter systemPlugin: The SystemPlugin to convert.
public init?(systemPlugin: SystemPlugin) {
let pluginFileNameWithoutExtension = systemPlugin.fileNameWithoutExtension
self.init(fileNameWithoutExtension: pluginFileNameWithoutExtension)
}

/// File name without extension in the plugin path.
/// Full plugin path is like `woocommerce/woocommerce.php`.
var fileNameWithoutExtension: String {
switch self {
case .blaze:
return "blaze-ads"
case .jetpack:
return "jetpack"
case .googleListingsAndAds:
return "google-listings-and-ads"
case .stripe:
return "woocommerce-gateway-stripe"
case .wooCommerce:
return "woocommerce"
case .wooCompositeProducts:
return "woocommerce-composite-products"
case .wooGiftCards:
return "woocommerce-gift-cards"
case .wooPayments:
return "woocommerce-payments"
case .wooPayPal:
return "woocommerce-paypal-payments"
case .wooProductBundles:
return "woocommerce-product-bundles"
case .wooSubscriptions:
return "woocommerce-subscriptions"
case .wooShipmentTracking:
Expand All @@ -27,3 +68,9 @@ public enum Plugin: Equatable, CaseIterable {
}
}
}

private extension SystemPlugin {
var fileNameWithoutExtension: String {
((plugin as NSString).lastPathComponent as NSString).deletingPathExtension
}
}
109 changes: 109 additions & 0 deletions Modules/Tests/YosemiteTests/Tools/PluginTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Foundation
import Testing
import Networking

@testable import Yosemite

struct PluginTests {
@Test(arguments: [
("blaze-ads/blaze-ads.php", Plugin.blaze),
("jetpack/jetpack.php", Plugin.jetpack),
("google-listings-and-ads/google-listings-and-ads.php", Plugin.googleListingsAndAds),
("woocommerce-gateway-stripe/woocommerce-gateway-stripe.php", Plugin.stripe),
("woocommerce/woocommerce.php", Plugin.wooCommerce),
("woocommerce-composite-products/woocommerce-composite-products.php", Plugin.wooCompositeProducts),
("woocommerce-gift-cards/woocommerce-gift-cards.php", Plugin.wooGiftCards),
("woocommerce-payments/woocommerce-payments.php", Plugin.wooPayments),
("woocommerce-paypal-payments/woocommerce-paypal-payments.php", Plugin.wooPayPal),
("woocommerce-product-bundles/woocommerce-product-bundles.php", Plugin.wooProductBundles),
("woocommerce-subscriptions/woocommerce-subscriptions.php", Plugin.wooSubscriptions),
("woocommerce-shipment-tracking/woocommerce-shipment-tracking.php", Plugin.wooShipmentTracking),
("woocommerce-square/woocommerce-square.php", Plugin.wooSquare),
// Altered paths
("_woocommerce-subscriptions/woocommerce-subscriptions.php", .wooSubscriptions),
("woocommerce-subscriptions.php", .wooSubscriptions),
("test/woocommerce-dev.php", nil)
])
func init_with_systemPlugin_returns_correct_plugin(pluginPath: String, expectedPlugin: Plugin?) {
// Given
let systemPlugin = SystemPlugin.fake().copy(
siteID: 134,
plugin: pluginPath,
active: true
)

// When
let plugin = Plugin(systemPlugin: systemPlugin)

// Then
#expect(plugin == expectedPlugin)
}

@Test func init_with_systemPlugin_returns_nil_for_unknown_plugin() {
// Given
let systemPlugin = SystemPlugin.fake().copy(
siteID: 134,
plugin: "unknown-plugin/unknown-plugin.php"
)

// When
let plugin = Plugin(systemPlugin: systemPlugin)

// Then
#expect(plugin == nil)
}

@Test func init_with_systemPlugin_returns_nil_for_empty_plugin_path() {
// Given
let systemPlugin = SystemPlugin.fake().copy(
plugin: ""
)

// When
let plugin = Plugin(systemPlugin: systemPlugin)

// Then
#expect(plugin == nil)
}

// MARK: - `init(fileNameWithoutExtension:)`

@Test(arguments: [
("blaze-ads", Plugin.blaze),
("jetpack", Plugin.jetpack),
("google-listings-and-ads", Plugin.googleListingsAndAds),
("woocommerce-gateway-stripe", Plugin.stripe),
("woocommerce", Plugin.wooCommerce),
("woocommerce-composite-products", Plugin.wooCompositeProducts),
("woocommerce-gift-cards", Plugin.wooGiftCards),
("woocommerce-payments", Plugin.wooPayments),
("woocommerce-paypal-payments", Plugin.wooPayPal),
("woocommerce-product-bundles", Plugin.wooProductBundles),
("woocommerce-subscriptions", Plugin.wooSubscriptions),
("woocommerce-shipment-tracking", Plugin.wooShipmentTracking),
("woocommerce-square", Plugin.wooSquare)
])
func init_with_fileNameWithoutExtension_returns_correct_plugin(fileName: String, expectedPlugin: Plugin) {
// When
let plugin = Plugin(fileNameWithoutExtension: fileName)

// Then
#expect(plugin == expectedPlugin)
}

@Test func init_with_fileNameWithoutExtension_returns_nil_for_unknown_filename() {
// When
let plugin = Plugin(fileNameWithoutExtension: "wooooocommerce")

// Then
#expect(plugin == nil)
}

@Test func init_with_fileNameWithoutExtension_returns_nil_for_empty_filename() {
// When
let plugin = Plugin(fileNameWithoutExtension: "")

// Then
#expect(plugin == nil)
}
}
8 changes: 1 addition & 7 deletions WooCommerce/Classes/Blaze/BlazeEligibilityChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private extension BlazeEligibilityChecker {
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: siteID) { result in
switch result {
case .success(let info):
let plugin = info.systemPlugins.first(where: { $0.plugin == Constants.pluginSlug })
let plugin = info.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .blaze && $0.active })
continuation.resume(returning: plugin)
case .failure:
continuation.resume(returning: nil)
Expand All @@ -80,9 +80,3 @@ private extension BlazeEligibilityChecker {
}
}
}

private extension BlazeEligibilityChecker {
enum Constants {
static let pluginSlug = "blaze-ads/blaze-ads.php"
}
}
8 changes: 0 additions & 8 deletions WooCommerce/Classes/Extensions/SitePlugin+Woo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import Yosemite
/// Defines the names of the Site Plugins officially supported by the app.
///
extension SitePlugin {
enum SupportedPlugin {
public static let WCSubscriptions = ["WooCommerce Subscriptions", "Woo Subscriptions"]
public static let WCProductBundles = ["WooCommerce Product Bundles", "Woo Product Bundles"]
public static let WCCompositeProducts = "WooCommerce Composite Products"
public static let WCGiftCards = ["WooCommerce Gift Cards", "Woo Gift Cards"]
public static let GoogleForWooCommerce = ["Google Listings and Ads", "Google for WooCommerce"]
}

enum SupportedPluginPath {
public static let LegacyWCShip = "woocommerce-services/woocommerce-services.php"
public static let WooShipping = "woocommerce-shipping/woocommerce-shipping.php"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private extension DefaultGoogleAdsEligibilityChecker {
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: siteID) { result in
switch result {
case .success(let info):
let plugin = info.systemPlugins.first(where: { $0.plugin == Constants.pluginSlug })
let plugin = info.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .googleListingsAndAds && $0.active })
continuation.resume(returning: plugin)
case .failure:
continuation.resume(returning: nil)
Expand All @@ -76,8 +76,6 @@ private extension DefaultGoogleAdsEligibilityChecker {
}

enum Constants {
static let pluginSlug = "google-listings-and-ads/google-listings-and-ads.php"

/// Version 2.7.7 is required for an optimized experience of the plugin on the mobile web.
/// Ref: https://github.com/woocommerce/google-listings-and-ads/releases/tag/2.7.7.
/// We can remove this limit once we support native experience.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ struct CardPresentPluginsDataProvider: CardPresentPluginsDataProviderProtocol {
}
return storageManager.viewStorage
.loadSystemPlugins(siteID: siteID).map { $0.toReadOnly() }
.first(where: { $0.fileNameWithoutExtension == configuration.fileNameWithoutExtension })
}
}

extension Yosemite.SystemPlugin {
var fileNameWithoutExtension: String {
((plugin as NSString).lastPathComponent as NSString).deletingPathExtension
.first(where: { Plugin(systemPlugin: $0) == configuration.plugin })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ private extension CustomersListViewModel {
}

enum Constants {
static let wcPluginName = "WooCommerce"
static let wcPluginMinimumVersion = "8.0.0-beta.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ final class AnalyticsHubViewModel: ObservableObject {
case .sessions:
isEligibleForSessionsCard
case .bundles:
isPluginActive(SitePlugin.SupportedPlugin.WCProductBundles)
isPluginActive(.wooProductBundles)
case .giftCards:
isPluginActive(SitePlugin.SupportedPlugin.WCGiftCards)
isPluginActive(.wooGiftCards)
case .googleCampaigns:
isPluginActive(SitePlugin.SupportedPlugin.GoogleForWooCommerce) && googleCampaignsCard.isEligibleForGoogleAds
isPluginActive(.googleListingsAndAds) && googleCampaignsCard.isEligibleForGoogleAds
default:
true
}
Expand Down Expand Up @@ -258,17 +258,17 @@ final class AnalyticsHubViewModel: ObservableObject {
///
private var timeRangeSelection: AnalyticsHubTimeRangeSelection

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

/// Request stats data from network
Expand Down Expand Up @@ -570,11 +570,11 @@ private extension AnalyticsHubViewModel {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ private extension JetpackSetupCoordinator {
stores.dispatch(SystemStatusAction.synchronizeSystemInformation(siteID: 0) { result in
switch result {
case let .success(systemInformation):
if let plugin = systemInformation.systemPlugins.first(where: { $0.name.lowercased() == Constants.jetpackPluginName.lowercased() }),
plugin.active {
if let plugin = systemInformation.systemPlugins.first(where: { Plugin(systemPlugin: $0) == .jetpack && $0.active }) {
continuation.resume(returning: true)
} else {
continuation.resume(returning: false)
Expand Down Expand Up @@ -504,7 +503,6 @@ private extension JetpackSetupCoordinator {

enum Constants {
static let magicLinkUrlHostname = "magic-login"
static let jetpackPluginName = "Jetpack"
}

enum Localization {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class WooSubscriptionProductsEligibilityChecker: WooSubscriptionProductsEl

private extension WooSubscriptionProductsEligibilityChecker {
func isWooSubscriptionsPluginActive() -> Bool {
let activePluginNames = resultsController.fetchedObjects
.map { $0.name }
return Set(activePluginNames).intersection(SitePlugin.SupportedPlugin.WCSubscriptions).count > 0
let activePlugins = resultsController.fetchedObjects
.compactMap { Plugin(systemPlugin: $0) }
return activePlugins.contains(.wooSubscriptions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ final class ProductPasswordEligibilityUseCase {
private func getSystemPlugin(siteID: Int64) -> Yosemite.SystemPlugin? {
return storageManager.viewStorage
.loadSystemPlugins(siteID: siteID).map { $0.toReadOnly() }
.first(where: { $0.fileNameWithoutExtension == Constants.wcPluginName })
.first(where: { Plugin(systemPlugin: $0) == .wooCommerce })
}
}

private extension ProductPasswordEligibilityUseCase {
enum Constants {
static let wcPluginName = "woocommerce"
static let wcPluginMinimumVersion = "8.1.0"
}
}
Loading
Loading