diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index cb62ced..47f506c 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -25,13 +25,20 @@ class PluginWonderful { * Initialize the object if it isn't already. */ function init() { + global $wp_version; + if (empty($this->adboxes_client)) { $this->messages = array(); $this->adboxes_client = new PWAdboxesClient(); $this->_get_publisher_info(); $this->_update_database_version(); - + + if ($wp_version < 2.8) { + register_sidebar_widget(__('Plugin Wonderful', 'plugin-wonderful'), array($this, 'render_pre28_widget')); + register_widget_control(__('Plugin Wonderful', 'plugin-wonderful'), array($this, 'render_pre28_widget_control')); + } + if (!empty($_POST)) { $this->handle_action(); } } } @@ -120,7 +127,7 @@ class PluginWonderful { } function plugin_wonderful_main() { - $this->get_view(__FUNCTION__); + $this->show_view(new PluginWonderfulViewMain()); } function show_messages() { @@ -131,16 +138,8 @@ class PluginWonderful { } } - function _create_target($name, $source) { - return dirname(__FILE__) . "/../{$source}/{$name}.php"; - } - - function _include($target) { include($target); } - function _file_exists($target) { return @file_exists($target); } - - function get_view($function_name) { - $target = $this->_create_target(str_replace('plugin_wonderful_', '', $function_name), "views"); - if ($this->_file_exists($target)) { + function show_view($view) { + if (is_object($view) && method_exists($view, 'render')) { $info = get_plugin_data(realpath(__FILE__)); echo '
'; @@ -149,7 +148,7 @@ class PluginWonderful { $this->show_messages(); - $this->_include($target); + $view->render(); echo '
'; echo '
'; @@ -159,14 +158,14 @@ class PluginWonderful { echo '
'; echo '
'; } else { - echo __("View not found: ", 'plugin-wonderful') . str_replace('plugin_wonderful_', '', $function_name); + var_dump($view); } } function handle_action() { if (!empty($_POST['_pw_nonce'])) { if (wp_verify_nonce($_POST['_pw_nonce'], 'plugin-wonderful')) { - $action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['action']))); + $action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['_pw_action']))); if (method_exists($this, $action)) { call_user_func(array($this, $action)); } } } @@ -329,6 +328,78 @@ class PluginWonderful { } } } + + function _render_adbox_admin($instance, $field_names) { + if ($this->publisher_info !== false) { + echo '

'; + echo 'Select an adbox:
'; + foreach ($this->publisher_info->adboxes as $box) { + echo '"; + echo "
"; + } + echo '

'; + + echo '

'; + echo ''; + echo '

'; + } + } + + function render_pre28_widget() { + $data = get_option('plugin-wonderful-pre28-widget-info'); + if (is_array($data)) { + if (count(array_intersect(array_keys($data), array("adboxid", "center"))) == 2) { + if ($this->publisher_info !== false) { + foreach ($this->publisher_info->adboxes as $adbox) { + if ($adbox->adboxid == $data['adboxid']) { + $this->_render_adbox($data['adboxid'], !empty($data['center'])); + } + } + } + } + } + } + + function _normalize_pre28_option() { + $instance = array( + 'adboxid' => false, + 'center' => 0 + ); + + $data = get_option('plugin-wonderful-pre28-widget-info'); + if (is_array($data)) { + foreach ($data as $field => $value) { + if (isset($instance[$field])) { + if (is_numeric($value)) { + $instance[$field] = $value; + } + } + } + } + + update_option('plugin-wonderful-pre28-widget-info', $instance); + return $instance; + } + + function render_pre28_widget_control() { + $instance = $this->_normalize_pre28_option(); + + echo ''; + echo ''; + $this->_render_adbox_admin($instance, array('adboxid' => 'pw[adboxid]', 'center' => 'pw[center]')); + } } function the_project_wonderful_ad($adboxid) { diff --git a/classes/PluginWonderfulWidget.php b/classes/PluginWonderfulWidget.php index 60bb1d7..e9ceded 100644 --- a/classes/PluginWonderfulWidget.php +++ b/classes/PluginWonderfulWidget.php @@ -22,32 +22,13 @@ if (class_exists('WP_Widget')) { function form($instance) { global $plugin_wonderful; - - if ($plugin_wonderful->publisher_info !== false) { - echo '

'; - echo 'Select an adbox:
'; - foreach ($plugin_wonderful->publisher_info->adboxes as $box) { - echo '"; - echo "
"; - } - echo '

'; - - echo '

'; - echo ''; - echo '

'; - } + $plugin_wonderful->_render_adbox_admin( + $instance, + array( + 'adboxid' => $this->get_field_name('adboxid'), + 'center' => $this->get_field_name('center') + ) + ); } function update($new_instance, $old_instance) { diff --git a/plugin-wonderful.php b/plugin-wonderful.php index 8fdd19f..e02b7a2 100644 --- a/plugin-wonderful.php +++ b/plugin-wonderful.php @@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ foreach (glob(dirname(__FILE__) . '/classes/*.php') as $file) { require_once($file); } -// require_once('FirePHPCore/fb.php'); +foreach (glob(dirname(__FILE__) . '/views/*.php') as $file) { require_once($file); } define('PLUGIN_WONDERFUL_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d'); define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours @@ -43,7 +43,9 @@ add_action('admin_menu', array($plugin_wonderful, 'set_up_menu')); add_action('init', array($plugin_wonderful, 'init')); add_filter('the_excerpt_rss', array($plugin_wonderful, 'insert_rss_feed_ads')); add_filter('the_content', array($plugin_wonderful, 'inject_ads_into_body_copy')); -add_action('widgets_init', '__plugin_wonderful_load_widgets'); +if ($wp_version >= 2.8) { + add_action('widgets_init', '__plugin_wonderful_load_widgets'); +} register_activation_hook(__FILE__, array($plugin_wonderful, 'handle_activation')); diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index 6dd5226..0474ea4 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -182,17 +182,18 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_body, $this->pw->inject_ads_into_body_copy("body")); } - function providerTestGetView() { + function providerTestShowView() { return array( - array("**bad**", false), - array("**good**", true), + array(null, false), + array((object)array(), false), + array($this->getMock('Test', array('render')), true) ); } /** - * @dataProvider providerTestGetView + * @dataProvider providerTestShowView */ - function testGetView($function_extension, $file_exists) { + function testShowView($class, $is_success) { global $wp_test_expectations; $wp_test_expectations['plugin_data'][realpath(dirname(__FILE__) . '/../classes/PluginWonderful.php')] = array( 'Title' => '**title**', @@ -200,17 +201,11 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { 'Author' => '**author**' ); - $pw = $this->getMock('PluginWonderful', array('_create_target', '_include', '_file_exists')); - - $pw->expects($this->once())->method("_file_exists")->will($this->returnValue($file_exists)); - ob_start(); - $pw->get_view("plugin_wonderful_" . $function_extension); + $this->pw->show_view($class); $source = ob_get_clean(); - - $this->assertEquals($file_exists, strpos($source, $function_extension) === false); - - if ($file_exists) { + + if ($is_success) { foreach (array("title", "version", "author") as $name) { $this->assertTrue(strpos($source, "**${name}**") !== false); } @@ -233,7 +228,7 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { _set_valid_nonce('plugin-wonderful', $has_verify_nonce ? '12345' : '54321'); $pw = $this->getMock('PluginWonderful', $method_exists ? array('handle_action_test') : array('handle_action_invalid')); - $_POST['action'] = 'test'; + $_POST['_pw_action'] = 'test'; if ($method_exists) { $pw->expects($this->once())->method('handle_action_test'); @@ -459,6 +454,112 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $this->pw->_render_adbox($requested_adboxid, $center_widget); $this->assertEquals($expected_result, ob_get_clean()); } + + function testRenderAdboxAdmin() { + $this->pw->publisher_info->adboxes = array( + (object)array('adboxid' => '123'), + (object)array('adboxid' => '234'), + (object)array('adboxid' => '345'), + ); + + ob_start(); + $this->pw->_render_adbox_admin(array('adboxid' => '123', 'center' => 1), array('adboxid' => 'adname', 'center' => 'centername')); + $source = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($source)) !== false); + + foreach (array( + '//input[@type="radio" and @name="adname" and @value="123" and @checked="checked"]' => true, + '//input[@type="radio" and @name="adname" and @value="234" and not(@checked="checked")]' => true, + '//input[@type="radio" and @name="adname" and @value="345" and not(@checked="checked")]' => true, + '//input[@type="checkbox" and @name="centername" and @value="1" and @checked="checked"]' => true + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } + + function providerTestRenderPre28Widget() { + return array( + array(false, false), + array(array('blah' => 'yadda'), false), + array(array('adboxid' => '1', 'center' => 1), false), + array(array('adboxid' => '123', 'center' => 1), true) + ); + } + + /** + * @dataProvider providerTestRenderPre28Widget + */ + function testRenderPre28Widget($option_value, $success) { + update_option('plugin-wonderful-pre28-widget-info', $option_value); + + $this->pw->publisher_info->adboxes = array( + (object)array('adboxid' => '123'), + ); + + ob_start(); + $this->pw->render_pre28_widget(); + $source = ob_get_clean(); + + $this->assertEquals($success, !empty($source)); + } + + function testRenderPre28WidgetControl() { + update_option('plugin-wonderful-pre28-widget-info', array('adboxid' => 123, 'center' => 1)); + + $this->pw->publisher_info->adboxes = array( + (object)array('adboxid' => '123'), + ); + + ob_start(); + $this->pw->render_pre28_widget_control(); + $source = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($source)) !== false); + + foreach (array( + '//input[@name="_pw_nonce"]' => true, + '//input[@name="pw[adboxid]"]' => true, + '//input[@name="pw[center]"]' => true, + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } + + function providerTestNormalizePre28Option() { + return array( + array( + false, + array('adboxid' => false, 'center' => 0) + ), + array( + array(), + array('adboxid' => false, 'center' => 0) + ), + array( + array('adboxid' => 'meow'), + array('adboxid' => false, 'center' => 0) + ), + array( + array('adboxid' => '123'), + array('adboxid' => '123', 'center' => 0) + ), + ); + } + + /** + * @dataProvider providerTestNormalizePre28Option + */ + function testNormalizePre28Option($option_value, $expected_result) { + update_option('plugin-wonderful-pre28-widget-info', $option_value); + + $this->assertEquals($expected_result, $this->pw->_normalize_pre28_option()); + $this->assertEquals($expected_result, get_option('plugin-wonderful-pre28-widget-info')); + } + + function testHandlePre28WidgetUpdate() { + $this->markTestIncomplete(); + } } ?> diff --git a/test/PluginWonderfulWidgetTest.php b/test/PluginWonderfulWidgetTest.php index d3e3896..a26a0ab 100644 --- a/test/PluginWonderfulWidgetTest.php +++ b/test/PluginWonderfulWidgetTest.php @@ -27,28 +27,10 @@ class PluginWonderfulWidgetTest extends PHPUnit_Framework_TestCase { function testRenderWidgetControl() { global $plugin_wonderful; - $plugin_wonderful = $this->getMock('PluginWonderful'); + $plugin_wonderful = $this->getMock('PluginWonderful', array('_render_adbox_admin')); + $plugin_wonderful->expects($this->once())->method('_render_adbox_admin'); - $plugin_wonderful->publisher_info->adboxes = array( - (object)array('adboxid' => '123'), - (object)array('adboxid' => '234'), - (object)array('adboxid' => '345'), - ); - - ob_start(); - $this->w->form(array('adboxid' => '123', 'center' => 1)); - $source = ob_get_clean(); - - $this->assertTrue(($xml = _to_xml($source)) !== false); - - foreach (array( - '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="123" and @checked="checked"]' => true, - '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="234" and not(@checked="checked")]' => true, - '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="345" and not(@checked="checked")]' => true, - '//input[@type="checkbox" and @name="' . $this->w->get_field_name('center') . '" and @value="1" and @checked="checked"]' => true - ) as $xpath => $value) { - $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); - } + $this->w->form(array()); } function testUpdateWidget() { diff --git a/views/PluginWonderfulViewMain/adbox-information.inc b/views/PluginWonderfulViewMain/adbox-information.inc index 72393c8..8573d6a 100644 --- a/views/PluginWonderfulViewMain/adbox-information.inc +++ b/views/PluginWonderfulViewMain/adbox-information.inc @@ -3,7 +3,7 @@

- + diff --git a/views/PluginWonderfulViewMain/memberid-settings.inc b/views/PluginWonderfulViewMain/memberid-settings.inc index 02f09f0..e2c2f15 100644 --- a/views/PluginWonderfulViewMain/memberid-settings.inc +++ b/views/PluginWonderfulViewMain/memberid-settings.inc @@ -1,7 +1,7 @@ - +