1 | <?php |
---|
2 | /** |
---|
3 | * A class that interacts with WP.com A/B tests. |
---|
4 | * |
---|
5 | * @package automattic/jetpack-abtest |
---|
6 | */ |
---|
7 | |
---|
8 | namespace Automattic\Jetpack; |
---|
9 | |
---|
10 | use Automattic\Jetpack\Connection\Client; |
---|
11 | |
---|
12 | /** |
---|
13 | * This class provides an interface to the WP.com A/B tests. |
---|
14 | */ |
---|
15 | class Abtest { |
---|
16 | /** |
---|
17 | * A variable to hold the tests we fetched, and their variations for the current user. |
---|
18 | * |
---|
19 | * @access private |
---|
20 | * |
---|
21 | * @var array |
---|
22 | */ |
---|
23 | private $tests = array(); |
---|
24 | |
---|
25 | /** |
---|
26 | * Retrieve the test variation for a provided A/B test. |
---|
27 | * |
---|
28 | * @access public |
---|
29 | * |
---|
30 | * @param string $test_name Name of the A/B test. |
---|
31 | * @return mixed|null A/B test variation, or null on failure. |
---|
32 | */ |
---|
33 | public function get_variation( $test_name ) { |
---|
34 | $variation = $this->fetch_variation( $test_name ); |
---|
35 | |
---|
36 | // If there was an error retrieving a variation, conceal the error for the consumer. |
---|
37 | if ( is_wp_error( $variation ) ) { |
---|
38 | return null; |
---|
39 | } |
---|
40 | |
---|
41 | return $variation; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Fetch and cache the test variation for a provided A/B test from WP.com. |
---|
46 | * |
---|
47 | * @access protected |
---|
48 | * |
---|
49 | * @param string $test_name Name of the A/B test. |
---|
50 | * @return mixed|Automattic\Jetpack\Error A/B test variation, or Automattic\Jetpack\Error on failure. |
---|
51 | */ |
---|
52 | protected function fetch_variation( $test_name ) { |
---|
53 | // Make sure test name exists. |
---|
54 | if ( ! $test_name ) { |
---|
55 | return new Error( 'test_name_not_provided', 'A/B test name has not been provided.' ); |
---|
56 | } |
---|
57 | |
---|
58 | // Make sure test name is a valid one. |
---|
59 | if ( ! preg_match( '/^[A-Za-z0-9_]+$/', $test_name ) ) { |
---|
60 | return new Error( 'invalid_test_name', 'Invalid A/B test name.' ); |
---|
61 | } |
---|
62 | |
---|
63 | // Return cached test variations. |
---|
64 | if ( isset( $this->tests[ $test_name ] ) ) { |
---|
65 | return $this->tests[ $test_name ]; |
---|
66 | } |
---|
67 | |
---|
68 | // Make the request to the WP.com API. |
---|
69 | $response = $this->request_variation( $test_name ); |
---|
70 | |
---|
71 | // Bail if there was an error or malformed response. |
---|
72 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
---|
73 | return new Error( 'failed_to_fetch_data', 'Unable to fetch the requested data.' ); |
---|
74 | } |
---|
75 | |
---|
76 | // Decode the results. |
---|
77 | $results = json_decode( $response['body'], true ); |
---|
78 | |
---|
79 | // Bail if there were no results or there is no test variation returned. |
---|
80 | if ( ! is_array( $results ) || empty( $results['variation'] ) ) { |
---|
81 | return new Error( 'unexpected_data_format', 'Data was not returned in the expected format.' ); |
---|
82 | } |
---|
83 | |
---|
84 | // Store the variation in our internal cache. |
---|
85 | $this->tests[ $test_name ] = $results['variation']; |
---|
86 | |
---|
87 | return $results['variation']; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Perform the request for a variation of a provided A/B test from WP.com. |
---|
92 | * |
---|
93 | * @access protected |
---|
94 | * |
---|
95 | * @param string $test_name Name of the A/B test. |
---|
96 | * @return mixed|Automattic\Jetpack\Error A/B test variation, or Automattic\Jetpack\Error on failure. |
---|
97 | */ |
---|
98 | protected function request_variation( $test_name ) { |
---|
99 | return Client::wpcom_json_api_request_as_blog( sprintf( '/abtest/%s', $test_name ), '2', array(), null, 'wpcom' ); |
---|
100 | } |
---|
101 | } |
---|