You are here

poptin.module in Website Popups, Email Popup, Exit-Intent Popup, and Contact Forms – Poptin 8

Same filename and directory in other branches
  1. 7 poptin.module

Module for adding popups on website.

File

poptin.module
View source
<?php

/**
 * @file
 * Module for adding popups on website.
 */
use Drupal\Component\Utility\Html;

/**
 * Implements poptin_help().
 */
function poptin_help($path, $arg) {
  switch ($path) {
    case 'help.page.poptin':
      $filepath = dirname(__FILE__) . '/README.md';
      if (file_exists($filepath)) {
        $readme = file_get_contents($filepath);
      }
      else {
        $filepath = dirname(__FILE__) . '/README.txt';
        if (file_exists($filepath)) {
          $readme = file_get_contents($filepath);
        }
      }
      if (!isset($readme)) {
        return NULL;
      }
      $output = '<pre>' . $readme . '</pre>';
      return $output;
  }
}

/**
 * Implements poptin_theme().
 */
function poptin_theme($existing, $type, $theme, $path) {
  return [
    'main_template' => [
      'variables' => [
        'csrftoken' => NULL,
        'test_var' => NULL,
        'module_img_dir' => NULL,
        'base_url' => NULL,
        'generateloginlink' => NULL,
        'deactivate_url' => NULL,
      ],
    ],
  ];
}

/**
 * Implements poptin_preprocess_html().
 */
function poptin_preprocess_html(&$variables) {
  $variables['#attached']['library'][] = 'poptin/poptin-js';
}

/**
 * Implements hook_page_attachments().
 */
function poptin_page_attachments(array &$attachments) {
  if (!\Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    $poptin_db = poptin_fetch_row();
    if (is_array($poptin_db) && isset($poptin_db['client_id']) && $poptin_db['client_id'] != '') {
      $client_id = Html::escape($poptin_db['client_id']);
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'html_tag',
          '#tag' => 'script',
          '#attributes' => [
            'src' => 'https://cdn.popt.in/pixel.js?id=' . $client_id,
            'async' => 'true',
            'id' => 'pixel-script-poptin',
          ],
        ],
        'poptin',
      ];
    }
  }
}

/**
 * Implements poptin_fetch_row().
 */
function poptin_fetch_row() {
  $query = \Drupal::database()
    ->select('poptin', 'p');
  $query
    ->addField('p', 'user_id');
  $query
    ->addField('p', 'client_id');
  $query
    ->addField('p', 'token');
  $query
    ->range(0, 1);
  $result = $query
    ->execute()
    ->fetch();
  if ($result) {
    return [
      'userid' => $result->user_id,
      'client_id' => $result->client_id,
      'token' => $result->token,
    ];
  }
  else {
    return FALSE;
  }
}