(.*?)<\/a>/i'; if (is_feed() && !$walm_settings['autounwrap']) return $text; if ($walm_settings['autounwrap']) { global $wpdb; // Make an associative array of short links and corresponding destinations preg_match_all($anchorPattern, $text, $matches); // Get all affiliate links in post $shorts = array(); foreach($matches[2] as $link) { $stub = WalmStubs::get($link); if (!in_array($stub, $stubs)) continue; $parsedURL = parse_url($link); $shorts[] = trim($parsedURL['path'], '/'); } $shorts = array_unique($shorts); // Load affiliate links data from DB into memory. 10 links at a time. while (count($shorts) != 0) { $counter = 10; unset($ten_shorts); while ($counter != 0 && count($shorts) != 0) { $ten_shorts[] = array_pop($shorts); $counter--; } $where_ten_shorts = "'" . implode("','", $ten_shorts) . "'"; $query = "SELECT short,original FROM `" . WALM_LINKS . "` WHERE `short` IN($where_ten_shorts);"; $link_batch_data = $wpdb->get_results($query); $this->link_data = array_merge($link_batch_data, $this->link_data); } } $text = preg_replace_callback($anchorPattern, array(&$this,'track_parse_link'), $text); return $text; } function track_parse_link($matches) { global $walm_settings, $stubs; $origin = $this->track_get_domain($_SERVER["HTTP_HOST"]); $target = $this->track_get_domain($matches[2]); if ($target['domain'] == $origin['domain']) { $pageURL = $matches[2]; $stub = WalmStubs::get($pageURL); // If URL has a stub that we know -- do the stuff. if (in_array($stub, $stubs)) { $parsedURL = parse_url($pageURL); $short = trim($parsedURL['path'], '/'); $trackBit = "_gaq.push(['_trackPageview','/$short/']);"; if (preg_match('/onclick=[\'\"](.*?)[\'\"]/i', $matches[3]) > 0) { // Check for manually tagged outbound clicks, and replace them with the tracking of choice. if (preg_match('/.*_track(Pageview|Event).*/i', $matches[3]) > 0) { $matches[3] = preg_replace('/onclick=[\'\"](javascript:)?(.*;)?[a-zA-Z0-9]+\._track(Pageview|Event)\([^\)]+\)(;)?(.*)?[\'\"]/i', 'onclick="javascript:' . $trackBit .'$2$5"', $matches[3]); } else { $matches[3] = preg_replace('/onclick=[\'\"](javascript:)?(.*?)[\'\"]/i', 'onclick="javascript:' . $trackBit .'$2"', $matches[3]); } } else { $matches[3] = 'onclick="javascript:' . $trackBit . '"' . $matches[3]; } } } $matches[3] = ($matches[3] == ' ' || empty($matches[3])) ? '' : ' ' . trim($matches[3]); global $walm_settings; // If auto-unwrapping is enabled if ($walm_settings['autounwrap']) { foreach($this->link_data as $link) { if ($link->short == $short) { if (strpos($matches[2], 'goto=')) { $url = parse_url($matches[2]); $pos = strpos($url['query'], 'goto='); $original = substr_replace($url['query'], '', $pos, strlen('goto=')); } else $original = $link->original; $matches[2] = $original; } } } // If tracking is disabled. if (!$walm_settings['track']) { $matches[3] = ''; } $return = '' . $matches[4] . ''; return $return; } function track_get_domain($uri) { $hostPattern = "/^(http:\/\/)?([^\/]+)/i"; $domainPatternUS = "/[^\.\/]+\.[^\.\/]+$/"; $domainPatternUK = "/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/"; preg_match($hostPattern, $uri, $matches); $host = $matches[2]; if (preg_match("/.*\..*\..*\..*$/",$host)) preg_match($domainPatternUK, $host, $matches); else preg_match($domainPatternUS, $host, $matches); $domain = (isset($matches[0])) ? $matches[0] : null; return array("domain"=>$domain,"host"=>$host); } } $walm_tracking = new WalmTracking();