Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes/react-ad...
File: class-experimental-abtest.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* NOTE: this is a temporary class and can be replaced by jetpack-abtest after
[2] Fix | Delete
* https://github.com/Automattic/jetpack/issues/19596 has been fixed.
[3] Fix | Delete
*
[4] Fix | Delete
* A class that interacts with Explat A/B tests.
[5] Fix | Delete
*
[6] Fix | Delete
* This class is experimental. It is a fork of the jetpack-abtest package and
[7] Fix | Delete
* updated for use with ExPlat. These changes are planned to be contributed
[8] Fix | Delete
* back to the upstream Jetpack package. If accepted, this class should then
[9] Fix | Delete
* be superseded by the Jetpack class using Composer.
[10] Fix | Delete
*
[11] Fix | Delete
* This class should not be used externally.
[12] Fix | Delete
*
[13] Fix | Delete
* @package WooCommerce\Admin
[14] Fix | Delete
* @link https://packagist.org/packages/automattic/jetpack-abtest
[15] Fix | Delete
*/
[16] Fix | Delete
[17] Fix | Delete
namespace WooCommerce\Admin;
[18] Fix | Delete
[19] Fix | Delete
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
[20] Fix | Delete
use Automattic\Jetpack\Connection\Client as Jetpack_Connection_client;
[21] Fix | Delete
use Automattic\WooCommerce\Admin\WCAdminHelper;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* This class provides an interface to the Explat A/B tests.
[25] Fix | Delete
*
[26] Fix | Delete
* Usage:
[27] Fix | Delete
*
[28] Fix | Delete
* $anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
[29] Fix | Delete
* $allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
[30] Fix | Delete
* $abtest = new \WooCommerce\Admin\Experimental_Abtest(
[31] Fix | Delete
* $anon_id,
[32] Fix | Delete
* 'woocommerce',
[33] Fix | Delete
* $allow_tracking
[34] Fix | Delete
* );
[35] Fix | Delete
*
[36] Fix | Delete
* OR use the helper function:
[37] Fix | Delete
*
[38] Fix | Delete
* WooCommerce\Admin\Experimental_Abtest::in_treatment('experiment_name');
[39] Fix | Delete
*
[40] Fix | Delete
*
[41] Fix | Delete
* $isTreatment = $abtest->get_variation('your-experiment-name') === 'treatment';
[42] Fix | Delete
*
[43] Fix | Delete
* @internal This class is experimental and should not be used externally due to planned breaking changes.
[44] Fix | Delete
*/
[45] Fix | Delete
final class Experimental_Abtest {
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* A variable to hold the tests we fetched, and their variations for the current user.
[49] Fix | Delete
*
[50] Fix | Delete
* @var array
[51] Fix | Delete
*/
[52] Fix | Delete
private $tests = array();
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* ExPlat Anonymous ID.
[56] Fix | Delete
*
[57] Fix | Delete
* @var string
[58] Fix | Delete
*/
[59] Fix | Delete
private $anon_id = null;
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* ExPlat Platform name.
[63] Fix | Delete
*
[64] Fix | Delete
* @var string
[65] Fix | Delete
*/
[66] Fix | Delete
private $platform = 'woocommerce';
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Whether trcking consent is given.
[70] Fix | Delete
*
[71] Fix | Delete
* @var bool
[72] Fix | Delete
*/
[73] Fix | Delete
private $consent = false;
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Request variation as a auth wpcom user or not.
[77] Fix | Delete
*
[78] Fix | Delete
* @var boolean
[79] Fix | Delete
*/
[80] Fix | Delete
private $as_auth_wpcom_user = false;
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Constructor.
[84] Fix | Delete
*
[85] Fix | Delete
* @param string $anon_id ExPlat anonymous ID.
[86] Fix | Delete
* @param string $platform ExPlat platform name.
[87] Fix | Delete
* @param bool $consent Whether tracking consent is given.
[88] Fix | Delete
* @param bool $as_auth_wpcom_user Request variation as a auth wp user or not.
[89] Fix | Delete
*/
[90] Fix | Delete
public function __construct( string $anon_id, string $platform, bool $consent, bool $as_auth_wpcom_user = false ) {
[91] Fix | Delete
$this->anon_id = $anon_id;
[92] Fix | Delete
$this->platform = $platform;
[93] Fix | Delete
$this->consent = $consent;
[94] Fix | Delete
$this->as_auth_wpcom_user = $as_auth_wpcom_user;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Returns true if the current user is in the treatment group of the given experiment.
[99] Fix | Delete
*
[100] Fix | Delete
* @param string $experiment_name Name of the experiment.
[101] Fix | Delete
* @param bool $as_auth_wpcom_user Request variation as a auth wp user or not.
[102] Fix | Delete
*
[103] Fix | Delete
* @return bool True if the user is in the treatment group, false otherwise.
[104] Fix | Delete
* @throws \Exception If there is an error retrieving the variation.
[105] Fix | Delete
*/
[106] Fix | Delete
public static function in_treatment( string $experiment_name, bool $as_auth_wpcom_user = false ) {
[107] Fix | Delete
$anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
[108] Fix | Delete
$allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
[109] Fix | Delete
$abtest = new self(
[110] Fix | Delete
$anon_id,
[111] Fix | Delete
'woocommerce',
[112] Fix | Delete
$allow_tracking,
[113] Fix | Delete
$as_auth_wpcom_user
[114] Fix | Delete
);
[115] Fix | Delete
[116] Fix | Delete
return $abtest->get_variation( $experiment_name ) === 'treatment';
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Retrieve the test variation for a provided A/B test.
[121] Fix | Delete
*
[122] Fix | Delete
* @param string $test_name Name of the A/B test.
[123] Fix | Delete
* @return mixed|null A/B test variation, or null on failure.
[124] Fix | Delete
* @throws \Exception If there is an error retrieving the variation and the environment is not production.
[125] Fix | Delete
*/
[126] Fix | Delete
public function get_variation( $test_name ) {
[127] Fix | Delete
// Default to the control variation when users haven't consented to tracking.
[128] Fix | Delete
if ( ! $this->consent ) {
[129] Fix | Delete
return 'control';
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$variation = $this->fetch_variation( $test_name );
[133] Fix | Delete
[134] Fix | Delete
// If there was an error retrieving a variation, conceal the error for the consumer.
[135] Fix | Delete
// If there was an error retrieving a variation, throw an exception in non-production environments.
[136] Fix | Delete
if ( is_wp_error( $variation ) ) {
[137] Fix | Delete
if ( 'production' !== wp_get_environment_type() ) {
[138] Fix | Delete
throw new \Exception( $variation->get_error_message() );
[139] Fix | Delete
}
[140] Fix | Delete
return 'control';
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
return $variation;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Perform the request for a experiment assignment of a provided A/B test from WP.com.
[149] Fix | Delete
*
[150] Fix | Delete
* @param array $args Arguments to pass to the request for A/B test.
[151] Fix | Delete
* @return array|\WP_Error A/B test variation error on failure.
[152] Fix | Delete
*/
[153] Fix | Delete
public function request_assignment( $args ) {
[154] Fix | Delete
// Request as authenticated wp user.
[155] Fix | Delete
if ( $this->as_auth_wpcom_user && class_exists( Jetpack_Connection_Manager::class ) ) {
[156] Fix | Delete
$jetpack_connection_manager = new Jetpack_Connection_Manager();
[157] Fix | Delete
if ( $jetpack_connection_manager->is_user_connected() ) {
[158] Fix | Delete
$response = Jetpack_Connection_client::wpcom_json_api_request_as_user(
[159] Fix | Delete
'/experiments/0.1.0/assignments/' . $this->platform,
[160] Fix | Delete
'2',
[161] Fix | Delete
$args
[162] Fix | Delete
);
[163] Fix | Delete
}
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
// Request as anonymous user.
[167] Fix | Delete
if ( ! isset( $response ) ) {
[168] Fix | Delete
if ( ! isset( $args['anon_id'] ) || empty( $args['anon_id'] ) ) {
[169] Fix | Delete
return new \WP_Error( 'invalid_anon_id', 'anon_id must be an none empty string.' );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
$url = add_query_arg(
[173] Fix | Delete
$args,
[174] Fix | Delete
sprintf(
[175] Fix | Delete
'https://public-api.wordpress.com/wpcom/v2/experiments/0.1.0/assignments/%s',
[176] Fix | Delete
$this->platform
[177] Fix | Delete
)
[178] Fix | Delete
);
[179] Fix | Delete
$response = wp_remote_get( $url );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
return $response;
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Fetch and cache the test variation for a provided A/B test from WP.com.
[187] Fix | Delete
*
[188] Fix | Delete
* ExPlat returns a null value when the assigned variation is control or
[189] Fix | Delete
* an assignment has not been set. In these instances, this method returns
[190] Fix | Delete
* a value of "control".
[191] Fix | Delete
*
[192] Fix | Delete
* @param string $test_name Name of the A/B test.
[193] Fix | Delete
* @return array|\WP_Error A/B test variation, or error on failure.
[194] Fix | Delete
*/
[195] Fix | Delete
protected function fetch_variation( $test_name ) {
[196] Fix | Delete
// Make sure test name exists.
[197] Fix | Delete
if ( ! $test_name ) {
[198] Fix | Delete
return new \WP_Error( 'test_name_not_provided', 'A/B test name has not been provided.' );
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
// Make sure test name is a valid one.
[202] Fix | Delete
if ( ! preg_match( '/^[a-z0-9_]+$/', $test_name ) ) {
[203] Fix | Delete
return new \WP_Error( 'invalid_test_name', 'Invalid A/B test name.' );
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
// Return internal-cached test variations.
[207] Fix | Delete
if ( isset( $this->tests[ $test_name ] ) ) {
[208] Fix | Delete
return $this->tests[ $test_name ];
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
// Return external-cached test variations.
[212] Fix | Delete
if ( ! empty( get_transient( 'abtest_variation_' . $test_name ) ) ) {
[213] Fix | Delete
return get_transient( 'abtest_variation_' . $test_name );
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
// Make the request to the WP.com API.
[217] Fix | Delete
$args = array(
[218] Fix | Delete
'experiment_name' => $test_name,
[219] Fix | Delete
'anon_id' => rawurlencode( $this->anon_id ),
[220] Fix | Delete
'woo_country_code' => rawurlencode( get_option( 'woocommerce_default_country', 'US:CA' ) ),
[221] Fix | Delete
'woo_wcadmin_install_timestamp' => rawurlencode( get_option( WCAdminHelper::WC_ADMIN_TIMESTAMP_OPTION ) ),
[222] Fix | Delete
);
[223] Fix | Delete
[224] Fix | Delete
/**
[225] Fix | Delete
* Get additional request args.
[226] Fix | Delete
*
[227] Fix | Delete
* @since 6.5.0
[228] Fix | Delete
*/
[229] Fix | Delete
$args = apply_filters( 'woocommerce_explat_request_args', $args );
[230] Fix | Delete
$response = $this->request_assignment( $args );
[231] Fix | Delete
[232] Fix | Delete
// Bail if there was an error or malformed response.
[233] Fix | Delete
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
[234] Fix | Delete
return new \WP_Error( 'failed_to_fetch_data', 'Unable to fetch the requested data.' );
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
// Decode the results.
[238] Fix | Delete
$results = json_decode( $response['body'], true );
[239] Fix | Delete
[240] Fix | Delete
// Bail if there were no resultsreturned.
[241] Fix | Delete
if ( ! is_array( $results ) ) {
[242] Fix | Delete
return new \WP_Error( 'unexpected_data_format', 'Data was not returned in the expected format.' );
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
// Store the variation in our internal cache.
[246] Fix | Delete
$this->tests[ $test_name ] = $results['variations'][ $test_name ] ?? null;
[247] Fix | Delete
[248] Fix | Delete
$variation = $results['variations'][ $test_name ] ?? 'control';
[249] Fix | Delete
// Store the variation in our external cache.
[250] Fix | Delete
if ( ! empty( $results['ttl'] ) ) {
[251] Fix | Delete
set_transient( 'abtest_variation_' . $test_name, $variation, $results['ttl'] );
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
return $variation;
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
[259] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function