-
Notifications
You must be signed in to change notification settings - Fork 12
/
archive-dropdown.php
222 lines (206 loc) · 9.25 KB
/
archive-dropdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
Widget Name: Comic Archive Dropdown
Widget URI: http://comiceasel.org/
Description: Display a list of links of the latest comics.
Author: Philip M. Hofer (Frumph)
Version: 1.1
*/
class ceo_walker_taxonomy_dropdown extends Walker_CategoryDropdown {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
$pad = str_repeat(' ', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
if( !isset($args['value'])) {
$args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
}
$value = ($args['value']=='slug' ? $category->slug : $category->term_id );
$permalink = '';
if (!isset($args['jumptoarchive'])) $args['jumptoarchive'] = false;
if (!$args['jumptoarchive']) {
$post_args = array(
'numberposts' => 1,
'post_type' => 'comic',
'order' => 'ASC',
'post_status' => 'publish',
'chapters' => $value,
);
$qposts = get_posts( $post_args );
if (is_array($qposts) && !is_wp_error($qposts) && !empty($qposts)) {
$qposts = reset($qposts);
$permalink = get_permalink($qposts->ID);
}
} else $permalink = get_term_link( $value, $category->taxonomy );
$output .= "\t<option class=\"level-$depth\" value=\"".$permalink."\"";
if ( $value === (string) $args['selected'] ) {
$output .= ' selected="selected"';
}
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= ' ('. $category->count .')';
$output .= "</option>\n";
}
}
function ceo_comic_archive_jump_to_chapter($hide = true, $exclude = '', $showcount = false, $jumptoarchive = false, $echo = true) {
ceo_protect();
$args = array(
'walker' => new ceo_walker_taxonomy_dropdown(),
'show_option_all' => __('Select','comiceasel').' '.ucwords(ceo_pluginfo('chapter_type_slug_name')),
'option_none_value' => '-1',
'orderby' => 'menu_order',
'order' => 'ASC',
'name' => ceo_pluginfo('chapter_type_slug_name'),
'show_count' => $showcount,
'hide_empty' => $hide,
'exclude' => $exclude,
'echo' => false,
'hierarchical' => 1,
'taxonomy' => 'chapters',
'hide_if_empty' => $hide,
'value_field' => 'slug',
'jumptoarchive' => $jumptoarchive,
);
$output = '';
$output .= '<form id="chapter-select" class="chapter-select" method="get">'."\r\n";
$select = wp_dropdown_categories( $args );
$replace = '<select$1 onchange="document.location.href=this.options[this.selectedIndex].value;">';
$output .= preg_replace( '#<select([^>]*)>#', $replace, $select );
$output .= "\t<noscript>\r\n";
$output .= "\t\t<input type=\"submit\" value=\"View\" />\r\n";
$output .= "\t</noscript>\r\n";
$output .= "</form>\r\n";
ceo_unprotect();
if ($echo) {
echo $output;
} else return $output;
}
/*
function ceo_comic_archive_jump_to_chapter($unhide = false, $exclude = '', $showcount = false, $jumptoarchive = true, $return = false) {
ceo_protect();
$args = array(
'pad_counts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'hide_empty' => $unhide,
'exclude' => $exclude,
'parent' => 0
);
$parent_chapters = get_terms( 'chapters', $args );
$output = '<form method="get" class="comic-archive-dropdown-form">';
$output .= '<select onchange="document.location.href=this.options[this.selectedIndex].value;">';
$level = 0;
$output .= '<option class="level-select" value="">'.__('Select','comiceasel').' '.ucwords(ceo_pluginfo('chapter_type_slug_name')).'</option>';
if (!is_null($parent_chapters)) {
foreach($parent_chapters as $parent_chapter) {
$thecount = ($showcount) ? ' ('.$parent_chapter->count.')' : '';
if ($parent_chapter->count) {
$permalink = '';
if (!$jumptoarchive) {
$parent_args = array(
'numberposts' => 1,
'post_type' => 'comic',
'order' => 'ASC',
'post_status' => 'publish',
'chapters' => $parent_chapter->slug,
);
$qposts = get_posts( $parent_args );
if (is_array($qposts) && !is_wp_error($qposts) && !empty($qposts)) {
$qposts = reset($qposts);
$permalink = get_permalink($qposts->ID);
}
} else $permalink = get_term_link($parent_chapter->slug, 'chapters');
if (!empty($permalink)) $output .='<option class="level-0" value="'.esc_url($permalink).'">'.$parent_chapter->name.$thecount.'</option>';
} elseif (!$unhide) {
$output .='<option class="level-0" value="" disabled>'.$parent_chapter->name.'</option>';
}
$child_chapters = get_term_children($parent_chapter->term_id, 'chapters');
$args = array(
'pad_counts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'hide_empty' => $unhide,
'child_of' => $parent_chapter->term_id,
'exclude' => array($exclude)
);
$child_chapters = get_terms( 'chapters', $args );
foreach ($child_chapters as $child) {
$child_term = get_term_by( 'id', $child->term_id, 'chapters' );
$thecount = 0;
$thecount = ($showcount) ? '('.$child_term->count.')' : '';
if ($child_term->count) {
$child_args = array(
'numberposts' => 1,
'post_type' => 'comic',
'order' => 'ASC',
'post_status' => 'publish',
'chapters' => $child_term->slug
);
if (!$jumptoarchive) {
$qcposts = get_posts( $child_args );
if (is_array($qcposts)) {
$qcposts = reset($qcposts);
$permalink = get_permalink($qcposts->ID);
}
} else $permalink = get_term_link($child_term->slug, 'chapters');
if (!empty($permalink)) $output .= '<option class="level-1" value="'.esc_url($permalink).'"> '.$child_term->name.' '.$thecount.'</option>';
}
}
}
}
$output .= '</select>';
$output .= '<noscript>';
$output .= '<div><input type="submit" value="View" /></div>';
$output .= '</noscript>';
$output .= '</form>';
if ($return) {
return $output;
} else echo $output;
ceo_unprotect();
}
*/
class ceo_comic_archive_dropdown_widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
__CLASS__, // Base ID
__( 'Comic Easel - Comic Chapters', 'comiceasel' ), // Name
array( 'classname' => __CLASS__, 'description' => __( 'Display dropdown list of comic chapters.', 'comiceasel' ), )
);
}
function widget($args, $instance) {
global $post;
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
if (!isset($instance['hide'])) $instance['hide'] = 1;
ceo_comic_archive_jump_to_chapter($instance['hide'], $instance['exclude'], $instance['showcount'], $instance['jumptoarchive']);
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['exclude'] = strip_tags($new_instance['exclude']);
$instance['hide'] = ($new_instance['hide']) ? 1:0;
$instance['showcount'] = ($new_instance['showcount']) ? 1:0;
$instance['jumptoarchive'] = ($new_instance['jumptoarchive']) ? 1:0;
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'exclude' => '', 'hide' => 1, 'showcount' => 1, 'jumptoarchive' => 0) );
$title = $instance['title'];
$exclude = $instance['exclude'];
$hide = ($instance['hide']) ? 1:0;
$showcount = ($instance['showcount']) ? 1:0;
$jumptoarchive = ($instance['jumptoarchive']) ? 1:0;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','comiceasel'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e('Exclude Chapters (comma seperated):','comiceasel'); ?> <input class="widefat" id="<?php echo $this->get_field_id('exclude'); ?>" name="<?php echo $this->get_field_name('exclude'); ?>" type="text" value="<?php echo esc_attr($exclude); ?>" /></label><br /></p>
<p><label for="<?php echo $this->get_field_id('hide'); ?>"><?php _e('Hide empty chapters?','comiceasel'); ?> <input id="<?php echo $this->get_field_id('hide'); ?>" name="<?php echo $this->get_field_name('hide'); ?>" type="checkbox" value="1" <?php checked(1, $hide); ?> /></label></p>
<p><label for="<?php echo $this->get_field_id('showcount'); ?>"><?php _e('Show the comic count in parenthesis?','comiceasel'); ?> <input id="<?php echo $this->get_field_id('showcount'); ?>" name="<?php echo $this->get_field_name('showcount'); ?>" type="checkbox" value="1" <?php checked(1, $showcount); ?> /></label></p>
<p><label for="<?php echo $this->get_field_id('jumptoarchive'); ?>"><?php _e('Jump to archive and not first page?','comiceasel'); ?> <input id="<?php echo $this->get_field_id('jumptoarchive'); ?>" name="<?php echo $this->get_field_name('jumptoarchive'); ?>" type="checkbox" value="1" <?php checked(1, $jumptoarchive); ?> /></label></p>
<?php
}
}