diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index ac4266d..7201b15 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -2,29 +2,21 @@ class PluginWonderful { var $messages, $adboxes_client, $publisher_info, $member_id; + var $widget_prefix = "plugin-wonderful"; function PluginWonderful() {} + function _retrieve_url($url) { + return @file_get_contents($url); + } + function init() { if (empty($this->adboxes_client)) { $this->messages = array(); $this->adboxes_client = new PWAdboxesClient(); - $this->publisher_info = false; - - if ($member_id = get_option('plugin-wonderful-memberid')) { - $this->publisher_info = $this->adboxes_client->get_ads($member_id); - - if ((get_option('plugin-wonderful-last-update') + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) { - if (($result = file_get_contents(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)get_option('plugin-wonderful-memberid')))) !== false) { - $this->publisher_info = new PublisherInfo(); - if ($this->publisher_info->parse($result)) { - $this->adboxes_client->post_ads($this->publisher_info); - update_option('plugin-wonderful-last-update', time()); - } - } - } - } - + + $this->_get_publisher_info(); + $result = get_option('plugin-wonderful-database-version'); if (empty($result) || ($result < PLUGIN_WONDERFUL_DATABASE_VERSION)) { if ($this->adboxes_client->initialize(true)) { @@ -40,6 +32,35 @@ class PluginWonderful { } } + function _get_new_publisher_info_object() { + return new PublisherInfo(); + } + + function _get_publisher_info() { + $this->publisher_info = false; + $member_id = get_option('plugin-wonderful-memberid'); + if (is_numeric($member_id)) { + $member_id = (int)$member_id; + $this->publisher_info = $this->adboxes_client->get_ads($member_id); + + $last_update = get_option('plugin-wonderful-last-update') ; + if (!is_numeric($last_update)) { $last_update = 0; } + $last_update = (int)$last_update; + + if (($last_update + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) { + if (($result = $this->_retrieve_url(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)get_option('plugin-wonderful-memberid')))) !== false) { + $this->publisher_info = $this->_get_new_publisher_info_object(); + if ($this->publisher_info->parse($result)) { + $this->adboxes_client->post_ads($this->publisher_info); + update_option('plugin-wonderful-last-update', time()); + } + } + } + } + + return $this->publisher_info; + } + function insert_rss_feed_ads($content) { if (is_feed()) { if ($this->publisher_info !== false) { @@ -55,6 +76,15 @@ class PluginWonderful { return $content; } + function get_ad_widget_ordering($adbox_id) { + if (($result = get_option('plugin-wonderful-sidebar-adboxes')) !== false) { + foreach ($result as $position => $id) { + if ($id == $adbox_id) { return $position; } + } + } + return null; + } + function insert_activation_ad() { $result = get_option('plugin-wonderful-activate-ad-code'); if (!empty($result)) { echo $result; } @@ -92,18 +122,6 @@ class PluginWonderful { add_options_page('Plugin Wonderful', __("Plugin Wonderful", 'plugin-wonderful'), 5, __FILE__, array($this, "plugin_wonderful_main")); } - function set_up_widgets() { - if ($this->publisher_info !== false) { - if (($widgets = $this->publisher_info->get_sidebar_widget_info()) !== false) { - 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) { if ($this->publisher_info !== false) { foreach ($this->publisher_info->adboxes as $box) { diff --git a/classes/PluginWonderfulWidget.php b/classes/PluginWonderfulWidget.php new file mode 100644 index 0000000..7918763 --- /dev/null +++ b/classes/PluginWonderfulWidget.php @@ -0,0 +1,24 @@ + 'plugin-wonderful', + 'description' => __('A widget for adding your Project Wonderful advertisements', 'plugin-wonderful') + ); + + $control_options = array( + 'id_base' => 'plugin-wonderful' + ); + + $this->WP_Widget('plugin-wonderful', __('Plugin Wonderful', 'plugin-wonderful'), $widget_options, $control_options); + } + + function widget($args, $instance) { + extract($args); + + + } +} + +?> \ No newline at end of file diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index 773afda..e789524 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -2,12 +2,14 @@ require_once('PHPUnit/Framework.php'); require_once(dirname(__FILE__) . '/../classes/PluginWonderful.php'); +require_once(dirname(__FILE__) . '/../classes/PublisherInfo.php'); require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php'); class PluginWonderfulTest extends PHPUnit_Framework_TestCase { function setUp() { $this->pw = new PluginWonderful(); $_POST = array(); + _reset_wp(); } function testSaveWidgetsIsCalled() { @@ -49,6 +51,96 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $pw->handle_activation(); } + + function providerTestGetPubliserInfo() { + return array( + array( + array( + 'plugin-wonderful-memberid' => "", + ), + false, + false, + false + ), + array( + array( + 'plugin-wonderful-memberid' => 1, + 'plugin-wonderful-last-update' => time() + ), + false, + false, + "~*test*~" + ), + array( + array( + 'plugin-wonderful-memberid' => 1, + 'plugin-wonderful-last-update' => 0 + ), + false, + false, + "~*test*~" + ), + array( + array( + 'plugin-wonderful-memberid' => 1, + 'plugin-wonderful-last-update' => 0 + ), + true, + false, + "~*test-xml*~" + ), + array( + array( + 'plugin-wonderful-memberid' => 1, + 'plugin-wonderful-last-update' => 0 + ), + true, + true, + "~*test-xml*~" + ) + ); + } + + /** + * @dataProvider providerTestGetPubliserInfo + */ + function testGetPublisherInfo($options, $retrieve_url_return, $parse_success, $expected_result) { + foreach ($options as $key => $value) { update_option($key, $value); } + $pw = $this->getMock('PluginWonderful', array('_retrieve_url', '_get_new_publisher_info_object')); + $pw->adboxes_client = $this->getMock('PWAdboxesClient', array('get_ads', 'post_ads')); + + $test_publisher_info = $this->getMock('PublisherInfo'); + $test_xml_publisher_info = $this->getMock('PublisherInfo', array('parse')); + + if (is_numeric($options['plugin-wonderful-memberid'])) { + $pw->adboxes_client->expects($this->once())->method('get_ads')->will($this->returnValue($test_publisher_info)); + + if (($options['plugin-wonderful-last-update'] + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) { + $pw->expects($this->once())->method('_retrieve_url')->will($this->returnValue($retrieve_url_return)); + + if ($retrieve_url_return) { + $pw->expects($this->once())->method('_get_new_publisher_info_object')->will($this->returnValue($test_xml_publisher_info)); + + $test_xml_publisher_info->expects($this->once())->method('parse')->will($this->returnValue($parse_success)); + + if ($parse_success) { + $pw->adboxes_client->expects($this->once())->method('post_ads'); + } else { + $pw->adboxes_client->expects($this->never())->method('post_ads'); + } + } else { + $pw->expects($this->never())->method('_get_new_publisher_info_object'); + } + } else { + $pw->expects($this->never())->method('_retrieve_url'); + } + } + + if ($expected_result == "~*test*~") { $expected_result = $test_publisher_info; } + if ($expected_result == "~*test-xml*~") { $expected_result = $test_xml_publisher_info; } + + $this->assertEquals($expected_result, $pw->_get_publisher_info()); + } } ?> \ No newline at end of file diff --git a/test/PluginWonderfulWidgetTest.php b/test/PluginWonderfulWidgetTest.php new file mode 100644 index 0000000..59c4adc --- /dev/null +++ b/test/PluginWonderfulWidgetTest.php @@ -0,0 +1,25 @@ +w = new PluginWonderfulWidget(); + } + + function testInitialize() { + global $wp_test_expectations; + + $this->w = new PluginWonderfulWidget(); + $this->assertEquals("Plugin Wonderful", $wp_test_expectations['wp_widgets']['plugin-wonderful']['name']); + } + + function testWidget() { + + } +} + +?> \ No newline at end of file