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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 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 / src / Internal / PushNotifications / Entities / PushToken.php
woocommerce / src / Internal / PushNotifications / Entities Last commit date
PushToken.php 3 months ago
PushToken.php
531 lines
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace Automattic\WooCommerce\Internal\PushNotifications\Entities;
6
7 defined( 'ABSPATH' ) || exit;
8
9 use Automattic\WooCommerce\Internal\PushNotifications\Exceptions\PushTokenInvalidDataException;
10 use Automattic\WooCommerce\Internal\PushNotifications\Validators\PushTokenValidator;
11
12 /**
13 * Object representation of a push token.
14 *
15 * @since 10.4.0
16 */
17 class PushToken {
18 /**
19 * WordPress post type for storing push tokens.
20 */
21 const POST_TYPE = 'wc_push_token';
22
23 /**
24 * The locale to use for tokens when the locale is not set, e.g. for tokens
25 * created before `device_locale` was added.
26 */
27 const DEFAULT_DEVICE_LOCALE = 'en_US';
28
29 /**
30 * Platform identifier for Apple devices.
31 */
32 const PLATFORM_APPLE = 'apple';
33
34 /**
35 * Platform identifier for Android devices.
36 */
37 const PLATFORM_ANDROID = 'android';
38
39 /**
40 * Platform identifier for web browsers.
41 */
42 const PLATFORM_BROWSER = 'browser';
43
44 /**
45 * Origin identifier for WooCommerce Android app.
46 */
47 const ORIGIN_WOOCOMMERCE_ANDROID = 'com.woocommerce.android';
48
49 /**
50 * Origin identifier for WooCommerce Android app development builds.
51 */
52 const ORIGIN_WOOCOMMERCE_ANDROID_DEV = 'com.woocommerce.android:dev';
53
54 /**
55 * Origin identifier for WooCommerce iOS app.
56 */
57 const ORIGIN_WOOCOMMERCE_IOS = 'com.automattic.woocommerce';
58
59 /**
60 * Origin identifier for WooCommerce iOS app development builds.
61 */
62 const ORIGIN_WOOCOMMERCE_IOS_DEV = 'com.automattic.woocommerce:dev';
63
64 /**
65 * Origin identifier for browsers.
66 */
67 const ORIGIN_BROWSER = 'browser';
68
69 /**
70 * List of valid platforms.
71 */
72 const PLATFORMS = array(
73 self::PLATFORM_APPLE,
74 self::PLATFORM_ANDROID,
75 self::PLATFORM_BROWSER,
76 );
77
78 /**
79 * List of valid origins.
80 */
81 const ORIGINS = array(
82 self::ORIGIN_BROWSER,
83 self::ORIGIN_WOOCOMMERCE_ANDROID,
84 self::ORIGIN_WOOCOMMERCE_ANDROID_DEV,
85 self::ORIGIN_WOOCOMMERCE_IOS,
86 self::ORIGIN_WOOCOMMERCE_IOS_DEV,
87 );
88
89 /**
90 * The ID of the token post.
91 *
92 * @var int|null
93 */
94 private ?int $id = null;
95
96 /**
97 * The ID of the user who owns the token.
98 *
99 * @var int|null
100 */
101 private ?int $user_id = null;
102
103 /**
104 * The token representing a device we can send a push notification to.
105 *
106 * @var string|null
107 */
108 private ?string $token = null;
109
110 /**
111 * The UUID of the device that generated the token.
112 *
113 * @var string|null
114 */
115 private ?string $device_uuid = null;
116
117 /**
118 * The platform the token was generated by.
119 *
120 * @var string|null
121 */
122 private ?string $platform = null;
123
124 /**
125 * The origin the token belongs to.
126 *
127 * @var string|null
128 */
129 private ?string $origin = null;
130
131 /**
132 * The locale of the device the token belongs to.
133 *
134 * @var string|null
135 */
136 private ?string $device_locale = null;
137
138 /**
139 * An array of metadata for the token.
140 *
141 * @var array|null
142 */
143 private ?array $metadata = null;
144
145 /**
146 * Creates a new PushToken instance with the given data.
147 *
148 * @param array $data Optional array with keys: id, user_id, token, device_uuid, platform, origin.
149 * @throws PushTokenInvalidDataException If any of the provided values fail validation.
150 *
151 * @since 10.6.0
152 */
153 public function __construct( array $data = array() ) {
154 if ( array_key_exists( 'id', $data ) ) {
155 $this->set_id( (int) $data['id'] );
156 }
157
158 if ( array_key_exists( 'user_id', $data ) ) {
159 $this->set_user_id( (int) $data['user_id'] );
160 }
161
162 if ( array_key_exists( 'token', $data ) ) {
163 $this->set_token( (string) $data['token'] );
164 }
165
166 if ( array_key_exists( 'device_uuid', $data ) ) {
167 $this->set_device_uuid( (string) $data['device_uuid'] );
168 }
169
170 if ( array_key_exists( 'platform', $data ) ) {
171 $this->set_platform( (string) $data['platform'] );
172 }
173
174 if ( array_key_exists( 'origin', $data ) ) {
175 $this->set_origin( (string) $data['origin'] );
176 }
177
178 if ( array_key_exists( 'device_locale', $data ) ) {
179 $this->set_device_locale( (string) $data['device_locale'] );
180 }
181
182 if ( array_key_exists( 'metadata', $data ) ) {
183 $this->set_metadata( (array) $data['metadata'] );
184 }
185 }
186
187 /**
188 * Validates and sets the ID.
189 *
190 * @param int $id The ID of the token post.
191 * @throws PushTokenInvalidDataException If ID is not valid.
192 * @return void
193 *
194 * @since 10.4.0
195 */
196 public function set_id( int $id ): void {
197 $result = PushTokenValidator::validate( compact( 'id' ), array( 'id' ) );
198
199 if ( is_wp_error( $result ) ) {
200 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
201 throw new PushTokenInvalidDataException( $result->get_error_message() );
202 }
203
204 $this->id = $id;
205 }
206
207 /**
208 * Validates and sets the user ID.
209 *
210 * @param int $user_id The ID of the user who owns the token.
211 * @throws PushTokenInvalidDataException If user ID is not valid.
212 * @return void
213 *
214 * @since 10.4.0
215 */
216 public function set_user_id( int $user_id ): void {
217 $result = PushTokenValidator::validate( compact( 'user_id' ), array( 'user_id' ) );
218
219 if ( is_wp_error( $result ) ) {
220 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
221 throw new PushTokenInvalidDataException( $result->get_error_message() );
222 }
223
224 $this->user_id = $user_id;
225 }
226
227 /**
228 * Validates and sets the token.
229 *
230 * @param string $token The token representing a device we can send a push notification to.
231 * @throws PushTokenInvalidDataException If token is not valid.
232 * @return void
233 *
234 * @since 10.4.0
235 */
236 public function set_token( string $token ): void {
237 $result = PushTokenValidator::validate( compact( 'token' ), array( 'token' ) );
238
239 if ( is_wp_error( $result ) ) {
240 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
241 throw new PushTokenInvalidDataException( $result->get_error_message() );
242 }
243
244 $this->token = trim( $token );
245 }
246
247 /**
248 * Validates and sets the device UUID, normalize empty (non-null) values to null.
249 *
250 * @param string|null $device_uuid The UUID of the device that generated the token.
251 * @throws PushTokenInvalidDataException If device UUID is not valid.
252 * @return void
253 *
254 * @since 10.4.0
255 */
256 public function set_device_uuid( ?string $device_uuid ): void {
257 $result = PushTokenValidator::validate( compact( 'device_uuid' ), array( 'device_uuid' ) );
258
259 if ( is_wp_error( $result ) ) {
260 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
261 throw new PushTokenInvalidDataException( $result->get_error_message() );
262 }
263
264 if ( null !== $device_uuid ) {
265 $device_uuid = trim( $device_uuid );
266 }
267
268 $this->device_uuid = $device_uuid ? $device_uuid : null;
269 }
270
271 /**
272 * Validates and sets the device locale.
273 *
274 * @param string $device_locale The locale of the device the token belongs to.
275 * @throws PushTokenInvalidDataException If device locale is not valid.
276 * @return void
277 *
278 * @since 10.6.0
279 */
280 public function set_device_locale( string $device_locale ): void {
281 $result = PushTokenValidator::validate( compact( 'device_locale' ), array( 'device_locale' ) );
282
283 if ( is_wp_error( $result ) ) {
284 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
285 throw new PushTokenInvalidDataException( $result->get_error_message() );
286 }
287
288 $this->device_locale = trim( $device_locale );
289 }
290
291 /**
292 * Validates and sets the platform.
293 *
294 * @param string $platform The platform the token was generated by.
295 * @throws PushTokenInvalidDataException If platform is not valid.
296 * @return void
297 *
298 * @since 10.4.0
299 */
300 public function set_platform( string $platform ): void {
301 $result = PushTokenValidator::validate( compact( 'platform' ), array( 'platform' ) );
302
303 if ( is_wp_error( $result ) ) {
304 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
305 throw new PushTokenInvalidDataException( $result->get_error_message() );
306 }
307
308 $this->platform = trim( $platform );
309 }
310
311 /**
312 * Validates and sets the origin.
313 *
314 * @param string $origin The origin of the token, e.g. the app it came from.
315 * @throws PushTokenInvalidDataException If origin is not valid.
316 * @return void
317 *
318 * @since 10.4.0
319 */
320 public function set_origin( string $origin ): void {
321 $result = PushTokenValidator::validate( compact( 'origin' ), array( 'origin' ) );
322
323 if ( is_wp_error( $result ) ) {
324 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
325 throw new PushTokenInvalidDataException( $result->get_error_message() );
326 }
327
328 $this->origin = trim( $origin );
329 }
330
331 /**
332 * Validates and sets the metadata.
333 *
334 * @param array $metadata An array of metadata for the token, e.g. the app version, device OS etc.
335 * @throws PushTokenInvalidDataException If metadata is not valid.
336 * @return void
337 *
338 * @since 10.6.0
339 */
340 public function set_metadata( array $metadata ): void {
341 $result = PushTokenValidator::validate( compact( 'metadata' ), array( 'metadata' ) );
342
343 if ( is_wp_error( $result ) ) {
344 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
345 throw new PushTokenInvalidDataException( $result->get_error_message() );
346 }
347
348 if ( ! empty( $metadata ) ) {
349 $keys = array_map( 'sanitize_key', array_keys( $metadata ) );
350 $values = array_map( 'sanitize_text_field', array_values( $metadata ) );
351
352 /**
353 * Typehint for PHPStan, as it can't infer the $keys and $values are
354 * the same length therefore array_combine won't return false.
355 *
356 * @var array<string, string> $metadata
357 */
358 $metadata = array_combine( $keys, $values );
359 }
360
361 $this->metadata = $metadata;
362 }
363
364 /**
365 * Gets the ID.
366 *
367 * @return int|null
368 *
369 * @since 10.4.0
370 */
371 public function get_id(): ?int {
372 return $this->id;
373 }
374
375 /**
376 * Gets the user ID.
377 *
378 * @return int|null
379 *
380 * @since 10.4.0
381 */
382 public function get_user_id(): ?int {
383 return $this->user_id;
384 }
385
386 /**
387 * Gets the token.
388 *
389 * @return string|null
390 *
391 * @since 10.4.0
392 */
393 public function get_token(): ?string {
394 return $this->token;
395 }
396
397 /**
398 * Gets the device UUID.
399 *
400 * @return string|null
401 *
402 * @since 10.4.0
403 */
404 public function get_device_uuid(): ?string {
405 return $this->device_uuid;
406 }
407
408 /**
409 * Gets the platform.
410 *
411 * @return string|null
412 *
413 * @since 10.4.0
414 */
415 public function get_platform(): ?string {
416 return $this->platform;
417 }
418
419 /**
420 * Gets the origin.
421 *
422 * @return string|null
423 *
424 * @since 10.4.0
425 */
426 public function get_origin(): ?string {
427 return $this->origin;
428 }
429
430 /**
431 * Gets the device locale.
432 *
433 * @return string|null
434 *
435 * @since 10.6.0
436 */
437 public function get_device_locale(): ?string {
438 return $this->device_locale;
439 }
440
441 /**
442 * Gets the metadata.
443 *
444 * @return array|null
445 *
446 * @since 10.6.0
447 */
448 public function get_metadata(): ?array {
449 return $this->metadata;
450 }
451
452 /**
453 * Returns this token formatted for the WPCOM push notifications endpoint.
454 *
455 * @return array{user_id: int|null, token: string|null, origin: string|null, device_locale: string|null}
456 *
457 * @since 10.7.0
458 */
459 public function to_wpcom_format(): array {
460 return array(
461 'user_id' => $this->user_id,
462 'token' => $this->token,
463 'origin' => $this->origin,
464 'device_locale' => $this->device_locale ?? self::DEFAULT_DEVICE_LOCALE,
465 );
466 }
467
468 /**
469 * Determines whether this token can be created.
470 *
471 * @return bool
472 *
473 * @since 10.4.0
474 */
475 public function can_be_created(): bool {
476 return ! $this->get_id() && $this->has_required_parameters();
477 }
478
479 /**
480 * Determines whether this token can be updated.
481 *
482 * @return bool
483 *
484 * @since 10.4.0
485 */
486 public function can_be_updated(): bool {
487 return $this->get_id() && $this->has_required_parameters();
488 }
489
490 /**
491 * Determines whether this token can be read.
492 *
493 * @return bool
494 *
495 * @since 10.4.0
496 */
497 public function can_be_read(): bool {
498 return (bool) $this->get_id();
499 }
500
501 /**
502 * Determines whether this token can be deleted.
503 *
504 * @return bool
505 *
506 * @since 10.4.0
507 */
508 public function can_be_deleted(): bool {
509 return (bool) $this->get_id();
510 }
511
512 /**
513 * Determines whether all the required non-ID parameters are filled.
514 *
515 * @return bool
516 *
517 * @since 10.4.0
518 */
519 private function has_required_parameters(): bool {
520 return $this->get_user_id()
521 && $this->get_token()
522 && $this->get_platform()
523 && $this->get_origin()
524 && $this->get_device_locale()
525 && (
526 $this->get_device_uuid()
527 || $this->get_platform() === self::PLATFORM_BROWSER
528 );
529 }
530 }
531