| 1 |
<?php |
| 2 |
|
| 3 |
// obliged to rewrite the whole WordPress function as there is no filter on sql queries and only a filter on final output |
| 4 |
// not very efficient -> add 4 to 5 sql queries |
| 5 |
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; |
| 6 |
|
| 7 |
$lang = $this->curlang->term_id; |
| 8 |
$initial = true; // no other way than hardcoded this -> no impact on calendar widget output |
| 9 |
$key = md5( $lang . $m . $monthnum . $year ); |
| 10 |
|
| 11 |
if ( ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) && is_array($cache) && isset( $cache[ $key ] ) ) |
| 12 |
return $cache[$key]; |
| 13 |
|
| 14 |
if ( isset($_GET['w']) ) |
| 15 |
$w = ''.intval($_GET['w']); |
| 16 |
|
| 17 |
// week_begins = 0 stands for Sunday |
| 18 |
$week_begins = intval(get_option('start_of_week')); |
| 19 |
|
| 20 |
// Let's figure out when we are |
| 21 |
if ( !empty($monthnum) && !empty($year) ) { |
| 22 |
$thismonth = ''.zeroise(intval($monthnum), 2); |
| 23 |
$thisyear = ''.intval($year); |
| 24 |
} elseif ( !empty($w) ) { |
| 25 |
// We need to get the month from MySQL |
| 26 |
$thisyear = ''.intval(substr($m, 0, 4)); |
| 27 |
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's |
| 28 |
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); |
| 29 |
} elseif ( !empty($m) ) { |
| 30 |
$thisyear = ''.intval(substr($m, 0, 4)); |
| 31 |
if ( strlen($m) < 6 ) |
| 32 |
$thismonth = '01'; |
| 33 |
else |
| 34 |
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); |
| 35 |
} else { |
| 36 |
$thisyear = gmdate('Y', current_time('timestamp')); |
| 37 |
$thismonth = gmdate('m', current_time('timestamp')); |
| 38 |
} |
| 39 |
|
| 40 |
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); |
| 41 |
$last_day = date('t', $unixmonth); |
| 42 |
|
| 43 |
// Get the next and previous month and year with at least one post |
| 44 |
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 45 |
FROM $wpdb->posts |
| 46 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 47 |
INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id |
| 48 |
WHERE post_date < '$thisyear-$thismonth-01' |
| 49 |
AND post_type = 'post' AND post_status = 'publish' AND tt.term_id = $lang |
| 50 |
ORDER BY post_date DESC |
| 51 |
LIMIT 1"); |
| 52 |
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 53 |
FROM $wpdb->posts |
| 54 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 55 |
INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id |
| 56 |
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' |
| 57 |
AND post_type = 'post' AND post_status = 'publish' AND tt.term_id = $lang |
| 58 |
ORDER BY post_date ASC |
| 59 |
LIMIT 1"); |
| 60 |
|
| 61 |
/* translators: Calendar caption: 1: month name, 2: 4-digit year */ |
| 62 |
$calendar_caption = _x('%1$s %2$s', 'calendar caption'); |
| 63 |
$calendar_output = '<table id="wp-calendar"> |
| 64 |
<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> |
| 65 |
<thead> |
| 66 |
<tr>'; |
| 67 |
|
| 68 |
$myweek = array(); |
| 69 |
|
| 70 |
for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { |
| 71 |
$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( $myweek as $wd ) { |
| 75 |
$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); |
| 76 |
$wd = esc_attr($wd); |
| 77 |
$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; |
| 78 |
} |
| 79 |
|
| 80 |
$calendar_output .= ' |
| 81 |
</tr> |
| 82 |
</thead> |
| 83 |
|
| 84 |
<tfoot> |
| 85 |
<tr>'; |
| 86 |
|
| 87 |
if ( $previous ) { |
| 88 |
$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>'; |
| 89 |
} else { |
| 90 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; |
| 91 |
} |
| 92 |
|
| 93 |
$calendar_output .= "\n\t\t".'<td class="pad"> </td>'; |
| 94 |
|
| 95 |
if ( $next ) { |
| 96 |
$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>'; |
| 97 |
} else { |
| 98 |
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; |
| 99 |
} |
| 100 |
|
| 101 |
$calendar_output .= ' |
| 102 |
</tr> |
| 103 |
</tfoot> |
| 104 |
|
| 105 |
<tbody> |
| 106 |
<tr>'; |
| 107 |
|
| 108 |
// Get days with posts |
| 109 |
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) |
| 110 |
FROM $wpdb->posts |
| 111 |
INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID |
| 112 |
INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id |
| 113 |
WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' |
| 114 |
AND post_type = 'post' AND post_status = 'publish' AND tt.term_id = $lang |
| 115 |
AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); |
| 116 |
if ( $dayswithposts ) { |
| 117 |
foreach ( (array) $dayswithposts as $daywith ) { |
| 118 |
$daywithpost[] = $daywith[0]; |
| 119 |
} |
| 120 |
} else { |
| 121 |
$daywithpost = array(); |
| 122 |
} |
| 123 |
|
| 124 |
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) |
| 125 |
$ak_title_separator = "\n"; |
| 126 |
else |
| 127 |
$ak_title_separator = ', '; |
| 128 |
|
| 129 |
$ak_titles_for_day = array(); |
| 130 |
$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " |
| 131 |
."FROM $wpdb->posts " |
| 132 |
."INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID " |
| 133 |
."INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id " |
| 134 |
."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " |
| 135 |
."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " |
| 136 |
."AND post_type = 'post' AND post_status = 'publish' AND tt.term_id = $lang" |
| 137 |
); |
| 138 |
if ( $ak_post_titles ) { |
| 139 |
foreach ( (array) $ak_post_titles as $ak_post_title ) { |
| 140 |
|
| 141 |
$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); |
| 142 |
|
| 143 |
if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) |
| 144 |
$ak_titles_for_day['day_'.$ak_post_title->dom] = ''; |
| 145 |
if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one |
| 146 |
$ak_titles_for_day["$ak_post_title->dom"] = $post_title; |
| 147 |
else |
| 148 |
$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// See how much we should pad in the beginning |
| 153 |
$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); |
| 154 |
if ( 0 != $pad ) |
| 155 |
$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>'; |
| 156 |
|
| 157 |
$daysinmonth = intval(date('t', $unixmonth)); |
| 158 |
for ( $day = 1; $day <= $daysinmonth; ++$day ) { |
| 159 |
if ( isset($newrow) && $newrow ) |
| 160 |
$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; |
| 161 |
$newrow = false; |
| 162 |
|
| 163 |
if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) |
| 164 |
$calendar_output .= '<td id="today">'; |
| 165 |
else |
| 166 |
$calendar_output .= '<td>'; |
| 167 |
|
| 168 |
if ( in_array($day, $daywithpost) ) // any posts today? |
| 169 |
$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; |
| 170 |
else |
| 171 |
$calendar_output .= $day; |
| 172 |
$calendar_output .= '</td>'; |
| 173 |
|
| 174 |
if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) |
| 175 |
$newrow = true; |
| 176 |
} |
| 177 |
|
| 178 |
$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); |
| 179 |
if ( $pad != 0 && $pad != 7 ) |
| 180 |
$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>'; |
| 181 |
|
| 182 |
$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; |
| 183 |
|
| 184 |
$cache[ $key ] = $calendar_output; |
| 185 |
wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 186 |
|
| 187 |
?> |
| 188 |
|