PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / rest-api / Controllers / Telemetry / class-wc-rest-telemetry-controller.php
class-wc-rest-telemetry-controller.php
174 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API WC Telemetry controller
4 *
5 * Handles requests to the /wc-telemetry endpoint.
6 *
7 * @package WooCommerce\RestApi
8 * @since 3.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Telemetry controller class.
15 *
16 * @package WooCommerce\RestApi
17 * @extends WC_REST_Controller
18 */
19 class WC_REST_Telemetry_Controller extends WC_REST_Controller {
20
21 /**
22 * Endpoint namespace.
23 *
24 * @var string
25 */
26 protected $namespace = 'wc-telemetry';
27
28 /**
29 * Route base.
30 *
31 * @var string
32 */
33 protected $rest_base = 'tracker';
34
35 /**
36 * Register the route for /tracker
37 */
38 public function register_routes() {
39 register_rest_route(
40 $this->namespace,
41 '/' . $this->rest_base,
42 array(
43 array(
44 'methods' => WP_REST_Server::CREATABLE,
45 'callback' => array( $this, 'record_usage_data' ),
46 'permission_callback' => array( $this, 'telemetry_permissions_check' ),
47 'args' => $this->get_collection_params(),
48 ),
49 'schema' => array( $this, 'get_public_item_schema' ),
50 )
51 );
52 }
53
54 /**
55 * Check whether a given request has permission to post telemetry data
56 *
57 * @param WP_REST_Request $request Full details about the request.
58 * @return WP_Error|boolean
59 */
60 public function telemetry_permissions_check( $request ) {
61 if ( ! is_user_logged_in() ) {
62 return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you post telemetry data.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
63 }
64 return true;
65 }
66
67 /**
68 * Record WCTracker Data
69 *
70 * @param WP_REST_Request $request Full details about the request.
71 */
72 public function record_usage_data( $request ) {
73 $new = $this->get_usage_data( $request );
74 if ( ! $new || ! $new['platform'] ) {
75 return;
76 }
77
78 $data = get_option( 'woocommerce_mobile_app_usage' );
79 if ( ! $data ) {
80 $data = array();
81 }
82
83 $platform = $new['platform'];
84
85 if ( isset( $data[ $platform ] ) ) {
86 $existing_usage = $data[ $platform ];
87
88 // Sets the installation date only if it has not been set before.
89 if ( isset( $new['installation_date'] ) && ! isset( $existing_usage['installation_date'] ) ) {
90 $data[ $platform ]['installation_date'] = $new['installation_date'];
91 }
92
93 if ( version_compare( $new['version'], $existing_usage['version'], '>=' ) ) {
94 $data[ $platform ]['version'] = $new['version'];
95 $data[ $platform ]['last_used'] = $new['last_used'];
96 }
97 } else {
98 // Only sets `first_used` when the platform usage data hasn't been set before.
99 $new['first_used'] = $new['last_used'];
100 $data[ $platform ] = $new;
101 }
102
103 update_option( 'woocommerce_mobile_app_usage', $data );
104 }
105
106 /**
107 * Get usage data from current request
108 *
109 * @param WP_REST_Request $request Full details about the request.
110 * @return Array
111 */
112 public function get_usage_data( $request ) {
113 $platform = strtolower( $request->get_param( 'platform' ) );
114 switch ( $platform ) {
115 case 'ios':
116 case 'android':
117 break;
118 default:
119 return;
120 }
121
122 $version = $request->get_param( 'version' );
123 if ( ! $version ) {
124 return;
125 }
126
127 // The installation date could be null from earlier mobile client versions.
128 $installation_date = $request->get_param( 'installation_date' );
129
130 return array_filter(
131 array(
132 'platform' => sanitize_text_field( $platform ),
133 'version' => sanitize_text_field( $version ),
134 'last_used' => gmdate( 'c' ),
135 'installation_date' => isset( $installation_date ) ? get_gmt_from_date( $installation_date, 'c' ) : null,
136 ),
137 function( $value ) {
138 return null !== $value;
139 }
140 );
141 }
142
143 /**
144 * Get any query params needed.
145 *
146 * @return array
147 */
148 public function get_collection_params() {
149 return array(
150 'platform' => array(
151 'description' => __( 'Platform to track.', 'woocommerce' ),
152 'required' => true,
153 'type' => 'string',
154 'sanitize_callback' => 'sanitize_text_field',
155 'validate_callback' => 'rest_validate_request_arg',
156 ),
157 'version' => array(
158 'description' => __( 'Platform version to track.', 'woocommerce' ),
159 'required' => true,
160 'type' => 'string',
161 'sanitize_callback' => 'sanitize_text_field',
162 'validate_callback' => 'rest_validate_request_arg',
163 ),
164 'installation_date' => array(
165 'description' => __( 'Installation date of the WooCommerce mobile app.', 'woocommerce' ),
166 'required' => false, // For backward compatibility.
167 'type' => 'string',
168 'format' => 'date-time',
169 'validate_callback' => 'rest_validate_request_arg',
170 ),
171 );
172 }
173 }
174