Nav apraksta

StatisticsClicks.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use DateTimeInterface;
  5. /**
  6. * @property int $newsletterId
  7. * @property int $subscriberId
  8. * @property int $queueId
  9. * @property int $linkId
  10. * @property int $count
  11. */
  12. class StatisticsClicks extends Model {
  13. public static $_table = MP_STATISTICS_CLICKS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  14. public static function findLatestPerNewsletterBySubscriber(Subscriber $subscriber, DateTimeInterface $from, DateTimeInterface $to) {
  15. // subquery to find latest click IDs for each newsletter
  16. $table = self::$_table;
  17. $latestClickIdsPerNewsletterQuery = "
  18. SELECT MAX(id)
  19. FROM $table
  20. WHERE subscriber_id = :subscriber_id
  21. AND updated_at > :from
  22. AND updated_at < :to
  23. GROUP BY newsletter_id
  24. ";
  25. return static::tableAlias('clicks')
  26. ->whereRaw("clicks.id IN ($latestClickIdsPerNewsletterQuery)", [
  27. 'subscriber_id' => $subscriber->id,
  28. 'from' => $from->format('Y-m-d H:i:s'),
  29. 'to' => $to->format('Y-m-d H:i:s'),
  30. ])
  31. ->findMany();
  32. }
  33. }