debug_log) . "\n-->";
}
function debug_info($info, $return = 0) {
if ($this->options['aff_debug']) {
$t = "\n";
$this->debug_log[] = $info;
if ($return)
return $t;
}
}
function check_exclusions($matches) {
if ($r = $this->check_follow($matches))
return $r;
if ($r = $this->check_excl_list($matches))
return $r;
return false;
}
function check_excl_list($matches) {
#checking for entry in exclusion list
$check_allowed = $matches[2];
$this->debug_info('Checking link "' . $check_allowed . '" VS exclusion list {' . var_export($this->options['exclude_links_'], 1) . '}');
foreach ($this->options['exclude_links_'] as $val)
if (stripos($check_allowed, $val) === 0) {
$this->debug_info('In exclusion list (' . $val . '), not masking...');
return $matches[0];
}
$this->debug_info('Not in exclusion list, masking...');
return false;
}
function check_follow($matches) {
#support of "meta=follow" option for admins. disabled by default to minify processing.
if (!$this->options['aff_dont_mask_admin_follow'])
return false;
$id = array(get_comment_ID(), get_the_ID());//it is either page or post
if ($id[0])
$this->debug_info('It is a comment. id ' . $id[0]);
elseif ($id[1])
$this->debug_info('It is a page. id ' . $id[1]);
$author = false;
if ($id[0])
$author = get_comment_author($id[0]);
else if ($id[1])
$author = get_the_author_meta('ID');
if (!$author)
$this->debug_info('it is neither post or page, applying usual rules');
elseif (user_can($author, 'manage_options') && (stripos($matches[0], 'rel="follow"') !== FALSE || stripos($matches[0], "rel='follow'") !== FALSE)) {
$this->debug_info('This link has a follow atribute and is posted by admin, not masking it.');
#wordpress adds rel="nofollow" by itself when posting new link in comments. get rid of it! Also, remove our follow attibute - it is unneccesary.
return str_ireplace(array('rel="follow"', "rel='follow'", 'rel="nofollow"'), '', $matches[0]);
} else
$this->debug_info('it does not have rel follow or is not posted by admin, masking it');
return false;
}
function parser($matches) {
global $wp_rewrite, $wpdb;
#parser init
$url = $matches[2];
$this->debug_info('Parser called. Parsing argument {' . var_export($matches, 1) . "}\nAgainst link {" . $url . "}\n ");
$r = $this->check_exclusions($matches);
if ($r !== FALSE)
return $r;
#checking for different options, setting other
if (!$wp_rewrite->using_permalinks())
$sep = '?' . $this->options['aff_LINK_SEP'] . '=';
else
$sep = '/' . $this->options['aff_LINK_SEP'] . '/';
if ($this->options['aff_add_blank'])
$ifblank = ' target="_blank"';
if ($this->options['aff_add_nofollow'])
$ifnofollow = ' rel="nofollow"';
/*masking url with numbers*/
if (!$this->options['aff_disable_mask_links']) {
$url = $this->encode_link($url);
if (!$wp_rewrite->using_permalinks())
$url = urlencode($url);
if ($sep[0] == '/')#to not create double backslashes
$sep = substr($sep, 1);
$tmp = $this->options['site'];
//add "/" to site url- some servers dont't work with urls like xxx.ru?goto, but with xxx.ru/?goto
if (substr($this->options['site'],0, -1) !== '/')
$tmp .= '/';
$url = $tmp . $sep . $url;
}
if ($this->options['aff_remove_links'])
return '' . $matches[4] . '';
if ($this->options['aff_link2text'])
return '' . $matches[4] . ' ^(' . $url . ')';
$link = '' . $matches[4] . '';
if ($this->options['aff_put_noindex'])
$link = '';
if ($this->options['aff_put_noindex_comment'])
$link = '' . $link . '';
return $link;
}
function encode_link($url)
{
global $wpdb;
if ($this->options['aff_base64']) {
$url = base64_encode($url);
} elseif ($this->options['maskurl']) {
$sql = 'select id from ' . $wpdb->prefix . 'masklinks where url= %s limit 1';
$result = $wpdb->get_var($wpdb->prepare($sql, $url));
if (is_null($result) && strpos($wpdb->last_error, "doesn't exist"))//no table found
{
/*create masklink table*/
echo '
' . __('Failed to make masked link. MySQL link table does not exist. Trying to create table.', 'affiliate-tools') . '
';
$sql2 = 'CREATE TABLE ' . $wpdb->prefix . 'masklinks(`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`url` VARCHAR(255), PRIMARY KEY (`id`))';
$res = $wpdb->query($sql2);
if (!$res) {
echo '
' . __('Failed to create table. Please, check mysql permissions.', 'affiliate-tools') . '