HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/shoetique/wp-content/plugins/facebook-for-woocommerce/includes/RolloutSwitches.php
<?php
/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

 * This source code is licensed under the license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @package FacebookCommerce
 */

namespace WooCommerce\Facebook;

use WooCommerce\Facebook\Framework\Api\Exception;
use WooCommerce\Facebook\Utilities\Heartbeat;

defined( 'ABSPATH' ) || exit;

/**
 * The rollout switches is used to control available
 * features in the Facebook for WooCommerce plugin.
 */
class RolloutSwitches {
	/** @var \WC_Facebookcommerce commerce handler */
	private \WC_Facebookcommerce $plugin;

	public const SWITCH_ROLLOUT_FEATURES          = 'rollout_enabled';
	public const WHATSAPP_UTILITY_MESSAGING       = 'whatsapp_utility_messages_enabled';
	public const SWITCH_PRODUCT_SETS_SYNC_ENABLED = 'product_sets_sync_enabled';
	private const SETTINGS_KEY                    = 'wc_facebook_for_woocommerce_rollout_switches';

	private const ACTIVE_SWITCHES = array(
		self::SWITCH_ROLLOUT_FEATURES,
		self::WHATSAPP_UTILITY_MESSAGING,
		self::SWITCH_PRODUCT_SETS_SYNC_ENABLED,
	);

	public function __construct( \WC_Facebookcommerce $plugin ) {
		$this->plugin = $plugin;
	}

	public function init() {
		$is_connected = $this->plugin->get_connection_handler()->is_connected();
		if ( ! $is_connected ) {
			return;
		}

		// This is to avoid calling the API multiple times
		$flag_name = '_wc_facebook_for_woocommerce_rollout_switch_flag';
		if ( 'yes' === get_transient( $flag_name ) ) {
			return;
		}
		set_transient( $flag_name, 'yes', 60 * MINUTE_IN_SECONDS );

		try {
			$external_business_id = $this->plugin->get_connection_handler()->get_external_business_id();
			$switches             = $this->plugin->get_api()->get_rollout_switches( $external_business_id );
			$data                 = $switches->get_data();
			if ( empty( $data ) ) {
				throw new Exception( 'Empty data' );
			}
			$fb_options = array();
			foreach ( $data as $switch ) {
				if ( ! isset( $switch['switch'] ) || ! $this->is_switch_active( $switch['switch'] ) ) {
					continue;
				}
				$fb_options[ $switch['switch'] ] = (bool) $switch['enabled'] ? 'yes' : 'no';
			}
			update_option( self::SETTINGS_KEY, $fb_options );
		} catch ( Exception $e ) {
			$fb_options = get_option( self::SETTINGS_KEY );
			if ( empty( $fb_options ) ) {
				$fb_options = array();
			}
			foreach ( $this->get_active_switches() as $switch_name ) {
				// if the switch is not in the response and we have a failure
				// we fallback to the old value first and false otherwise
				if ( ! isset( $fb_options[ $switch_name ] ) ) {
					$fb_options[ $switch_name ] = 'no';
				}
			}
			update_option( self::SETTINGS_KEY, $fb_options );
			\WC_Facebookcommerce_Utils::fblog(
				$e,
				[
					'event'      => 'rollout_switches',
					'event_type' => 'init',
				]
			);
		}
	}

	/**
	 * Get if the switch is enabled or not.
	 * If the switch is not active ->
	 *   FALSE
	 *
	 * If the switch is active but not in the response ->
	 *    TRUE: we assume this is an old version of the plugin
	 *    and the backend since has changed and the switch was released
	 *    in the backend we will otherwise always return false for unreleased
	 *    features
	 *
	 * If the feature is active and in the response ->
	 *   we will return the value of the switch from the response
	 *
	 * @param string $switch_name The name of the switch.
	 */
	public function is_switch_enabled( string $switch_name ) {
		if ( ! $this->is_switch_active( $switch_name ) ) {
			return false;
		}
		$features = get_option( self::SETTINGS_KEY );
		if ( empty( $features ) ) {
			return false;
		}

		if ( ! isset( $features[ $switch_name ] ) ) {
			return true;
		}

		return 'yes' === $features[ $switch_name ] ? true : false;
	}

	public function is_switch_active( string $switch_name ): bool {
		return in_array( $switch_name, self::ACTIVE_SWITCHES, true );
	}

	public function get_active_switches(): array {
		return self::ACTIVE_SWITCHES;
	}
}