-
Notifications
You must be signed in to change notification settings - Fork 1
/
month-calendar.php
285 lines (242 loc) · 9.3 KB
/
month-calendar.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/*
Plugin Name: Month Calendar
Plugin URI: http://github.com/rsieiro/month-calendar
Description: A calendar widget that shows post count per month.
Version: 1.1
Author: Rodrigo Sieiro
Author URI: http://rodrigo.sharpcube.com
*/
// Plugin version
define('MONTH_CALENDAR_VERSION', '1.1');
// Find the plugin path
// Thanks to Alex King for this code snippet
// http://alexking.org/blog/2011/12/15/wordpress-plugins-and-symlinks
$mc_file = __FILE__;
if (isset($plugin)) {
$my_plugin_file = $plugin;
} else if (isset($mu_plugin)) {
$my_plugin_file = $mu_plugin;
} else if (isset($network_plugin)) {
$my_plugin_file = $network_plugin;
}
// This URL will always point to the path our plugin files are located
define('MC_FILE', $mc_file);
define('MC_PATH', WP_PLUGIN_DIR . '/' . basename(dirname($mc_file)) . '/');
define('MC_URL', plugins_url() . '/' . basename(dirname($mc_file)) . '/');
class Month_Widget_Calendar extends WP_Widget
{
function Month_Widget_Calendar()
{
// Register the widget with WP
$widget_ops = array('classname' => 'month_calendar', 'description' => __( 'A calendar widget that shows post count per month') );
$this->WP_Widget('month_calendar', __('Month Calendar'), $widget_ops);
}
function widget( $args, $instance )
{
// Get the instance settings
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title'], $instance, $this->id_base);
// Show the widget, including title and the loading div
echo $before_widget;
echo $before_title . $title . $after_title;
echo '<div id="mc_wrapper">';
echo '<div id="mc_calendar">';
echo '<script type="text/javascript">';
echo 'var mc_url = "' . MC_URL . '";';
echo '</script>';
mc_get_calendar(true);
echo '</div>';
echo '<div id="mc_calendar_loading" style="display: none;">Loading...</div>';
echo '</div>';
echo $after_widget;
}
function update( $new_instance, $old_instance )
{
// Save the new widget settings for this instance
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['use_css'] = strip_tags($new_instance['use_css']);
update_option('mc_use_css', $instance['use_css']);
return $instance;
}
function form( $instance )
{
// Get the instance settings
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'use_css' => '1' ) );
$title = strip_tags($instance['title']);
// use_css is loaded from general WP options
$use_css = get_option('mc_use_css', '1');
// Prepare and show the settings for the admin interface
$checked = '';
if ($use_css == '1') $checked = 'checked="checked"';
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<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); ?>" /></p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('use_css'); ?>" name="<?php echo $this->get_field_name('use_css'); ?>" value="1" <?php echo $checked; ?> />
<label for="<?php echo $this->get_field_id('use_css'); ?>"><?php _e('Include default CSS file'); ?></label>
</p>
<?php
}
}
function mc_widget_init()
{
// We use WP options to save this because we need it in places where
// there's no instance for the widget, so we can't use widget settings
add_option('mc_use_css', '1');
register_widget('Month_Widget_Calendar');
}
function mc_get_calendar($echo = true, $my_year = 0)
{
global $wpdb, $wp_locale, $posts;
if ( $my_year == 0 )
{
// If no year came via function parameter, check if we're inside an archive page
// so we can get the year from there. If not, use the current year
if (is_year())
{
$thisyear = get_query_var('year');
}
else if (is_month())
{
$thisyear = intval(get_query_var('year')) == 0 ? substr(get_query_var('m'), 0, 4) : get_query_var('year');
}
else
{
$thisyear = gmdate('Y', current_time('timestamp'));
}
}
else
{
// Use the year that came via function parameter
$thisyear = ''.intval($my_year);
}
// We use a simple per-year text cache.
// Here we check if there's a cache for the requested year, and use it if found
$cache = array();
$key = md5( 'month_calendar_' . $thisyear );
if ( $cache = wp_cache_get( 'get_mcalendar', 'calendar' ) ) {
if ( is_array($cache) && isset( $cache[ $key ] ) ) {
if ( $echo ) {
echo apply_filters( 'get_mcalendar', $cache[$key] );
return;
} else {
return apply_filters( 'get_mcalendar', $cache[$key] );
}
}
}
// If no cache was found, create an empty one
if ( !is_array($cache) )
$cache = array();
// Check if we have at least one published post, otherwise the calendar will always be empty
if ( !$posts ) {
$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
if ( !$gotsome ) {
$cache[ $key ] = '';
wp_cache_set( 'get_mcalendar', $cache, 'calendar' );
return;
}
}
// If the calendar year is the current year, we disable the "next year" link
if ( $thisyear == gmdate('Y', current_time('timestamp')))
{
$prevyear = intval($thisyear)-1;
$prev_link = "<a id='mc_prev_year' onclick='javascript:mc_reload_calendar($prevyear);' title='$prevyear'><<</a>";
$next_link = "<a id='mc_next_year' class='mc_inactive'>>></a>";
}
else
{
$prevyear = intval($thisyear)-1;
$nextyear = intval($thisyear)+1;
$prev_link = "<a id='mc_prev_year' onclick='javascript:mc_reload_calendar($prevyear);' title='$prevyear'><<</a>";
$next_link = "<a id='mc_next_year' onclick='javascript:mc_reload_calendar($nextyear);' title='$nextyear'>>></a>";
}
// Let's start the output, including a header
$calendar_output = "
<div id='mc_header'>
$thisyear
$prev_link
$next_link
</div>
<div class='mc_months'>
<ul class='mc_list'>\n";
// This query loads a post count per month for the specified year
// Only published posts with dates in the past are considered
$sql = "select month(post_date), count(1)
from $wpdb->posts
where year(post_date) = '$thisyear'
and post_type = 'post' AND post_status = 'publish'
and post_date < '" . current_time('mysql') . "'
group by month(post_date)
order by month(post_date)";
// Run the query and get the results in an array
$month_posts = $wpdb->get_results($sql, ARRAY_N);
$month_cal = array();
// First we fill the array with zeroes, so we have an item for each month
for ($i = 1; $i <= 12; $i++)
{
$month_cal[$i] = 0;
}
// Now we fill the array with post count for months that have it
foreach ($month_posts as $row)
{
$month_cal[$row[0]] = $row[1];
}
for ($i = 1; $i <= 12; $i++)
{
// We get the month abbreviation according to the current WP locale
$mname = $wp_locale->get_month_abbrev($wp_locale->get_month($i));
$mlink = get_month_link( $thisyear, $i );
// The month link will be disabled if there were no posts
// Otherwise we show either "1 post" or "n posts"
if ($month_cal[$i] == 0)
$calendar_output .= "\t\t\t<li class='mc_month mc_m$i mc_inactive'><a class='mc_link'>$mname<span class='mc_postcount'>(0 posts)</span></a></li>\n";
else if ($month_cal[$i] == 1)
$calendar_output .= "\t\t\t<li class='mc_month mc_m$i'><a class='mc_link' href='$mlink'>$mname<span class='mc_postcount'>($month_cal[$i] post)</span></a></li>\n";
else
$calendar_output .= "\t\t\t<li class='mc_month mc_m$i'><a class='mc_link' href='$mlink'>$mname<span class='mc_postcount'>($month_cal[$i] posts)</span></a></li>\n";
}
// Finish the output
$calendar_output .= "\t\t</ul>
</div>\n";
// Here we save a cache for the current year, so we don't have to load it every time
// We let WP decide when to expire the cache
$cache[ $key ] = $calendar_output;
wp_cache_set( 'get_mcalendar', $cache, 'calendar' );
// Depending on the function parameter, we either show or return the output
if ( $echo )
echo apply_filters( 'get_mcalendar', $calendar_output );
else
return apply_filters( 'get_mcalendar', $calendar_output );
}
function mc_add_header_code()
{
// Get our use_css setting from WP options
$use_css = get_option('mc_use_css', '1');
if (function_exists('wp_enqueue_script') && !is_admin())
{
// Registers jquery and jquery-ui, if not already registered
if (!wp_script_is('jquery', 'registered')) wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js', false, '1.4.3');
if (!wp_script_is('jquery-ui', 'registered')) wp_register_script('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js', array('jquery'), '1.8.6');
// Enqueues the javascript file
wp_enqueue_script('mc_js', MC_URL . 'month-calendar.js', array('jquery', 'jquery-ui'), MONTH_CALENDAR_VERSION);
// Only enqueue the CSS file if the user wants to
if ($use_css) {
wp_enqueue_style('mc_style', MC_URL . 'month-calendar.css', array(), MONTH_CALENDAR_VERSION);
}
}
}
function mc_reload_calendar()
{
// The year will come via a GET parameter
// But if no year is informed, defaults to 0 (will be treated later)
if (isset($_GET['my_year']))
$my_year = intval($_GET['my_year']);
else
$my_year = 0;
// Reload the calendar
mc_get_calendar(true, $my_year);
}
add_action('widgets_init', 'mc_widget_init');
add_action('init', 'mc_add_header_code');