From 8c4d3136f939141f36b4316fcf4f1ec1523cc834 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Feb 2009 20:07:23 -0500 Subject: [PATCH] add rss feed and tagging support --- classes/PWAdboxesClient.php | 96 ++++++++++++++++++++++++++---- plugin-wonderful.php | 76 ++++++++++++++++++++++-- readme.txt | 6 +- test/TestPWAdboxesClient.php | 76 ++++++++++++++++++++++-- views/main.php | 111 +++++++++++++++++++++++------------ 5 files changed, 305 insertions(+), 60 deletions(-) diff --git a/classes/PWAdboxesClient.php b/classes/PWAdboxesClient.php index 9b05e12..ac21887 100644 --- a/classes/PWAdboxesClient.php +++ b/classes/PWAdboxesClient.php @@ -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; + } } ?> \ No newline at end of file diff --git a/plugin-wonderful.php b/plugin-wonderful.php index 15d12bb..da62aaa 100644 --- a/plugin-wonderful.php +++ b/plugin-wonderful.php @@ -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("##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 '
'; - echo 'Plugin Wonderful Version 0.1 by John Bintz | '; + echo 'Plugin Wonderful Version 0.2 by John Bintz | '; echo 'Manage your Project Wonderful publisher account'; echo '
'; echo ''; @@ -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 %1$s set to %2$s.', 'plugin-wonderful'), $box->adboxid, $tag); + } else { + if (!empty($prior_value) && empty($tag)) { + $this->messages[] = sprintf(__('Template tag identifier for ad %s 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 %1$s 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 %1$s 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) { diff --git a/readme.txt b/readme.txt index e756e88..f3ecc19 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -15,4 +15,4 @@ Plugin Wonderful downloads your adbox information from Project Wonderful and cre = Where do I get my member number? = -Log in to your Project Wonderful account and view your profile by clicking on your profile picture on the right. Your member number is the first item listed in your profile. \ No newline at end of file +Log in to your Project Wonderful account and view your profile by clicking on your profile picture on the right. Your member number is the first item listed in your profile. diff --git a/test/TestPWAdboxesClient.php b/test/TestPWAdboxesClient.php index 2198de7..ec7cef4 100644 --- a/test/TestPWAdboxesClient.php +++ b/test/TestPWAdboxesClient.php @@ -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; + } } ?> \ No newline at end of file diff --git a/views/main.php b/views/main.php index 6f3ab26..5d7aea3 100644 --- a/views/main.php +++ b/views/main.php @@ -2,83 +2,118 @@ - + - +
Your member number " /> - (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) +
Use Standard Adboxes?
  - +
publisher_info !== false) { ?> -

Adbox Information

- - - - - - - - - - publisher_info->adboxes as $adbox) { - $first_adboxid = $adbox->adboxid; ?> - - - - - - - - - -
Site NameDescriptionSizeDimensionsCategoryTemplate Tag (for direct use in theme)
sitename ?>description ?>adtype ?>dimensions ?>category ?>the_project_wonderful_ad(adboxid ?>)
+

+
+ + + + + + + + + + + + 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; + } + } ?> + + + + + + + + + + +
(for direct use in theme)', 'plugin-wonderful') ?>
sitename ?> + description) > 70) { + echo substr($adbox->description, 0, 70) . '...'; + } else { + echo $adbox->description; + } + ?> + adtype ?> - dimensions ?>category ?>in_rss_feed) ? " checked" : "" ?> /> + the_project_wonderful_ad(template_tag_id)) { + echo "'" . $adbox->template_tag_id . "'"; + } else { + echo $adbox->adboxid; + } + ?>) +
+
+ +
+
-

Using Widgets to Put Ads on Your Site

+

- Visit Appearance -> Widgets to quickly add Project Wonderful advertisements to your site. Plugin Wonderful widgets start with "PW". + Appearance -> Widgets to quickly add Project Wonderful advertisements to your site. Plugin Wonderful widgets start with "PW".', 'plugin-wonderful') ?>

-

Using the Template Tags in Your Theme

+

- 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 the_project_wonderful_ad() ?> +

(experimental)', 'plugin-wonderful') ?>

+

+ +

+ -

Rebuilding Your Project Wonderful Ad Database

+

- 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. +

- +
\ No newline at end of file