| 1 |
<?php |
| 2 |
/** |
| 3 |
* Custom Archive Widget |
| 4 |
* |
| 5 |
* @package Essential_Widgets |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Custom Archive Widget |
| 11 |
*/ |
| 12 |
class EW_Archives extends WP_Widget { |
| 13 |
|
| 14 |
/** |
| 15 |
* Holds widget settings defaults, populated in constructor. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected $defaults; |
| 20 |
|
| 21 |
function __construct() { |
| 22 |
|
| 23 |
// Set up defaults. |
| 24 |
$this->defaults = array( |
| 25 |
'title' => esc_attr__( 'Archives', 'essential-widgets' ), |
| 26 |
'limit' => 10, |
| 27 |
'type' => 'monthly', |
| 28 |
'post_type' => 'post', |
| 29 |
'order' => 'DESC', |
| 30 |
'format' => 'html', |
| 31 |
'before' => '', |
| 32 |
'after' => '', |
| 33 |
'show_post_count' => false, |
| 34 |
); |
| 35 |
|
| 36 |
$widget_ops = array( |
| 37 |
'classname' => 'essential-widgets widget_archive ew-archive ewarchive', |
| 38 |
'description' => esc_html__( 'Displays a list of categories', 'essential-widgets' ), |
| 39 |
); |
| 40 |
|
| 41 |
$control_ops = array( |
| 42 |
'id_base' => 'ew-archive', |
| 43 |
); |
| 44 |
|
| 45 |
parent::__construct( |
| 46 |
'ew-archive', // Base ID |
| 47 |
__( 'EW: Archives', 'essential-widgets' ), // Name |
| 48 |
$widget_ops, |
| 49 |
$control_ops |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
function form($instance) { |
| 54 |
// Merge the user-selected arguments with the defaults. |
| 55 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 56 |
|
| 57 |
// Get post types. |
| 58 |
$post_types = get_post_types( array( 'name' => 'post' ), 'objects' ); |
| 59 |
$post_types = array_merge( $post_types, get_post_types( array( 'publicly_queryable' => true, '_builtin' => false ), 'objects' ) ); |
| 60 |
|
| 61 |
// Create an array of archive types. |
| 62 |
$type = array( |
| 63 |
'alpha' => esc_attr__( 'Alphabetical', 'essential-widgets' ), |
| 64 |
'daily' => esc_attr__( 'Daily', 'essential-widgets' ), |
| 65 |
'monthly' => esc_attr__( 'Monthly', 'essential-widgets' ), |
| 66 |
'postbypost' => esc_attr__( 'Post By Post', 'essential-widgets' ), |
| 67 |
'weekly' => esc_attr__( 'Weekly', 'essential-widgets' ), |
| 68 |
'yearly' => esc_attr__( 'Yearly', 'essential-widgets' ) |
| 69 |
); |
| 70 |
|
| 71 |
// Create an array of order options. |
| 72 |
$order = array( |
| 73 |
'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ), |
| 74 |
'DESC' => esc_attr__( 'Descending', 'essential-widgets' ) |
| 75 |
); |
| 76 |
|
| 77 |
// Create an array of archive formats. |
| 78 |
$format = array( |
| 79 |
'custom' => esc_attr__( 'Plain', 'essential-widgets' ), |
| 80 |
'html' => esc_attr__( 'List', 'essential-widgets' ), |
| 81 |
'option' => esc_attr__( 'Dropdown', 'essential-widgets' ) |
| 82 |
); ?> |
| 83 |
|
| 84 |
<p> |
| 85 |
<label> |
| 86 |
<?php esc_html_e( 'Title:', 'essential-widgets' ); ?> |
| 87 |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" /> |
| 88 |
</label> |
| 89 |
</p> |
| 90 |
|
| 91 |
<p> |
| 92 |
<label> |
| 93 |
<?php esc_html_e( 'Limit:', 'essential-widgets' ); ?></label> |
| 94 |
<input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" value="<?php echo esc_attr( $instance['limit'] ); ?>" placeholder="10" /> |
| 95 |
</label> |
| 96 |
</p> |
| 97 |
|
| 98 |
<p> |
| 99 |
<label> |
| 100 |
<?php esc_html_e( 'Type:', 'essential-widgets' ); ?> |
| 101 |
|
| 102 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>"> |
| 103 |
|
| 104 |
<?php foreach ( $type as $option_value => $option_label ) : ?> |
| 105 |
|
| 106 |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['type'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 107 |
|
| 108 |
<?php endforeach; ?> |
| 109 |
|
| 110 |
</select> |
| 111 |
</label> |
| 112 |
</p> |
| 113 |
|
| 114 |
<p> |
| 115 |
<label> |
| 116 |
<?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?> |
| 117 |
|
| 118 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>"> |
| 119 |
|
| 120 |
<?php foreach ( $post_types as $post_type ) : ?> |
| 121 |
|
| 122 |
<option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option> |
| 123 |
|
| 124 |
<?php endforeach; ?> |
| 125 |
|
| 126 |
</select> |
| 127 |
</label> |
| 128 |
</p> |
| 129 |
|
| 130 |
<p> |
| 131 |
<label> |
| 132 |
<?php esc_html_e( 'Order:', 'essential-widgets' ); ?> |
| 133 |
|
| 134 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>"> |
| 135 |
|
| 136 |
<?php foreach ( $order as $option_value => $option_label ) : ?> |
| 137 |
|
| 138 |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 139 |
|
| 140 |
<?php endforeach; ?> |
| 141 |
|
| 142 |
</select> |
| 143 |
</label> |
| 144 |
</p> |
| 145 |
|
| 146 |
<p> |
| 147 |
<label> |
| 148 |
<?php esc_html_e( 'Display as:', 'essential-widgets' ); ?></label> |
| 149 |
|
| 150 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>"> |
| 151 |
|
| 152 |
<?php foreach ( $format as $option_value => $option_label ) : ?> |
| 153 |
|
| 154 |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['format'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 155 |
|
| 156 |
<?php endforeach; ?> |
| 157 |
|
| 158 |
</select> |
| 159 |
</label> |
| 160 |
</p> |
| 161 |
|
| 162 |
<p> |
| 163 |
<label> |
| 164 |
<?php esc_html_e( 'Before:', 'essential-widgets' ); ?> |
| 165 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'before' ) ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" /> |
| 166 |
</label> |
| 167 |
</p> |
| 168 |
|
| 169 |
<p> |
| 170 |
<label> |
| 171 |
<?php esc_html_e( 'After:', 'essential-widgets' ); ?> |
| 172 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'after' ) ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" /> |
| 173 |
</label> |
| 174 |
</p> |
| 175 |
|
| 176 |
<p> |
| 177 |
<label> |
| 178 |
<input type="checkbox" <?php checked( $instance['show_post_count'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_post_count' ) ); ?>" /> |
| 179 |
<?php esc_html_e( 'Show post count?', 'essential-widgets' ); ?> |
| 180 |
</label> |
| 181 |
</p> |
| 182 |
<?php |
| 183 |
} |
| 184 |
|
| 185 |
function update( $new_instance, $old_instance ) { |
| 186 |
|
| 187 |
// Sanitize title. |
| 188 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 189 |
|
| 190 |
// Sanitize key. |
| 191 |
$instance['post_type'] = sanitize_key( $new_instance['post_type'] ); |
| 192 |
|
| 193 |
// Whitelist options. |
| 194 |
$type = array( 'alpha', 'daily', 'monthly', 'postbypost', 'weekly', 'yearly' ); |
| 195 |
$order = array( 'ASC', 'DESC' ); |
| 196 |
$format = array( 'custom', 'html', 'option' ); |
| 197 |
|
| 198 |
$instance['type'] = in_array( $new_instance['type'], $type ) ? $new_instance['type'] : 'monthly'; |
| 199 |
$instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'DESC'; |
| 200 |
$instance['format'] = in_array( $new_instance['format'], $format ) ? $new_instance['format'] : 'html'; |
| 201 |
|
| 202 |
// Integers. |
| 203 |
$instance['limit'] = intval( $new_instance['limit'] ); |
| 204 |
$instance['limit'] = 0 === $instance['limit'] ? '' : $instance['limit']; |
| 205 |
|
| 206 |
// Text boxes. Make sure user can use 'unfiltered_html'. |
| 207 |
$instance['before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] ); |
| 208 |
$instance['after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] ); |
| 209 |
|
| 210 |
// Checkboxes. |
| 211 |
$instance['show_post_count'] = isset( $new_instance['show_post_count'] ) ? 1 : 0; |
| 212 |
|
| 213 |
// Return sanitized options. |
| 214 |
return $instance; |
| 215 |
} |
| 216 |
|
| 217 |
function widget( $args, $instance ) { |
| 218 |
// Set the $args for wp_get_archives() to the $instance array. |
| 219 |
$instance = wp_parse_args( $instance, $this->defaults ); |
| 220 |
|
| 221 |
// Overwrite the $echo argument and set it to false. |
| 222 |
$instance['echo'] = false; |
| 223 |
|
| 224 |
// Output the sidebar's $before_widget wrapper. |
| 225 |
echo $args['before_widget']; |
| 226 |
|
| 227 |
// If a title was input by the user, display it. |
| 228 |
if ( ! empty( $instance['title'] ) ) |
| 229 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; |
| 230 |
|
| 231 |
// Get the archives list. |
| 232 |
$archives = str_replace( array( "\r", "\n", "\t" ), '', wp_get_archives( $instance ) ); |
| 233 |
|
| 234 |
$archives = str_replace('</a> (', '</a> <span>(', $archives); |
| 235 |
$archives = str_replace(')', ')</span>', $archives); |
| 236 |
|
| 237 |
// If the archives should be shown in a <select> drop-down. |
| 238 |
if ( 'option' == $instance['format'] ) { |
| 239 |
|
| 240 |
// Create a title for the drop-down based on the archive type. |
| 241 |
if ( 'yearly' == $instance['type'] ) |
| 242 |
$option_title = esc_html__( 'Select Year', 'essential-widgets' ); |
| 243 |
|
| 244 |
elseif ( 'monthly' == $instance['type'] ) |
| 245 |
$option_title = esc_html__( 'Select Month', 'essential-widgets' ); |
| 246 |
|
| 247 |
elseif ( 'weekly' == $instance['type'] ) |
| 248 |
$option_title = esc_html__( 'Select Week', 'essential-widgets' ); |
| 249 |
|
| 250 |
elseif ( 'daily' == $instance['type'] ) |
| 251 |
$option_title = esc_html__( 'Select Day', 'essential-widgets' ); |
| 252 |
|
| 253 |
elseif ( 'postbypost' == $instance['type'] || 'alpha' == $instance['type'] ) |
| 254 |
$option_title = esc_html__( 'Select Post', 'essential-widgets' ); |
| 255 |
|
| 256 |
// Output the <select> element and each <option>. |
| 257 |
echo '<p><select name="archive-dropdown" onchange=\'document.location.href=this.options[this.selectedIndex].value;\'>'; |
| 258 |
echo '<option value="">' . $option_title . '</option>'; |
| 259 |
echo $archives; |
| 260 |
echo '</select></p>'; |
| 261 |
} |
| 262 |
|
| 263 |
// If the format should be an unordered list. |
| 264 |
elseif ( 'html' == $instance['format'] ) { |
| 265 |
echo '<ul class="ew-archives">' . $archives . '</ul><!-- .xoxo .archives -->'; |
| 266 |
} |
| 267 |
|
| 268 |
// All other formats. |
| 269 |
else { |
| 270 |
echo $archives; |
| 271 |
} |
| 272 |
|
| 273 |
// Close the sidebar's widget wrapper. |
| 274 |
echo $args['after_widget']; |
| 275 |
} |
| 276 |
}// end Archive_Widget class |
| 277 |
|
| 278 |
/** |
| 279 |
* Intiate Archive_Widget Class. |
| 280 |
* |
| 281 |
* @since 1.0.0 |
| 282 |
*/ |
| 283 |
function ew_archives_register() { |
| 284 |
register_widget( 'EW_Archives' ); |
| 285 |
} |
| 286 |
add_action( 'widgets_init', 'ew_archives_register' ); |
| 287 |
|