pre 2.8 widget work

This commit is contained in:
John Bintz 2009-06-30 22:50:55 -04:00
parent 7c923b958e
commit 8ad3163afc
7 changed files with 218 additions and 81 deletions

View File

@ -25,6 +25,8 @@ class PluginWonderful {
* Initialize the object if it isn't already. * Initialize the object if it isn't already.
*/ */
function init() { function init() {
global $wp_version;
if (empty($this->adboxes_client)) { if (empty($this->adboxes_client)) {
$this->messages = array(); $this->messages = array();
$this->adboxes_client = new PWAdboxesClient(); $this->adboxes_client = new PWAdboxesClient();
@ -32,6 +34,11 @@ class PluginWonderful {
$this->_get_publisher_info(); $this->_get_publisher_info();
$this->_update_database_version(); $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(); } if (!empty($_POST)) { $this->handle_action(); }
} }
} }
@ -120,7 +127,7 @@ class PluginWonderful {
} }
function plugin_wonderful_main() { function plugin_wonderful_main() {
$this->get_view(__FUNCTION__); $this->show_view(new PluginWonderfulViewMain());
} }
function show_messages() { function show_messages() {
@ -131,16 +138,8 @@ class PluginWonderful {
} }
} }
function _create_target($name, $source) { function show_view($view) {
return dirname(__FILE__) . "/../{$source}/{$name}.php"; if (is_object($view) && method_exists($view, 'render')) {
}
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)) {
$info = get_plugin_data(realpath(__FILE__)); $info = get_plugin_data(realpath(__FILE__));
echo '<div class="wrap">'; echo '<div class="wrap">';
@ -149,7 +148,7 @@ class PluginWonderful {
$this->show_messages(); $this->show_messages();
$this->_include($target); $view->render();
echo '<div style="margin-top: 20px; border-top: solid #E3E3E3 1px; overflow: hidden">'; echo '<div style="margin-top: 20px; border-top: solid #E3E3E3 1px; overflow: hidden">';
echo '<form style="float: right; display: inline" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="3215507"><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt=""><img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>'; echo '<form style="float: right; display: inline" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="3215507"><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt=""><img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>';
@ -159,14 +158,14 @@ class PluginWonderful {
echo '</div>'; echo '</div>';
echo '</div>'; echo '</div>';
} else { } else {
echo __("View not found: ", 'plugin-wonderful') . str_replace('plugin_wonderful_', '', $function_name); var_dump($view);
} }
} }
function handle_action() { function handle_action() {
if (!empty($_POST['_pw_nonce'])) { if (!empty($_POST['_pw_nonce'])) {
if (wp_verify_nonce($_POST['_pw_nonce'], 'plugin-wonderful')) { 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)); } 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 '<p>';
echo 'Select an adbox:<br />';
foreach ($this->publisher_info->adboxes as $box) {
echo '<label>';
echo '<input type="radio" name="'
. $field_names['adboxid']
. '" value="'
. $box->adboxid
. '" '
. (($instance['adboxid'] == $box->adboxid) ? 'checked="checked"' : "")
. ' />';
echo $box->adtype . " " . $box->dimensions . " (" . $box->adboxid . ")";
echo "</label>";
echo "<br />";
}
echo '</p>';
echo '<p>';
echo '<label>';
echo '<input type="checkbox" value="1" name="' . $field_names['center'] . '" ' . (($instance['center'] == 1) ? 'checked="checked"' : "") . ' /> ';
echo 'Wrap ad in &lt;center&gt; tags';
echo '</label>';
echo '</p>';
}
}
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 '<input type="hidden" name="_pw_nonce" value="' . wp_create_nonce('plugin-wonderful') . '" />';
echo '<input type="hidden" name="_pw_action" value="update-pre28-widget" />';
$this->_render_adbox_admin($instance, array('adboxid' => 'pw[adboxid]', 'center' => 'pw[center]'));
}
} }
function the_project_wonderful_ad($adboxid) { function the_project_wonderful_ad($adboxid) {

View File

@ -22,32 +22,13 @@ if (class_exists('WP_Widget')) {
function form($instance) { function form($instance) {
global $plugin_wonderful; global $plugin_wonderful;
$plugin_wonderful->_render_adbox_admin(
if ($plugin_wonderful->publisher_info !== false) { $instance,
echo '<p>'; array(
echo 'Select an adbox:<br />'; 'adboxid' => $this->get_field_name('adboxid'),
foreach ($plugin_wonderful->publisher_info->adboxes as $box) { 'center' => $this->get_field_name('center')
echo '<label>'; )
echo '<input type="radio" name="' );
. $this->get_field_name('adboxid')
. '" value="'
. $box->adboxid
. '" '
. (($instance['adboxid'] == $box->adboxid) ? 'checked="checked"' : "")
. ' />';
echo $box->adtype . " " . $box->dimensions . " (" . $box->adboxid . ")";
echo "</label>";
echo "<br />";
}
echo '</p>';
echo '<p>';
echo '<label>';
echo '<input type="checkbox" value="1" name="' . $this->get_field_name('center') . '" ' . (($instance['center'] == 1) ? 'checked="checked"' : "") . ' /> ';
echo 'Wrap ad in &lt;center&gt; tags';
echo '</label>';
echo '</p>';
}
} }
function update($new_instance, $old_instance) { function update($new_instance, $old_instance) {

View File

@ -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); } 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_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d');
define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours 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_action('init', array($plugin_wonderful, 'init'));
add_filter('the_excerpt_rss', array($plugin_wonderful, 'insert_rss_feed_ads')); 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_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')); register_activation_hook(__FILE__, array($plugin_wonderful, 'handle_activation'));

View File

@ -182,17 +182,18 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_body, $this->pw->inject_ads_into_body_copy("body")); $this->assertEquals($expected_body, $this->pw->inject_ads_into_body_copy("body"));
} }
function providerTestGetView() { function providerTestShowView() {
return array( return array(
array("**bad**", false), array(null, false),
array("**good**", true), 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; global $wp_test_expectations;
$wp_test_expectations['plugin_data'][realpath(dirname(__FILE__) . '/../classes/PluginWonderful.php')] = array( $wp_test_expectations['plugin_data'][realpath(dirname(__FILE__) . '/../classes/PluginWonderful.php')] = array(
'Title' => '**title**', 'Title' => '**title**',
@ -200,17 +201,11 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
'Author' => '**author**' '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(); ob_start();
$pw->get_view("plugin_wonderful_" . $function_extension); $this->pw->show_view($class);
$source = ob_get_clean(); $source = ob_get_clean();
$this->assertEquals($file_exists, strpos($source, $function_extension) === false); if ($is_success) {
if ($file_exists) {
foreach (array("title", "version", "author") as $name) { foreach (array("title", "version", "author") as $name) {
$this->assertTrue(strpos($source, "**${name}**") !== false); $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'); _set_valid_nonce('plugin-wonderful', $has_verify_nonce ? '12345' : '54321');
$pw = $this->getMock('PluginWonderful', $method_exists ? array('handle_action_test') : array('handle_action_invalid')); $pw = $this->getMock('PluginWonderful', $method_exists ? array('handle_action_test') : array('handle_action_invalid'));
$_POST['action'] = 'test'; $_POST['_pw_action'] = 'test';
if ($method_exists) { if ($method_exists) {
$pw->expects($this->once())->method('handle_action_test'); $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->pw->_render_adbox($requested_adboxid, $center_widget);
$this->assertEquals($expected_result, ob_get_clean()); $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();
}
} }
?> ?>

View File

@ -27,28 +27,10 @@ class PluginWonderfulWidgetTest extends PHPUnit_Framework_TestCase {
function testRenderWidgetControl() { function testRenderWidgetControl() {
global $plugin_wonderful; 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( $this->w->form(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);
}
} }
function testUpdateWidget() { function testUpdateWidget() {

View File

@ -3,7 +3,7 @@
<h3><?php _e('Adbox Information', 'plugin-wonderful') ?></h3> <h3><?php _e('Adbox Information', 'plugin-wonderful') ?></h3>
<form action="" method="post"> <form action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" /> <input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" />
<input type="hidden" name="action" value="change-adbox-settings" /> <input type="hidden" name="_pw_action" value="change-adbox-settings" />
<table class="widefat post fixed"> <table class="widefat post fixed">
<tr> <tr>
<th width="12%" class="manage-column"><?php _e('Site Name', 'plugin-wonderful') ?></th> <th width="12%" class="manage-column"><?php _e('Site Name', 'plugin-wonderful') ?></th>

View File

@ -1,7 +1,7 @@
<?php if (is_admin()) { ?> <?php if (is_admin()) { ?>
<form id="pw-handler" action="" method="post"> <form id="pw-handler" action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" /> <input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" />
<input type="hidden" name="action" value="change-memberid" /> <input type="hidden" name="_pw_action" value="change-memberid" />
<table class="form-table"> <table class="form-table">
<tr> <tr>
<th scope="row"><?php _e('Your member number', 'plugin-wonderful') ?></th> <th scope="row"><?php _e('Your member number', 'plugin-wonderful') ?></th>