add rss feed and tagging support

This commit is contained in:
John Bintz 2009-02-12 20:07:23 -05:00
parent 41a7d9d782
commit 8c4d3136f9
5 changed files with 305 additions and 60 deletions

View File

@ -2,6 +2,8 @@
require_once('PublisherInfo.php');
define("PLUGIN_WONDERFUL_DATABASE_VERSION", 2);
/**
* The interface to the PW database table.
*/
@ -34,7 +36,9 @@ class PWAdboxesClient {
description text NOT NULL,
tags text NOT NULL,
standardcode text NOT NULL,
advancedcode text NOT NULL
advancedcode text NOT NULL,
template_tag_id char(30),
in_rss_feed int(1)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
@ -61,11 +65,25 @@ class PWAdboxesClient {
if (is_a($ads, 'PublisherInfo')) {
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}"))) {
foreach ($results as $result) {
$mappings[$result->adboxid] = $result;
}
}
$wpdb->query("DELETE FROM {$this->table_name} WHERE type = $type");
foreach ($ads->adboxes as $box) {
$columns = array("type");
$values = array($type);
if (isset($mappings[$box->adboxid])) {
foreach ((array)$mappings[$box->adboxid] as $key => $value) {
$box->{$key} = $value;
}
}
foreach ((array)$box as $key => $value) {
if ($key !== "type") {
$columns[] = $key;
@ -73,23 +91,20 @@ class PWAdboxesClient {
}
}
$wpdb->query("INSERT INTO {$this->table_name} (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")");
if (!$wpdb->query("INSERT INTO {$this->table_name} (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")")) {
return false;
}
}
return true;
}
}
return false;
}
/**
* 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) {
function _handle_ad_retrieval($member_id, $query) {
global $wpdb;
$query = "SELECT * FROM {$this->table_name}";
if (!is_null($type)) { $query .= " WHERE type = {$type}"; }
if (count($results = $wpdb->get_results($query)) > 0) {
$ads = new PublisherInfo();
$ads->memberid = $member_id;
@ -102,6 +117,21 @@ class PWAdboxesClient {
return false;
}
/**
* 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);
}
/**
* Remove all ads from the database.
*/
@ -110,6 +140,50 @@ class PWAdboxesClient {
$wpdb->query("DELETE FROM {$this->table_name}");
}
/**
* Set the template tag id for an advertisement.
*/
function set_template_tag($adboxid, $tag) {
global $wpdb;
$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;
}
}
?>

View File

