query($query);
// remove orphaned address records
$query = " DELETE
{$table_prefix}alc_address
FROM
{$table_prefix}alc_address
LEFT OUTER JOIN
{$table_prefix}alc_link
ON ({$table_prefix}alc_link.id = {$table_prefix}alc_address.linkId)
WHERE
({$table_prefix}alc_link.id IS NULL)";
$wpdb->query($query);
// finally, remove orphaned log records
$query = " DELETE
{$table_prefix}alc_redirectlog
FROM
{$table_prefix}alc_redirectlog
LEFT OUTER JOIN
{$table_prefix}alc_address
ON ({$table_prefix}alc_address.id = {$table_prefix}alc_redirectlog.addressId)
WHERE
({$table_prefix}alc_address.id IS NULL)";
$wpdb->query($query);
$message = 'Affiliate link(s) removed.';
}
?>
5)
LEFT OUTER JOIN
(SELECT
linkId,
COUNT(id)
AS addresses
FROM
{$table_prefix}alc_address
AS alc_address
GROUP BY
alc_address.linkId)
AS alc_addresscount
ON (alc_addresscount.linkId = alc_link.id)
LEFT OUTER JOIN
{$table_prefix}alc_address
AS alc_defaultaddress
ON (alc_defaultaddress.linkId = alc_link.id)
AND (alc_defaultaddress.default = 'Yes')
LEFT OUTER JOIN
{$table_prefix}alc_redirectlog
AS alc_redirectlog
ON (alc_redirectlog.addressId = alc_address.id)
GROUP BY
alc_link.id
ORDER BY
alc_link.replacementOrder ASC";
$rows = $wpdb->get_results($query);
foreach ($rows as $row) {
$background = '';
if (!$row->addresses ||
!$row->hasDefault) {
$background = "background: #FAEBD7;";
}
?>
query("DELETE FROM {$table_prefix}alc_address WHERE id = $addressId");
// and now delete any entries in the redirect log table..
$wpdb->query("DELETE FROM {$table_prefix}alc_redirectlog WHERE addressId = $addressId");
$message = 'Address deleted.';
}
// update an address?
if (isset($_POST['update_address'])) {
// is this a default link ?
if (isset($_POST['default'])) {
// make sure that we dont end up with more than one default
$wpdb->update($table_prefix . 'alc_address',
array('default' => 'No'),
array('linkId' => (int)$linkId));
}
// prepare the data
$where = array('id' => (int)$_POST['update_address']);
$data = array('default' => isset($_POST['default']) ? 'Yes' : 'No',
'country' => $_POST['country'],
'address' => $_POST['address']);
// update the record
$wpdb->update($table_prefix . 'alc_address', $data, $where, array('%s'), array('%d'));
$message = 'Address updated.';
}
// create a new record ?
if (isset($_POST['command']) &&
$_POST['command'] == 'new') {
// validate data....
if (!strlen($_POST['urlsuffix'])) {
$errors[] = 'Please provide a url suffix.';
}
// check that the url suffix is unique
if (!isSuffixUnique($_POST['urlsuffix'])) {
$errors[] = 'Please provide a unique suffix.';
}
// any errors ?
if (!sizeof($errors)) {
$flags = 0;
//sanitise data
if (isset($_POST['replacementactive'])) {
$flags |= ALC_ACTIVE;
}
if (isset($_POST['countredirects'])) {
$flags |= ALC_COUNTREDIRECTS;
}
if (isset($_POST['nofollow'])) {
$flags |= ALC_NOFOLLOW;
}
if (isset($_POST['opennewwindow'])) {
$flags |= ALC_OPENNEWWINDOW;
}
$titleText = $wpdb->escape($_POST['titletext']);
$searchText = $wpdb->escape($_POST['searchtext']);
$urlSuffix = $wpdb->escape($_POST['urlsuffix']);
$replacementOrder = (int)$_POST['replacementorder'];
$maxReplacements = (int)$_POST['maxreplacements'];
$redirectType = $_POST['redirecttype'];
$data = array(
'flags' => $flags,
'titleText' => $titleText,
'searchText' => $searchText,
'urlSuffix' => $urlSuffix,
'replacementOrder' => $replacementOrder,
'maxReplacements' => $maxReplacements,
'redirectType' => $redirectType
);
// perform the insert
$wpdb->insert($table_prefix . 'alc_link', $data);
$command = 'edit';
$linkId = $wpdb->insert_id;
$message = 'New affiliate link added.';
}
}
// update record ?
if (isset($_POST['command']) &&
$_POST['command'] == 'edit' &&
$linkId) {
// validate data....
if (!strlen($_POST['urlsuffix'])) {
$errors[] = 'Please provide a url suffix.';
}
// check that the url suffix is unique
if (!isSuffixUnique($_POST['urlsuffix'], $linkId)) {
$errors[] = 'Please provide a unique suffix.';
}
// any errors ?
if (!sizeof($errors)) {
$flags = 0;
//sanitise data
if (isset($_POST['replacementactive'])) {
$flags |= ALC_ACTIVE;
}
if (isset($_POST['countredirects'])) {
$flags |= ALC_COUNTREDIRECTS;
}
if (isset($_POST['nofollow'])) {
$flags |= ALC_NOFOLLOW;
}
if (isset($_POST['opennewwindow'])) {
$flags |= ALC_OPENNEWWINDOW;
}
$titleText = $wpdb->escape($_POST['titletext']);
$searchText = $wpdb->escape($_POST['searchtext']);
$urlSuffix = $wpdb->escape($_POST['urlsuffix']);
$replacementOrder = (int)$_POST['replacementorder'];
$maxReplacements = (int)$_POST['maxreplacements'];
$redirectType = $_POST['redirecttype'];
$data = array(
'flags' => $flags,
'titleText' => $titleText,
'searchText' => $searchText,
'urlSuffix' => $urlSuffix,
'replacementOrder' => $replacementOrder,
'maxReplacements' => $maxReplacements,
'redirectType' => $redirectType
);
$where = array('id' => $linkId);
// perform the insert
$wpdb->update($table_prefix . 'alc_link', $data, $where);
$message = 'Affiliate link updated.';
}
}
if ($command == 'edit' &&
$linkId) {
$posturl = get_option('siteurl') . '/wp-admin/admin.php?page=affiliatelinkcloaker&edit=' . $linkId;
$query = " SELECT
id,
flags,
searchText,
urlSuffix,
replacementOrder,
maxReplacements,
redirectType
FROM
{$table_prefix}alc_link
WHERE
id = $linkId";
$row = $wpdb->get_row($query);
$id = $row->id;
$flags = $row->flags;
$searchText = $row->searchText;
$urlSuffix = $row->urlSuffix;
$replacementOrder = $row->replacementOrder;
$maxReplacements = $row->maxReplacements;
$redirectType = $row->redirectType;
}
if ($command == 'new') {
$posturl = get_option('siteurl') . '/wp-admin/admin.php?page=affiliatelinkcloaker&edit&new';
}
// has the user created a new link record?
if ($linkId) {
$posturl = get_option('siteurl') . '/wp-admin/admin.php?page=affiliatelinkcloaker&edit=' . $linkId;
}
// validate address stuff
if ($linkId) {
$query = " SELECT
COUNT(alc_address.id)
AS addresses,
(CASE
WHEN alc_defaultaddress.id IS NULL
THEN 0
ELSE 1
END)
AS defaultAddress
FROM
{$table_prefix}alc_link
AS alc_link
LEFT OUTER JOIN
{$table_prefix}alc_address
AS alc_address
ON (alc_address.linkId = alc_link.id)
LEFT OUTER JOIN
{$table_prefix}alc_address
AS alc_defaultaddress
ON (alc_defaultaddress.linkId = alc_link.id)
AND (alc_defaultaddress.default = 'Yes')
WHERE
alc_link.id = $linkId
GROUP BY
alc_link.id";
$result = $wpdb->get_row($query);
// have we got some addresses?
if (!$result->addresses) {
$errors[] = "Please add at least one address.";
}
// do we have at least one default address?
if (!$result->defaultAddress) {
$errors[] = "Please make one of the addresses a default";
}
}
?>
Affiliate Link Options
' . $message . '
';
}
if (sizeof($errors)) {
echo '
' . implode(' ', $errors) . '
';
}
?>
Options
/>
This enables the replacement of text in content with the cloaked affiliate links.
Replacement Order This is the order that the replacements are done.
Max Replacements Maximum number of replacements per post. Setting this to zero will cause all keyword occurances to be replaced.
Search Text This is the text that is searched for in the article content.
Cloaked URL suffix This text will be placed at the end of the cloaked url like this: /recommends/cloaked_url_suffix. This suffix must be unique.
/>
This enables the counting of redirects that are performed.
/>
This puts a no follow tag on the link.
/>
This causes the link to open in a new browser window.
These are http status codes - documentation for them can be found here. This will default to a 302 redirect. If this is set to a 301 redirect, most modern browsers will cache the redirect and any changes to the addresses below will not show for a specific user if they have already following the link.