Merge branch 'archive-dropdown'

This commit is contained in:
John Bintz 2009-12-14 21:45:17 -05:00
commit 95c1b7df3a
2 changed files with 187 additions and 61 deletions

View File

@ -58,4 +58,60 @@ class ArchiveDropdownWidgetTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(empty($html)); $this->assertTrue(empty($html));
} }
function testBuildComicArchiveDropdown() {
$w = $this->getMock('ArchiveDropdownWidget', array('_new_comicpressstoryline', '_new_wp_query', 'build_dropdown'));
$storyline = $this->getMock('ComicPressStoryline', array('read_from_options', 'build_from_restrictions'));
$storyline->expects($this->once())->method('read_from_options');
$storyline->expects($this->once())->method('build_from_restrictions')->will($this->returnValue(array(1,2,3)));
$w->expects($this->once())->method('_new_comicpressstoryline')->will($this->returnValue($storyline));
$query = $this->getMock('WP_Query', array('query', 'have_posts', 'next_post'));
$query->expects($this->once())->method('query')->with(array(
'showposts' => -1,
'category__in' => array(1,2,3)
));
wp_insert_post((object)array('ID' => 1, 'guid' => 'guid', 'post_title' => 'title'));
$query->expects($this->at(1))->method('have_posts')->will($this->returnValue(true));
$query->expects($this->at(2))->method('next_post')->will($this->returnValue((object)array('ID' => 1, 'guid' => 'guid', 'post_title' => 'title')));
$query->expects($this->at(3))->method('have_posts')->will($this->returnValue(false));
$w->expects($this->once())->method('_new_wp_query')->will($this->returnValue($query));
$w->expects($this->once())->method('build_dropdown')->with(array('guid' => 'title'));
$w->build_comic_archive_dropdown();
}
function providerTestUpdate() {
$w = new ArchiveDropdownWidget();
$valid_mode = array_shift(array_keys($w->modes));
return array(
array(array(), array()),
array(
array('title' => '<b>test</b>'),
array('title' => 'test'),
),
array(
array('mode' => 'bad'),
array()
),
array(
array('mode' => $valid_mode),
array('mode' => $valid_mode)
)
);
}
/**
* @dataProvider providerTestUpdate
*/
function testUpdate($input, $expected_output) {
$this->assertEquals($expected_output, $this->w->update($input, array()));
}
} }

View File

