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');
define("PLUGIN_WONDERFUL_DATABASE_VERSION", 3);
define("PLUGIN_WONDERFUL_DATABASE_VERSION", 4);
/**
* The interface to the PW database table.
@ -29,17 +29,18 @@ class PWAdboxesClient {
array('standardcode', 'text', '', 'NOT NULL'),
array('advancedcode', 'text', '', 'NOT NULL'),
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.
*/
function initialize() {
function initialize($force = false) {
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";
$statements = array();
@ -56,8 +57,12 @@ class PWAdboxesClient {
if (!$wpdb->is_mock) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
return true;
}
}
return false;
}
/**
@ -81,7 +86,7 @@ class PWAdboxesClient {
if ($ads->is_valid) {
$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) {
$mappings[$result->adboxid] = $result;
}
@ -105,6 +110,11 @@ class PWAdboxesClient {
$columns[] = $key;
$value = $box->{$key};
if (!empty($size)) { $value = substr($value, 0, $size); }
if (empty($value)) {
switch ($column_type) {
case "int": $value = 0; break;
}
}
$values[] = '"' . $wpdb->escape($value) . '"';
}
}
@ -203,19 +213,30 @@ class PWAdboxesClient {
}
}
/**
* Enable or disable RSS feed usage.
*/
function set_rss_feed_usage($adboxid, $status = false) {
function _handle_toggle($column, $adboxid, $status = false) {
global $wpdb;
$query = "UPDATE {$this->table_name} SET ";
$query .= "in_rss_feed = '" . ($status ? 1 : 0) . "'";
$query .= "{$column} = '" . ($status ? 1 : 0) . "'";
$query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'";
$result = $wpdb->get_results($query);
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 URI: http://www.coswellproductions.com/wordpress/wordpress-plugins/
Description: Easily embed a Project Wonderful publisher's advertisements.
Version: 0.4
Version: 0.4.2
Author: John Bintz
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('FirePHPCore/fb.php');
require_once('FirePHPCore/fb.php');
define('PLUGIN_WONDERFUL_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d');
define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours
@ -55,8 +55,11 @@ class PluginWonderful {
$result = get_option('plugin-wonderful-database-version');
if (empty($result) || ($result < PLUGIN_WONDERFUL_DATABASE_VERSION)) {
$this->adboxes_client->initialize();
update_option('plugin-wonderful-database-version', PLUGIN_WONDERFUL_DATABASE_VERSION);
if ($this->adboxes_client->initialize(true)) {
update_option('plugin-wonderful-database-version', PLUGIN_WONDERFUL_DATABASE_VERSION);
} else {
$this->messages[] = "Unable to update database schema!";
}
}
if (!empty($_POST)) { $this->handle_action(); }
@ -85,10 +88,14 @@ class PluginWonderful {
foreach ($this->publisher_info->adboxes as $adbox) {
if (($adbox->adboxid == $adboxid) || ($adbox->template_tag_id == $adboxid)) {
if (get_option("plugin-wonderful-use-standardcode") == 1) {
echo $adbox->standardcode;
$output = $adbox->standardcode;
} else {
echo $adbox->advancedcode;
$output = $adbox->advancedcode;
}
if ($adbox->center_widget == 1) {
$output = "<center>{$output}</center>";
}
echo $output;
break;
}
}
@ -105,11 +112,24 @@ class PluginWonderful {
foreach ($widgets as $widget_info) {
extract($widget_info);
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() {
$this->adboxes_client->initialize();
}
@ -159,53 +179,75 @@ class PluginWonderful {
function handle_action() {
$action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['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() {
if ($member_id = get_option('plugin-wonderful-memberid')) {
if (isset($_POST['template_tag_id']) && is_array($_POST['template_tag_id'])) {
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['template_tag_id'][$box->adboxid])) {
$tag = $_POST['template_tag_id'][$box->adboxid];
$prior_value = $box->template_tag_id;
if (is_array($this->publisher_info->adboxes)) {
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['template_tag_id'][$box->adboxid])) {
$tag = $_POST['template_tag_id'][$box->adboxid];
$prior_value = $box->template_tag_id;
$tag = $this->adboxes_client->trim_field('template_tag_id', $tag);
$tag = $this->adboxes_client->trim_field('template_tag_id', $tag);
$this->adboxes_client->set_template_tag($box->adboxid, $tag);
$box->template_tag_id = $tag;
$this->adboxes_client->set_template_tag($box->adboxid, $tag);
$box->template_tag_id = $tag;
if (!empty($tag) && ($prior_value != $tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%1$s</strong> set to <strong>%2$s</strong>.', 'plugin-wonderful'), $box->adboxid, $tag);
} else {
if (!empty($prior_value) && empty($tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%s</strong> removed.', 'plugin-wonderful'), $box->adboxid);
if (!empty($tag) && ($prior_value != $tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%1$s</strong> set to <strong>%2$s</strong>.', 'plugin-wonderful'), $box->adboxid, $tag);
} else {
if (!empty($prior_value) && empty($tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%s</strong> removed.', 'plugin-wonderful'), $box->adboxid);
}
}
}
$new_boxes[] = $box;
}
$this->publisher_info->adboxes = $new_boxes;
}
}
if (is_array($this->publisher_info->adboxes)) {
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['in_rss_feed'][$box->adboxid])) {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, true);
if ($box->in_rss_feed == 0) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> enabled.', 'plugin-wonderful'), $box->adboxid);
}
$box->in_rss_feed = "1";
} else {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, false);
if ($box->in_rss_feed == 1) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> disabled.', 'plugin-wonderful'), $box->adboxid);
}
$box->in_rss_feed = "0";
}
$new_boxes[] = $box;
}
$this->publisher_info->adboxes = $new_boxes;
}
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['in_rss_feed'][$box->adboxid])) {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, true);
if ($box->in_rss_feed == 0) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> enabled.', 'plugin-wonderful'), $box->adboxid);
}
$box->in_rss_feed = "1";
} else {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, false);
if ($box->in_rss_feed == 1) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> disabled.', 'plugin-wonderful'), $box->adboxid);
}
$box->in_rss_feed = "0";
}
$new_boxes[] = $box;
}
$this->publisher_info->adboxes = $new_boxes;
}
if (count($this->messages) == 0) {

View File

@ -3,7 +3,7 @@ Contributors: johncoswell
Tags: ads, sidebar, widget
Requires at least: 2.7
Tested up to: 2.7.1
Stable tag: 0.4
Stable tag: 0.4.2
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.

View File

@ -14,7 +14,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
'dimensions' => "1x1", 'rating' => "a", 'category' => "a",
'description' => "a", 'tags' => 'a', 'standardcode' => 'a',
'advancedcode' => 'a', 'adtype' => 'a', 'template_tag_id' => 'a',
'in_rss_feed' => 0);
'in_rss_feed' => 0, 'center_widget' => 0);
}
function testCreateTables() {
@ -23,7 +23,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$wpdb->prefix = "wp_";
$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();
}
@ -50,13 +50,31 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$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(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);
}
function testRetrieveAds() {
global $wpdb;
$wpdb = $this->getMock('wpdb', array('get_results'));
@ -178,13 +196,13 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
'dimensions', 'rating', 'category',
'description', 'tags', 'standardcode',
'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);
}
$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(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) {
$size = $this->database_client->schema_info[$this->escape_count][2];
if (!empty($size)) {