You are here

function optimizely_page_attachments in Optimizely 8.0

Same name and namespace in other branches
  1. 8.3 optimizely.module \optimizely_page_attachments()
  2. 8 optimizely.module \optimizely_page_attachments()

Implements hook_page_build().

Checks each page that is about to be rendered as to whether to insert a snippet of Optimizely code or not.

File

./optimizely.module, line 105
Optimizely module

Code

function optimizely_page_attachments(array &$page) {
  $url = \Drupal\Core\Url::fromRoute('<current>');
  $current_path = $url
    ->toString();
  $current_path_alias = _lookup_path_alias($current_path);
  $add_snippet = FALSE;

  // Load all entries in the optimizely table
  $query = db_select('optimizely', 'o', array(
    'target' => 'slave',
  ))
    ->fields('o')
    ->condition('o.enabled', 1, '=')
    ->orderBy('oid');

  // Fetch the result set.
  $result = $query
    ->execute();
  if (!$result) {
    return;
  }

  // Query results found
  // Loop through each row of the found results
  while ($project = $result
    ->fetchAssoc()) {

    // Only process the entries that are enabled
    if (!$project['enabled']) {
      continue;
    }

    // Target paths from database for project and get real path for <front> if a part of target paths for project
    $project_paths = unserialize($project['path']);
    $front_index = array_search('<front>', $project_paths);
    if (is_int($front_index)) {
      $project_paths[$front_index] = \Drupal::config('system.site')
        ->get('page.front');
    }

    // Check all paths for alias or sytem URL values, foreach loop based on
    // orginal array value, addiitonal values added will not effect the array
    foreach ($project_paths as $proj_path) {

      // Remove parameters
      if (strpos($proj_path, '?') !== FALSE) {
        $proj_path = substr($proj_path, 0, strpos($proj_path, '?'));
        if (stristr($current_path, $proj_path) || stristr(_lookup_path_alias($current_path), $proj_path) || stristr(_lookup_system_path($current_path), $proj_path)) {
          $add_snippet = TRUE;
          break 2;
        }
      }

      // Look for wildcard match
      if (strpos($proj_path, '*') !== FALSE) {

        // Sitewide wild card
        if ($proj_path == '*') {
          $add_snippet = TRUE;
          break 2;
        }
        else {

          // Remove wildcard, get base path(s)
          $proj_path = substr($proj_path, 0, -2);

          // Look for wildcard match
          if (stristr($current_path, $proj_path) || stristr(_lookup_path_alias($current_path), $proj_path) || stristr(_lookup_system_path($current_path), $proj_path)) {
            $add_snippet = TRUE;
            break 2;
          }
        }
      }

      // Build out $project_paths with possible source and alias values to
      // check for path matching.
      $proj_path_source = _lookup_system_path($proj_path);
      if ($proj_path_source) {
        $project_paths[] = $proj_path_source;
      }
      $proj_path_alias = _lookup_path_alias($proj_path);
      if ($proj_path_alias) {
        $project_paths[] = $proj_path_alias;
      }
    }

    // Prep for path matching.
    $project_paths = implode("\n", array_unique($project_paths));
    if (\Drupal::service('path.matcher')
      ->matchPath($current_path, $project_paths) || \Drupal::service('path.matcher')
      ->matchPath($current_path_alias, $project_paths)) {
      $add_snippet = TRUE;
      break;
    }
  }
  if ($add_snippet) {

    // Add javascript call to page markup
    $snippet_url = '//cdn.optimizely.com/js/' . $project['project_code'] . '.js';
    $page['#attached']['html_head'][] = array(
      array(
        '#tag' => 'script',
        '#attributes' => array(
          'type' => 'text/javascript',
          'src' => $snippet_url,
        ),
        '#value' => '',
      ),
      'optimizely-snippet',
    );
  }
}