fix database update errors and add individual widget controls

This commit is contained in:
John Bintz 2009-02-20 15:52:57 -05:00
parent 98c86a9afd
commit 6d1c2d88ee
4 changed files with 144 additions and 55 deletions

View File

@ -2,7 +2,7 @@
require_once('PublisherInfo.php'); require_once('PublisherInfo.php');
define("PLUGIN_WONDERFUL_DATABASE_VERSION", 3); define("PLUGIN_WONDERFUL_DATABASE_VERSION", 4);
/** /**
* The interface to the PW database table. * The interface to the PW database table.
@ -29,17 +29,18 @@ class PWAdboxesClient {
array('standardcode', 'text', '', 'NOT NULL'), array('standardcode', 'text', '', 'NOT NULL'),
array('advancedcode', 'text', '', 'NOT NULL'), array('advancedcode', 'text', '', 'NOT NULL'),
array('template_tag_id', 'char', '30', ''), array('template_tag_id', 'char', '30', ''),
array('in_rss_feed', 'int', '1', '') array('in_rss_feed', 'int', '1', ''),
array('center_widget', 'int', '1', '')
); );
} }
/** /**
* Initialize the table if it doesn't exist. * Initialize the table if it doesn't exist.
*/ */
function initialize() { function initialize($force = false) {
global $wpdb; global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE {$this->table_name}") != $this->table_name) { if (($wpdb->get_var("SHOW TABLES LIKE '{$this->table_name}'") != $this->table_name) || $force) {
$sql = "CREATE TABLE {$this->table_name} (\n"; $sql = "CREATE TABLE {$this->table_name} (\n";
$statements = array(); $statements = array();
@ -56,8 +57,12 @@ class PWAdboxesClient {
if (!$wpdb->is_mock) { if (!$wpdb->is_mock) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); dbDelta($sql);
return true;
} }
} }
return false;
} }
/** /**
@ -81,7 +86,7 @@ class PWAdboxesClient {
if ($ads->is_valid) { if ($ads->is_valid) {
$mappings = array(); $mappings = array();
if (is_array($results = $wpdb->get_results("SELECT adboxid, template_tag_id, in_rss_feed FROM {$this->table_name}"))) { if (is_array($results = $wpdb->get_results("SELECT adboxid, template_tag_id, in_rss_feed, center_widget FROM {$this->table_name}"))) {
foreach ($results as $result) { foreach ($results as $result) {
$mappings[$result->adboxid] = $result; $mappings[$result->adboxid] = $result;
} }
@ -105,6 +110,11 @@ class PWAdboxesClient {
$columns[] = $key; $columns[] = $key;
$value = $box->{$key}; $value = $box->{$key};
if (!empty($size)) { $value = substr($value, 0, $size); } if (!empty($size)) { $value = substr($value, 0, $size); }
if (empty($value)) {
switch ($column_type) {
case "int": $value = 0; break;
}
}
$values[] = '"' . $wpdb->escape($value) . '"'; $values[] = '"' . $wpdb->escape($value) . '"';
} }
} }
@ -203,19 +213,30 @@ class PWAdboxesClient {
} }
} }
/** function _handle_toggle($column, $adboxid, $status = false) {
* Enable or disable RSS feed usage.
*/
function set_rss_feed_usage($adboxid, $status = false) {
global $wpdb; global $wpdb;
$query = "UPDATE {$this->table_name} SET "; $query = "UPDATE {$this->table_name} SET ";
$query .= "in_rss_feed = '" . ($status ? 1 : 0) . "'"; $query .= "{$column} = '" . ($status ? 1 : 0) . "'";
$query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'"; $query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'";
$result = $wpdb->get_results($query); $result = $wpdb->get_results($query);
return count($result) > 0; return count($result) > 0;
} }
/**
* Enable or disable RSS feed usage.
*/
function set_rss_feed_usage($adboxid, $status = false) {
return $this->_handle_toggle("in_rss_feed", $adboxid, $status);
}
/**
* Enable or disable widget centering.
*/
function set_widget_centering($adboxid, $status = false) {
return $this->_handle_toggle("center_widget", $adboxid, $status);
}
} }
?> ?>

View File

