first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
{
"name": "wpdesk\/wp-abtesting",
"authors": [
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"require": {
"php": ">=5.6",
"wpdesk\/wp-persistence": "^2.0"
},
"require-dev": {
"phpunit\/phpunit": "^5",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"wimg\/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"FSVendor\\WPDesk\\ABTesting\\": "src\/"
}
},
"autoload-dev": {
"psr-4": {
"FSVendor\\WPDesk\\ABTesting\\Tests\\Unit\\": "tests\/unit\/"
}
},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace FSVendor\WPDesk\ABTesting;
/**
* Test should know what variant should be used and that is all.
*
* @package WPDesk\ABTesting
*/
interface ABTest
{
/**
* Return info about what variant of AB test should be used.
*
* @return ABVariant
*/
public function get_variant();
}

View File

@@ -0,0 +1,103 @@
<?php
namespace FSVendor\WPDesk\ABTesting\ABTest;
use FSVendor\WPDesk\ABTesting\ABTest;
use FSVendor\WPDesk\Persistence\ElementNotExistsException;
use FSVendor\WPDesk\Persistence\PersistentContainer;
/**
* Base class for AB tests with equal groups generated by random number generator
*
* @package WPDesk\ABTesting\ABTest
*/
abstract class EqualGroupsRandomABTest implements \FSVendor\WPDesk\ABTesting\ABTest
{
const CONTAINER_VALUE_PREFIX = 'ab_test';
/**
* Used to save info about generated variant
*
* @var PersistentContainer
*/
private $container;
/**
* How many groups/variants are in the whole test
*
* @var int
*/
private $variant_count;
/**
* Some unique test name for persitence pusposes and for higher readability
*
* @var string
*/
private $test_name;
/**
* Derived classes can use this value to know what variant id is generated
*
* @var int
*/
protected $current_variant_id;
/**
* @param int $variant_count Number of equal size groups to generate.
* @param string $test_name Specific test name. Should be unique. Used to persist.
* @param PersistentContainer $container Container to persist data.
*/
public function __construct($variant_count, $test_name, \FSVendor\WPDesk\Persistence\PersistentContainer $container)
{
\assert(\is_int($variant_count) && $variant_count > 0, '$variant_count makes no sense');
$this->variant_count = $variant_count;
$this->test_name = $test_name;
$this->container = $container;
$this->initialize_variant_id();
}
/**
* Clears info about variant and draws again
*/
public function reset()
{
$this->container->set($this->get_container_key(), null);
$this->initialize_variant_id();
}
/**
* @return void
*/
private function initialize_variant_id()
{
$variant_id = $this->get_variant_id_from_container();
if ($variant_id === null) {
$variant_id = $this->generate_variant_id();
$this->container->set($this->get_container_key(), $variant_id);
}
$this->current_variant_id = $variant_id;
}
/**
* Returns variant id if exists.
*
* @return int|null Returns null if not exists.
*/
private function get_variant_id_from_container()
{
try {
$variant_id = $this->container->get($this->get_container_key());
return (int) $variant_id;
} catch (\FSVendor\WPDesk\Persistence\ElementNotExistsException $e) {
return null;
}
}
/**
* Key for where variant id should be saved in the persistence container
*
* @return string
*/
protected function get_container_key()
{
return \implode('_', [self::CONTAINER_VALUE_PREFIX, $this->test_name, 'variant_id']);
}
/**
* @return int
*/
private function generate_variant_id()
{
return \mt_rand(1, $this->variant_count);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace FSVendor\WPDesk\ABTesting;
/**
* Specific variant should know what functionalities should be on/off.
*
* @package WPDesk\ABTesting
*/
interface ABVariant
{
/**
* Checks if a variant does have a given functionality working.
*
* @param string $functionality
*
* @return bool
*/
public function is_on($functionality);
/**
* Returns the variant id (can be numeric). For example for standard AB testing it would be A or B.
*
* @return string
*/
public function get_variant_id();
}

View File

@@ -0,0 +1,27 @@
<?php
namespace FSVendor\WPDesk\ABTesting\ABVariant;
use FSVendor\WPDesk\ABTesting\ABVariant;
/**
* Base class for variants. Nothing special.
*
* @package WPDesk\ABTesting\ABVariant
*/
abstract class BasicABVariant implements \FSVendor\WPDesk\ABTesting\ABVariant
{
/** @var string */
private $variant_id;
/**
* @param string $variant_id
*/
public function __construct($variant_id)
{
$this->variant_id = (string) $variant_id;
}
public abstract function is_on($functionality);
public function get_variant_id()
{
return $this->variant_id;
}
}