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