You are here

function optimizely_init in Optimizely 7.2

Same name and namespace in other branches
  1. 6 optimizely.module \optimizely_init()
  2. 7.3 optimizely.module \optimizely_init()
  3. 7 optimizely.module \optimizely_init()

Implements hook_init().

This function impliments the rules entered for each project entry. The goal is to control when an entry (Optimizely hosted javascript file) is applied to a page.

The "Paths" setting for each entry are interpited as literal paths or a whole range of paths noted with wildcards "*". A Project entry can appear on one or more paths that can include wildcards. The <front> token is supported in the possible path values.

For example, '<front>' will add the Optimizely snippet on the front page, and 'clubs/*' will place the snippet on all pages bellow 'clubs/'. Note: The javascript call / snippet will only appear on the actual 'clubs' page if you specify 'clubs' without the wildcard.

A disabled project entry will be ignored allowing entries to persist but be applied.

File

./optimizely.module, line 196
Optimizely module

Code

function optimizely_init() {
  $current_path = current_path();
  $current_path_alias = drupal_lookup_path('alias', $current_path);
  $wildcard_match = FALSE;
  $add_snippet = FALSE;
  $every_page = 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();

  // Query results found
  if ($result) {

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

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

        // 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);
        !is_int($front_index) ?: ($project_paths[$front_index] = variable_get('site_frontpage'));

        // 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 $project_path) {

          // Remove parameters
          if (strpos($project_path, '?') !== FALSE) {
            $project_path = substr($project_path, 0, strpos($project_path, '?'));

            // Look for wildcard match
            if (stristr($current_path, $project_path) || stristr(drupal_lookup_path('alias', $current_path), $project_path) || stristr(drupal_lookup_path('source', $current_path), $project_path)) {
              $add_snippet = TRUE;
              break 2;
            }
          }
          if (strpos($project_path, '*') !== FALSE) {

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

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

              // Look for wildcard match
              if (stristr($current_path, $project_path) || stristr(drupal_lookup_path('alias', $current_path), $project_path) || stristr(drupal_lookup_path('source', $current_path), $project_path)) {
                $add_snippet = TRUE;
                break 2;
              }
            }
          }

          // Build out $project_paths with possible source and alias values to
          // test with drupal_match_path()
          $project_path_source = drupal_lookup_path('source', $project_path);
          !$project_path_source ?: ($project_paths[] = $project_path_source);
          $project_path_alias = drupal_lookup_path('alias', $project_path);
          !$project_path_alias ?: ($project_paths[] = $project_path_alias);
        }

        // Prep for drupal_match_path()
        $project_paths = implode("\n", array_unique($project_paths));
        if (drupal_match_path($current_path, $project_paths) || drupal_match_path($current_path_alias, $project_paths)) {
          $add_snippet = TRUE;
          break;
        }
      }
    }
    if ($add_snippet) {
      $options = array(
        'type' => 'external',
        'every_page' => $every_page,
        'group' => JS_LIBRARY,
        'weight' => 0,
      );

      // Add javascript call to page markup
      drupal_add_js('//cdn.optimizely.com/js/' . $project['project_code'] . '.js', $options);
    }
  }
}