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( string $type, int $resource_id )
  2. public get_identifier()
  3. public get_resource_id()
  4. public get_type()
  5. public to_array()
  6. public to_payload()
  7. ERROR: no method name found on line `/**`
  8. ERROR: no method name found on line ``
  9. ERROR: no method name found on line ``
  10. ERROR: no method name found on line ``
  11. ERROR: no method name found on line ``
  12. ERROR: no method name found on line ``
  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 ``
  19. ERROR: no method name found on line `* deduplication.`
  20. ERROR: no method name found on line ``
  21. ERROR: no method name found on line ``
  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 ``

Changelog

Since 10.7.0 Introduced.

Notification{} code WC 10.7.0

abstract class Notification {
	/**
	 * The notification type.
	 *
	 * @var string
	 */
	private string $type;

	/**
	 * 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 string $type        The notification type.
	 * @param int    $resource_id The resource ID.
	 *
	 * @throws InvalidArgumentException If any argument is invalid.
	 *
	 * @since 10.7.0
	 */
	public function __construct( string $type, int $resource_id ) {
		if ( '' === trim( $type ) ) {
			throw new InvalidArgumentException( 'Notification type must not be empty.' );
		}

		if ( $resource_id <= 0 ) {
			throw new InvalidArgumentException( 'Notification resource_id must be positive.' );
		}

		$this->type        = trim( $type );
		$this->resource_id = $resource_id;
	}

	/**
	 * 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;

	/**
	 * 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->type,
			'resource_id' => $this->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->type, $this->resource_id );
	}

	/**
	 * Gets the notification type.
	 *
	 * @return string
	 *
	 * @since 10.7.0
	 */
	public function get_type(): string {
		return $this->type;
	}

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