HEX
Server:
System: Linux aac286ea486c 5.14.0-687.15.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 11 08:51:45 EDT 2026 x86_64
User: root (0)
PHP: 8.2.30
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,disk_free_space,diskfreespace
Upload Files
File: /dom877180/wp-content/mu-plugins/gd-system-plugin/includes/class-experiment.php
<?php

namespace WPaaS;

if ( ! defined( 'ABSPATH' ) ) {

	exit;

}

final class Experiment {

	/**
	 * Transient key prefix for caching experiment API responses.
	 */
	const TRANSIENT_KEY_PREFIX = 'wpaas_experiment_';

	/**
	 * Default cache duration in seconds (10 days).
	 */
	const DEFAULT_CACHE_TTL = 864000;

	/**
	 * Cache duration for failed API calls in seconds (5 minutes).
	 */
	const FAILURE_CACHE_TTL = 300;

	/**
	 * Instance of the API.
	 *
	 * @var API_Interface
	 */
	private $api;

	/**
	 * In-memory cache of experiment results for the current request.
	 *
	 * @var array
	 */
	private $results = [];

	/**
	 * @param API_Interface $api
	 */
	public function __construct( API_Interface $api ) {

		$this->api = $api;

	}

	/**
	 * Check whether an experiment is enabled for the current account.
	 *
	 * Results are cached in a WordPress transient keyed by experiment name
	 * and account UID so that changing the experiment name automatically
	 * invalidates stale cache entries.
	 *
	 * @param string $exp_name Experiment name passed to the API.
	 * @param int    $cache_ttl Cache lifetime in seconds (default 10 days).
	 *
	 * @return bool
	 */
	public function is_enabled( $exp_name, $cache_ttl = self::DEFAULT_CACHE_TTL ) {

		if ( isset( $this->results[ $exp_name ] ) ) {
			return $this->results[ $exp_name ];
		}

		$account_uid = defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : '';

		if ( empty( $account_uid ) ) {
			return false;
		}

		$transient_key = self::TRANSIENT_KEY_PREFIX . $exp_name . '_' . $account_uid;

		$cached = get_transient( $transient_key );

		if ( false !== $cached ) {
			$this->results[ $exp_name ] = (bool) $cached;

			return $this->results[ $exp_name ];
		}

		$show = $this->api->get_experiment( $exp_name );

		if ( null === $show ) {
			set_transient( $transient_key, 0, self::FAILURE_CACHE_TTL );
			$this->results[ $exp_name ] = false;

			return false;
		}

		set_transient( $transient_key, $show ? 1 : 0, $cache_ttl );
		$this->results[ $exp_name ] = $show;

		return $show;

	}

}