forked from robinadr/smarter-archives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smarter-archives.php
executable file
·153 lines (124 loc) · 4.65 KB
/
smarter-archives.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
<?php
/***************************************************************************************************
Plugin Name: Smarter Archives
Plugin URI: http://wordpress.org/plugins/smarter-archives/
Author: Robin Adrianse
Author URI: http://robinadr.com/
Description: Easy, simple, and intuitive way to access archives via months, broken down by year.
Version: 3.2.4
Text Domain: smarter-archives
Copyright (c) 2014 Robin Adrianse; see license.txt for full license
***************************************************************************************************/
function get_smarter_archives()
{
global $wpdb;
$sql_where = apply_filters( 'smart_archives_where', "WHERE post_type = 'post' AND post_status = 'publish'" );
$sql_join = apply_filters( 'smart_archives_join', '' );
$sql = 'SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, COUNT(ID) AS `count` ' .
"FROM $wpdb->posts $sql_join $sql_where " .
'GROUP BY MONTH(post_date), YEAR(post_date) ' .
'ORDER BY `year` DESC, `month` ASC';
$results = $wpdb->get_results( $sql );
$archives = array();
if ( !empty( $results ) ) {
foreach ( $results as $result ) {
if ( !isset( $archives[$result->year] ) )
$archives[$result->year] = array();
$archives[$result->year][$result->month] = $result->count;
}
}
return $archives;
}
function smarter_archives( $args = '' )
{
$defaults = array(
'mode' => 'output',
'wrapper_class' => 'smart-archives',
'wrapper_tag' => 'div',
'year_link_class' => 'year-link',
'year_tag' => 'p',
'year_class' => '',
'after_year' => ': ',
'month_link_class' => 'month-link',
'month_tag' => 'span',
'after_month' => ' ',
'empty_month_class' => 'empty-month',
'order' => 'DESC'
);
$defaults = apply_filters( 'smarter_archives_defaults', $defaults );
extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
if ( $mode != 'output' && $mode != 'return' )
$mode = 'output';
$archives = get_smarter_archives();
if ( empty( $archives ) )
return '';
$order = strtoupper( $order );
if ( $order == 'ASC' )
ksort( $archives );
$month_names = array( '',
__( 'Jan', 'smarter-archives' ), __( 'Feb', 'smarter-archives' ), __( 'Mar', 'smarter-archives' ),
__( 'Apr', 'smarter-archives' ), __( 'May', 'smarter-archives' ), __( 'Jun', 'smarter-archives' ),
__( 'Jul', 'smarter-archives' ), __( 'Aug', 'smarter-archives' ), __( 'Sep', 'smarter-archives' ),
__( 'Oct', 'smarter-archives' ), __( 'Nov', 'smarter-archives' ), __( 'Dec', 'smarter-archives' )
);
unset( $month_names[0] );
$month_names = apply_filters( 'smarter_archives_months', $month_names );
$output = '<' . _smarter_archives_tag( $wrapper_tag, $wrapper_class ) . ">\n";
foreach ( $archives as $year => $months ) {
$output .= '<' . _smarter_archives_tag( $year_tag, $year_class ) . '>';
$output .= '<' . _smarter_archives_tag( 'a', $year_link_class ) . ' href="' . get_year_link( $year ) . '">';
$output .= $year;
$output .= "</a>$after_year";
foreach ( $month_names as $month_number => $month_name ) {
if ( isset( $months[$month_number] ) ) {
$output .= "<$month_tag>";
$output .= '<' . _smarter_archives_tag( 'a', $month_link_class ) . ' href="';
$output .= get_month_link( $year, $month_number );
$output .= '" title="';
$output .= sprintf( _n( '1 post', '%d posts', $months[$month_number], 'smarter-archives' ), $months[$month_number] );
$output .= '">';
$output .= $month_name;
$output .= "</a></$month_tag>";
} else {
$output .= '<' . _smarter_archives_tag( $month_tag, $empty_month_class ) . '>';
$output .= $month_name;
$output .= "</$month_tag>";
}
if ( $month_number < 12 )
$output .= $after_month;
}
$output .= "</$year_tag>\n";
}
$output .= "</$wrapper_tag>\n";
if ( $mode == 'output' )
print $output;
else
return $output;
}
function _smarter_archives_tag( $tag, $class = '' )
{
if ( !empty( $class ) )
return "$tag class=\"$class\"";
else
return "$tag";
}
if ( !function_exists( 'wp_smart_archives' ) ) :
function wp_smart_archives( $args = '' )
{
return smarter_archives( $args );
}
endif;
// pre-4.0 uses WPLANG, 4.0+ uses an admin option
if ( get_locale() != '' || ( defined( 'WPLANG' ) && constant( 'WPLANG' ) != '' ) ) :
function _smarter_archives_textdomain()
{
load_plugin_textdomain( 'smarter-archives', false, basename( dirname( __FILE__ ) ) . '/lang/' );
}
add_action( 'plugins_loaded', '_smarter_archives_textdomain' );
endif;
function _smarter_archives_shortcode( $args = array() )
{
$args = shortcode_atts( array( 'mode' => 'return' ), $args, 'smarter-archives' );
return smarter_archives( $args );
}
add_shortcode( 'smarter-archives', '_smarter_archives_shortcode' );