@ -3,7 +3,7 @@
Plugin Name: Plugin Wonderful
Plugin URI: http://www.coswellproductions.com
Description: Easily embed a Project Wonderful publisher's advertisements.
Version: 0.1
Version: 0.2
Author: John Bintz
Author URI: http://www.coswellproductions.org/wordpress/
@ -41,14 +41,31 @@ class PluginWonderful {
$this->publisher_info = $this->adboxes_client->get_ads($member_id);
}
$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 (!empty($_POST)) { $this->handle_action(); }
}
function insert_rss_feed_ads($content) {
if (is_feed()) {
foreach ($this->publisher_info->adboxes as $adbox) {
if ($adbox->in_rss_feed == 1) {
if (preg_match("#<noscript>(.*)</noscript>#mis", $adbox->advancedcode, $matches) > 0) {
echo $matches[1];
}
}
}
}
}
function render_widget($options, $adboxid) {
if ($this->publisher_info !== false) {
foreach ($this->publisher_info->adboxes as $adbox) {
if ($adbox->adboxid == $adboxid) {
if (($adbox->adboxid == $adboxid) || ($adbox->template_tag_id == $adboxid)) {
if (get_option("plugin-wonderful-use-standardcode") == 1) {
echo $adbox->standardcode;
} else {
@ -107,7 +124,7 @@ class PluginWonderful {
include($target);
echo '<div style="margin-top: 20px; border-top: solid #E3E3E3 1px">';
echo 'Plugin Wonderful Version 0.1 by <a href="mailto:john@coswellproductions.com">John Bintz</a> | ';
echo 'Plugin Wonderful Version 0.2 by <a href="http://www.coswellproductions.com/wordpress/">John Bintz</a> | ';
echo '<a href="http://www.projectwonderful.com/login.php">Manage your Project Wonderful publisher account</a>';
echo '</div>';
echo '</div>';
@ -121,6 +138,55 @@ class PluginWonderful {
if (method_exists($this, $action)) { call_user_func(array($this, $action)); }
}
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;
$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);
}
}
}
$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) {
$this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful');
}
}
function handle_action_rebuild_database() {
$this->adboxes_client->destroy();
$this->adboxes_client->initialize();
@ -183,6 +249,8 @@ $plugin_wonderful = new PluginWonderful();
add_action('admin_menu', array($plugin_wonderful, 'set_up_menu'));
add_action('init', array($plugin_wonderful, 'set_up_widgets'));
add_filter('the_excerpt_rss', array($plugin_wonderful, 'insert_rss_feed_ads'));
register_activation_hook(__FILE__, array($plugin_wonderful, 'handle_activation'));
function the_project_wonderful_ad($adboxid) {

View File

@ -2,8 +2,8 @@
Contributors: johncoswell
Tags: ads, sidebar, widget
Requires at least: 2.7
Tested up to: 2.7
Stable tag: 0.1
Tested up to: 2.7.1
Stable tag: 0.2
Plugin Wonderful lets Project Wonderful publishers quickly and easily add their adboxes to thier WordPress blog.

View File

@ -13,7 +13,8 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$this->sample_ad = (object)array('adboxid' => 1, 'sitename' => "a", 'url' => "http://meow.raow/",
'dimensions' => "1x1", 'rating' => "a", 'category' => "a",
'description' => "a", 'tags' => 'a', 'standardcode' => 'a',
'advancedcode' => 'a', 'adtype' => 'a');
'advancedcode' => 'a', 'adtype' => 'a', 'template_tag_id' => 'a',
'in_rss_feed' => 0);
}
function testCreateTables() {
@ -40,7 +41,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
function testPostAds() {
global $wpdb;
$wpdb = $this->getMock('wpdb', array('escape', 'query'));
$wpdb = $this->getMock('wpdb', array('escape', 'query', 'get_results'));
$wpdb->prefix = "wp_";
$ads = $this->getMock('PublisherInfo', array());
@ -49,8 +50,9 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$ads->adboxes = array($this->sample_ad);
$wpdb->expects($this->exactly(11))->method('escape');
$wpdb->expects($this->exactly(13))->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}");
$this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL);
}
@ -113,7 +115,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
function testFilterTypeFromAd() {
global $wpdb;
$wpdb = $this->getMock('wpdb', array('escape', 'query'));
$wpdb = $this->getMock('wpdb', array('escape', 'query', 'get_results'));
$wpdb->prefix = "wp_";
$ads = $this->getMock('PublisherInfo', array());
@ -128,6 +130,39 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL);
}
function testUseTemplateTags() {
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;
$ads->adboxes = array($this->sample_ad);
$test_tag = "my_tag";
$wpdb->expects($this->any())->method('escape')->will($this->returnCallback(array($this, 'useTemplateTagsEscapeCallback')));
$wpdb->expects($this->any())->method('query')->will($this->returnCallback(array($this, 'useTemplateTagsCallback')));
$wpdb->expects($this->any())->method('get_results')->will($this->returnCallback(array($this, 'useTemplateTagsCallback')));
$this->assertTrue($this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL));
$this->target_ad = $this->sample_ad;
$this->target_ad->template_tag_id = $test_tag;
$this->assertFalse($this->database_client->set_template_tag(0, $test_tag));
$this->assertTrue($this->database_client->set_template_tag(1, $test_tag));
$this->assertEquals($this->target_ad, $this->database_client->get_ad_by_template_tag(1, $test_tag));
$this->has_set_template_tag = true;
$this->assertTrue($this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL));
$this->assertEquals($this->target_ad, $this->database_client->get_ad_by_template_tag(1, $test_tag));
}
function postAdsCallback($query) {
if (strpos($query, "DELETE") === 0) {
return $query == ("DELETE FROM {$this->database_client->table_name} WHERE type = " . PW_ADBOXES_PROJECT_WONDERFUL);
@ -143,6 +178,39 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
return true;
}
}
function useTemplateTagsCallback($query) {
if (strpos($query, "adboxid = '0'") !== false) {
return array();
}
if (strpos($query, "SELECT * FROM pw_adboxes") !== false) {
return array($this->target_ad);
}
if (strpos($query, "SELECT adboxid, template_tag_id, in_rss_feed") !== false) {
if ($this->has_set_template_tag) {
$info = new stdClass();
$info->adboxid = "1";
$info->template_tag_id = "my_tag";
$info->in_rss_feed = "0";
return array($info);
} else {
return array();
}
}
if (strpos($query, "INSERT INTO pw_adboxes") !== false) {
if ($this->has_set_template_tag) {
return (strpos($query, "my_tag") > 0) ? 1 : false;
} else {
return 1;
}
}
return array("item");
}
function useTemplateTagsEscapeCallback($value) {
return $value;
}
}
?>

