Automattic\WooCommerce\Internal\PushNotifications\Notifications

Notification{}abstractWC 10.7.0

Base class for push notifications.

Each notification type (e.g. new order, new review) extends this class and implements to_payload() with its own title, message, icon, and meta.

No Hooks.

Usage

$Notification = new Notification();
// use class methods

Methods

  1. public __construct( int $resource_id )
  2. public delete_meta( string $key )
  3. public static from_array( array $data )
  4. public get_identifier()
  5. public has_meta( string $key )
  6. public get_resource_id()
  7. public array(
  8. public get_type()
  9. public write_meta( string $key )
  10. public to_array()
  11. public to_payload()
  12. ERROR: no method name found on line `* WordPress.com.`
  13. ERROR: no method name found on line ``
  14. ERROR: no method name found on line ``
  15. ERROR: no method name found on line ``
  16. ERROR: no method name found on line ``
  17. ERROR: no method name found on line `/**`
  18. ERROR: no method name found on line `* @return self`
  19. ERROR: no method name found on line `}`
  20. ERROR: no method name found on line `$class = self::NOTIFICATION_CLASSES[ $type ] ?? null;`
  21. ERROR: no method name found on line `return $this->resource_id;`
  22. ERROR: no method name found on line ``
  23. ERROR: no method name found on line ``
  24. ERROR: no method name found on line ``
  25. ERROR: no method name found on line ``
  26. ERROR: no method name found on line ``
  27. ERROR: no method name found on line ``
  28. ERROR: no method name found on line ``
  29. ERROR: no method name found on line ``
  30. ERROR: no method name found on line ``
  31. ERROR: no method name found on line ``
  32. ERROR: no method name found on line ``
  33. ERROR: no method name found on line ``
  34. ERROR: no method name found on line ``
  35. ERROR: no method name found on line ``
  36. ERROR: no method name found on line ``
  37. ERROR: no method name found on line ``
  38. ERROR: no method name found on line ``
  39. ERROR: no method name found on line ``
  40. ERROR: no method name found on line ``

Changelog

Since 10.7.0 Introduced.

Notification{} code WC 10.8.1

abstract class Notification {
	/**
	 * Map of notification type identifiers to their corresponding subclass.
	 *
	 * @var array<string, class-string<Notification>>
	 */
	const NOTIFICATION_CLASSES = array(
		'store_order'  => NewOrderNotification::class,
		'store_review' => NewReviewNotification::class,
	);

	/**
	 * The ID of the resource this notification is about (e.g. order ID, comment
	 * ID).
	 *
	 * @var int
	 */
	private int $resource_id;

	/**
	 * Creates a new Notification instance.
	 *
	 * @param int $resource_id The resource ID.
	 *
	 * @throws InvalidArgumentException If the resource ID is invalid.
	 *
	 * @since 10.7.0
	 */
	public function __construct( int $resource_id ) {
		if ( $resource_id <= 0 ) {
			throw new InvalidArgumentException( 'Notification resource_id must be positive.' );
		}

		$this->resource_id = $resource_id;
	}

	/**
	 * Returns the notification type identifier, this should match the subtype
	 * or type (if there isn't a subtype) values attributed to notes in
	 * WordPress.com.
	 *
	 * @return string
	 *
	 * @since 10.7.0
	 */
	abstract public function get_type(): string;

	/**
	 * Returns the WPCOM-ready payload for this notification.
	 *
	 * Returns null if the underlying resource no longer exists.
	 *
	 * @return array|null
	 *
	 * @since 10.7.0
	 */
	abstract public function to_payload(): ?array;

	/**
	 * Checks whether a meta key exists for this notification's resource.
	 *
	 * @param string $key The meta key.
	 * @return bool
	 *
	 * @since 10.7.0
	 */
	abstract public function has_meta( string $key ): bool;

	/**
	 * Writes a meta key with a timestamp to this notification's resource.
	 *
	 * @param string $key The meta key.
	 * @return void
	 *
	 * @since 10.7.0
	 */
	abstract public function write_meta( string $key ): void;

	/**
	 * Deletes a meta key from this notification's resource.
	 *
	 * @param string $key The meta key.
	 * @return void
	 *
	 * @since 10.8.0
	 */
	abstract public function delete_meta( string $key ): void;

	/**
	 * Returns the notification data as an array.
	 *
	 * @return array{type: string, resource_id: int}
	 *
	 * @since 10.7.0
	 */
	public function to_array(): array {
		return array(
			'type'        => $this->get_type(),
			'resource_id' => $this->resource_id,
		);
	}

	/**
	 * Reconstructs a Notification subclass from a serialized array.
	 *
	 * @param array{type: string, resource_id: int} $data The notification data.
	 * @return self
	 *
	 * @throws InvalidArgumentException If the type is unknown.
	 *
	 * @since 10.7.0
	 */
	public static function from_array( array $data ): self {
		$type        = $data['type'] ?? '';
		$resource_id = (int) ( $data['resource_id'] ?? 0 );

		$class = self::NOTIFICATION_CLASSES[ $type ] ?? null;

		if ( ! $class ) {
			// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
			throw new InvalidArgumentException( sprintf( 'Unknown notification type: %s', $type ) );
		}

		return new $class( $resource_id );
	}

	/**
	 * Returns a unique identifier for this notification, used for
	 * deduplication.
	 *
	 * @return string
	 *
	 * @since 10.7.0
	 */
	public function get_identifier(): string {
		return sprintf( '%s_%s_%s', get_current_blog_id(), $this->get_type(), $this->resource_id );
	}

	/**
	 * Gets the resource ID.
	 *
	 * @return int
	 *
	 * @since 10.7.0
	 */
	public function get_resource_id(): int {
		return $this->resource_id;
	}
}