| 1 |
<?php |
| 2 |
// obliged to rewrite the whole WordPress function as there is no filter on sql queries and only a filter on final output |
| 3 |
// a request for making a filter on sql queries exists: http://core.trac.wordpress.org/ticket/15202 |
| 4 |
// method used in 0.4.x: use of the get_calendar filter and overwrite the output of get_calendar function -> not very efficient (add 4 to 5 sql queries) |
| 5 |
// since 0.5: remove the WP widget and replace it by our own -> our language filter will not work if get_calendar is called directly by a theme |
| 6 |
|
| 7 |
if(!class_exists('WP_Widget_Calendar')){ |
| 8 |
require_once( ABSPATH . '/wp-includes/default-widgets.php' ); |
| 9 |
} |
| 10 |
|
| 11 |
class Polylang_Widget_Calendar extends WP_Widget_Calendar { |
| 12 |
|
| 13 |
function widget( $args, $instance ) { |
| 14 |
global $polylang; #added# |
| 15 |
|
| 16 |
extract($args); |
| 17 |
$title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title'], $instance, $this->id_base); |
| 18 |
echo $before_widget; |
| 19 |
if ( $title ) |
| 20 |
echo $before_title . $title . $after_title; |
| 21 |
echo '<div id="calendar_wrap">'; |
| 22 |
isset($polylang) && $polylang->get_languages_list() && $polylang->get_current_language() ? $this->get_calendar() : get_calendar(); #modified# |
| 23 |
echo '</div>'; |
| 24 |
echo $after_widget; |
| 25 |
} |
| 26 |
|
| 27 |
// modified version of WP get_calendar function to filter the query |
| 28 |
function get_calendar($initial = true, $echo = true) { |
| 29 |
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; |
| 30 |
global $polylang; #added# |
| 31 |
|
| 32 |
$lang = $polylang->get_current_language()->term_taxonomy_id; #added# |
| 33 |
|
| 34 |
$cache = array(); |
| 35 |
$key = md5( $lang . $m . $monthnum . $year ); #modified# |
| 36 |
if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) { |
| 37 |
if ( is_array($cache) && isset( $cache[ $key ] ) ) { |
| 38 |
if ( $echo ) { |
| 39 |
echo apply_filters( 'get_calendar', $cache[$key] ); |
| 40 |
return; |
| 41 |
} else { |
| 42 |
return apply_filters( 'get_calendar', $cache[$key] ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if ( !is_array($cache) ) |
| 48 |
$cache = array(); |
| 49 |
|
| 50 |
// Quick check. If we have no posts at all, abort! |
| 51 |
if ( !$posts ) { |
| 52 |
$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); |
| 53 |
if ( !$gotsome ) { |
| 54 |
$cache[ $key ] = ''; |
| 55 |
wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 56 |
return; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset($_GET['w']) ) |
| 61 |
$w = ''.intval($_GET['w']); |
| 62 |
|
| 63 |
// week_begins = 0 stands for Sunday |
| 64 |
$week_begins = intval(get_option('start_of_week')); |
| 65 |
|
| 66 |
// Let's figure out when we are |
| 67 |
if ( !empty($monthnum) && !empty($year) ) { |
| 68 |
$thismonth = ''.zeroise(intval($monthnum), 2); |
| 69 |
$thisyear = ''.intval($year); |
| 70 |
} elseif ( !empty($w) ) { |
| 71 |
// We need to get the month from MySQL |
| 72 |
$thisyear = ''.intval(substr($m, 0, 4)); |
| 73 |
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's |
| 74 |
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); |
| 75 |
} elseif ( !empty($m) ) { |
| 76 |
$thisyear = ''.intval(substr($m, 0, 4)); |
| 77 |
if ( strlen($m) < 6 ) |
| 78 |
$thismonth = '01'; |
| 79 |
else |
| 80 |
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); |
| 81 |
} else { |
| 82 |
$thisyear = gmdate('Y', current_time('timestamp')); |
| 83 |
$thismonth = gmdate('m', current_time('timestamp')); |
| 84 |
} |
| 85 |
|
| 86 |
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); |
| 87 |
$last_day = date('t', $unixmonth); |
| 88 |
|
| 89 |
// Get the next and previous month and year with at least one post |
| 90 |
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 91 |
FROM $wpdb->posts |
| 92 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 93 |
WHERE post_date < '$thisyear-$thismonth-01' |
| 94 |
AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang |
| 95 |
ORDER BY post_date DESC |
| 96 |
LIMIT 1"); #modified# |
| 97 |
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 98 |
FROM $wpdb->posts |
| 99 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 100 |
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' |
| 101 |
AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang |
| 102 |
ORDER BY post_date ASC |
| 103 |
LIMIT 1"); #modified# |
| 104 |
|
| 105 |
/* translators: Calendar caption: 1: month name, 2: 4-digit year */ |
| 106 |
$calendar_caption = _x('%1$s %2$s', 'calendar caption'); |
| 107 |
$calendar_output = '<table id="wp-calendar"> |
| 108 |
<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> |
| 109 |
<thead> |
| 110 |
<tr>'; |
| 111 |
|
| 112 |
$myweek = array(); |
| 113 |
|
| 114 |
for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { |
| 115 |
$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); |
| 116 |
} |
| 117 |
|
| 118 |
foreach ( $myweek as $wd ) { |
| 119 |
$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); |
| 120 |
$wd = esc_attr($wd); |
| 121 |
$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; |
| 122 |
} |
| 123 |
|
| 124 |
$calendar_output .= ' |
| 125 |
</tr> |
| 126 |
</thead> |
| 127 |
|
| 128 |
<tfoot> |
| 129 |
<tr>'; |
| 130 |
|
| 131 |
if ( $previous ) { |
| 132 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; |
| 133 |
} else { |
| 134 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; |
| 135 |
} |
| 136 |
|
| 137 |
$calendar_output .= "\n\t\t".'<td class="pad"> </td>'; |
| 138 |
|
| 139 |
if ( $next ) { |
| 140 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>'; |
| 141 |
} else { |
| 142 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; |
| 143 |
} |
| 144 |
|
| 145 |
$calendar_output .= ' |
| 146 |
</tr> |
| 147 |
</tfoot> |
| 148 |
|
| 149 |
<tbody> |
| 150 |
<tr>'; |
| 151 |
|
| 152 |
// Get days with posts |
| 153 |
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) |
| 154 |
FROM $wpdb->posts |
| 155 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 156 |
WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' |
| 157 |
AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang |
| 158 |
AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); #modified# |
| 159 |
if ( $dayswithposts ) { |
| 160 |
foreach ( (array) $dayswithposts as $daywith ) { |
| 161 |
$daywithpost[] = $daywith[0]; |
| 162 |
} |
| 163 |
} else { |
| 164 |
$daywithpost = array(); |
| 165 |
} |
| 166 |
|
| 167 |
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) |
| 168 |
$ak_title_separator = "\n"; |
| 169 |
else |
| 170 |
$ak_title_separator = ', '; |
| 171 |
|
| 172 |
$ak_titles_for_day = array(); |
| 173 |
$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " |
| 174 |
."FROM $wpdb->posts " |
| 175 |
."INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID " |
| 176 |
."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " |
| 177 |
."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " |
| 178 |
."AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang" |
| 179 |
); #modified# |
| 180 |
if ( $ak_post_titles ) { |
| 181 |
foreach ( (array) $ak_post_titles as $ak_post_title ) { |
| 182 |
|
| 183 |
$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); |
| 184 |
|
| 185 |
if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) |
| 186 |
$ak_titles_for_day['day_'.$ak_post_title->dom] = ''; |
| 187 |
if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one |
| 188 |
$ak_titles_for_day["$ak_post_title->dom"] = $post_title; |
| 189 |
else |
| 190 |
$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// See how much we should pad in the beginning |
| 195 |
$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); |
| 196 |
if ( 0 != $pad ) |
| 197 |
$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>'; |
| 198 |
|
| 199 |
$daysinmonth = intval(date('t', $unixmonth)); |
| 200 |
for ( $day = 1; $day <= $daysinmonth; ++$day ) { |
| 201 |
if ( isset($newrow) && $newrow ) |
| 202 |
$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; |
| 203 |
$newrow = false; |
| 204 |
|
| 205 |
if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) |
| 206 |
$calendar_output .= '<td id="today">'; |
| 207 |
else |
| 208 |
$calendar_output .= '<td>'; |
| 209 |
|
| 210 |
if ( in_array($day, $daywithpost) ) // any posts today? |
| 211 |
$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; |
| 212 |
else |
| 213 |
$calendar_output .= $day; |
| 214 |
$calendar_output .= '</td>'; |
| 215 |
|
| 216 |
if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) |
| 217 |
$newrow = true; |
| 218 |
} |
| 219 |
|
| 220 |
$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); |
| 221 |
if ( $pad != 0 && $pad != 7 ) |
| 222 |
$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>'; |
| 223 |
|
| 224 |
$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; |
| 225 |
|
| 226 |
$cache[ $key ] = $calendar_output; |
| 227 |
wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 228 |
|
| 229 |
if ( $echo ) |
| 230 |
echo apply_filters( 'get_calendar', $calendar_output ); |
| 231 |
else |
| 232 |
return apply_filters( 'get_calendar', $calendar_output ); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
?> |
| 237 |
|