You are here

function anonymous_publishing_cl_node_presave in Anonymous Publishing 8

Same name and namespace in other branches
  1. 7 modules/cl/anonymous_publishing_cl.module \anonymous_publishing_cl_node_presave()

Implements hook_node_presave().

File

modules/anonymous_publishing_cl/anonymous_publishing_cl.module, line 560
Main hooks for anonymous publishing cl module.

Code

function anonymous_publishing_cl_node_presave(NodeInterface $node) {
  $settings = \Drupal::config('anonymous_publishing_cl.settings');

  // Check if we are saving a node that needs treatment.
  if (isset($node->anonymous_publishing_email)) {
    $needs_confirmation = TRUE;
    $verification_persistency = $settings
      ->get('verification_persistency');

    // If the verification can be persisted, first check if we actually already
    // have a verification persisted for this node.
    if ($verification_persistency != 'each_post') {
      $result = \Drupal::database()
        ->select('anonymous_publishing_emails')
        ->fields('anonymous_publishing_emails', array(
        'email',
        'ipaddress',
        'blocked',
      ))
        ->condition('email', $node->anonymous_publishing_email)
        ->execute()
        ->fetchAssoc();
      if ($result['email']) {
        $ip = \Drupal::request()
          ->getClientIp();
        if ($verification_persistency == 'persist' || $verification_persistency == 'ip_duration' && $ip == $result['ipaddress']) {

          // Blocked status handled by_anonymous_publishing_cl_content_validate().
          $node
            ->set('status', NODE_PUBLISHED);
          \Drupal::messenger()
            ->addMessage(t('Your email has been activated previously.'));
          $needs_confirmation = FALSE;
        }
      }
    }
    if ($needs_confirmation) {
      $node
        ->set('status', NODE_NOT_PUBLISHED);
      \Drupal::messenger()
        ->addMessage(t('Thank you for posting here. You must also confirm that @email belongs to you.', array(
        '@email' => $node->anonymous_publishing_email,
      )));
    }
  }
}