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