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 |