@ -3,68 +3,41 @@
Widget Name: comicpress archive dropdown Widget Name: comicpress archive dropdown
Widget URI: http://comicpress.org/ Widget URI: http://comicpress.org/
Description: Description:
Author: Philip M. Hofer (Frumph) Author: Philip M. Hofer (Frumph) & John Bintz
Version: 1.04 Version: 1.1
Author URI: http://frumph.net/ Author URI: http://frumph.net/
*/ */
/*
function comicpress_archive_dropdown_storyline() {
$storyline = new ComicPressStoryline();
$storyline->create_structure(get_option('comicpress-storyline-category-order'));
$categories = array_keys($storyline->_structure);
foreach ($categories as $id) {
$post = ComicPressDBInterface::get_instance()->get_first_comic($id);
}
}
*/
function comicpress_archive_dropdown() { ?>
<div class="archive-dropdown-wrap">
<select name="archive-dropdown" class="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Archives...','comicpress')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=-1'); ?>
</select>
</div>
<?php }
function comicpress_archive_dropdown_comics() {
global $post, $wp_query;
$temp_post = $post;
$temp_query = $wp_query;
?>
<div class="archive-dropdown-wrap">
<select name="archive-dropdown" class="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Archives...','comicpress')); ?></option>
<?php $comicArchive = new WP_Query(); $comicArchive->query('showposts=-1&cat='.get_all_comic_categories_as_cat_string());
while ($comicArchive->have_posts()) : $comicArchive->the_post() ?>
<option value="<?php echo get_permalink($post->ID) ?>"><?php the_title() ?></option>
<?php endwhile; ?>
</select>
</div>
<?php
$post = $temp_post; $temp_post = null;
$wp_query = $temp_query; $temp_query = null;
}
class ArchiveDropdownWidget extends WP_Widget { class ArchiveDropdownWidget extends WP_Widget {
var $modes;
function ArchiveDropdownWidget() { function ArchiveDropdownWidget() {
$widget_ops = array('classname' => 'ArchiveDropdownWidget', 'description' => __('Display a dropdown list of your archives, styled.','comicpress') ); $widget_ops = array('classname' => 'ArchiveDropdownWidget', 'description' => __('Display a dropdown list of your archives, styled.','comicpress') );
$this->WP_Widget('archive_dropdown', __('ComicPress Archive Dropdown','comicpress'), $widget_ops); $this->WP_Widget('archive_dropdown', __('ComicPress Archive Dropdown','comicpress'), $widget_ops);
$this->modes = array(
'monthly_archive' => __('Monthly archive', 'comicpress'),
'comic_archive' => __('Comic archive', 'comicpress'),
'storyline_order' => __('Storyline order', 'comicpress'),
);
} }
function _verify_nonce() { return __comicpress_verify_nonce(); } function _verify_nonce() { return __comicpress_verify_nonce(); }
function template_redirect() { function template_redirect() {
if ($this->_verify_nonce() == 'follow-archive-dropdown') { if (ArchiveDropdownWidget::_verify_nonce() == 'follow-archive-dropdown') {
wp_redirect($_GET['cp']['urls']);
} }
} }
/**
* Build a dropdown geared toward quick links to posts.
*/
function build_dropdown($entries) { function build_dropdown($entries) {
if (is_string($entries) || is_array($entries)) { if (is_string($entries) || is_array($entries)) {
$id = 'archive-dropdown-' . md5(rand()); $id = 'archive-dropdown-' . md5(rand());
$button_id = 'archive-dropdown-submit-' . md5(rand());
$nonce = wp_create_nonce('comicpress'); $nonce = wp_create_nonce('comicpress');
$action_nonce = wp_create_nonce('comicpress-follow-archive-dropdown'); $action_nonce = wp_create_nonce('comicpress-follow-archive-dropdown');
@ -88,46 +61,143 @@ class ArchiveDropdownWidget extends WP_Widget {
} }
?> ?>
</select> </select>
<input type="submit" value="<?php echo esc_attr(apply_filters('comicpress_archive_dropdown_submit_button', __('Go', 'comicpress'))) ?>" /> <input id="<?php echo esc_attr($button_id) ?>" type="submit" value="<?php echo esc_attr(apply_filters('comicpress_archive_dropdown_submit_button', __('Go', 'comicpress'))) ?>" />
</form> </form>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
document.getElementById('<?php echo esc_js($button_id) ?>').style.display = 'none';
document.getElementById('<?php echo esc_js($id) ?>').onchange = function(e) {
document.location.href = e.target.options[e.target.selectedIndex].value;
};
</script> </script>
<?php return ob_get_clean(); <?php return ob_get_clean();
} }
return ''; return '';
} }
/**
* Build the monthly archive dropdown.
*/
function build_monthly_archive_dropdown() {
return $this->build_dropdown(wp_get_archives('type=monthly&format=option&show_post_count=-1&echo=0'));
}
function _new_comicpressstoryline() { return new ComicPressStoryline(); }
function _new_comicpressdbinterface() { return new ComicPressDBInterface(); }
function _new_wp_query() { return new WP_Query(); }
/**
* Build the comic archive dropdown.
*/
function build_comic_archive_dropdown() {
$storyline = $this->_new_comicpressstoryline();
$storyline->read_from_options();
$query = $this->_new_wp_query();
$query->query(array(
'showposts' => -1,
'category__in' => $storyline->build_from_restrictions()
));
$results = array();
while($query->have_posts()) {
$post = $query->next_post();
$results[get_permalink($post)] = get_the_title($post);
}
return $this->build_dropdown($results);
}
/**
* Build dropdown based on storyline order.
*/
function build_storyline_order_dropdown() {
$storyline = $this->_new_comicpressstoryline();
$storyline->read_from_options();
$results = array();
if (!empty($storyline->_structure)) {
foreach ($storyline->_structure as $id => $info) {
$category = get_category($id);
if (!empty($category)) {
$results[get_category_link($id)] = str_repeat('&nbsp;', ($info['level'] - 1) * 3) . $category->name;
}
}
}
return $this->build_dropdown($results);
}
/**
* Render the widget.
*/
function widget($args, $instance) { function widget($args, $instance) {
extract($args, EXTR_SKIP); extract($args, EXTR_SKIP);
echo $before_widget; echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; if (!empty($title)) { echo $before_title . $title . $after_title; };
if ($instance['showcomicposts'] == 'on') {
comicpress_archive_dropdown_comics(); if (method_exists($this, "build_{$instance['mode']}_dropdown")) {
} else { echo $this->{"build_{$instance['mode']}_dropdown"}();
comicpress_archive_dropdown();
} }
echo $after_widget; echo $after_widget;
} }
/**
* Update widget parameters.
*/
function update($new_instance, $old_instance) { function update($new_instance, $old_instance) {
$instance = $old_instance; $instance = array();
$instance['title'] = strip_tags($new_instance['title']);
$instance['showcomicposts'] = $new_instance['showcomicposts']; foreach (array('title', 'mode') as $field) {
if (isset($new_instance[$field])) {
switch ($field) {
case 'mode':
if (isset($this->modes[$new_instance[$field]])) {
$instance[$field] = $new_instance[$field];
}
break;
default:
$instance[$field] = strip_tags($new_instance[$field]);
break;
}
}
}
return $instance; return $instance;
} }
/**
* Show the widget editor.
*/
function form($instance) { function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'showcomicposts' => 'off' ) ); $valid_mode = array_shift(array_keys($this->modes));
$title = strip_tags($instance['title']); $instance = wp_parse_args((array)$instance, array('title' => '', 'mode' => $valid_mode));
$showcomicposts = $instance['showcomicposts']; if (empty($showcomicposts)) $showcomicposts = 'off';
?> foreach (array(
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','comicpress'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p> 'title' => __('Title:', 'comicpress'),
<p><label for="<?php echo $this->get_field_id('showcomicposts'); ?>"><strong><?php _e('Show individual comic posts?','comicpress'); ?></strong><br /> 'mode' => __('Show in widget:', 'comicpress')
<input id="<?php echo $this->get_field_id('showcomicposts'); ?>" name="<?php echo $this->get_field_name('showcomicposts'); ?>" type="radio" value="on"<?php if ( $showcomicposts == "on") { echo " checked"; } ?> />On</label>&nbsp;<input id="<?php echo $this->get_field_id('showcomicposts'); ?>" name="<?php echo $this->get_field_name('showcomicposts'); ?>" type="radio" value="off"<?php if ( $showcomicposts == "off") { echo " checked"; } ?> />Off</label></p> ) as $field => $label) { ?>
<?php <p>
<?php switch ($field) {
case 'mode': ?>
<?php echo $label ?><br />
<?php foreach ($this->modes as $mode => $label) { ?>
<label><input type="radio" name="<?php echo $this->get_field_name($field); ?>" value="<?php echo $mode ?>" <?php echo ($mode == $instance['mode']) ? 'checked="checked"' : '' ?> /> <?php echo $label ?></label><br />
<?php }
break;
default: ?>
<label for="<?php echo $this->get_field_id($field); ?>"><?php echo $label ?>
<input class="widefat" id="<?php echo $this->get_field_id($field); ?>" name="<?php echo $this->get_field_name($field); ?>" type="text" value="<?php echo esc_attr($instance[$field]) ?>" />
</label>
<br />
<?php break;
} ?>
</p>
<?php }
} }
} }
add_action('template_redirect', array('ArchiveDropdownWidget', 'template_redirect'));