finish cleaning out main.php

This commit is contained in:
John Bintz 2009-06-30 07:40:23 -04:00
parent 2e1df12bc1
commit 7c923b958e
7 changed files with 305 additions and 189 deletions

View File

@ -7,9 +7,23 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
class ViewMainTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$_GET = array();
_set_current_option('is_admin', true);
$this->m = new PluginWonderfulViewMain();
}
function testRender() {
$m = $this->getMock('PluginWonderfulViewMain', array('_render_memberid_settings', '_render_adbox_information', '_render_instructions', '_create_nonce', '_get_first_adboxid'));
$m->expects($this->once())->method('_render_memberid_settings');
$m->expects($this->once())->method('_render_adbox_information');
$m->expects($this->once())->method('_render_instructions');
$m->expects($this->once())->method('_create_nonce');
$m->expects($this->once())->method('_get_first_adboxid');
$m->render();
}
function testCreateNonce() {
$this->m->_create_nonce();
$result = _get_nonce('plugin-wonderful');
@ -36,6 +50,40 @@ class ViewMainTest extends PHPUnit_Framework_TestCase {
);
}
function providerTestGetFirstAdboxID() {
return array(
array(false, null),
array(
(object)array('template_tag_id' => 'meow'),
"'meow'"
),
array(
(object)array('adboxid' => '123'),
"123"
)
);
}
/**
* @dataProvider providerTestGetFirstAdboxID
*/
function testGetFirstAdboxID($publisher_info, $expected_result) {
global $plugin_wonderful;
$plugin_wonderful = (object)array(
'publisher_info' => false
);
if (is_object($publisher_info)) {
$plugin_wonderful->publisher_info = (object)array(
'adboxes' => array($publisher_info)
);
}
$this->m->_get_first_adboxid();
$this->assertSame($expected_result, $this->m->first_adboxid);
}
/**
* @dataProvider providerTestRenderMemberIDSettings
*/
@ -44,6 +92,8 @@ class ViewMainTest extends PHPUnit_Framework_TestCase {
update_option($key, $value);
}
$this->m->_pw_nonce = "345";
ob_start();
$this->m->_render_memberid_settings();
$source = ob_get_clean();
@ -53,12 +103,91 @@ class ViewMainTest extends PHPUnit_Framework_TestCase {
foreach (array(
'//input[@id="memberid" and @value="123"]' => true,
'//input[@name="_pw_nonce" and @value="345"]' => true,
'//input[@name="use-standardcode" and ' . (($options['plugin-wonderful-use-standardcode'] == 1) ? '@checked' : 'not(@checked)') . ']' => true,
'//input[@name="enable-body-copy-embedding" and ' . (($options['plugin-wonderful-enable-body-copy-embedding'] == 1) ? '@checked' : 'not(@checked)') . ']' => true,
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
function testRenderAdboxInformation() {
global $plugin_wonderful;
$plugin_wonderful = (object)array(
'publisher_info' => false
);
$this->m->_pw_nonce = "345";
foreach (array(false, true) as $has_publisher_info) {
if ($has_publisher_info) {
$plugin_wonderful->publisher_info = (object)array(
'adboxes' => array(
(object)array(
'url' => 'url',
'sitename' => 'sitename',
'description' => 'this is a very long description that is over seventy characters in length and should get an ellipsis',
'adtype' => 'square',
'dimensions' => '1x2',
'adboxid' => '12345',
'template_tag_id' => 'tag',
'in_rss_feed' => 1
)
)
);
}
}
ob_start();
$this->m->_render_adbox_information();
$source = ob_get_clean();
if ($has_publisher_info) {
$this->assertTrue(!empty($source));
$this->assertTrue(($xml = _to_xml($source, true)) !== false);
foreach (array(
'//input[@name="_pw_nonce" and @value="345"]' => true,
'//table[1]/tr[2]/td[1]/a[@href="url" and contains(@title, "url")]' => 'sitename',
'//table[1]/tr[2]/td[2 and contains(text(), "...")]' => true,
'//table[1]/tr[2]/td[3]' => 'square - 1x2',
'//table[1]/tr[2]/td[4]/input[@name="template_tag_id[12345]" and @value="tag"]' => true,
'//table[1]/tr[2]/td[5]/input[@name="in_rss_feed[12345]" and @checked]' => true,
'//table[1]/tr[2]/td[6]/tt' => "the_project_wonderful_ad('tag')"
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
} else {
$this->assertTrue(empty($source));
}
}
function testInstructions() {
global $plugin_wonderful;
$plugin_wonderful->publisher_info = true;
$this->m->_pw_nonce = "345";
$this->m->first_adboxid = "123";
$_GET['allow-destroy'] = 1;
ob_start();
$this->m->_render_instructions();
$source = ob_get_clean();
$this->assertTrue(!empty($source));
$this->assertTrue(($xml = _to_xml($source, true)) !== false);
foreach (array(
'//tt[contains(text(), "the_project_wonderful_ad(123)")]' => true,
'//tt[contains(text(), "PW(123)")]' => true,
'//tt[contains(text(), "PW\(123\)")]' => true,
'//form[@id="allow-destroy-handler"]/input[@name="_pw_nonce" and @value="345"]' => true
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
}
?>

View File

@ -1,17 +1,53 @@
<?php
class PluginWonderfulViewMain {
function render() {
$this->_create_nonce();
$this->_get_first_adboxid();
$this->_render_memberid_settings();
$this->_render_adbox_information();
$this->_render_instructions();
}
function _create_nonce() {
$this->_pw_nonce = wp_create_nonce('plugin-wonderful');
}
function _get_first_adboxid() {
global $plugin_wonderful;
$this->first_adboxid = null;
if ($plugin_wonderful->publisher_info !== false) {
foreach ($plugin_wonderful->publisher_info->adboxes as $adbox) {
if (empty($this->first_adboxid)) {
if (!empty($adbox->template_tag_id)) {
$this->first_adboxid = "'" . $adbox->template_tag_id . "'";
} else {
$this->first_adboxid = $adbox->adboxid;
}
break;
}
}
}
}
function _partial_path($name) {
return dirname(__FILE__) . '/' . __CLASS__ . '/' . $name . '.php';
return dirname(__FILE__) . '/' . __CLASS__ . '/' . $name . '.inc';
}
function _render_memberid_settings() {
include($this->_partial_path('memberid-settings'));
}
function _render_adbox_information() {
global $plugin_wonderful;
include($this->_partial_path('adbox-information'));
}
function _render_instructions() {
global $plugin_wonderful;
include($this->_partial_path('instructions'));
}
}
?>

View File

@ -0,0 +1,51 @@
<?php if (is_admin()) { ?>
<?php if ($plugin_wonderful->publisher_info !== false) { ?>
<h3><?php _e('Adbox Information', 'plugin-wonderful') ?></h3>
<form action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" />
<input type="hidden" name="action" value="change-adbox-settings" />
<table class="widefat post fixed">
<tr>
<th width="12%" class="manage-column"><?php _e('Site Name', 'plugin-wonderful') ?></th>
<th width="13%" class="manage-column"><?php _e('Description', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Size &amp; Dimensions', 'plugin-wonderful') ?></th>
<th width="100" 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="35%" class="manage-column"><?php _e('Raw Template Tag <em>(for direct use in theme)</em>', 'plugin-wonderful') ?></th>
</tr>
<?php
foreach ($plugin_wonderful->publisher_info->adboxes as $adbox) { ?>
<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><input type="text" style="width: 100px" name="template_tag_id[<?php echo $adbox->adboxid ?>]" value="<?php echo $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='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>
<?php } ?>
<?php } ?>

View File

@ -0,0 +1,44 @@
<?php if ($plugin_wonderful->publisher_info !== false) { ?>
<h3><?php _e('Using Widgets to Put Ads on Your Site', 'plugin-wonderful') ?></h3>
<p>
<?php _e('Visit <a href="widgets.php">Appearance -> Widgets</a> to quickly add Project Wonderful advertisements to your site. Having multiple widgets on your site only works in WordPress 2.8 and above.', 'plugin-wonderful') ?>
</p>
<h3><?php _e('Using the Template Tags in Your Theme', 'plugin-wonderful') ?></h3>
<p>
<?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 $this->first_adboxid ?>) ?&gt;
</tt>
<h3><?php _e('Embedding Ads Directly In Body Copy', 'plugin-wonderful') ?></h3>
<p>
<?php _e('In your blog post, use a PW() tag with either the adbox ID or the template tag name to embed the adbox directly in your entry:', 'plugin-wonderful') ?>
</p>
<tt>PW(<?php echo $this->first_adboxid ?>)</tt>
<p><?php _e('If you need to display the PW() code in your post, escape the parenthesis:', 'plugin-wonderful') ?></p>
<tt>PW\(<?php echo $this->first_adboxid ?>\)</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><?php _e('Rebuilding Your Project Wonderful Ad Database', 'plugin-wonderful') ?></h3>
<p>
<?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="allow-destroy-handler" action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" />
<input type="hidden" name="action" value="rebuild-database" />
<input type="submit" value="<?php _e('Destroy and Rebuild Database', 'plugin-wonderful') ?>" class="button" />
</form>
<?php } ?>
<?php } ?>

View File

@ -0,0 +1,44 @@
<?php if (is_admin()) { ?>
<form id="pw-handler" action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $this->_pw_nonce ?>" />
<input type="hidden" name="action" value="change-memberid" />
<table class="form-table">
<tr>
<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><?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"><?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='checked'" : "" ?> />
<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>
<th scope="row"><?php _e('Enable Body Copy Embedding?', 'plugin-wonderful') ?></th>
<td>
<label>
<input type="checkbox"
name="enable-body-copy-embedding"
value="yes"
<?php echo (get_option("plugin-wonderful-enable-body-copy-embedding") == 1) ? "checked='checked'" : "" ?> />
<em><?php _e('(When enabled, you can embed ads directly in body copy using a PW() tag. Read below for more details.)', 'plugin-wonderful') ?></em>
</label>
</td>
</tr> <tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="<?php _e('Change and Redownload Adbox Information', 'plugin-wonderful') ?>" class="button" /> <em>(<?php _e('if you\'ve modified adbox settings on Project Wonderful, just click this button to refresh your adbox code.', 'plugin-wonderful') ?>)</em>
</td>
</tr>
</table>
</form>
<?php } ?>

View File

@ -1,42 +0,0 @@
<form id="pw-handler" action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $nonce ?>" />
<input type="hidden" name="action" value="change-memberid" />
<table class="form-table">
<tr>
<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><?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"><?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='checked'" : "" ?> />
<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>
<th scope="row"><?php _e('Enable Body Copy Embedding?', 'plugin-wonderful') ?></th>
<td>
<label>
<input type="checkbox"
name="enable-body-copy-embedding"
value="yes"
<?php echo (get_option("plugin-wonderful-enable-body-copy-embedding") == 1) ? "checked='checked'" : "" ?> />
<em><?php _e('(When enabled, you can embed ads directly in body copy using a PW() tag. Read below for more details.)', 'plugin-wonderful') ?></em>
</label>
</td>
</tr> <tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="<?php _e('Change and Redownload Adbox Information', 'plugin-wonderful') ?>" class="button" /> <em>(<?php _e('if you\'ve modified adbox settings on Project Wonderful, just click this button to refresh your adbox code.', 'plugin-wonderful') ?>)</em>
</td>
</tr>
</table>
</form>

View File

@ -1,146 +0,0 @@
<?php $nonce = wp_create_nonce('plugin-wonderful'); ?>
<form id="pw-handler" action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $nonce ?>" />
<input type="hidden" name="action" value="change-memberid" />
<table class="form-table">
<tr>
<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><?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"><?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><?php _e('(If you want to use standard code adboxes instead of advanced code, enable this option)', 'plugin-wonderful') ?></em>
</label>
</td>
</tr>
<tr>
<th scope="row"><?php _e('Enable Body Copy Embedding?', 'plugin-wonderful') ?></th>
<td>
<label>
<input type="checkbox"
name="enable-body-copy-embedding"
value="yes"
<?php echo (get_option("plugin-wonderful-enable-body-copy-embedding") == 1) ? "checked" : "" ?> />
<em><?php _e('(When enabled, you can embed ads directly in body copy using a PW() tag. Read below for more details.)', 'plugin-wonderful') ?></em>
</label>
</td>
</tr> <tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="<?php _e('Change and Redownload Adbox Information', 'plugin-wonderful') ?>" class="button" /> <em>(<?php _e('if you\'ve modified adbox settings on Project Wonderful, just click this button to refresh your adbox code.', 'plugin-wonderful') ?>)</em>
</td>
</tr>
</table>
</form>
<?php if ($this->publisher_info !== false) { ?>
<h3><?php _e('Adbox Information', 'plugin-wonderful') ?></h3>
<form action="" method="post">
<input type="hidden" name="_pw_nonce" value="<?php echo $nonce ?>" />
<input type="hidden" name="action" value="change-adbox-settings" />
<table class="widefat post fixed">
<tr>
<th width="12%" class="manage-column"><?php _e('Site Name', 'plugin-wonderful') ?></th>
<th width="13%" class="manage-column"><?php _e('Description', 'plugin-wonderful') ?></th>
<th class="manage-column" align="center"><?php _e('Size &amp; Dimensions', 'plugin-wonderful') ?></th>
<th width="100" 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="35%" 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><input type="text" style="width: 100px" name="template_tag_id[<?php echo $adbox->adboxid ?>]" value="<?php echo $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>
<?php } ?>
<?php if ($this->publisher_info !== false) { ?>
<h3><?php _e('Using Widgets to Put Ads on Your Site', 'plugin-wonderful') ?></h3>
<p>
<?php _e('Visit <a href="widgets.php">Appearance -> Widgets</a> to quickly add Project Wonderful advertisements to your site. Widgets only work in WordPress 2.8 and above.', 'plugin-wonderful') ?>
</p>
<h3><?php _e('Using the Template Tags in Your Theme', 'plugin-wonderful') ?></h3>
<p>
<?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('Embedding Ads Directly In Body Copy', 'plugin-wonderful') ?></h3>
<p>
<?php _e('In your blog post, use a PW() tag with either the adbox ID or the template tag name to embed the adbox directly in your entry:', 'plugin-wonderful') ?>
</p>
<tt>PW(<?php echo $first_adboxid ?>)</tt>
<p><?php _e('If you need to display the PW() code in your post, escape the parenthesis:', 'plugin-wonderful') ?></p>
<tt>PW\(<?php echo $first_adboxid ?>\)</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><?php _e('Rebuilding Your Project Wonderful Ad Database', 'plugin-wonderful') ?></h3>
<p>
<?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="_pw_nonce" value="<?php echo $nonce ?>" />
<input type="hidden" name="action" value="rebuild-database" />
<input type="submit" value="<?php _e('Destroy and Rebuild Database', 'plugin-wonderful') ?>" class="button" />
</form>
<?php } ?>
<?php } ?>