adsTable = $wpdb->prefix."AdMangler_ads";
$this->settingsTable = $wpdb->prefix."AdMangler_settings";
} // End function AdMangler
function AdminMenu()
{
add_menu_page('AdMangler Settings', 'AdMangler', 9, __FILE__, array($this, 'CreateAdminPage'), '/'. PLUGINDIR . '/admangler/images/logo.gif');
add_submenu_page(__FILE__, 'AdMangler Settings', 'Settings', 9, 'settings', array($this, 'CreateAdminPage'));
add_submenu_page(__FILE__, 'AdMangler Settings', 'Banners', 9, 'banners', array($this, 'CreateAdminPage'));
} // End function AdminMenu
function Activate()
{
global $wpdb;
// Plugin database table version
$db_version = "0.0.1"; // You must increment this if we change the database other wise leave it alone
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_ads (
id INT(11) NOT NULL AUTO_INCREMENT,
advertiser VARCHAR(256) COLLATE utf8_bin NOT NULL DEFAULT 'admin',
width INT(11) NOT NULL,
height INT(11) NOT NULL,
active BOOL NOT NULL DEFAULT 0,
approved BOOL NOT NULL DEFAULT 0,
base BOOL NOT NULL DEFAULT 0,
type VARCHAR(5) COLLATE utf8_bin NOT NULL DEFAULT 'image',
code TEXT COLLATE utf8_bin,
url VARCHAR(256) COLLATE utf8_bin NOT NULL DEFAULT 'http://".$_SERVER['SERVER_NAME']."',
UNIQUE KEY id (id)
);";
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_settings (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(256) COLLATE utf8_bin NOT NULL,
value VARCHAR(256) COLLATE utf8_bin NOT NULL,
UNIQUE KEY id (id),
PRIMARY KEY name (name)
);";
// Installed plugin database table version
$installed_ver = get_option('AdMangler_db_version');
// If the database has changed, update the structure while preserving data
if (empty($installed_ver) || $db_version != $installed_ver)
{
require_once ABSPATH . "wp-admin/includes/upgrade.php";
foreach($sql as $temp)
dbDelta($temp);
add_option('AdMangler_db_version', $db_version);
}
return true;
} // End function Activate
function CreateAdminPage()
{
echo "
AdMangler Admin
";
switch($_GET['page'])
{
case 'settings':
include_once "forms/settings.php";
break;
case 'banners':
include_once "forms/banners.php";
break;
default:
include_once "forms/dashboard.php";
break;
}
echo "";
} // End function CreateAdminPage
function FilterTheContent($content)
{
global $wpdb;
$sql = "SELECT a.width, b.height FROM ".$wpdb->prefix."AdMangler_ads as a, ".$wpdb->prefix."AdMangler_ads as b ";
$sql .= "WHERE a.height = b.height GROUP by b.height, a.width";
$results = $wpdb->get_results($sql);
foreach ($results as $banner)
{
$content = str_replace("[AdMangler:".$banner->width."x".$banner->height."]", $this->GetAds($banner->width, $banner->height), $content);
}
$content = str_replace('[AdMangler:BuyForm]', '', $content);
return $content;
} // End function FilterTheContent
function FormatAd($type,$code)
{
switch($type)
{
case 'html':
$code = $code;
}
return $code;
} //End function FormatAd
function GetAdById($id, $return)
{
global $wpdb;
$sql = "SELECT * FROM $this->adsTable WHERE id=".intval($id);
$row = $wpdb->get_row($sql);
$str = $this->FormatAd($row->type, $row->code);
if ($return) return $str; else echo $str;
} // End function GetAdById
function GetAds($width=468, $height=60, $return=true)
{
global $wpdb;
$str = "";
if (!isset($this->banners[$width."x".$height]))
{
$sql = "SELECT type,code FROM $this->adsTable WHERE width=$width and height=$height and active and approved and NOT base ORDER BY RAND()";
$results = $wpdb->get_results($sql);
if ($results)
$this->banners[$width."x".$height] = $results;
else
{
$sql = "SELECT type,code FROM $this->adsTable WHERE width=$width and height=$height and active and approved and base ORDER BY RAND()";
$results = $wpdb->get_results($sql);
if ($results)
$this->banners[$width."x".$height] = $results;
}
}
if (is_array($this->banners[$width."x".$height]))
{
$banner = array_shift($this->banners[$width."x".$height]);
array_push($this->banners[$width."x".$height], $banner);
$str = $this->FormatAd($banner->type, $banner->code);
}
if ($return) return $str; else echo $str;
} // End function GetAds
function RegisterWidgets()
{
register_widget('AdManglerWidget'); // This adds the Widget to the backend
}
} // End class AdMangler
?>