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
7 changes: 4 additions & 3 deletions class-wc-facebookcommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,24 @@ 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();

// 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.
*
Expand All @@ -271,7 +273,6 @@ function () {
},
0
);
add_action( 'admin_init', [ $this->rollout_switches, 'init' ] );
}

/**
Expand Down
42 changes: 30 additions & 12 deletions includes/Admin/Enhanced_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' ) );

Expand All @@ -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
Expand All @@ -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.
*
Expand Down
37 changes: 23 additions & 14 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' ) );
Expand All @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Admin/Settings_Screens/Whatsapp_Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Previously initHook was only executed when wordpress init action takes place, which happens before the RolloutSwitch API fetch causing an error. This would allow initHook to only be executed after RolloutSwitch fetch


add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}
Expand Down
9 changes: 6 additions & 3 deletions includes/RolloutSwitches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ) ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need access_token? Where is it being used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used within get_api call for the RolloutSwitch API fetch.

return;
}

Expand Down Expand Up @@ -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 ) {
Expand Down