You are here

function adserve_invoke_hook in Advertisement 5.2

Same name and namespace in other branches
  1. 5 adserve.inc \adserve_invoke_hook()
  2. 6.3 adcache.inc \adserve_invoke_hook()
  3. 6 adserve.inc \adserve_invoke_hook()
  4. 6.2 adcache.inc \adserve_invoke_hook()
  5. 7 adcache.inc \adserve_invoke_hook()

Invoke adserve cache hook, including files as necessary.

4 calls to adserve_invoke_hook()
adserve_cache_display in ./adcache.inc
Default function for displaying advertisements. This is not generally replaced by ad cache modules.
adserve_cache_get_ad_ids in ./adcache.inc
Default wrapper function for displaying advertisements. This generally is not replaced by ad caches modules.
adserve_cache_increment in ./adcache.inc
Increment action directly in the database.
ad_cache_file_increment in cache/file/ad_cache_file.inc
Increment an advertisement counter.

File

./adcache.inc, line 36

Code

function adserve_invoke_hook() {
  static $hooks = array();
  $args = func_get_args();
  $hook = array_shift($args);
  $action = array_shift($args);
  _debug_echo("adserve_invoke_hook hook({$hook}) action({$action})");
  if (!isset($hooks[$hook])) {
    $hooks[$hook] = adserve_cache('hook', $hook);
    if (is_array($hooks[$hook]) && !empty($hooks[$hook]) && is_array($hooks[$hook]['file'])) {

      // Include all necessary files.
      foreach ($hooks[$hook]['file'] as $files) {
        if (is_array($files)) {
          foreach ($files as $file) {
            $include_file = adserve_variable('root_dir') . '/' . $file;
            if (file_exists($include_file) && is_file($include_file)) {
              _debug_echo("Including file: '{$include_file}'.");
              include_once $include_file;
            }
            else {
              _debug_echo("Failed to include file: '{$include_file}'.");
            }
          }
        }
      }
    }
  }
  $return = array();
  if (is_array($hooks[$hook]) && !empty($hooks[$hook]) && is_array($hooks[$hook]['function'])) {
    foreach ($hooks[$hook]['function'] as $weight => $functions) {
      foreach ($functions as $function) {
        if (function_exists($function)) {
          _debug_echo("Invoking '{$function}'.");
          $return[] = call_user_func_array($function, $args);
        }
        else {
          _debug_echo("Function '{$function}' does not exist.\n");
        }
      }
    }
  }
  else {
    $function = "adserve_hook_{$hook}";
    if (function_exists($function)) {
      _debug_echo("Invoking '{$function}'.");
      $return[] = call_user_func_array($function, $args);
    }
    else {
      _debug_echo("Function '{$function}' does not exist.\n");
    }
  }
  switch ($action) {
    case 'intersect':
      if (sizeof($return) == 1) {
        return $return[0];
      }
      else {
        return call_user_func_array('array_intersect', $return);
      }
    case 'merge':
      if (sizeof($return) == 1) {
        return $return[0];
      }
      else {
        $merge = array();
        foreach ($return as $array) {
          $merge += $array;
        }
        return $merge;
      }
    case 'first':
      foreach ($return as $item) {
        if (is_array($item) && !empty($item)) {
          return $item;
        }
      }
      return array();
    case 'append':
      $append = '';
      foreach ($return as $item) {
        if (!is_array($item)) {
          $append .= $item;
        }
      }
      return $append;
    default:
    case 'raw':
    default:
      return $return;
  }
}