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 |