You are here

livechat.module in LiveChat 8

Same filename and directory in other branches
  1. 8.3 livechat.module
  2. 8.2 livechat.module
  3. 7 livechat.module

LiveChat module.

File

livechat.module
View source
<?php

/**
 * @file
 * LiveChat module.
 */
use Drupal\Component\Utility\Unicode;

/**
 * LiveChat configuration name.
 */
const LIVECHAT_CONFIGURATION_NAME = 'livechat.configuration';

/**
 * API path for getting license number by login.
 */
const LIVECHAT_API_LICENSE_NUMBER = 'https://api.livechatinc.com/license/number';

/**
 * Shows LiveChat on every page except the listed pages.
 */
const LIVECHAT_VISIBILITY_NOTLISTED = 0;

/**
 * Shows LiveChat on only the listed pages.
 */
const LIVECHAT_VISIBILITY_LISTED = 1;

/**
 * System paths that LiveChat can be set to always exclude.
 *
 * @TODO: Need to test. Maybe need to add trailing slash to path.
 */
const LIVECHAT_VISIBILITY_SYSTEM_PATHS = "batch\nfile/ajax/*\nsystem/ajax\nadmin_menu/*\njs/admin_menu/*";

/**
 * Implements hook_page_attachments().
 */
function livechat_page_attachments(array &$attachments) {
  $configs = livechat_configs();
  if (!empty($configs
    ->get('livechat_enabled'))) {

    // Validate permissions for current user and visibility for current path.
    $user = \Drupal::currentUser();
    if ($user
      ->hasPermission('use livechat') && livechat_check_visibility()) {
      $license = $configs
        ->get('livechat_license');
      $group_id = $configs
        ->get('livechat_group');
      $settings = livechat_attach_js_settings($license, $group_id);
      if (!empty($settings)) {
        $attachments['#attached']['drupalSettings']['LiveChat'] = $settings;
        $attachments['#attached']['library'][] = 'livechat/livechat';
      }
    }
  }
}

/**
 * Return settings for attaching LiveChat tracking code.
 *
 * @param string $license
 *   LiveChat account license number.
 * @param string $group_id
 *   Id of the website for multi-site.
 *
 * @return array
 */
function livechat_attach_js_settings($license, $group_id) {
  $settings = [];
  if (!empty($license)) {

    // Allow modules to add custom parameters and visitor information.
    $params = [];
    $visitor = [];
    \Drupal::moduleHandler()
      ->alter('livechat', $params, $visitor);
    $settings = [
      'license' => $license,
      'params' => $params,
      'visitor' => $visitor,
    ];

    // Add in the group id if it has been set.
    if (!empty($group_id)) {
      $settings['LiveChat']['group'] = $group_id;
    }
  }
  return $settings;
}

/**
 * Checks whether LiveChat is correctly set up.
 *
 * @return mixed
 *   License number or FALSE.
 */
function livechat_is_installed() {
  $configs = livechat_configs();
  $license = $configs
    ->get('livechat_license');
  if (empty($license)) {
    return FALSE;
  }
  return livechat_validate_license($license);
}

/**
 * Validates a LiveChat license.
 */
function livechat_validate_license($license) {
  if (empty($license)) {
    return FALSE;
  }
  return preg_match('/^[0-9]{1,20}$/', $license);
}

/**
 * LiveChat configurations.
 *
 * @return \Drupal\Core\Config\ImmutableConfig
 *   An immutable configuration object.
 */
function livechat_configs() {
  $configs = \Drupal::config(LIVECHAT_CONFIGURATION_NAME);
  return $configs;
}

/**
 * Checks whether LiveChat should be visible on a path, according to settings.
 *
 * @param string $path
 *   The raw URI.
 *
 * @return bool
 *   Display or not LiveChat popup.
 */
function livechat_check_visibility($path = NULL) {
  $configs = livechat_configs();

  // Default to the current path.
  if (!isset($path)) {

    // Important: in Drupal 8 any path returns with trailing slash.
    // 'currentPath' !== '/currentPath in matchPath result.
    // Thanks for this pattern with ^.
    $path = \Drupal::service('path.current')
      ->getPath();
  }

  // Visibility settings.
  $visibility = $configs
    ->get('livechat_visibility');
  $pages = Unicode::strtolower($configs
    ->get('livechat_pages'));

  // If $visibility is set to LIVECHAT_VISIBILITY_NOTLISTED and the setting for
  // excluding system paths is enabled, add system paths to the list of pages.
  $exclude_system_paths = $configs
    ->get('livechat_exclude_system_paths');
  if ($visibility == LIVECHAT_VISIBILITY_NOTLISTED && $exclude_system_paths) {
    $pages .= "\n" . LIVECHAT_VISIBILITY_SYSTEM_PATHS;
  }

  // Get the path alias as lowercase.
  $path_alias = \Drupal::service('path.alias_manager')
    ->getAliasByPath($path);
  $expanded_path = Unicode::strtolower($path_alias);

  // Compare the lowercase internal and lowercase path alias (if any).
  $page_match = \Drupal::service('path.matcher')
    ->matchPath($expanded_path, $pages);
  if ($expanded_path != $path) {
    $page_match = $page_match || \Drupal::service('path.matcher')
      ->matchPath($path, $pages);
  }

  // When $visibility has a value of 0 (LIVECHAT_VISIBILITY_NOTLISTED), the
  // widget should be displayed on all pages except those listed. When set to 1
  // (LIVECHAT_VISIBILITY_LISTED), it should only be displayed on listed pages.
  return !($visibility xor $page_match);
}

Functions

Namesort descending Description
livechat_attach_js_settings Return settings for attaching LiveChat tracking code.
livechat_check_visibility Checks whether LiveChat should be visible on a path, according to settings.
livechat_configs LiveChat configurations.
livechat_is_installed Checks whether LiveChat is correctly set up.
livechat_page_attachments Implements hook_page_attachments().
livechat_validate_license Validates a LiveChat license.

Constants

Namesort descending Description
LIVECHAT_API_LICENSE_NUMBER API path for getting license number by login.
LIVECHAT_CONFIGURATION_NAME LiveChat configuration name.
LIVECHAT_VISIBILITY_LISTED Shows LiveChat on only the listed pages.
LIVECHAT_VISIBILITY_NOTLISTED Shows LiveChat on every page except the listed pages.
LIVECHAT_VISIBILITY_SYSTEM_PATHS System paths that LiveChat can be set to always exclude.