You are here

function livechat_check_visibility in LiveChat 8

Same name and namespace in other branches
  1. 8.2 livechat.module \livechat_check_visibility()
  2. 7 livechat.module \livechat_check_visibility()

Checks whether LiveChat should be visible on a path, according to settings.

Parameters

string $path: The raw URI.

Return value

bool Display or not LiveChat popup.

1 call to livechat_check_visibility()
livechat_page_attachments in ./livechat.module
Implements hook_page_attachments().

File

./livechat.module, line 140
LiveChat module.

Code

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);
}