View File

@ -2,83 +2,118 @@
<input type="hidden" name="action" value="change-memberid" />
<table class="form-table">
<tr>
<th scope="row">Your member number</th>
<th scope="row"><?php _e('Your member number', 'plugin-wonderful') ?></th>
<td>
<input id="memberid" name="memberid" value="<?php echo get_option("plugin-wonderful-memberid") ?>" />
<em>(you can find your member number by logging in to Project Wonderful and clicking on your profile image in the upper right of the page)</em>
<em><?php _e('(you can find your member number by logging in to Project Wonderful and clicking on your profile image in the upper right of the page)', 'plugin-wonderful') ?></em>
</td>
</tr>
<tr>
<th scope="row">Use Standard Adboxes?</th>
<th scope="row"><?php _e('Use Standard Adboxes?', 'plugin-wonderful') ?></th>
<td>
<label>
<input type="checkbox"
name="use-standardcode"
value="yes"
<?php echo (get_option("plugin-wonderful-use-standardcode") == 1) ? "checked" : "" ?> />
<em>(If you want to use standard code adboxes instead of advanced code, enable this option)</em>
<em><?php _e('(If you want to use standard code adboxes instead of advanced code, enable this option)', 'plugin-wonderful') ?></em>
</label>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Change" class="button" />
<input type="submit" value="<?php _e('Change', 'plugin-wonderful') ?>" class="button" />
</td>
</tr>
</table>
</form>
<?php if ($this->publisher_info !== false) { ?>
<h3>Adbox Information</h3>
<table class="widefat post fixed">
<tr>
<th width="20%" class="manage-column">Site Name</th>
<th width="30%" class="manage-column">Description</th>
<th class="manage-column" align="center">Size</th>
<th class="manage-column" align="center">Dimensions</th>
<th class="manage-column" align="center">Category</th>
<th style="text-align: right !important" width="25%" class="manage-column">Template Tag <em>(for direct use in theme)</em></th>
</tr>
<?php
$first_adboxid = null;
foreach ($this->publisher_info->adboxes as $adbox) {
$first_adboxid = $adbox->adboxid; ?>
<tr>
<td><a href="<?php echo $adbox->url ?>" target="_top" title="Ad for use on <?php echo $adbox->url ?> (opens in new window)"><?php echo $adbox->sitename ?></a></td>
<td><?php echo $adbox->description ?></td>
<td><?php echo $adbox->adtype ?></td>
<td><?php echo $adbox->dimensions ?></td>
<td><?php echo $adbox->category ?></td>
<td align="right"><tt>the_project_wonderful_ad(<?php echo $adbox->adboxid ?>)</tt></td>
</tr>
<?php
}
?>
</table>
<h3><?php _e('Adbox Information', 'plugin-wonderful') ?></h3>
<form action="" method="post">
<input type="hidden" name="action" value="change-adbox-settings" />
<table class="widefat post fixed">
<tr>
<th width="15%" class="manage-column"><?php _e('Site Name', 'plugin-wonderful') ?></th>
<th width="15%" class="manage-column"><?php _e('Description', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Size &amp; Dimensions', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Category', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Template Tag Identifier', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Use in RSS Feed?', 'plugin-wonderful') ?></th>
<th style="text-align: right !important" width="25%" class="manage-column"><?php _e('Raw Template Tag <em>(for direct use in theme)</em>', 'plugin-wonderful') ?></th>
</tr>
<?php
$first_adboxid = null;
foreach ($this->publisher_info->adboxes as $adbox) {
if (empty($first_adboxid)) {
if (!empty($adbox->template_tag_id)) {
$first_adboxid = "'" . $adbox->template_tag_id . "'";
} else {
$first_adboxid = $adbox->adboxid;
}
} ?>
<tr>
<td><a href="<?php echo $adbox->url ?>" target="_top" title="<?php printf(__('Ad for use on %s (opens in new window)', 'plugin-wonderful'), $adbox->url) ?>"><?php echo $adbox->sitename ?></a></td>
<td>
<?php
if (strlen($adbox->description) > 70) {
echo substr($adbox->description, 0, 70) . '...';
} else {
echo $adbox->description;
}
?>
</td>
<td><?php echo $adbox->adtype ?> - <?php echo $adbox->dimensions ?></td>
<td><?php echo $adbox->category ?></td>
<td><input type="text" size="8" name="template_tag_id[<?php echo $adbox->adboxid ?>]" value="<?= $adbox->template_tag_id ?>" /></td>
<td align="center"><input type="checkbox" name="in_rss_feed[<?php echo $adbox->adboxid ?>]" value="yes" <?php echo !empty($adbox->in_rss_feed) ? " checked" : "" ?> /></td>
<td align="right">
<tt>the_project_wonderful_ad(<?php
if (!empty($adbox->template_tag_id)) {
echo "'" . $adbox->template_tag_id . "'";
} else {
echo $adbox->adboxid;
}
?>)</tt>
</td>
</tr>
<?php
}
?>
</table>
<div style="text-align: center">
<input type="submit" class="button" value="<?php _e('Submit Adbox Changes', 'plugin-wonderful') ?>" />
</div>
</form>
<h3>Using Widgets to Put Ads on Your Site</h3>
<h3><?php _e('Using Widgets to Put Ads on Your Site', 'plugin-wonderful') ?></h3>
<p>
Visit <a href="widgets.php">Appearance -> Widgets</a> to quickly add Project Wonderful advertisements to your site. Plugin Wonderful widgets start with &quot;PW&quot;.
<?php _e('Visit <a href="widgets.php">Appearance -> Widgets</a> to quickly add Project Wonderful advertisements to your site. Plugin Wonderful widgets start with &quot;PW&quot;.', 'plugin-wonderful') ?>
</p>
<h3>Using the Template Tags in Your Theme</h3>
<h3><?php _e('Using the Template Tags in Your Theme', 'plugin-wonderful') ?></h3>
<p>
Find the location in your theme where you want the ad to appear. Type in the template tag for that ad, surrounded in PHP tags, like this:
<?php _e('Find the location in your theme where you want the ad to appear. Type in the template tag for that ad, surrounded in PHP tags, like this:', 'plugin-wonderful') ?>
</p>
<tt>
&lt;?php the_project_wonderful_ad(<?php echo $first_adboxid ?>) ?&gt;
</tt>
<h3><?php _e('Inserting Ads Into Your RSS Feeds <em>(experimental)</em>', 'plugin-wonderful') ?></h3>
<p>
<?php _e('You can insert your Project Wonderful ads into you RSS feeds. The ads you insert into your feed also need to be crawlable by the Project Wonderful ad checking robot, so it\'s recommended that you put ads into your RSS feed that you\'re already showing on your site. Not all RSS feed readers support displaying the embedded ads.', 'plugin-wonderful') ?>
</p>
<?php if (isset($_GET['allow-destroy'])) { ?>
<h3>Rebuilding Your Project Wonderful Ad Database</h3>
<h3><?php _e('Rebuilding Your Project Wonderful Ad Database', 'plugin-wonderful') ?></h3>
<p>
If you are having issues with your ads not downloading correctly from Project Wonderful, click this button to destroy and rebuild the database that stores ad info.
<?php _e('If you are having issues with your ads not downloading correctly from Project Wonderful, click this button to destroy and rebuild the database that stores ad info.', 'plugin-wonderful') ?>
</p>
<form id="pw-handler" action="" method="post">
<input type="hidden" name="action" value="rebuild-database" />
<input type="submit" value="Destroy and Rebuild Database" class="button" />
<input type="submit" value="<?php _e('Destroy and Rebuild Database', 'plugin-wonderful') ?>" class="button" />
</form>
<?php } ?>
<?php } ?>