2009-02-07 00:10:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once('PublisherInfo.php');
|
|
|
|
|
2009-02-17 21:54:13 +00:00
|
|
|
define("PLUGIN_WONDERFUL_DATABASE_VERSION", 3);
|
2009-02-13 01:07:23 +00:00
|
|
|
|
2009-02-07 00:10:58 +00:00
|
|
|
/**
|
|
|
|
* The interface to the PW database table.
|
|
|
|
*/
|
|
|
|
class PWAdboxesClient {
|
|
|
|
var $table_name;
|
|
|
|
function PWAdboxesClient() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$this->table_name = $wpdb->prefix . "pw_adboxes";
|
|
|
|
$this->table_exists = false;
|
2009-02-18 02:25:37 +00:00
|
|
|
|
|
|
|
$this->schema_info = array(
|
|
|
|
array('type', 'int', '1', "NOT NULL"),
|
|
|
|
array('adboxid', 'int', '11', "NOT NULL"),
|
|
|
|
array('sitename', 'char', '100', 'NOT NULL'),
|
|
|
|
array('adtype', 'char', '30', 'NOT NULL'),
|
|
|
|
array('url', 'char', '255', 'NOT NULL'),
|
|
|
|
array('dimensions', 'char', '10', 'NOT NULL'),
|
|
|
|
array('rating', 'char', '30', 'NOT NULL'),
|
|
|
|
array('category', 'char', '50', 'NOT NULL'),
|
|
|
|
array('description', 'text', '', 'NOT NULL'),
|
|
|
|
array('tags', 'text', '', 'NOT NULL'),
|
|
|
|
array('standardcode', 'text', '', 'NOT NULL'),
|
|
|
|
array('advancedcode', 'text', '', 'NOT NULL'),
|
|
|
|
array('template_tag_id', 'char', '30', ''),
|
|
|
|
array('in_rss_feed', 'int', '1', '')
|
|
|
|
);
|
2009-02-07 00:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the table if it doesn't exist.
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if ($wpdb->get_var("SHOW TABLES LIKE {$this->table_name}") != $this->table_name) {
|
2009-02-18 02:25:37 +00:00
|
|
|
$sql = "CREATE TABLE {$this->table_name} (\n";
|
|
|
|
|
|
|
|
$statements = array();
|
|
|
|
foreach ($this->schema_info as $info) {
|
|
|
|
list($name, $type, $size, $extra) = $info;
|
|
|
|
$statement = "{$name} {$type}";
|
|
|
|
if (!empty($size)) { $statement .= "({$size})"; }
|
|
|
|
if (!empty($extra)) { $statement .= " {$extra}"; }
|
|
|
|
$statements[] = $statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= implode(",\n", $statements) . ");";
|
|
|
|
|
2009-02-07 00:10:58 +00:00
|
|
|
if (!$wpdb->is_mock) {
|
|
|
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
|
|
|
dbDelta($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the table.
|
|
|
|
*/
|
|
|
|
function destroy() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$wpdb->query("DROP TABLE {$this->table_name}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post PublisherInfo to the database.
|
|
|
|
* @param PublisherInfo $ads The PublisherInfo to post to the database.
|
|
|
|
* @param integer $type The ad type.
|
|
|
|
*/
|
|
|
|
function post_ads($ads, $type = PW_ADBOXES_PROJECT_WONDERFUL) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if (is_a($ads, 'PublisherInfo')) {
|
|
|
|
if ($ads->is_valid) {
|
2009-02-13 01:07:23 +00:00
|
|
|
$mappings = array();
|
|
|
|
|
|
|
|
if (is_array($results = $wpdb->get_results("SELECT adboxid, template_tag_id, in_rss_feed FROM {$this->table_name}"))) {
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$mappings[$result->adboxid] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-07 00:10:58 +00:00
|
|
|
$wpdb->query("DELETE FROM {$this->table_name} WHERE type = $type");
|
|
|
|
foreach ($ads->adboxes as $box) {
|
|
|
|
$columns = array("type");
|
|
|
|
$values = array($type);
|
|
|
|
|
2009-02-13 01:07:23 +00:00
|
|
|
if (isset($mappings[$box->adboxid])) {
|
|
|
|
foreach ((array)$mappings[$box->adboxid] as $key => $value) {
|
|
|
|
$box->{$key} = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 02:25:37 +00:00
|
|
|
foreach ($this->schema_info as $info) {
|
|
|
|
list($key, $column_type, $size, $extra) = $info;
|
|
|
|
|
2009-02-07 00:10:58 +00:00
|
|
|
if ($key !== "type") {
|
|
|
|
$columns[] = $key;
|
2009-02-18 02:25:37 +00:00
|
|
|
$value = $box->{$key};
|
|
|
|
if (!empty($size)) { $value = substr($value, 0, $size); }
|
2009-02-07 00:10:58 +00:00
|
|
|
$values[] = '"' . $wpdb->escape($value) . '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 02:25:37 +00:00
|
|
|
$sql = "INSERT INTO {$this->table_name} (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")";
|
|
|
|
|
|
|
|
if (!$wpdb->query($sql)) {
|
2009-02-13 01:07:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-07 00:10:58 +00:00
|
|
|
}
|
2009-02-13 01:07:23 +00:00
|
|
|
return true;
|
2009-02-07 00:10:58 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-13 01:07:23 +00:00
|
|
|
|
|
|
|
return false;
|
2009-02-07 00:10:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-13 01:07:23 +00:00
|
|
|
function _handle_ad_retrieval($member_id, $query) {
|
2009-02-07 00:10:58 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if (count($results = $wpdb->get_results($query)) > 0) {
|
|
|
|
$ads = new PublisherInfo();
|
|
|
|
$ads->memberid = $member_id;
|
|
|
|
$ads->adboxes = $results;
|
|
|
|
$ads->is_valid = true;
|
|
|
|
|
|
|
|
return $ads;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-13 01:07:23 +00:00
|
|
|
/**
|
|
|
|
* Retrieve all ads from the database and create a new PublisherInfo object.
|
|
|
|
* @param integer $member_id The Project Wonderful member ID to use.
|
|
|
|
* @return PublisherInfo The PublisherInfo object for the ads, or false if no ads are found.
|
|
|
|
*/
|
|
|
|
function get_ads($member_id, $type = null) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$query = "SELECT * FROM {$this->table_name}";
|
|
|
|
if (!is_null($type)) { $query .= " WHERE type = {$type}"; }
|
|
|
|
$query .= " ORDER BY adboxid ASC";
|
|
|
|
|
|
|
|
return $this->_handle_ad_retrieval($member_id, $query);
|
|
|
|
}
|
|
|
|
|
2009-02-07 00:10:58 +00:00
|
|
|
/**
|
|
|
|
* Remove all ads from the database.
|
|
|
|
*/
|
|
|
|
function clean_ads() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$wpdb->query("DELETE FROM {$this->table_name}");
|
|
|
|
}
|
2009-02-13 01:07:23 +00:00
|
|
|
|
2009-02-18 02:25:37 +00:00
|
|
|
function trim_field($field, $value) {
|
|
|
|
foreach ($this->schema_info as $info) {
|
|
|
|
list($key, $type, $size, $extra) = $info;
|
|
|
|
if ($key == $field) {
|
|
|
|
$value = substr($value, 0, $size); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2009-02-13 01:07:23 +00:00
|
|
|
/**
|
|
|
|
* Set the template tag id for an advertisement.
|
|
|
|
*/
|
|
|
|
function set_template_tag($adboxid, $tag) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2009-02-18 02:25:37 +00:00
|
|
|
$tag = $this->trim_field('template_tag_id', $tag);
|
|
|
|
|
2009-02-13 01:07:23 +00:00
|
|
|
$query = "UPDATE {$this->table_name} SET ";
|
|
|
|
$query .= "template_tag_id = '" . $wpdb->escape($tag) . "'";
|
|
|
|
$query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'";
|
|
|
|
$query .= " ORDER BY adboxid ASC";
|
|
|
|
|
|
|
|
$result = $wpdb->get_results($query);
|
|
|
|
return count($result) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an adbox by template tag id.
|
|
|
|
*/
|
|
|
|
function get_ad_by_template_tag($member_id, $tag) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$query = "SELECT * FROM {$this->table_name} WHERE template_tag_id = '" . $wpdb->escape($tag) . "'";
|
|
|
|
|
|
|
|
if (($result = $this->_handle_ad_retrieval($member_id, $query)) !== false) {
|
|
|
|
return reset($result->adboxes);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable or disable RSS feed usage.
|
|
|
|
*/
|
|
|
|
function set_rss_feed_usage($adboxid, $status = false) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$query = "UPDATE {$this->table_name} SET ";
|
|
|
|
$query .= "in_rss_feed = '" . ($status ? 1 : 0) . "'";
|
|
|
|
$query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'";
|
|
|
|
|
|
|
|
$result = $wpdb->get_results($query);
|
|
|
|
return count($result) > 0;
|
|
|
|
}
|
2009-02-07 00:10:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 21:54:13 +00:00
|
|
|
?>
|