diff --git a/class-wc-facebookcommerce.php b/class-wc-facebookcommerce.php index ff5f74eca..c327accb6 100644 --- a/class-wc-facebookcommerce.php +++ b/class-wc-facebookcommerce.php @@ -240,6 +240,7 @@ public function init() { // Init jobs $this->job_manager = new WooCommerce\Facebook\Jobs\JobManager(); add_action( 'init', [ $this->job_manager, 'init' ] ); + add_action( 'admin_init', [ $this->rollout_switches, 'init' ] ); // Instantiate the debug tools. $this->debug_tools = new DebugTools(); @@ -247,15 +248,16 @@ public function init() { // load admin handlers, before admin_init if ( is_admin() ) { if ($this->use_enhanced_onboarding()) { - $this->admin_enhanced_settings = new WooCommerce\Facebook\Admin\Enhanced_Settings( $this->connection_handler->is_connected() ); + $this->admin_enhanced_settings = new WooCommerce\Facebook\Admin\Enhanced_Settings( $this ); } else { - $this->admin_settings = new WooCommerce\Facebook\Admin\Settings( $this->connection_handler->is_connected() ); + $this->admin_settings = new WooCommerce\Facebook\Admin\Settings( $this ); } } } } + /** * Initializes the admin handling. * @@ -271,7 +273,6 @@ function () { }, 0 ); - add_action( 'admin_init', [ $this->rollout_switches, 'init' ] ); } /** diff --git a/includes/Admin/Enhanced_Settings.php b/includes/Admin/Enhanced_Settings.php index 2bb030693..d1f0297f4 100644 --- a/includes/Admin/Enhanced_Settings.php +++ b/includes/Admin/Enhanced_Settings.php @@ -16,6 +16,7 @@ use WooCommerce\Facebook\Framework\Helper; use WooCommerce\Facebook\Framework\Plugin\Exception as PluginException; use WooCommerce\Facebook\Admin\Settings_Screens\Whatsapp_Utility; +use WooCommerce\Facebook\RolloutSwitches; defined( 'ABSPATH' ) || exit; @@ -39,19 +40,24 @@ class Enhanced_Settings { */ const SUBMENU_PAGE_ID = 'edit-tags.php?taxonomy=fb_product_set&post_type=product'; - /** @var bool flag to check if whatsapp utility is enabled, this is just a boolean for now, will implement a flagging mechanism */ - const WHATSAPP_UTILITY_FEATURE_FLAG = true; + /** @var \WC_Facebookcommerce */ + private $plugin; + /** * Enhanced settings constructor. * * @since 3.5.0 * - * @param bool $is_connected + * @param \WC_Facebookcommerce $plugin is the plugin instance of WC_Facebookcommerce */ - public function __construct( bool $is_connected ) { - $this->screens = $this->build_menu_item_array( $is_connected ); + public function __construct( \WC_Facebookcommerce $plugin ) { + $this->plugin = $plugin; + + $this->screens = $this->build_menu_item_array(); + add_action( 'admin_menu', array( $this, 'build_menu_item_array' ) ); + add_action( 'admin_init', array( $this, 'add_extra_screens' ) ); add_action( 'admin_menu', array( $this, 'add_menu_item' ) ); add_action( 'wp_loaded', array( $this, 'save' ) ); @@ -65,10 +71,10 @@ public function __construct( bool $is_connected ) { * * @since 3.5.0 * - * @param bool $is_connected is Facebook connected * @return array */ - private function build_menu_item_array( bool $is_connected ): array { + public function build_menu_item_array(): array { + $is_connected = $this->plugin->get_connection_handler()->is_connected(); if ( $is_connected ) { // TODO: Remove Product sync and Product sets tab once catalog changes are complete @@ -81,14 +87,26 @@ private function build_menu_item_array( bool $is_connected ): array { $screens = [ Settings_Screens\Shops::ID => new Settings_Screens\Shops() ]; } - if ( self::WHATSAPP_UTILITY_FEATURE_FLAG ) { - $whatsapp_utility_screens = [ Settings_Screens\Whatsapp_Utility::ID => new Settings_Screens\Whatsapp_Utility() ]; - $screens = array_merge( $screens, $whatsapp_utility_screens ); - } - return $screens; } + /** + * Add extra screens to $this->screens - basic settings_screens + * + * @since 3.5.0 + * + * @return void + */ + public function add_extra_screens(): void { + $rollout_switches = $this->plugin->get_rollout_switches(); + $is_connected = $this->plugin->get_connection_handler()->is_connected(); + $is_whatsapp_utility_messaging_enabled = $rollout_switches->is_switch_enabled( RolloutSwitches::WHATSAPP_UTILITY_MESSAGING ); + + if ( true === $is_connected && true === $is_whatsapp_utility_messaging_enabled ) { + $this->screens[ Settings_Screens\Whatsapp_Utility::ID ] = new Settings_Screens\Whatsapp_Utility(); + } + } + /** * Adds the Facebook menu item. * diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 1b0d95c72..8bdf66780 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -16,6 +16,7 @@ use WooCommerce\Facebook\Admin\Settings_Screens\Whatsapp_Utility; use WooCommerce\Facebook\Framework\Helper; use WooCommerce\Facebook\Framework\Plugin\Exception as PluginException; +use WooCommerce\Facebook\RolloutSwitches; defined( 'ABSPATH' ) || exit; @@ -39,19 +40,23 @@ class Settings { /** @var Abstract_Settings_Screen[] */ private $screens; - /** @var flag to check if whatsapp utility is enabled, this is just a boolean for now, will implement a flagging mechanism */ - const WHATSAPP_UTILITY_FEATURE_FLAG = true; + /** @var \WC_Facebookcommerce */ + private $plugin; /** * Settings constructor. * - * @param bool $is_connected is the state of the plugin connection to the Facebook Marketing API + * @param \WC_Facebookcommerce $plugin is the plugin instance of WC_Facebookcommerce * @since 2.0.0 */ - public function __construct( bool $is_connected ) { + public function __construct( \WC_Facebookcommerce $plugin ) { - $this->screens = $this->build_menu_item_array( $is_connected ); + $this->plugin = $plugin; + $this->screens = $this->build_menu_item_array(); + + add_action( 'admin_menu', array( $this, 'build_menu_item_array' ) ); + add_action( 'admin_init', array( $this, 'add_extra_screens' ) ); add_action( 'admin_menu', array( $this, 'add_menu_item' ) ); add_action( 'wp_loaded', array( $this, 'save' ) ); add_filter( 'parent_file', array( $this, 'set_parent_and_submenu_file' ) ); @@ -62,29 +67,33 @@ public function __construct( bool $is_connected ) { /** * Arranges the tabs. If the plugin is connected to FB, Advertise tab will be first, otherwise the Connection tab will be the first tab. * - * @param bool $is_connected is Facebook connected * @since 3.0.7 */ - private function build_menu_item_array( bool $is_connected ): array { + public function build_menu_item_array(): array { $advertise = [ Settings_Screens\Advertise::ID => new Settings_Screens\Advertise() ]; $connection = [ Settings_Screens\Connection::ID => new Settings_Screens\Connection() ]; - $first = ( $is_connected ) ? $advertise : $connection; - $last = ( $is_connected ) ? $connection : $advertise; + $is_connected = $this->plugin->get_connection_handler()->is_connected(); + $first = ( $is_connected ) ? $advertise : $connection; + $last = ( $is_connected ) ? $connection : $advertise; $screens = array( Settings_Screens\Product_Sync::ID => new Settings_Screens\Product_Sync(), Settings_Screens\Product_Sets::ID => new Settings_Screens\Product_Sets(), ); - if ( self::WHATSAPP_UTILITY_FEATURE_FLAG ) { - $whatsapp_utility_screens = [ Settings_Screens\Whatsapp_Utility::ID => new Settings_Screens\Whatsapp_Utility() ]; - $last = array_merge( $last, $whatsapp_utility_screens ); - } - return array_merge( array_merge( $first, $screens ), $last ); } + public function add_extra_screens(): void { + $rollout_switches = $this->plugin->get_rollout_switches(); + $is_connected = $this->plugin->get_connection_handler()->is_connected(); + $is_whatsapp_utility_messaging_enabled = $rollout_switches->is_switch_enabled( RolloutSwitches::WHATSAPP_UTILITY_MESSAGING ); + if ( true === $is_connected && true === $is_whatsapp_utility_messaging_enabled ) { + $this->screens[ Settings_Screens\Whatsapp_Utility::ID ] = new Settings_Screens\Whatsapp_Utility(); + } + } + /** * Adds the Facebook menu item. * diff --git a/includes/Admin/Settings_Screens/Whatsapp_Utility.php b/includes/Admin/Settings_Screens/Whatsapp_Utility.php index cb4a0f1a4..6e0ba6900 100644 --- a/includes/Admin/Settings_Screens/Whatsapp_Utility.php +++ b/includes/Admin/Settings_Screens/Whatsapp_Utility.php @@ -39,7 +39,7 @@ class Whatsapp_Utility extends Abstract_Settings_Screen { * Whatsapp Utility constructor. */ public function __construct() { - add_action( 'init', array( $this, 'initHook' ) ); + $this->initHook(); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } diff --git a/includes/RolloutSwitches.php b/includes/RolloutSwitches.php index b43eed577..6fc4c385a 100644 --- a/includes/RolloutSwitches.php +++ b/includes/RolloutSwitches.php @@ -22,10 +22,12 @@ class RolloutSwitches { /** @var WooCommerce\Facebook\Commerce commerce handler */ private \WC_Facebookcommerce $plugin; - public const SWITCH_ROLLOUT_FEATURES = 'rollout_enabled'; + public const SWITCH_ROLLOUT_FEATURES = 'rollout_enabled'; + public const WHATSAPP_UTILITY_MESSAGING = 'whatsapp_utility_messages_enabled'; private const ACTIVE_SWITCHES = array( self::SWITCH_ROLLOUT_FEATURES, + self::WHATSAPP_UTILITY_MESSAGING, ); /** * Stores the rollout switches and their enabled/disabled states. @@ -41,7 +43,8 @@ public function __construct( \WC_Facebookcommerce $plugin ) { public function init() { $external_business_id = $this->plugin->get_connection_handler()->get_external_business_id(); - if ( empty( $external_business_id ) ) { + $access_token = $this->plugin->get_connection_handler()->get_access_token(); + if ( empty( $access_token ) || empty( $external_business_id ) ) { return; } @@ -76,7 +79,7 @@ public function is_switch_enabled( string $switch_name ) { return false; } - return $this->rollout_switches[ $switch_name ] ?? true; + return isset( $this->rollout_switches[ $switch_name ] ) ? $this->rollout_switches[ $switch_name ] : true; } public function is_switch_active( string $switch_name ) {