@ -3,7 +3,7 @@
Plugin Name: Plugin Wonderful Plugin Name: Plugin Wonderful
Plugin URI: http://www.coswellproductions.com/wordpress/wordpress-plugins/ Plugin URI: http://www.coswellproductions.com/wordpress/wordpress-plugins/
Description: Easily embed a Project Wonderful publisher's advertisements. Description: Easily embed a Project Wonderful publisher's advertisements.
Version: 0.4 Version: 0.4.2
Author: John Bintz Author: John Bintz
Author URI: http://www.coswellproductions.org/wordpress/ Author URI: http://www.coswellproductions.org/wordpress/
@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
require_once('classes/PWAdboxesClient.php'); require_once('classes/PWAdboxesClient.php');
//require_once('FirePHPCore/fb.php'); require_once('FirePHPCore/fb.php');
define('PLUGIN_WONDERFUL_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d'); define('PLUGIN_WONDERFUL_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d');
define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours
@ -55,8 +55,11 @@ class PluginWonderful {
$result = get_option('plugin-wonderful-database-version'); $result = get_option('plugin-wonderful-database-version');
if (empty($result) || ($result < PLUGIN_WONDERFUL_DATABASE_VERSION)) { if (empty($result) || ($result < PLUGIN_WONDERFUL_DATABASE_VERSION)) {
$this->adboxes_client->initialize(); if ($this->adboxes_client->initialize(true)) {
update_option('plugin-wonderful-database-version', PLUGIN_WONDERFUL_DATABASE_VERSION); update_option('plugin-wonderful-database-version', PLUGIN_WONDERFUL_DATABASE_VERSION);
} else {
$this->messages[] = "Unable to update database schema!";
}
} }
if (!empty($_POST)) { $this->handle_action(); } if (!empty($_POST)) { $this->handle_action(); }
@ -85,10 +88,14 @@ class PluginWonderful {
foreach ($this->publisher_info->adboxes as $adbox) { foreach ($this->publisher_info->adboxes as $adbox) {
if (($adbox->adboxid == $adboxid) || ($adbox->template_tag_id == $adboxid)) { if (($adbox->adboxid == $adboxid) || ($adbox->template_tag_id == $adboxid)) {
if (get_option("plugin-wonderful-use-standardcode") == 1) { if (get_option("plugin-wonderful-use-standardcode") == 1) {
echo $adbox->standardcode; $output = $adbox->standardcode;
} else { } else {
echo $adbox->advancedcode; $output = $adbox->advancedcode;
} }
if ($adbox->center_widget == 1) {
$output = "<center>{$output}</center>";
}
echo $output;
break; break;
} }
} }
@ -105,11 +112,24 @@ class PluginWonderful {
foreach ($widgets as $widget_info) { foreach ($widgets as $widget_info) {
extract($widget_info); extract($widget_info);
wp_register_sidebar_widget($id, $name, array($this, 'render_widget'), "", $options['adboxid']); wp_register_sidebar_widget($id, $name, array($this, 'render_widget'), "", $options['adboxid']);
register_widget_control($id, array($this, 'render_widget_control'), null, null, $options['adboxid']);
} }
} }
} }
} }
function render_widget_control($adboxid) {
foreach ($this->publisher_info->adboxes as $box) {
if ($box->adboxid == $adboxid) {
echo '<label>';
echo '<input type="checkbox" name="pw[center][' . $adboxid . ']" ' . (($box->center_widget == 1) ? "checked" : "") . ' /> ';
echo 'Wrap ad in &lt;center&gt; tags';
echo '</label>';
break;
}
}
}
function handle_activation() { function handle_activation() {
$this->adboxes_client->initialize(); $this->adboxes_client->initialize();
} }
@ -159,11 +179,30 @@ class PluginWonderful {
function handle_action() { function handle_action() {
$action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['action']))); $action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['action'])));
if (method_exists($this, $action)) { call_user_func(array($this, $action)); } if (method_exists($this, $action)) { call_user_func(array($this, $action)); }
// handle widget updates
if (isset($_POST['save-widgets'])) { $this->handle_action_save_widgets(); }
}
function handle_action_save_widgets() {
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['pw']['center'][$box->adboxid])) {
$this->adboxes_client->set_widget_centering($box->adboxid, true);
$box->center_widget = "1";
} else {
$this->adboxes_client->set_widget_centering($box->adboxid, false);
$box->center_widget = "0";
}
$new_boxes[] = $box;
}
$this->publisher_info->adboxes = $new_boxes;
} }
function handle_action_change_adbox_settings() { function handle_action_change_adbox_settings() {
if ($member_id = get_option('plugin-wonderful-memberid')) { if ($member_id = get_option('plugin-wonderful-memberid')) {
if (isset($_POST['template_tag_id']) && is_array($_POST['template_tag_id'])) { if (isset($_POST['template_tag_id']) && is_array($_POST['template_tag_id'])) {
if (is_array($this->publisher_info->adboxes)) {
$new_boxes = array(); $new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) { foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['template_tag_id'][$box->adboxid])) { if (isset($_POST['template_tag_id'][$box->adboxid])) {
@ -187,7 +226,9 @@ class PluginWonderful {
} }
$this->publisher_info->adboxes = $new_boxes; $this->publisher_info->adboxes = $new_boxes;
} }
}
if (is_array($this->publisher_info->adboxes)) {
$new_boxes = array(); $new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) { foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['in_rss_feed'][$box->adboxid])) { if (isset($_POST['in_rss_feed'][$box->adboxid])) {
@ -207,6 +248,7 @@ class PluginWonderful {
} }
$this->publisher_info->adboxes = $new_boxes; $this->publisher_info->adboxes = $new_boxes;
} }
}
if (count($this->messages) == 0) { if (count($this->messages) == 0) {
$this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful'); $this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful');

View File

@ -3,7 +3,7 @@ Contributors: johncoswell
Tags: ads, sidebar, widget Tags: ads, sidebar, widget
Requires at least: 2.7 Requires at least: 2.7
Tested up to: 2.7.1 Tested up to: 2.7.1
Stable tag: 0.4 Stable tag: 0.4.2
Donate link: http://www.coswellproductions.com/wordpress/wordpress-plugins/ Donate link: http://www.coswellproductions.com/wordpress/wordpress-plugins/
Plugin Wonderful lets Project Wonderful publishers quickly and easily add their adboxes to thier WordPress site. Plugin Wonderful lets Project Wonderful publishers quickly and easily add their adboxes to thier WordPress site.

View File

@ -14,7 +14,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
'dimensions' => "1x1", 'rating' => "a", 'category' => "a", 'dimensions' => "1x1", 'rating' => "a", 'category' => "a",
'description' => "a", 'tags' => 'a', 'standardcode' => 'a', 'description' => "a", 'tags' => 'a', 'standardcode' => 'a',
'advancedcode' => 'a', 'adtype' => 'a', 'template_tag_id' => 'a', 'advancedcode' => 'a', 'adtype' => 'a', 'template_tag_id' => 'a',
'in_rss_feed' => 0); 'in_rss_feed' => 0, 'center_widget' => 0);
} }
function testCreateTables() { function testCreateTables() {
@ -23,7 +23,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$wpdb->prefix = "wp_"; $wpdb->prefix = "wp_";
$wpdb->is_mock = true; $wpdb->is_mock = true;
$wpdb->expects($this->once())->method('get_var')->with($this->equalTo("SHOW TABLES LIKE {$this->database_client->table_name}"))->will($this->returnValue(array())); $wpdb->expects($this->once())->method('get_var')->with($this->equalTo("SHOW TABLES LIKE '{$this->database_client->table_name}'"))->will($this->returnValue(array()));
$this->database_client->initialize(); $this->database_client->initialize();
} }
@ -50,9 +50,27 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$ads->adboxes = array($this->sample_ad); $ads->adboxes = array($this->sample_ad);
$wpdb->expects($this->exactly(13))->method('escape'); $wpdb->expects($this->exactly(14))->method('escape');
$wpdb->expects($this->exactly(2))->method('query')->will($this->returnCallback(array($this, 'postAdsCallback'))); $wpdb->expects($this->exactly(2))->method('query')->will($this->returnCallback(array($this, 'postAdsCallback')));
$wpdb->expects($this->exactly(1))->method('get_results')->will($this->returnValue(array()))->with("SELECT adboxid, template_tag_id, in_rss_feed FROM {$this->database_client->table_name}"); $wpdb->expects($this->exactly(1))->method('get_results')->will($this->returnValue(array()))->with("SELECT adboxid, template_tag_id, in_rss_feed, center_widget FROM {$this->database_client->table_name}");
$this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL);
}
function testPostAdsEmptyColumn() {
global $wpdb;
$wpdb = $this->getMock('wpdb', array('escape', 'query', 'get_results'));
$wpdb->prefix = "wp_";
$ads = $this->getMock('PublisherInfo', array());
$ads->member_id = "1";
$ads->is_valid = true;
$this->sample_ad->in_rss_feed = "";
$ads->adboxes = array($this->sample_ad);
$wpdb->expects($this->exactly(2))->method('query')->will($this->returnCallback(array($this, 'postAdsEmptyColumnCallback')));
$this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL); $this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL);
} }
@ -178,13 +196,13 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
'dimensions', 'rating', 'category', 'dimensions', 'rating', 'category',
'description', 'tags', 'standardcode', 'description', 'tags', 'standardcode',
'advancedcode', 'adtype', 'template_tag_id', 'advancedcode', 'adtype', 'template_tag_id',
'in_rss_feed') as $field) { 'in_rss_feed', 'center_widget') as $field) {
$large_sample_ad[$field] = $field . "-" . str_repeat("x", 300); $large_sample_ad[$field] = $field . "-" . str_repeat("x", 300);
} }
$ads->adboxes = array((object)$large_sample_ad); $ads->adboxes = array((object)$large_sample_ad);
$wpdb->expects($this->exactly(13))->method('escape')->will($this->returnCallback(array($this, 'postDataTooLargeCallback'))); $wpdb->expects($this->exactly(14))->method('escape')->will($this->returnCallback(array($this, 'postDataTooLargeCallback')));
$wpdb->expects($this->exactly(2))->method('query'); $wpdb->expects($this->exactly(2))->method('query');
$wpdb->expects($this->exactly(1))->method('get_results'); $wpdb->expects($this->exactly(1))->method('get_results');
@ -200,6 +218,14 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
} }
} }
function postAdsEmptyColumnCallback($query) {
if (strpos($query, "INSERT") === 0) {
return strpos($query, '"0")') !== false;
} else {
return true;
}
}
function postDataTooLargeCallback($query) { function postDataTooLargeCallback($query) {
$size = $this->database_client->schema_info[$this->escape_count][2]; $size = $this->database_client->schema_info[$this->escape_count][2];
if (!empty($size)) { if (!empty($size)) {