comicpress-2.8/widgets/ArchiveDropdownWidget.inc

203 lines
6.3 KiB
PHP
Raw Normal View History

2009-12-10 12:38:18 +00:00
<?php
/*
Widget Name: comicpress archive dropdown
Widget URI: http://comicpress.org/
Description:
Author: Philip M. Hofer (Frumph) & John Bintz
Version: 1.1
2009-12-10 12:38:18 +00:00
Author URI: http://frumph.net/
*/
class ArchiveDropdownWidget extends WP_Widget {
2009-12-15 02:19:21 +00:00
var $modes;
2009-12-10 12:38:18 +00:00
function ArchiveDropdownWidget() {
$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);
2009-12-15 02:19:21 +00:00
$this->modes = array(
'monthly_archive' => __('Monthly archive', 'comicpress'),
2009-12-15 02:41:40 +00:00
'comic_archive' => __('Comic archive', 'comicpress'),
'storyline_order' => __('Storyline order', 'comicpress'),
2009-12-15 02:19:21 +00:00
);
2009-12-10 12:38:18 +00:00
}
function _verify_nonce() { return __comicpress_verify_nonce(); }
function template_redirect() {
2009-12-15 02:44:45 +00:00
if (ArchiveDropdownWidget::_verify_nonce() == 'follow-archive-dropdown') {
wp_redirect($_GET['cp']['urls']);
2009-12-10 12:38:18 +00:00
}
}
2009-12-15 02:41:40 +00:00
/**
* Build a dropdown geared toward quick links to posts.
*/
2009-12-10 12:38:18 +00:00
function build_dropdown($entries) {
if (is_string($entries) || is_array($entries)) {
$id = 'archive-dropdown-' . md5(rand());
$button_id = 'archive-dropdown-submit-' . md5(rand());
2009-12-10 12:38:18 +00:00
$nonce = wp_create_nonce('comicpress');
$action_nonce = wp_create_nonce('comicpress-follow-archive-dropdown');
ob_start(); ?>
<div class="archive-dropdown-wrap" id="<?php echo esc_attr($id) ?>">
<form action="" method="get">
<input type="hidden" name="cp[_nonce]" value="<?php echo esc_attr($nonce) ?>" />
<input type="hidden" name="cp[action]" value="follow-archive-dropdown" />
<input type="hidden" name="cp[_action_nonce]" value="<?php echo esc_attr($action_nonce) ?>" />
<select name="cp[urls]">
<option value=""><?php echo esc_html(apply_filters('comicpress_archive_dropdown_default_entry', __('Archives...', 'comicpress'))) ?></option>
<?php
if (is_string($entries)) {
echo $entries;
} else {
foreach ($entries as $url => $value) { ?>
<option value="<?php echo esc_attr($url) ?>"><?php echo esc_html($value) ?></option>
<?php }
}
?>
</select>
<input id="<?php echo esc_attr($button_id) ?>" type="submit" value="<?php echo esc_attr(apply_filters('comicpress_archive_dropdown_submit_button', __('Go', 'comicpress'))) ?>" />
2009-12-10 12:38:18 +00:00
</form>
</div>
<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;
};
2009-12-10 12:38:18 +00:00
</script>
<?php return ob_get_clean();
}
return '';
}
2009-12-15 02:41:40 +00:00
/**
* 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(); }
2009-12-11 04:24:45 +00:00
function _new_wp_query() { return new WP_Query(); }
2009-12-15 02:41:40 +00:00
/**
* Build the comic archive dropdown.
*/
function build_comic_archive_dropdown() {
$storyline = $this->_new_comicpressstoryline();
$storyline->read_from_options();
2009-12-11 04:24:45 +00:00
$query = $this->_new_wp_query();
$query->query(array(
'showposts' => -1,
'category__in' => $storyline->build_from_restrictions()
));
$results = array();
2009-12-15 02:34:41 +00:00
while($query->have_posts()) {
2009-12-11 04:24:45 +00:00
$post = $query->next_post();
$results[get_permalink($post)] = get_the_title($post);
}
2009-12-11 04:24:45 +00:00
return $this->build_dropdown($results);
}
2009-12-15 02:41:40 +00:00
/**
* 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.
*/
2009-12-10 12:38:18 +00:00
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
2009-12-15 02:34:41 +00:00
if (!empty($title)) { echo $before_title . $title . $after_title; };
if (method_exists($this, "build_{$instance['mode']}_dropdown")) {
echo $this->{"build_{$instance['mode']}_dropdown"}();
2009-12-10 12:38:18 +00:00
}
2009-12-15 02:34:41 +00:00
2009-12-10 12:38:18 +00:00
echo $after_widget;
}
2009-12-15 02:41:40 +00:00
/**
* Update widget parameters.
*/
2009-12-10 12:38:18 +00:00
function update($new_instance, $old_instance) {
2009-12-15 02:19:21 +00:00
$instance = array();
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;
}
}
}
2009-12-10 12:38:18 +00:00
return $instance;
}
2009-12-15 02:41:40 +00:00
/**
* Show the widget editor.
*/
2009-12-10 12:38:18 +00:00
function form($instance) {
2009-12-15 02:30:52 +00:00
$valid_mode = array_shift(array_keys($this->modes));
$instance = wp_parse_args((array)$instance, array('title' => '', 'mode' => $valid_mode));
foreach (array(
'title' => __('Title:', 'comicpress'),
'mode' => __('Show in widget:', 'comicpress')
) as $field => $label) { ?>
<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 }
2009-12-10 12:38:18 +00:00
}
}
2009-12-15 02:44:45 +00:00
add_action('template_redirect', array('ArchiveDropdownWidget', 'template_redirect'));