| 1 | function get_calendar($daylength = 1) { |
|---|
| 2 | global $wpdb, $m, $monthnum, $year, $timedifference, $month, $month_abbrev, $weekday, $weekday_initial, $weekday_abbrev, $posts; |
|---|
| 3 | |
|---|
| 4 | // Quick check. If we have no posts at all, abort! |
|---|
| 5 | if (!$posts) { |
|---|
| 6 | $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); |
|---|
| 7 | if (!$gotsome) |
|---|
| 8 | return; |
|---|
| 9 | } |
|---|
| 10 | |
|---|
| 11 | if (isset($_GET['w'])) { |
|---|
| 12 | $w = ''.intval($_GET['w']); |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | // week_begins = 0 stands for sunday |
|---|
| 16 | $week_begins = intval(get_settings('start_of_week')); |
|---|
| 17 | $add_hours = intval(get_settings('gmt_offset')); |
|---|
| 18 | $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); |
|---|
| 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 | $calendar = substr($m, 0, 6); |
|---|
| 31 | $thisyear = ''.intval(substr($m, 0, 4)); |
|---|
| 32 | if (strlen($m) < 6) { |
|---|
| 33 | $thismonth = '01'; |
|---|
| 34 | } else { |
|---|
| 35 | $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); |
|---|
| 36 | } |
|---|
| 37 | } else { |
|---|
| 38 | $thisyear = gmdate('Y', current_time('timestamp') + get_settings('gmt_offset') * 3600); |
|---|
| 39 | $thismonth = gmdate('m', current_time('timestamp') + get_settings('gmt_offset') * 3600); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); |
|---|
| 43 | |
|---|
| 44 | // Get the next and previous month and year with at least one post |
|---|
| 45 | $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year |
|---|
| 46 | FROM $wpdb->posts |
|---|
| 47 | WHERE post_date < '$thisyear-$thismonth-01' |
|---|
| 48 | AND post_status = 'publish' |
|---|
| 49 | ORDER BY post_date DESC |
|---|
| 50 | LIMIT 1"); |
|---|
| 51 | $next = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year |
|---|
| 52 | FROM $wpdb->posts |
|---|
| 53 | WHERE post_date > '$thisyear-$thismonth-01' |
|---|
| 54 | AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' ) |
|---|
| 55 | AND post_status = 'publish' |
|---|
| 56 | ORDER BY post_date ASC |
|---|
| 57 | LIMIT 1"); |
|---|
| 58 | |
|---|
| 59 | echo '<table id="wp-calendar" summary="calendar"> |
|---|
| 60 | <caption>' . $month[zeroise($thismonth, 2)] . ' ' . date('Y', $unixmonth) . '</caption> |
|---|
| 61 | <thead> |
|---|
| 62 | <tr>'; |
|---|
| 63 | |
|---|
| 64 | $day_abbrev = $weekday_initial; |
|---|
| 65 | if ($daylength > 1) { |
|---|
| 66 | $day_abbrev = $weekday_abbrev; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | $myweek = array(); |
|---|
| 70 | |
|---|
| 71 | for ($wdcount=0; $wdcount<=6; $wdcount++) { |
|---|
| 72 | $myweek[]=$weekday[($wdcount+$week_begins)%7]; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | foreach ($myweek as $wd) { |
|---|
| 76 | echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">" . $day_abbrev[$wd] . '</th>'; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | echo ' |
|---|
| 80 | </tr> |
|---|
| 81 | </thead> |
|---|
| 82 | |
|---|
| 83 | <tfoot> |
|---|
| 84 | <tr>'; |
|---|
| 85 | |
|---|
| 86 | if ($previous) { |
|---|
| 87 | echo "\n\t\t".'<td abbr="' . $month[zeroise($previous->month, 2)] . '" colspan="3" id="prev"><a href="' . |
|---|
| 88 | get_month_link($previous->year, $previous->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $month[zeroise($previous->month, 2)], date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . '">« ' . $month_abbrev[$month[zeroise($previous->month, 2)]] . '</a></td>'; |
|---|
| 89 | } else { |
|---|
| 90 | echo "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | echo "\n\t\t".'<td class="pad"> </td>'; |
|---|
| 94 | |
|---|
| 95 | if ($next) { |
|---|
| 96 | echo "\n\t\t".'<td abbr="' . $month[zeroise($next->month, 2)] . '" colspan="3" id="next"><a href="' . |
|---|
| 97 | get_month_link($next->year, $next->month) . '" title="View posts for ' . $month[zeroise($next->month, 2)] . ' ' . |
|---|
| 98 | date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year)) . '">' . $month_abbrev[$month[zeroise($next->month, 2)]] . ' »</a></td>'; |
|---|
| 99 | } else { |
|---|
| 100 | echo "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | echo ' |
|---|
| 104 | </tr> |
|---|
| 105 | </tfoot> |
|---|
| 106 | |
|---|
| 107 | <tbody> |
|---|
| 108 | <tr>'; |
|---|
| 109 | |
|---|
| 110 | // Get days with posts |
|---|
| 111 | $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) |
|---|
| 112 | FROM $wpdb->posts WHERE MONTH(post_date) = $thismonth |
|---|
| 113 | AND YEAR(post_date) = $thisyear |
|---|
| 114 | AND post_status = 'publish' |
|---|
| 115 | AND post_date < '" . current_time('mysql') . '\'', ARRAY_N); |
|---|
| 116 | if ($dayswithposts) { |
|---|
| 117 | foreach ($dayswithposts as $daywith) { |
|---|
| 118 | $daywithpost[] = $daywith[0]; |
|---|
| 119 | } |
|---|
| 120 | } else { |
|---|
| 121 | $daywithpost = array(); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || |
|---|
| 127 | strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || |
|---|
| 128 | strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari')) { |
|---|
| 129 | $ak_title_separator = "\n"; |
|---|
| 130 | } else { |
|---|
| 131 | $ak_title_separator = ', '; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | $ak_titles_for_day = array(); |
|---|
| 135 | $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom " |
|---|
| 136 | ."FROM $wpdb->posts " |
|---|
| 137 | ."WHERE YEAR(post_date) = '$thisyear' " |
|---|
| 138 | ."AND MONTH(post_date) = '$thismonth' " |
|---|
| 139 | ."AND post_date < '".current_time('mysql')."' " |
|---|
| 140 | ."AND post_status = 'publish'" |
|---|
| 141 | ); |
|---|
| 142 | if ($ak_post_titles) { |
|---|
| 143 | foreach ($ak_post_titles as $ak_post_title) { |
|---|
| 144 | if (empty($ak_titles_for_day['day_'.$ak_post_title->dom])) { |
|---|
| 145 | $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; |
|---|
| 146 | } |
|---|
| 147 | if (empty($ak_titles_for_day["$ak_post_title->dom"])) { // first one |
|---|
| 148 | $ak_titles_for_day["$ak_post_title->dom"] = str_replace('"', '"', wptexturize($ak_post_title->post_title)); |
|---|
| 149 | } else { |
|---|
| 150 | $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . str_replace('"', '"', wptexturize($ak_post_title->post_title)); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | // See how much we should pad in the beginning |
|---|
| 157 | $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); |
|---|
| 158 | if (0 != $pad) echo "\n\t\t".'<td colspan="'.$pad.'" class="pad"> </td>'; |
|---|
| 159 | |
|---|
| 160 | $daysinmonth = intval(date('t', $unixmonth)); |
|---|
| 161 | for ($day = 1; $day <= $daysinmonth; ++$day) { |
|---|
| 162 | if (isset($newrow) && $newrow) |
|---|
| 163 | echo "\n\t</tr>\n\t<tr>\n\t\t"; |
|---|
| 164 | $newrow = false; |
|---|
| 165 | |
|---|
| 166 | if ($day == gmdate('j', (time() + (get_settings('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_settings('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_settings('gmt_offset') * 3600))) |
|---|
| 167 | echo '<td id="today">'; |
|---|
| 168 | else |
|---|
| 169 | echo '<td>'; |
|---|
| 170 | |
|---|
| 171 | if (in_array($day, $daywithpost)) { // any posts today? |
|---|
| 172 | echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>"; |
|---|
| 173 | } else { |
|---|
| 174 | echo $day; |
|---|
| 175 | } |
|---|
| 176 | echo '</td>'; |
|---|
| 177 | |
|---|
| 178 | if (6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins)) |
|---|
| 179 | $newrow = true; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); |
|---|
| 183 | if ($pad != 0 && $pad != 7) |
|---|
| 184 | echo "\n\t\t".'<td class="pad" colspan="'.$pad.'"> </td>'; |
|---|
| 185 | |
|---|
| 186 | echo "\n\t</tr>\n\t</tbody>\n\t</table>"; |
|---|
| 187 | } |
|---|