You are here

function mailing_list_cron in Mailing List 8

Implements hook_cron().

File

./mailing_list.module, line 119
Contains the Mailing list module.

Code

function mailing_list_cron() {

  // Purge old subscriptions.

  /** @var \Drupal\mailing_list\MailingListInterface $list */
  foreach (\Drupal::entityTypeManager()
    ->getStorage('mailing_list')
    ->loadMultiple() as $list) {
    $lifetime = $list
      ->getInactiveLifetime();
    if (empty($lifetime)) {

      // No purge.
      return;
    }
    $subscription_query = \Drupal::entityQuery('mailing_list_subscription');
    $old_subscriptions = $subscription_query
      ->condition('mailing_list', $list
      ->id())
      ->condition('status', SubscriptionInterface::INACTIVE)
      ->condition('changed', REQUEST_TIME - $lifetime, '<')
      ->execute();
    $count = 0;
    foreach ($old_subscriptions as $sid) {
      \Drupal::entityTypeManager()
        ->getStorage('mailing_list_subscription')
        ->load($sid)
        ->delete();
      $count++;
    }
    if ($count > 0) {
      \Drupal::logger('mailing_list')
        ->info('Purged @count old inactive subscriptions.', [
        '@count' => $count,
      ]);
    }
  }
}