forked from pdewouters/better-archives-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget-archives.php
More file actions
124 lines (105 loc) · 4.35 KB
/
widget-archives.php
File metadata and controls
124 lines (105 loc) · 4.35 KB
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
<?php
/**
* Class Baw_Widgetarchives_Widget_My_Archives
*/
class Baw_Widgetarchives_Widget_My_Archives extends WP_Widget {
//process the new widget
function baw_widgetarchives_widget_my_archives() {
$widget_ops = array(
'classname' => 'baw_widgetarchives_widget_class',
'description' => __( 'Display links to archives grouped by year then month.', 'better-archives-widget' ),
);
parent::__construct( 'baw_widgetarchives_widget_my_archives', __( 'Custom Archives Widget', 'better-archives-widget' ), $widget_ops );
}
//build the widget settings form
function form( $instance ) {
$defaults = array( 'title' => 'archives' );
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
?>
<p><?php esc_html_e( 'Title:', 'better-archives-widget' ); ?>
<input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
//save the widget settings
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
//display the widget
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Archives', 'better-archives-widget' ) : $instance['title'], $instance, $this->id_base );
if ( ! empty( $title ) ) {
echo $before_title . $title . $after_title;
};
// years - months
global $wpdb;
$prevYear = '';
$currentYear = '';
/**
* Filter the SQL WHERE clause for retrieving archives.
*/
$where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= now()" );
/**
* Filter the SQL JOIN clause for retrieving archives.
*/
$join = apply_filters( 'getarchives_join', '' );
if ( $months = $wpdb->get_results( "SELECT YEAR(post_date) AS year, MONTH(post_date) AS numMonth, DATE_FORMAT(post_date, '%M') AS month, count(ID) as post_count FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" ) ) {
echo '<ul class="baw-years">';
foreach ( $months as $month ) {
$currentYear = $month->year;
if ( ( $currentYear !== $prevYear ) && ( '' !== $prevYear ) ) {
echo '</ul></li>';
}
$args = array(
'post_type' => 'post',
'date_query' => array(
array(
'year' => $month->year,
'month' => $month->numMonth
),
),
);
$queriedPosts = get_posts( $args );
if ( $currentYear !== $prevYear ) {
?>
<li class="baw-year">
<a class="baw-header" href="#"><?php echo esc_html( $month->year ); ?></a>
<span class="baw-trigger">
<svg version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!--Generated by IJSVG (https://github.com/curthard89/IJSVG)-->
<g fill="#000000">
<path d="M23.834,2.335l-2.582,-2.213c-0.101,-0.086 -0.231,-0.122 -0.365,-0.118c-0.132,0.01 -0.255,0.073 -0.341,0.175l-8.54,10.051l-8.534,-10.051c-0.087,-0.102 -0.21,-0.165 -0.342,-0.175c-0.128,-0.003 -0.264,0.032 -0.365,0.118l-2.582,2.213c-0.101,0.087 -0.163,0.21 -0.173,0.343c-0.01,0.133 0.033,0.264 0.121,0.364l11.498,13.287c0.095,0.109 0.233,0.173 0.378,0.173c0.145,0 0.283,-0.063 0.378,-0.173l11.502,-13.287c0.087,-0.101 0.13,-0.231 0.121,-0.364c-0.01,-0.133 -0.073,-0.256 -0.174,-0.343Z" transform="translate(-0.009, 3.998)"></path>
</g>
<path fill="none" d="M0,0h24v24h-24Z"></path>
</svg>
</span>
<ul class="baw-months">
<?php
} ?>
<li class="baw-month">
<a href="<?php echo esc_url( get_month_link( $month->year, $month->numMonth ) ); ?>"><?php echo esc_html( $month->month ); ?></a>
<ul class="baw-posts">
<?php foreach ( $queriedPosts as $queriedPost ): ?>
<li class="baw-post">
<a href="<?php echo get_the_permalink( $queriedPost->ID ); ?>"><?php echo $queriedPost->post_title; ?></a>
<span class="baw-post-date"><?php echo get_the_date( 'F j, Y', $queriedPost->ID ); ?></span>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php
$prevYear = $month->year;
}
}
?>
</ul></li>
<?php
echo '</ul>';
echo $after_widget;
}
}