diff --git a/plugins/woocommerce/src/Admin/API/MarketingOverview.php b/plugins/woocommerce/src/Admin/API/MarketingOverview.php index 883ce04c1bb..930dcb4c0fc 100644 --- a/plugins/woocommerce/src/Admin/API/MarketingOverview.php +++ b/plugins/woocommerce/src/Admin/API/MarketingOverview.php @@ -125,14 +125,7 @@ class MarketingOverview extends \WC_REST_Data_Controller { * @return \WP_Error|\WP_REST_Response */ public function get_installed_plugins( $request ) { - /** - * InstalledExtensions - * - * @var InstalledExtensions $installed_extensions - */ - $installed_extensions = wc_get_container()->get( InstalledExtensions::class ); - - return rest_ensure_response( $installed_extensions->get_data() ); + return rest_ensure_response( InstalledExtensions::get_data() ); } } diff --git a/plugins/woocommerce/src/Admin/Marketing/InstalledExtensions.php b/plugins/woocommerce/src/Admin/Marketing/InstalledExtensions.php index 9669b9014c6..b1e9ad82b80 100644 --- a/plugins/woocommerce/src/Admin/Marketing/InstalledExtensions.php +++ b/plugins/woocommerce/src/Admin/Marketing/InstalledExtensions.php @@ -5,46 +5,597 @@ namespace Automattic\WooCommerce\Admin\Marketing; +use Automattic\WooCommerce\Admin\PluginsHelper; + /** * Installed Marketing Extensions class. */ class InstalledExtensions { - /** - * MarketingChannels repository - * - * @var MarketingChannels - */ - protected $marketing_channels; - - /** - * Class initialization, invoked by the DI container. - * - * @param MarketingChannels $marketing_channels The MarketingChannels repository. - * - * @internal - */ - final public function init( MarketingChannels $marketing_channels ) { - $this->marketing_channels = $marketing_channels; - } /** * Gets an array of plugin data for the "Installed marketing extensions" card. + * + * Valid extensions statuses are: installed, activated, configured */ - public function get_data(): array { - return array_map( - function ( MarketingChannelInterface $channel ) { - return [ - 'slug' => $channel->get_slug(), - 'status' => $channel->is_setup_completed() ? 'configured' : 'activated', - 'settingsUrl' => $channel->get_setup_url(), - 'name' => $channel->get_name(), - 'description' => $channel->get_description(), - 'product_listings_status' => $channel->get_product_listings_status(), - 'errors_no' => $channel->get_errors_no(), - 'icon' => $channel->get_icon_url(), - ]; - }, - $this->marketing_channels->get_registered_channels() - ); + public static function get_data() { + $data = []; + + $automatewoo = self::get_automatewoo_extension_data(); + $aw_referral = self::get_aw_referral_extension_data(); + $aw_birthdays = self::get_aw_birthdays_extension_data(); + $mailchimp = self::get_mailchimp_extension_data(); + $facebook = self::get_facebook_extension_data(); + $pinterest = self::get_pinterest_extension_data(); + $google = self::get_google_extension_data(); + $amazon_ebay = self::get_amazon_ebay_extension_data(); + $mailpoet = self::get_mailpoet_extension_data(); + $creative_mail = self::get_creative_mail_extension_data(); + $tiktok = self::get_tiktok_extension_data(); + $jetpack_crm = self::get_jetpack_crm_extension_data(); + $zapier = self::get_zapier_extension_data(); + $salesforce = self::get_salesforce_extension_data(); + $vimeo = self::get_vimeo_extension_data(); + $trustpilot = self::get_trustpilot_extension_data(); + + if ( $automatewoo ) { + $data[] = $automatewoo; + } + + if ( $aw_referral ) { + $data[] = $aw_referral; + } + + if ( $aw_birthdays ) { + $data[] = $aw_birthdays; + } + + if ( $mailchimp ) { + $data[] = $mailchimp; + } + + if ( $facebook ) { + $data[] = $facebook; + } + + if ( $pinterest ) { + $data[] = $pinterest; + } + + if ( $google ) { + $data[] = $google; + } + + if ( $amazon_ebay ) { + $data[] = $amazon_ebay; + } + + if ( $mailpoet ) { + $data[] = $mailpoet; + } + + if ( $creative_mail ) { + $data[] = $creative_mail; + } + + if ( $tiktok ) { + $data[] = $tiktok; + } + + if ( $jetpack_crm ) { + $data[] = $jetpack_crm; + } + + if ( $zapier ) { + $data[] = $zapier; + } + + if ( $salesforce ) { + $data[] = $salesforce; + } + + if ( $vimeo ) { + $data[] = $vimeo; + } + + if ( $trustpilot ) { + $data[] = $trustpilot; + } + + return $data; } + + /** + * Get allowed plugins. + * + * @return array + */ + public static function get_allowed_plugins() { + return [ + 'automatewoo', + 'mailchimp-for-woocommerce', + 'creative-mail-by-constant-contact', + 'facebook-for-woocommerce', + 'pinterest-for-woocommerce', + 'google-listings-and-ads', + 'hubspot-for-woocommerce', + 'woocommerce-amazon-ebay-integration', + 'mailpoet', + ]; + } + + /** + * Get AutomateWoo extension data. + * + * @return array|bool + */ + protected static function get_automatewoo_extension_data() { + $slug = 'automatewoo'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg'; + + if ( 'activated' === $data['status'] && function_exists( 'AW' ) ) { + $data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings' ); + $data['docsUrl'] = 'https://automatewoo.com/docs/'; + $data['status'] = 'configured'; // Currently no configuration step. + } + + return $data; + } + + /** + * Get AutomateWoo Refer a Friend extension data. + * + * @return array|bool + */ + protected static function get_aw_referral_extension_data() { + $slug = 'automatewoo-referrals'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg'; + + if ( 'activated' === $data['status'] ) { + $data['docsUrl'] = 'https://automatewoo.com/docs/refer-a-friend/'; + $data['status'] = 'configured'; + if ( function_exists( 'AW_Referrals' ) ) { + $data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings&tab=referrals' ); + } + } + + return $data; + } + + /** + * Get AutomateWoo Birthdays extension data. + * + * @return array|bool + */ + protected static function get_aw_birthdays_extension_data() { + $slug = 'automatewoo-birthdays'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/automatewoo.svg'; + + if ( 'activated' === $data['status'] ) { + $data['docsUrl'] = 'https://automatewoo.com/docs/getting-started-with-birthdays/'; + $data['status'] = 'configured'; + if ( function_exists( 'AW_Birthdays' ) ) { + $data['settingsUrl'] = admin_url( 'admin.php?page=automatewoo-settings&tab=birthdays' ); + } + } + + return $data; + } + + /** + * Get MailChimp extension data. + * + * @return array|bool + */ + protected static function get_mailchimp_extension_data() { + $slug = 'mailchimp-for-woocommerce'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/mailchimp.svg'; + + if ( 'activated' === $data['status'] && function_exists( 'mailchimp_is_configured' ) ) { + $data['docsUrl'] = 'https://mailchimp.com/help/connect-or-disconnect-mailchimp-for-woocommerce/'; + $data['settingsUrl'] = admin_url( 'admin.php?page=mailchimp-woocommerce' ); + + if ( mailchimp_is_configured() ) { + $data['status'] = 'configured'; + } + } + + return $data; + } + + /** + * Get Facebook extension data. + * + * @return array|bool + */ + protected static function get_facebook_extension_data() { + $slug = 'facebook-for-woocommerce'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/facebook-icon.svg'; + + if ( 'activated' === $data['status'] && function_exists( 'facebook_for_woocommerce' ) ) { + $integration = facebook_for_woocommerce()->get_integration(); + + if ( $integration->is_configured() ) { + $data['status'] = 'configured'; + } + + $data['settingsUrl'] = facebook_for_woocommerce()->get_settings_url(); + $data['docsUrl'] = facebook_for_woocommerce()->get_documentation_url(); + } + + return $data; + } + + /** + * Get Pinterest extension data. + * + * @return array|bool + */ + protected static function get_pinterest_extension_data() { + $slug = 'pinterest-for-woocommerce'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/pinterest.svg'; + + $data['docsUrl'] = 'https://woocommerce.com/document/pinterest-for-woocommerce/?utm_medium=product'; + + if ( 'activated' === $data['status'] && class_exists( 'Pinterest_For_Woocommerce' ) ) { + $pinterest_onboarding_completed = Pinterest_For_Woocommerce()::is_setup_complete(); + if ( $pinterest_onboarding_completed ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/pinterest/settings' ); + } else { + $data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/pinterest/landing' ); + } + } + + return $data; + } + + /** + * Get Google extension data. + * + * @return array|bool + */ + protected static function get_google_extension_data() { + $slug = 'google-listings-and-ads'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/google.svg'; + + if ( 'activated' === $data['status'] && function_exists( 'woogle_get_container' ) && class_exists( '\Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService' ) ) { + + $merchant_center = woogle_get_container()->get( \Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService::class ); + + if ( $merchant_center->is_setup_complete() ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/google/settings' ); + } else { + $data['settingsUrl'] = admin_url( 'admin.php?page=wc-admin&path=/google/start' ); + } + + $data['docsUrl'] = 'https://woocommerce.com/document/google-listings-and-ads/?utm_medium=product'; + } + + return $data; + } + + /** + * Get Amazon / Ebay extension data. + * + * @return array|bool + */ + protected static function get_amazon_ebay_extension_data() { + $slug = 'woocommerce-amazon-ebay-integration'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/amazon-ebay.svg'; + + if ( 'activated' === $data['status'] && class_exists( '\CodistoConnect' ) ) { + + $codisto_merchantid = get_option( 'codisto_merchantid' ); + + // Use same check as codisto admin tabs. + if ( is_numeric( $codisto_merchantid ) ) { + $data['status'] = 'configured'; + } + + $data['settingsUrl'] = admin_url( 'admin.php?page=codisto-settings' ); + $data['docsUrl'] = 'https://woocommerce.com/document/multichannel-for-woocommerce-google-amazon-ebay-walmart-integration/?utm_medium=product'; + } + + return $data; + } + + /** + * Get MailPoet extension data. + * + * @return array|bool + */ + protected static function get_mailpoet_extension_data() { + $slug = 'mailpoet'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/mailpoet.svg'; + + if ( 'activated' === $data['status'] && class_exists( '\MailPoet\API\API' ) ) { + $mailpoet_api = \MailPoet\API\API::MP( 'v1' ); + + if ( ! method_exists( $mailpoet_api, 'isSetupComplete' ) || $mailpoet_api->isSetupComplete() ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=mailpoet-settings' ); + } else { + $data['settingsUrl'] = admin_url( 'admin.php?page=mailpoet-newsletters' ); + } + + $data['docsUrl'] = 'https://kb.mailpoet.com/'; + $data['supportUrl'] = 'https://www.mailpoet.com/support/'; + } + + return $data; + } + + /** + * Get Creative Mail for WooCommerce extension data. + * + * @return array|bool + */ + protected static function get_creative_mail_extension_data() { + $slug = 'creative-mail-by-constant-contact'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/creative-mail-by-constant-contact.png'; + + if ( 'activated' === $data['status'] && class_exists( '\CreativeMail\Helpers\OptionsHelper' ) ) { + if ( ! method_exists( '\CreativeMail\Helpers\OptionsHelper', 'get_instance_id' ) || \CreativeMail\Helpers\OptionsHelper::get_instance_id() !== null ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=creativemail_settings' ); + } else { + $data['settingsUrl'] = admin_url( 'admin.php?page=creativemail' ); + } + + $data['docsUrl'] = 'https://app.creativemail.com/kb/help/WooCommerce'; + $data['supportUrl'] = 'https://app.creativemail.com/kb/help/'; + } + + return $data; + } + + /** + * Get TikTok for WooCommerce extension data. + * + * @return array|bool + */ + protected static function get_tiktok_extension_data() { + $slug = 'tiktok-for-business'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/tiktok.jpg'; + + if ( 'activated' === $data['status'] ) { + if ( false !== get_option( 'tt4b_access_token' ) ) { + $data['status'] = 'configured'; + } + + $data['settingsUrl'] = admin_url( 'admin.php?page=tiktok' ); + $data['docsUrl'] = 'https://woocommerce.com/document/tiktok-for-woocommerce/'; + $data['supportUrl'] = 'https://ads.tiktok.com/athena/user-feedback/?identify_key=6a1e079024806640c5e1e695d13db80949525168a052299b4970f9c99cb5ac78'; + } + + return $data; + } + + /** + * Get Jetpack CRM for WooCommerce extension data. + * + * @return array|bool + */ + protected static function get_jetpack_crm_extension_data() { + $slug = 'zero-bs-crm'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/jetpack-crm.png'; + + if ( 'activated' === $data['status'] ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=zerobscrm-plugin-settings' ); + $data['docsUrl'] = 'https://kb.jetpackcrm.com/'; + $data['supportUrl'] = 'https://kb.jetpackcrm.com/crm-support/'; + } + + return $data; + } + + /** + * Get WooCommerce Zapier extension data. + * + * @return array|bool + */ + protected static function get_zapier_extension_data() { + $slug = 'woocommerce-zapier'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/zapier.png'; + + if ( 'activated' === $data['status'] ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=wc-settings&tab=wc_zapier' ); + $data['docsUrl'] = 'https://docs.om4.io/woocommerce-zapier/'; + } + + return $data; + } + + /** + * Get Salesforce extension data. + * + * @return array|bool + */ + protected static function get_salesforce_extension_data() { + $slug = 'integration-with-salesforce'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/salesforce.jpg'; + + if ( 'activated' === $data['status'] && class_exists( '\Integration_With_Salesforce_Admin' ) ) { + if ( ! method_exists( '\Integration_With_Salesforce_Admin', 'get_connection_status' ) || \Integration_With_Salesforce_Admin::get_connection_status() ) { + $data['status'] = 'configured'; + } + + $data['settingsUrl'] = admin_url( 'admin.php?page=integration-with-salesforce' ); + $data['docsUrl'] = 'https://woocommerce.com/document/salesforce-integration/'; + $data['supportUrl'] = 'https://wpswings.com/submit-query/'; + } + + return $data; + } + + /** + * Get Vimeo extension data. + * + * @return array|bool + */ + protected static function get_vimeo_extension_data() { + $slug = 'vimeo'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/vimeo.png'; + + if ( 'activated' === $data['status'] && class_exists( '\Tribe\Vimeo_WP\Vimeo\Vimeo_Auth' ) ) { + if ( method_exists( '\Tribe\Vimeo_WP\Vimeo\Vimeo_Auth', 'has_access_token' ) ) { + $vimeo_auth = new \Tribe\Vimeo_WP\Vimeo\Vimeo_Auth(); + if ( $vimeo_auth->has_access_token() ) { + $data['status'] = 'configured'; + } + } else { + $data['status'] = 'configured'; + } + + $data['settingsUrl'] = admin_url( 'options-general.php?page=vimeo_settings' ); + $data['docsUrl'] = 'https://woocommerce.com/document/vimeo/'; + $data['supportUrl'] = 'https://vimeo.com/help/contact'; + } + + return $data; + } + + /** + * Get Trustpilot extension data. + * + * @return array|bool + */ + protected static function get_trustpilot_extension_data() { + $slug = 'trustpilot-reviews'; + + if ( ! PluginsHelper::is_plugin_installed( $slug ) ) { + return false; + } + + $data = self::get_extension_base_data( $slug ); + $data['icon'] = WC_ADMIN_IMAGES_FOLDER_URL . '/marketing/trustpilot.png'; + + if ( 'activated' === $data['status'] ) { + $data['status'] = 'configured'; + $data['settingsUrl'] = admin_url( 'admin.php?page=woocommerce-trustpilot-settings-page' ); + $data['docsUrl'] = 'https://woocommerce.com/document/trustpilot-reviews/'; + $data['supportUrl'] = 'https://support.trustpilot.com/hc/en-us/requests/new'; + } + + return $data; + } + + + /** + * Get an array of basic data for a given extension. + * + * @param string $slug Plugin slug. + * + * @return array|false + */ + protected static function get_extension_base_data( $slug ) { + $status = PluginsHelper::is_plugin_active( $slug ) ? 'activated' : 'installed'; + $plugin_data = PluginsHelper::get_plugin_data( $slug ); + + if ( ! $plugin_data ) { + return false; + } + + return [ + 'slug' => $slug, + 'status' => $status, + 'name' => $plugin_data['Name'], + 'description' => html_entity_decode( wp_trim_words( $plugin_data['Description'], 20 ) ), + 'supportUrl' => 'https://woocommerce.com/my-account/create-a-ticket/?utm_medium=product', + ]; + } + } diff --git a/plugins/woocommerce/src/Admin/Marketing/MarketingChannelInterface.php b/plugins/woocommerce/src/Admin/Marketing/MarketingChannelInterface.php index 3a7233f073b..b2dbc3819a2 100644 --- a/plugins/woocommerce/src/Admin/Marketing/MarketingChannelInterface.php +++ b/plugins/woocommerce/src/Admin/Marketing/MarketingChannelInterface.php @@ -71,7 +71,7 @@ interface MarketingChannelInterface { * * @return int The number of issues to resolve, or 0 if there are no issues with the channel. */ - public function get_errors_no(): int; + public function get_errors_count(): int; /** * Returns an array of the channel's marketing campaigns. diff --git a/plugins/woocommerce/src/Admin/Marketing/MarketingChannels.php b/plugins/woocommerce/src/Admin/Marketing/MarketingChannels.php index 0fcb6fd1e5e..786aeb33b5f 100644 --- a/plugins/woocommerce/src/Admin/Marketing/MarketingChannels.php +++ b/plugins/woocommerce/src/Admin/Marketing/MarketingChannels.php @@ -5,7 +5,7 @@ namespace Automattic\WooCommerce\Admin\Marketing; -use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs; +use Exception; /** * MarketingChannels repository class @@ -20,49 +20,18 @@ class MarketingChannels { */ private $registered_channels = []; - /** - * Array of plugin slugs for allowed marketing channels. - * - * @var string[] - */ - private $allowed_channels; - - /** - * MarketingSpecs repository - * - * @var MarketingSpecs - */ - protected $marketing_specs; - - /** - * Class initialization, invoked by the DI container. - * - * @param MarketingSpecs $marketing_specs The MarketingSpecs class. - * - * @internal - */ - final public function init( MarketingSpecs $marketing_specs ) { - $this->marketing_specs = $marketing_specs; - $this->allowed_channels = $this->get_allowed_channels(); - } - /** * Registers a marketing channel. * - * Note that only a predetermined list of third party extensions can be registered as a marketing channel. - * * @param MarketingChannelInterface $channel The marketing channel to register. * * @return void * - * @see MarketingChannels::is_channel_allowed() Checks if the marketing channel is allowed to be registered or not. + * @throws Exception If the given marketing channel is already registered. */ public function register( MarketingChannelInterface $channel ): void { - if ( ! $this->is_channel_allowed( $channel ) ) { - // Silently log an error and bail. - wc_get_logger()->error( sprintf( 'Marketing channel %s (%s) cannot be registered!', $channel->get_name(), $channel->get_slug() ) ); - - return; + if ( isset( $this->registered_channels[ $channel->get_slug() ] ) ) { + throw new Exception( __( 'Marketing channel cannot be registered because there is already a channel registered with the same slug!', 'woocommerce' ) ); } $this->registered_channels[ $channel->get_slug() ] = $channel; @@ -77,55 +46,12 @@ class MarketingChannels { /** * Filter the list of registered marketing channels. * - * Note that only a predetermined list of third party extensions can be registered as a marketing channel. - * Any new plugins added to this array will be cross-checked with that list, which is obtained from WooCommerce.com API. - * * @param MarketingChannelInterface[] $channels Array of registered marketing channels. * * @since x.x.x */ $channels = apply_filters( 'woocommerce_marketing_channels', $this->registered_channels ); - // Only return allowed channels. - $allowed_channels = array_filter( - $channels, - function ( MarketingChannelInterface $channel ) { - if ( ! $this->is_channel_allowed( $channel ) ) { - // Silently log an error and bail. - wc_get_logger()->error( sprintf( 'Marketing channel %s (%s) cannot be registered!', $channel->get_name(), $channel->get_slug() ) ); - - return false; - } - - return true; - } - ); - - return array_values( $allowed_channels ); - } - - /** - * Returns an array of plugin slugs for the marketing channels that are allowed to be registered. - * - * @return array - */ - protected function get_allowed_channels(): array { - $recommended_channels = $this->marketing_specs->get_recommended_plugins(); - if ( empty( $recommended_channels ) ) { - return []; - } - - return array_column( $recommended_channels, 'product', 'product' ); - } - - /** - * Determines whether the given marketing channel is allowed to be registered. - * - * @param MarketingChannelInterface $channel The marketing channel object. - * - * @return bool - */ - protected function is_channel_allowed( MarketingChannelInterface $channel ): bool { - return isset( $this->allowed_channels[ $channel->get_slug() ] ); + return array_values( $channels ); } } diff --git a/plugins/woocommerce/src/Internal/Admin/Marketing.php b/plugins/woocommerce/src/Internal/Admin/Marketing.php index f11cd2d31d9..47d4ab241bb 100644 --- a/plugins/woocommerce/src/Internal/Admin/Marketing.php +++ b/plugins/woocommerce/src/Internal/Admin/Marketing.php @@ -165,14 +165,7 @@ class Marketing { return $settings; } - /** - * InstalledExtensions helper class. - * - * @var InstalledExtensions $installed_extensions - */ - $installed_extensions = wc_get_container()->get( InstalledExtensions::class ); - - $settings['marketing']['installedExtensions'] = $installed_extensions->get_data(); + $settings['marketing']['installedExtensions'] = InstalledExtensions::get_data(); return $settings; } diff --git a/plugins/woocommerce/src/Internal/DependencyManagement/ServiceProviders/MarketingServiceProvider.php b/plugins/woocommerce/src/Internal/DependencyManagement/ServiceProviders/MarketingServiceProvider.php index 8e27386ae86..c00e484fe89 100644 --- a/plugins/woocommerce/src/Internal/DependencyManagement/ServiceProviders/MarketingServiceProvider.php +++ b/plugins/woocommerce/src/Internal/DependencyManagement/ServiceProviders/MarketingServiceProvider.php @@ -5,7 +5,6 @@ namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders; -use Automattic\WooCommerce\Admin\Marketing\InstalledExtensions; use Automattic\WooCommerce\Admin\Marketing\MarketingChannels; use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs; use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider; @@ -30,7 +29,6 @@ class MarketingServiceProvider extends AbstractServiceProvider { protected $provides = array( MarketingSpecs::class, MarketingChannels::class, - InstalledExtensions::class, ); /** @@ -38,7 +36,6 @@ class MarketingServiceProvider extends AbstractServiceProvider { */ public function register() { $this->share( MarketingSpecs::class ); - $this->share( MarketingChannels::class )->addArgument( MarketingSpecs::class ); - $this->share( InstalledExtensions::class )->addArgument( MarketingChannels::class ); + $this->share( MarketingChannels::class ); } } diff --git a/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingChannelsTest.php b/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingChannelsTest.php index cf5885a0557..fdc112ae370 100644 --- a/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingChannelsTest.php +++ b/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingChannelsTest.php @@ -4,7 +4,6 @@ namespace Automattic\WooCommerce\Tests\Admin\Marketing; use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface; use Automattic\WooCommerce\Admin\Marketing\MarketingChannels; -use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs; use WC_Unit_Test_Case; /** @@ -16,29 +15,17 @@ class MarketingChannelsTest extends WC_Unit_Test_Case { * Runs before each test. */ public function setUp(): void { - delete_transient( MarketingSpecs::RECOMMENDED_PLUGINS_TRANSIENT ); + remove_all_filters( 'woocommerce_marketing_channels' ); } /** - * @testdox A marketing channel can be registered using the `register` method if it is in the allowed list. + * @testdox A marketing channel can be registered using the `register` method if the same channel slug is NOT previously registered. */ - public function test_registers_allowed_channels() { + public function test_registers_channel() { $test_channel = $this->createMock( MarketingChannelInterface::class ); $test_channel->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); - $marketing_specs = $this->createMock( MarketingSpecs::class ); - $marketing_specs->expects( $this->once() ) - ->method( 'get_recommended_plugins' ) - ->willReturn( - [ - [ - 'product' => 'test-channel-1', - ], - ] - ); - $marketing_channels = new MarketingChannels(); - $marketing_channels->init( $marketing_specs ); $marketing_channels->register( $test_channel ); $this->assertNotEmpty( $marketing_channels->get_registered_channels() ); @@ -46,42 +33,33 @@ class MarketingChannelsTest extends WC_Unit_Test_Case { } /** - * @testdox A marketing channel can NOT be registered using the `register` method if it is NOT in the allowed list. + * @testdox A marketing channel can NOT be registered using the `register` method if it is previously registered. */ - public function test_does_not_register_disallowed_channels() { - $test_channel = $this->createMock( MarketingChannelInterface::class ); - $test_channel->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); + public function test_throws_exception_if_registering_existing_channels() { + $test_channel_1 = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); - $marketing_specs = $this->createMock( MarketingSpecs::class ); - $marketing_specs->expects( $this->once() )->method( 'get_recommended_plugins' )->willReturn( [] ); + $test_channel_1_duplicate = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1_duplicate->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); $marketing_channels = new MarketingChannels(); - $marketing_channels->init( $marketing_specs ); - $marketing_channels->register( $test_channel ); + $marketing_channels->register( $test_channel_1 ); - $this->assertEmpty( $marketing_channels->get_registered_channels() ); + $this->expectException( \Exception::class ); + $marketing_channels->register( $test_channel_1_duplicate ); + + $this->assertCount( 1, $marketing_channels->get_registered_channels() ); + $this->assertEquals( $test_channel_1, $marketing_channels->get_registered_channels()[0] ); } /** - * @testdox A marketing channel can be registered using the `woocommerce_marketing_channels` WordPress filter if it is in the allowed list. + * @testdox A marketing channel can be registered using the `woocommerce_marketing_channels` WordPress filter if the same channel slug is NOT previously registered. */ - public function test_registers_allowed_channels_using_wp_filter() { + public function test_registers_channel_using_wp_filter() { $test_channel = $this->createMock( MarketingChannelInterface::class ); $test_channel->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); - $marketing_specs = $this->createMock( MarketingSpecs::class ); - $marketing_specs->expects( $this->once() ) - ->method( 'get_recommended_plugins' ) - ->willReturn( - [ - [ - 'product' => 'test-channel-1', - ], - ] - ); - $marketing_channels = new MarketingChannels(); - $marketing_channels->init( $marketing_specs ); add_filter( 'woocommerce_marketing_channels', @@ -97,24 +75,29 @@ class MarketingChannelsTest extends WC_Unit_Test_Case { } /** - * @testdox A marketing channel can NOT be registered using the `woocommerce_marketing_channels` WordPress filter if it NOT is in the allowed list. + * @testdox A marketing channel can NOT be registered using the `woocommerce_marketing_channels` WordPress filter if it is previously registered. */ - public function test_does_not_register_disallowed_channels_using_wp_filter() { - $test_channel = $this->createMock( MarketingChannelInterface::class ); - $test_channel->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); + public function test_overrides_existing_channel_if_registered_using_wp_filter() { + $marketing_channels = new MarketingChannels(); - set_transient( MarketingSpecs::RECOMMENDED_PLUGINS_TRANSIENT, [] ); + $test_channel_1 = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); + + $marketing_channels->register( $test_channel_1 ); + + $test_channel_1_duplicate = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1_duplicate->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); add_filter( 'woocommerce_marketing_channels', - function ( array $channels ) use ( $test_channel ) { - $channels[ $test_channel->get_slug() ] = $test_channel; + function ( array $channels ) use ( $test_channel_1_duplicate ) { + $channels[ $test_channel_1_duplicate->get_slug() ] = $test_channel_1_duplicate; return $channels; } ); - $marketing_channels = new MarketingChannels(); - $this->assertEmpty( $marketing_channels->get_registered_channels() ); + $this->assertCount( 1, $marketing_channels->get_registered_channels() ); + $this->assertEquals( $test_channel_1_duplicate, $marketing_channels->get_registered_channels()[0] ); } }