/** * Calendar view. * * @global type $wpdb * @global type $m * @global type $wp_locale * @global type $posts * @param type $params * @param type $initial * @param type $echo * @return type */ function wpcf_fields_date_get_calendar($params, $initial = true, $echo = true) { global $wpdb, $m, $wp_locale, $posts; // wpcf Set our own date $monthnum = date('n', $params['field_value']); $year = date('Y', $params['field_value']); $wpcf_date = date('j', $params['field_value']); $cache = array(); $key = md5($params['field']['slug'] . $wpcf_date); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { if ($echo) { echo apply_filters('get_calendar', $cache[$key]); return; } else { return apply_filters('get_calendar', $cache[$key]); } } } if (!is_array($cache)) { $cache = array(); } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar" summary="' . esc_attr__('Calendar') . '"> <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } // wpcf if ($wpcf_date == $day) { $calendar_output .= '<a href="javascript:void(0);">' . $day . '</a>'; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); if ($echo) { echo apply_filters('get_calendar', $calendar_output); } else { return apply_filters('get_calendar', $calendar_output); } }
function get_calendar($initial = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $key = md5($m . $monthnum . $year); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (isset($cache[$key])) { echo $cache[$key]; return; } } ob_start(); // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT ID from {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); if (!$gotsome) { return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date < '{$thisyear}-{$thismonth}-01'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date DESC\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT\tDISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date >\t'{$thisyear}-{$thismonth}-01'\n\t\tAND MONTH( post_date ) != MONTH( '{$thisyear}-{$thismonth}-01' )\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER\tBY post_date ASC\n\t\t\tLIMIT 1"); echo '<table id="wp-calendar" summary="' . __('Calendar') . '"> <caption>' . sprintf(_c('%1$s %2$s|Used as a calendar caption'), $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); echo "\n\t\t<th abbr=\"{$wd}\" scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } echo ' </tr> </thead> <tfoot> <tr>'; if ($previous) { echo "\n\t\t" . '<td abbr="' . $wp_locale->get_month($previous->month) . '" colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . 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>'; } else { echo "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } echo "\n\t\t" . '<td class="pad"> </td>'; if ($next) { echo "\n\t\t" . '<td abbr="' . $wp_locale->get_month($next->month) . '" colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . 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>'; } else { echo "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } echo ' </tr> </tfoot> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)\n\t\tFROM {$wpdb->posts} WHERE MONTH(post_date) = '{$thismonth}'\n\t\tAND YEAR(post_date) = '{$thisyear}'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\tAND post_date < '" . current_time('mysql') . '\'', ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom " . "FROM {$wpdb->posts} " . "WHERE YEAR(post_date) = '{$thisyear}' " . "AND MONTH(post_date) = '{$thismonth}' " . "AND post_date < '" . current_time('mysql') . "' " . "AND post_type = 'post' AND post_status = 'publish'"); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { $post_title = apply_filters("the_title", $ak_post_title->post_title); $post_title = str_replace('"', '"', wptexturize($post_title)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { echo "\n\t\t" . '<td colspan="' . $pad . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { echo "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', time() + get_option('gmt_offset') * 3600) && $thismonth == gmdate('m', time() + get_option('gmt_offset') * 3600) && $thisyear == gmdate('Y', time() + get_option('gmt_offset') * 3600)) { echo '<td id="today">'; } else { echo '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"{$ak_titles_for_day[$day]}\">{$day}</a>"; } else { echo $day; } echo '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { echo "\n\t\t" . '<td class="pad" colspan="' . $pad . '"> </td>'; } echo "\n\t</tr>\n\t</tbody>\n\t</table>"; $output = ob_get_contents(); ob_end_clean(); echo $output; $cache[$key] = $output; wp_cache_set('get_calendar', $cache, 'calendar'); }
if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (array_key_exists($day, $daywithpost)) { // any posts today? $calendar_output .= '<a data-tooltip data-options="disable_for_touch:true" class="has-tip" href="' . (sizeof($daywithpost[$day]) > 1 ? add_query_arg(array('post_type' => 'sp_event'), get_day_link($thisyear, $thismonth, $day)) . '" title="' . sprintf('%s events', sizeof($daywithpost[$day])) : get_post_permalink($daywithpost[$day][0], false, true) . '" title="' . esc_attr($ak_titles_for_day[$day])) . "\">{$day}</a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>\n\t</div>"; if ($id && $show_all_events_link) { $calendar_output .= '<a class="sp-calendar-link sp-view-all-link" href="' . get_permalink($id) . '">' . __('View all events', 'sportspress') . '</a>'; } ?> <div class="sp-template sp-template-event-calendar"> <?php echo $calendar_output; ?> </div>
/** * MODIFIED VERSION OF WP CALENDAR * Display calendar with days that have posts as links. * * * @since 1.0.0 * @uses calendar_week_mod() * * @param bool $initial Optional, default is true. Use initial calendar names. * @param bool $echo Optional, default is true. Set to false for return. * @return string|null String when retrieving, null when displaying. */ function wplms_event_calendar($course = NULL) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $initial = false; $echo = true; if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } if (isset($_GET['month'])) { $thismonth = $_GET['month']; } if (isset($_GET['y'])) { $thisyear = $_GET['y']; } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); $premonth = zeroise($thismonth - 1, 2); $preyear = $nextyear = $thisyear; if ($thismonth == 1) { $premonth = 12; $preyear = $thisyear - 1; } $nextmonth = zeroise($thismonth + 1, 2); if ($thismonth == 12) { $nextmonth = '01'; $nextyear = $thisyear + 1; } $previous = $next = 1; /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption', 'wplms-events'); $calendar_output = '<table id="wplms-calendar"> <caption>' . __('EVENTS IN ', 'wplms-events') . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { if ($thismonth == 1) { $newmonth = 12; $newyear = $thisyear - 1; $monthstring = 'month=12&y=' . $newyear; } else { $newmonth = zeroise($thismonth - 1, 2); $newyear = $thisyear; $monthstring = 'month=' . $newmonth; if (isset($_GET['y'])) { $monthstring .= '&y=' . $newyear; } } $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="?action=events&' . $monthstring . '" title="' . esc_attr(sprintf(__('View Events for %1$s %2$s', 'wplms-events'), $wp_locale->get_month($newmonth), date('Y', mktime(0, 0, 0, $newmonth, 1, $newyear)))) . '" rel="nofollow"> ‹ ' . $wp_locale->get_month_abbrev($wp_locale->get_month($newmonth)) . '</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { if ($thismonth == 12) { $newmonth = 01; $newyear = $thisyear + 1; $monthstring = 'month=01&y=' . ($thisyear + 1); } else { $newmonth = zeroise($thismonth + 1, 2); $newyear = $thisyear; $monthstring = 'month=' . $newmonth; if (isset($_GET['y'])) { $monthstring .= '&y=' . $newyear; } } $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="?action=events&' . $monthstring . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'wplms-events'), $wp_locale->get_month($newmonth), date('Y', mktime(0, 0, 0, $newmonth, 1, $newyear)))) . '" rel="nofollow"> ' . $wp_locale->get_month_abbrev($wp_locale->get_month($newmonth)) . ' ›</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; $result = strtotime("{$thisyear}-{$thismonth}-01"); $result = strtotime('-1 second', strtotime('+1 month', $result)); $end_date_month = date('Y-m-d', $result); $result = strtotime("{$thisyear}-{$thismonth}-01"); $start_date_month = date('Y-m-d', $result); if (isset($course) && $course != '') { $eventdaysquery = $wpdb->get_results($wpdb->prepare("SELECT start.post_id as id, start.meta_value as start_date, end.meta_value as end_date\n FROM {$wpdb->postmeta} AS start\n INNER JOIN {$wpdb->postmeta} AS end\n ON start.post_id=end.post_id\n WHERE start.post_id IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'vibe_event_course' AND meta_value = %d)\n AND start.meta_key = 'vibe_start_date'\n AND end.meta_key = 'vibe_end_date'\n AND start.meta_value <= DATE('{$end_date_month}')\n AND end.meta_value >= DATE('{$start_date_month}')\n ", $course)); } else { $eventdaysquery = $wpdb->get_results("SELECT start.post_id as id, start.meta_value as start_date, end.meta_value as end_date\n FROM {$wpdb->postmeta} AS start\n INNER JOIN {$wpdb->postmeta} AS end\n ON start.post_id=end.post_id\n WHERE start.meta_key = 'vibe_start_date'\n AND end.meta_key = 'vibe_end_date'\n AND start.meta_value <= '{$end_date_month}'\n AND end.meta_value >= '{$start_date_month}'\n "); } $eventdays = array(); foreach ($eventdaysquery as $event) { $start = $event->start_date <= $start_date_month ? $start_date_month : $event->start_date; $end = $event->end_date >= $end_date_month ? $end_date_month : $event->end_date; $startTime = intval(date('d', strtotime($start))); $endTime = intval(date('d', strtotime($end))); while ($startTime <= $endTime) { if (!isset($eventdays[$startTime])) { $eventdays[$startTime] = 1; } else { $eventdays[$startTime]++; } $startTime++; } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (isset($eventdays[$day])) { // any posts today? $calendar_output .= '<a href="#" class="event_list" data-course="' . (get_post_type() == BP_COURSE_SLUG ? get_the_ID() : '*') . '" data-day="' . $day . '" data-month="' . $thismonth . '" data-year="' . $thisyear . '" rel="nofollow">' . $day . ' <span>' . $eventdays[$day] . '</span></a>'; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; if ($echo) { echo apply_filters('wplms_event_calendar', $calendar_output); } else { return apply_filters('wplms_event_calendar', $calendar_output); } }
function wpestate_draw_month_front($month_no, $reservation_array, $unixmonth, $daywithpost, $thismonth, $thisyear, $last_day) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $week_begins = intval(get_option('start_of_week')); $initial = true; $echo = true; $table_style = ''; if ($month_no > 2) { $table_style = 'style="display:none;"'; } $calendar_output = '<div class="booking-calendar-wrapper" data-mno="' . $month_no . '" ' . $table_style . '> <div class="month-title"> ' . date("F", mktime(0, 0, 0, $thismonth, 10)) . ' ' . $thisyear . ' </div> <table class="wp-calendar booking-calendar"> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tbody> <tr>'; // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); $reserverd_first = ''; $reserved_last = ''; for ($day = 1; $day <= $daysinmonth; ++$day) { $timestamp = strtotime($day . '-' . $thismonth . '-' . $thisyear) . ' | '; $timestamp_java = strtotime($day . '-' . $thismonth . '-' . $thisyear); if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; $has_past_class = ''; if ($timestamp_java < time() - 24 * 60 * 60) { $has_past_class = "has_past"; } else { $has_past_class = "has_future"; } $is_reserved = 0; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { // if is today check for reservation if (in_array($timestamp_java, $reservation_array)) { $calendar_output .= '<td class="calendar-reserved calendar-today ' . $has_past_class . ' " data-curent-date="' . $timestamp_java . '">'; } else { $calendar_output .= '<td class="calendar-today ' . $has_past_class . ' " data-curent-date="' . $timestamp_java . '">'; } } else { if (in_array($timestamp_java, $reservation_array)) { // check for reservation $calendar_output .= '<td class="calendar-reserved ' . $has_past_class . ' " data-curent-date="' . $timestamp_java . '">'; } else { // is not today and no resrvation $calendar_output .= '<td class="calendar-free ' . $has_past_class . '" data-curent-date="' . $timestamp_java . '">'; } } if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . get_day_link($thisyear, $thismonth, $day) . '" title="' . esc_attr($ak_titles_for_day[$day]) . "\">{$day}</a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table></div>"; if ($echo) { echo apply_filters('get_calendar', $calendar_output); } else { return apply_filters('get_calendar', $calendar_output); } }
function get_calendar($daylength = 1) { global $wpdb, $m, $monthnum, $year, $timedifference, $month, $month_abbrev, $weekday, $weekday_initial, $weekday_abbrev, $posts; // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); if (!$gotsome) return; } if (isset($_GET['w'])) { $w = ''.intval($_GET['w']); } // week_begins = 0 stands for sunday $week_begins = intval(get_settings('start_of_week')); $add_hours = intval(get_settings('gmt_offset')); $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = ''.zeroise(intval($monthnum), 2); $thisyear = ''.intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = ''.intval(substr($m, 0, 4)); $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')"); } elseif (!empty($m)) { $calendar = substr($m, 0, 6); $thisyear = ''.intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp') + get_settings('gmt_offset') * 3600); $thismonth = gmdate('m', current_time('timestamp') + get_settings('gmt_offset') * 3600); } $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year FROM $wpdb->posts WHERE post_date < '$thisyear-$thismonth-01' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); $next = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year FROM $wpdb->posts WHERE post_date > '$thisyear-$thismonth-01' AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' ) AND post_status = 'publish' ORDER BY post_date ASC LIMIT 1"); echo '<table id="wp-calendar"> <caption>' . $month[zeroise($thismonth, 2)] . ' ' . date('Y', $unixmonth) . '</caption> <thead> <tr>'; $day_abbrev = $weekday_initial; if ($daylength > 1) { $day_abbrev = $weekday_abbrev; } $myweek = array(); for ($wdcount=0; $wdcount<=6; $wdcount++) { $myweek[]=$weekday[($wdcount+$week_begins)%7]; } foreach ($myweek as $wd) { echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">" . $day_abbrev[$wd] . '</th>'; } echo ' </tr> </thead> <tfoot> <tr>'; if ($previous) { echo "\n\t\t".'<td abbr="' . $month[zeroise($previous->month, 2)] . '" colspan="3" id="prev"><a href="' . 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>'; } else { echo "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; } echo "\n\t\t".'<td class="pad"> </td>'; if ($next) { echo "\n\t\t".'<td abbr="' . $month[zeroise($next->month, 2)] . '" colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="View posts for ' . $month[zeroise($next->month, 2)] . ' ' . date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year)) . '">' . $month_abbrev[$month[zeroise($next->month, 2)]] . ' »</a></td>'; } else { echo "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; } echo ' </tr> </tfoot> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) FROM $wpdb->posts WHERE MONTH(post_date) = $thismonth AND YEAR(post_date) = $thisyear AND post_status = 'publish' AND post_date < '" . current_time('mysql') . '\'', ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari')) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom " ."FROM $wpdb->posts " ."WHERE YEAR(post_date) = '$thisyear' " ."AND MONTH(post_date) = '$thismonth' " ."AND post_date < '".current_time('mysql')."' " ."AND post_status = 'publish'" ); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { if (empty($ak_titles_for_day['day_'.$ak_post_title->dom])) { $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["$ak_post_title->dom"])) { // first one $ak_titles_for_day["$ak_post_title->dom"] = str_replace('"', '"', wptexturize($ak_post_title->post_title)); } else { $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . str_replace('"', '"', wptexturize($ak_post_title->post_title)); } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); if (0 != $pad) echo "\n\t\t".'<td colspan="'.$pad.'" class="pad"> </td>'; $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) echo "\n\t</tr>\n\t<tr>\n\t\t"; $newrow = false; 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))) echo '<td id="today">'; else echo '<td>'; if (in_array($day, $daywithpost)) { // any posts today? echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>"; } else { echo $day; } echo '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins)) $newrow = true; } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); if ($pad != 0 && $pad != 7) echo "\n\t\t".'<td class="pad" colspan="'.$pad.'"> </td>'; echo "\n\t</tr>\n\t</tbody>\n\t</table>"; }
function oplb_events_get_calendar($post_types = '', $initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $post_types = '\'event\''; $cache = array(); $key = md5($m . $monthnum . $year); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { remove_filter('get_calendar', 'oplb_events_get_calendar_filter'); $output = apply_filters('get_calendar', $cache[$key]); add_filter('get_calendar', 'oplb_events_get_calendar_filter'); if ($echo) { echo $output; return; } else { return $output; } } } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! if (!$posts) { $sql = "SELECT 1 as test FROM {$wpdb->posts} WHERE post_type IN ( {$post_types} ) AND post_status IN ('publish','future') LIMIT 1"; $gotsome = $wpdb->get_var($sql); if (!$gotsome) { $cache[$key] = ''; wp_cache_set('get_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT( ( DATE_ADD( '{$thisyear}0101' , INTERVAL {$d} DAY ) ) , '%m' ) "); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year\n FROM {$wpdb->posts}\n WHERE post_date < '{$thisyear}-{$thismonth}-01'\n AND post_type IN ( {$post_types} ) AND post_status IN ('publish','future')\n ORDER BY post_date DESC\n LIMIT 1"); $next = $wpdb->get_row("SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year\n FROM {$wpdb->posts}\n WHERE post_date > '{$thisyear}-{$thismonth}-01'\n AND MONTH( post_date ) != MONTH( '{$thisyear}-{$thismonth}-01' )\n AND post_type IN ( {$post_types} ) AND post_status IN ('publish','future')\n ORDER BY post_date ASC\n LIMIT 1"); //$calendar_caption = _x( '%1$s %2$s' , 'calendar caption' ); $calendar_caption = _x('%1$s', 'calendar caption'); $month_desc = $wp_locale->get_month($thismonth); switch ($month_desc) { case "Ιανουαρίου": $month_desc = 'ΙΑΝΟΥΑΡΙΟΣ'; break; case "Φεβρουαρίου": $month_desc = 'ΦΕΒΡΟΥΑΡΙΟΣ'; break; case "Μαρτίου": $month_desc = 'ΜΑΡΤΙΟΣ'; break; case "Απριλίου": $month_desc = 'ΑΠΡΙΛΙΟΣ'; break; case "Μαΐου": $month_desc = 'ΜΑΪΟΣ'; break; case "Ιουνίου": $month_desc = 'ΙΟΥΝΙΟΣ'; break; case "Ιουλίου": $month_desc = 'ΙΟΥΛΙΟΣ'; break; case "Αυγούστου": $month_desc = 'ΑΥΓΟΥΣΤΟΣ'; break; case "Σεπτεμβρίου": $month_desc = 'ΣΕΠΤΕΜΒΡΙΟΣ'; break; case "Οκτωβρίου": $month_desc = 'ΟΚΤΩΒΡΙΟΣ'; break; case "Νοεμβρίου": $month_desc = 'ΝΟΕΜΒΡΙΟΣ'; break; case "Δεκεμβρίου": $month_desc = 'ΔΕΚΕΜΒΡΙΟΣ'; break; } $calendar_output = '<table id="wp-events-calendar"> <h3>' . sprintf($calendar_caption, $month_desc, date('Y', $unixmonth)) . '</h3> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead>'; $calendar_output .= ' <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH( post_date )\n FROM {$wpdb->posts} WHERE MONTH( post_date ) = '{$thismonth}'\n AND YEAR( post_date ) = '{$thisyear}'\n AND post_type IN ( {$post_types} ) AND post_status IN ('publish','future')\n AND post_date < '" . current_time('mysql') . '\'', ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH( post_date ) as dom " . "FROM {$wpdb->posts} " . "WHERE YEAR( post_date ) = '{$thisyear}' " . "AND MONTH( post_date ) = '{$thismonth}' " . "AND post_date < '" . current_time('mysql') . "' " . "AND post_type IN ( {$post_types} ) AND post_status IN ('publish','future')"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . get_day_link($thisyear, $thismonth, $day) . '&post_type=event' . "\" title=\"" . esc_attr($ak_titles_for_day[$day]) . "\">{$day}</a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); remove_filter('get_calendar', 'oplb_events_get_calendar_filter'); $output = apply_filters('get_calendar', $calendar_output); add_filter('get_calendar', 'oplb_events_get_calendar_filter'); if ($echo) { echo $output; } else { return $output; } }
function wpestate_draw_month_price($property_price, $month_no, $custom_price_array, $unixmonth, $daywithpost, $thismonth, $thisyear, $last_day) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $week_begins = intval(get_option('start_of_week')); $initial = true; $echo = true; $table_style = ''; if ($month_no > 2) { $table_style = 'style="display:none;"'; } $calendar_output = '<div class="booking-calendar-wrapper-in-price booking-price col-md-6" data-mno="' . $month_no . '" ' . $table_style . '> <div class="month-title"> ' . date("F", mktime(0, 0, 0, $thismonth, 10)) . ' ' . $thisyear . ' </div> <table class="wp-calendar booking-calendar"> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { $timestamp = strtotime($day . '-' . $thismonth . '-' . $thisyear) . ' | '; $timestamp_java = strtotime($day . '-' . $thismonth . '-' . $thisyear); if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; $has_past_class = 'has_future'; $is_reserved = 0; $calendar_output .= '<td class="calendar-free ' . $has_past_class . '" data-curent-date="' . $timestamp_java . '">'; $calendar_output .= '<span class="day-label">' . $day . '</span>'; if (array_key_exists($timestamp_java, $custom_price_array)) { // custom price $calendar_output .= '<span class="custom_set_price">' . wpestate_show_price_custom($custom_price_array[$timestamp_java]) . '</span>'; } else { // default price $calendar_output .= '<span class="price-day">' . wpestate_show_price_custom($property_price) . '</span>'; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table></div>"; if ($echo) { echo apply_filters('get_calendar', $calendar_output); } else { return apply_filters('get_calendar', $calendar_output); } }
/** * Display calendar with days that have posts as links. * * The calendar is cached, which will be retrieved, if it exists. If there are * no posts for the month, then it will not be displayed. * * @param string $post_type * @param string $post_meta_date - The Meta key you want to compare against. This must be in the datatime format (ie 2013-12-25 08:15:55) * @return string|null String when retrieving, null when displaying. */ function grav_get_post_calendar($post_type = 'post', $post_meta_date = 'post_date') { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $field = !empty($post_meta_date) && $post_meta_date != 'post_date' ? 'meta_value' : 'post_date'; $linkage = $field == 'post_date' ? " WHERE " . $field . " " : ", " . $wpdb->postmeta . " WHERE post_id = ID AND meta_key = '" . $post_meta_date . "' AND " . $field . " "; $cache = array(); $key = md5($m . $monthnum . $year); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { return apply_filters('get_calendar', $cache[$key]); } } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = '" . $post_type . "' AND post_status = 'publish' LIMIT 1"); if (!$gotsome) { $cache[$key] = ''; wp_cache_set('get_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($_GET['cy']) && !empty($_GET['cm'])) { $thisyear = $_GET['cy']; $thismonth = $_GET['cm']; } elseif (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT MONTH(" . $field . ") AS month, YEAR(" . $field . ") AS year\n\t\tFROM {$wpdb->posts}" . $linkage . "< '{$thisyear}-{$thismonth}-01'\n\t\tAND post_type = '" . $post_type . "' AND post_status = 'publish'\n\t\t\tORDER BY " . $field . " DESC\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT MONTH(" . $field . ") AS month, YEAR(" . $field . ") AS year\n\t\tFROM {$wpdb->posts}" . $linkage . "> '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'\n\t\tAND post_type = '" . $post_type . "' AND post_status = 'publish'\n\t\t\tORDER BY " . $field . " ASC\n\t\t\tLIMIT 1"); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = ' <input type="hidden" name="cy" value="' . $thisyear . '"> <input type="hidden" name="cm" value="' . $thismonth . '"> <table id="wp-calendar"> <thead> <tr class="thead-top-row">'; if ($previous) { $calendar_output .= "\n\t\t" . '<td colspan="1" id="prev"><a href="?cy=' . $previous->year . '&cm=' . $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)))) . '"><</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="1" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="month" colspan="5">' . $wp_locale->get_month($thismonth) . '</td>'; if ($next) { $calendar_output .= "\n\t\t" . '<td colspan="1" id="next"><a href="?cy=' . $next->year . '&cm=' . $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)))) . '">></a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="1" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> <tr class="thead-bottom-row">'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">" . substr($day_name, 0, 1) . '<span class="calendar-weekday-abbreviation">' . substr($day_name, 1) . '</span></th>'; } $calendar_output .= ' </tr> </thead> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(" . $field . ")\n\t\tFROM {$wpdb->posts}" . $linkage . ">= '{$thisyear}-{$thismonth}-01 00:00:00'\n\t\tAND post_type = '" . $post_type . "' AND post_status = 'publish'\n\t\tAND " . $field . " <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_ids_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(" . $field . ") as dom " . "FROM {$wpdb->posts}" . $linkage . ">= '{$thisyear}-{$thismonth}-01 00:00:00' " . "AND " . $field . " <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " . "AND post_type = '" . $post_type . "' AND post_status = 'publish'"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; $ak_ids_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; $ak_ids_for_day["{$ak_post_title->dom}"] = $ak_post_title->ID; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td' . (in_array($day, $daywithpost) ? ' class="event"' : '') . '>'; } if (in_array($day, $daywithpost)) { $event_class = ''; $calendar_output .= '<a' . $event_class . ' href="' . get_permalink($ak_ids_for_day[$day]) . '" title="' . esc_attr($ak_titles_for_day[$day]) . "\">{$day}</a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); return apply_filters('get_calendar', $calendar_output); }
/** * Display calendar for Vicuna. */ function vicuna_calendar($initial = true) { $weekday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); global $wpdb, $m, $monthnum, $year, $timedifference, $wp_locale, $posts; $key = md5($m . $monthnum . $year); // get cache if ($cache = wp_cache_get('vicuna_calendar', 'calendar')) { if (isset($cache[$key])) { echo $cache[$key]; return; } } ob_start(); // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT ID from {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); if (!$gotsome) { return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); $add_hours = intval(get_option('gmt_offset')); $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours)); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $calendar = substr($m, 0, 6); $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date < '{$thisyear}-{$thismonth}-01'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date DESC\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT\tDISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date >\t'{$thisyear}-{$thismonth}-01'\n\t\tAND MONTH( post_date ) != MONTH( '{$thisyear}-{$thismonth}-01' )\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER\tBY post_date ASC\n\t\t\tLIMIT 1"); ?> <table class="calendar" cellpadding="0" cellspacing="0" summary="Monthly calendar"> <caption><?php if ($previous) { echo '<a href="' . get_month_link($previous->year, $previous->month) . '" title="Older" rel="nofollow">«</a>'; } else { echo '«'; } printf(__(' %1$s %2$s ', 'vicuna'), $wp_locale->get_month($thismonth), date('Y', $unixmonth)); if ($next) { echo '<a href="' . get_month_link($next->year, $next->month) . '" title="Newer">»</a>'; } else { echo '»'; } $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $weekday[($wdcount + $week_begins) % 7]; } ?> </caption> <tr> <?php foreach ($myweek as $wd) { // $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $day_name = mb_substr($wd, 0, 1); if ($day_name == "S") { echo "\t\t\t\t\t<th class=\"" . strtolower(substr($wd, 0, 3)) . "\" title=\"" . __($wd, 'vicuna') . "\">{$day_name}</th>\n"; } else { echo "\t\t\t\t\t<th title=\"" . __($wd, 'vicuna') . "\">{$day_name}</th>\n"; } } ?> </tr> <tr> <?php // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)\n\t\tFROM {$wpdb->posts} WHERE MONTH(post_date) = '{$thismonth}'\n\t\tAND YEAR(post_date) = '{$thisyear}'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\tAND post_date < '" . current_time('mysql') . '\'', ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari')) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom " . "FROM {$wpdb->posts} " . "WHERE YEAR(post_date) = '{$thisyear}' " . "AND MONTH(post_date) = '{$thismonth}' " . "AND post_date < '" . current_time('mysql') . "' " . "AND post_type = 'post' AND post_status = 'publish'"); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = str_replace('"', '"', wptexturize($ak_post_title->post_title)); } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . str_replace('"', '"', wptexturize($ak_post_title->post_title)); } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { for ($i = 0; $i < $pad; $i++) { echo "\t\t\t\t\t<td> </td>\n"; } } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { echo "\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n"; } $newrow = false; if ($day == gmdate('j', time() + get_option('gmt_offset') * 3600) && $thismonth == gmdate('m', time() + get_option('gmt_offset') * 3600) && $thisyear == gmdate('Y', time() + get_option('gmt_offset') * 3600)) { echo "\t\t\t\t\t<td class=\"today\">"; } else { echo "\t\t\t\t\t<td>"; } if (in_array($day, $daywithpost)) { // any posts today? echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"{$ak_titles_for_day[$day]}\">{$day}</a>"; } else { echo $day; } echo "</td>\n"; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { for ($i = 0; $i < $pad; $i++) { echo "\t\t\t\t\t<td> </td>\n"; } } echo "\t\t\t\t</tr>\n\t\t\t</table>\n"; $output = ob_get_contents(); ob_end_clean(); echo $output; $cache[$key] = $output; wp_cache_set('vicuna_calendar', $cache, 'calendar'); }
function wp_get_pcalendar($args = '') { global $wpdb, $m, $monthnum, $year, $timedifference, $month, $day, $posts; if (!empty($m)) { $m = preg_replace("/[^0-9]/", "", $m); $jy = substr($m, 0, 4); } elseif (!empty($year)) { $jy = $year; } if ($jy > 1500) { get_calendar(); } else { $pd = bn_parsidate::getInstance(); 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) { return; } } $week_begins = intval(get_option('start_of_week')); $w = isset($_GET['w']) ? intval($_GET['w']) : ''; $is_gregorian = false; if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $is_gregorian = true; $thisyear = gmdate('Y', current_time('timestamp') + get_option('gmt_offset') * 3600); $thismonth = gmdate('m', current_time('timestamp') + get_option('gmt_offset') * 3600); $thisday = gmdate('d', current_time('timestamp') + get_option('gmt_offset') * 3600); } if ($is_gregorian) { list($jthisyear, $jthismonth, $jthisday) = $pd->gregorian_to_persian($thisyear, $thismonth, $thisday); $unixmonth = $pd->gregurian_date('Y-m-d 00:00:00', "{$jthisyear}-{$jthismonth}-01"); } else { $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $jthisyear = $thisyear; $jthismonth = $thismonth; } } $jnextmonth = $jthismonth + 1; $jnextyear = $jthisyear; if ($jnextmonth > 12) { $jnextmonth = 1; $jnextyear++; } $start = $pd->gregurian_date('Y-m-d 00:00:00', "{$jthisyear}-{$jthismonth}-01"); $end = $pd->gregurian_date('Y-m-d 00:00:00', "{$jnextyear}-{$jnextmonth}-01"); $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t FROM {$wpdb->posts}\n\t WHERE post_date < '{$start}'\n\t AND post_type = 'post' AND post_status = 'publish'\n ORDER BY post_date DESC\n LIMIT 1"); $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t FROM {$wpdb->posts}\n\t WHERE post_date >= '{$end}'\n\t AND post_type = 'post' AND post_status = 'publish'\n ORDER BY post_date ASC\n LIMIT 1"); $calendar_output .= '<table id="wp-calendar" style="direction: rtl"> <caption>' . $pd->persian_month_names[(int) $jthismonth] . ' ' . $pd->persian_date('Y', $unixmonth) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $pd->persian_day_small[($wdcount + $week_begins) % 7]; } foreach ($myweek as $wd) { $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$wd}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { $previous_month = $jthismonth - 1; $previous_year = $jthisyear; if ($previous_month == 0) { $previous_month = 12; $previous_year--; } $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($previous_year, $previous_month) . '">« ' . $pd->persian_month_names[$previous_month] . '</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { $next_month = $jthismonth + 1; $next_year = $jthisyear; if ($next_month == 13) { $next_month = 1; $next_year++; } $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($next_year, $next_month) . '">' . $pd->persian_month_names[$next_month] . ' »</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; //____________________________________________________________________________________________________________________________________ $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date),MONTH(post_date),YEAR(post_date)\n FROM {$wpdb->posts} \n \tWHERE post_date > '{$start}' AND post_date < '{$end}'\n AND post_status = 'publish'", ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $pd->persian_date('j', "{$daywith['2']}-{$daywith['1']}-{$daywith['0']}", 'eng'); } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom, MONTH(post_date) as month, YEAR(post_date) as year \n FROM {$wpdb->posts} WHERE post_date >= '{$start}' \n AND post_date <= '{$end}' \n AND post_type = 'post' AND post_status = 'publish'", ARRAY_N); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); $ak_post_title[2] = $pd->persian_date('j', "{$ak_post_title['4']}-{$ak_post_title['3']}-{$ak_post_title['2']}", 'eng'); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day[$ak_post_title->dom])) { // first one $ak_titles_for_day[$ak_post_title->dom] = $post_title; } else { $ak_titles_for_day[$ak_post_title->dom] .= $ak_title_separator . $post_title; } } } $pad = calendar_week_mod(date('w', strtotime($unixmonth)) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . $pad . '" class="pad"> </td>'; } $daysinmonth = intval($pd->persian_date('t', $unixmonth, 'eng')); for ($day = 1; $day <= $daysinmonth; ++$day) { list($thiyear, $thismonth, $thisday) = $pd->persian_to_gregorian($jthisyear, $jthismonth, $day); if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($thisday == gmdate('j', time() + get_option('gmt_offset') * 3600) && $thismonth == gmdate('m', time() + get_option('gmt_offset') * 3600) && $thisyear == gmdate('Y', time() + get_option('gmt_offset') * 3600)) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } $p_day = empty($val['sep_datesnum']) ? $day : per_number($day); if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . get_day_link($jthisyear, $jthismonth, $day) . "\" title=\"{$ak_titles_for_day[$day]}\">{$p_day}</a>"; } else { $calendar_output .= $p_day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod($pd->gregurian_date('w', "{$jthisyear}-{$jthismonth}-{$day}") - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod($pd->gregurian_date('w', "{$jthisyear}-{$jthismonth}-{$day}", 'eng') - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . $pad . '"> </td>'; } echo $calendar_output . "\n\t</tr>\n\t</tbody>\n\t</table>"; }
/** * own widget function */ function ztjalali_calendar_widget($shortname = TRUE, $echo = TRUE, $thisyear = 0, $thismonth = 0) { global $wpdb, $posts, $wp; global $jdate_month_name, $ztjalali_option; if (isset($wp->query_vars['m'])) { $m_year = (int) substr($wp->query_vars['m'], 0, 4); $m_month = (int) substr($wp->query_vars['m'], 4, 2); if ($m_year < 1700) { list($m_year, $m_month, $tmp_day) = jalali_to_gregorian($m_year, $m_month, 15); } } elseif (isset($wp->query_vars['m'])) { $thisyear = (int) substr($wp->query_vars['m'], 0, 4); } if (empty($thisyear)) { if (isset($wp->query_vars['year'])) { $thisyear = (int) $wp->query_vars['year']; } elseif (isset($m_year)) { $thisyear = $m_year; } else { $thisyear = date('Y', time()); } } if (empty($thismonth)) { if (isset($wp->query_vars['monthnum'])) { $thismonth = (int) $wp->query_vars['monthnum']; } elseif (isset($m_month)) { $thismonth = $m_month; } else { $thismonth = date('m', time()); } } //doing: support $_GET['w'] // if (isset($_GET['w'])) // $w = '' . (int)($_GET['w']); // if (!empty($w)) { //// We need to get the month from MySQL // $thisyear = '' . (int)(substr($m, 0, 4)); // $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's // $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); // } /* doing : cache */ $cache = array(); $key = md5($thismonth . $thisyear); if ($cache = wp_cache_get('ztjalali_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { if ($echo) { /** This filter is documented in wp-includes/general-template.php */ echo apply_filters('ztjalali_calendar', $cache[$key]); return; } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('ztjalali_calendar', $cache[$key]); } } } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! 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('ztjalali_calendar', $cache, 'calendar'); return; } } if ($thisyear > 1700) { list($thisyear, $thismonth, $thisday) = gregorian_to_jalali($thisyear, $thismonth, 1); } $unixmonth = jmktime(0, 0, 0, $thismonth, 1, $thisyear); $jthisyear = $thisyear; $jthismonth = $thismonth; list($thisyear, $thismonth, $jthisday) = jalali_to_gregorian($jthisyear, $jthismonth, 1); $last_day = jdate('t', $unixmonth, FALSE, FALSE); // Get the next and previous month and year with at least one post $startdate = date("Y:m:d", jmktime(0, 0, 0, $jthismonth, 1, $jthisyear)); $enddate = date("Y:m:d", jmktime(23, 59, 59, $jthismonth, $last_day, $jthisyear)); $previous = $wpdb->get_row("SELECT DAYOFMONTH(post_date) AS `dayofmonth`,MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date < '{$startdate}'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date DESC\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT DAYOFMONTH(post_date) AS `dayofmonth`,MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date > '{$enddate} 23:59:59'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date ASC\n\t\t\tLIMIT 1"); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar" class="widget_calendar"> <caption>' . sprintf($calendar_caption, $jdate_month_name[(int) $jthismonth], jdate('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = $myshortweek = array(); $week_begins = (int) get_option('start_of_week'); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = ztjalali_get_week_name(($wdcount + $week_begins) % 7); $myshortweek[] = ztjalali_get_short_week_name(($wdcount + $week_begins) % 7); } foreach ($myweek as $k => $wd) { $day_name = true == $shortname ? $myshortweek[$k] : $wd; $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { $jprevious = gregorian_to_jalali($previous->year, $previous->month, $previous->dayofmonth); if ($ztjalali_option['change_url_date_to_jalali']) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($jprevious[0], $jprevious[1]) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'ztjalali'), $jdate_month_name[$jprevious[1]], jdate('Y', mktime(0, 0, 0, $previous->month, 1, $previous->year)))) . '">« ' . $jdate_month_name[$jprevious[1]] . '</a></td>'; } else { $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', 'ztjalali'), $jdate_month_name[$jprevious[1]], jdate('Y', mktime(0, 0, 0, $previous->month, 1, $previous->year)))) . '">« ' . $jdate_month_name[$jprevious[1]] . '</a></td>'; } } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { $jnext = gregorian_to_jalali($next->year, $next->month, $next->dayofmonth); if ($ztjalali_option['change_url_date_to_jalali']) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($jnext[0], $jnext[1]) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'ztjalali'), $jdate_month_name[$jnext[1]], jdate('Y', mktime(0, 0, 0, $next->month, 1, $next->year)))) . '">' . $jdate_month_name[$jnext[1]] . ' »</a></td>'; } else { $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', 'ztjalali'), $jdate_month_name[$jnext[1]], jdate('Y', mktime(0, 0, 0, $next->month, 1, $next->year)))) . '">' . $jdate_month_name[$jnext[1]] . ' »</a></td>'; } } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT post_date\n\t\tFROM {$wpdb->posts} WHERE post_date >= '{$startdate} 00:00:00'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\tAND post_date <= '{$enddate} 23:59:59'", ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $jdaywithpost[] = jdate('j', strtotime($daywith[0]), FALSE, FALSE); } } else { $jdaywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, post_date as dom " . "FROM {$wpdb->posts} " . "WHERE post_date >= '{$startdate} 00:00:00' " . "AND post_date <= '{$enddate} 23:59:59' " . "AND post_type = 'post' AND post_status = 'publish'"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); $jdom = $jdaywithpost[] = jdate('j', strtotime($ak_post_title->dom), FALSE, FALSE); if (empty($ak_titles_for_day['day_' . $jdom])) { $ak_titles_for_day['day_' . $jdom] = ''; } if (empty($ak_titles_for_day["{$jdom}"])) { // first one $ak_titles_for_day["{$jdom}"] = $post_title; } else { $ak_titles_for_day["{$jdom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(jdate('w', $unixmonth, false, false) - $week_begins); $pad--; if ($pad < 0) { $pad = 6; } if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $jdaysinmonth = (int) jdate('t', $unixmonth, FALSE, FALSE); for ($jday = 1; $jday <= $jdaysinmonth; ++$jday) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($jday == jdate('j', time(), FALSE, FALSE) && $jthismonth == jdate('m', time(), FALSE, FALSE) && $jthisyear == jdate('Y', time(), FALSE, FALSE)) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (in_array($jday, $jdaywithpost)) { // any posts today? $day = jalali_to_gregorian($jthisyear, $jthismonth, $jday); if ($ztjalali_option['change_url_date_to_jalali']) { $calendar_output .= '<a href="' . get_day_link($jthisyear, $jthismonth, $jday) . '" title="' . esc_attr($ak_titles_for_day[$jday]) . "\">{$jday}</a>"; } else { $calendar_output .= '<a href="' . get_day_link($day[0], $day[1], $day[2]) . '" title="' . esc_attr($ak_titles_for_day[$jday]) . "\">{$jday}</a>"; } } else { $calendar_output .= $jday; } $calendar_output .= '</td>'; jdate('w', jmktime(0, 0, 0, $jthismonth, $jday, $jthisyear), FALSE, FALSE); if (6 == calendar_week_mod(date('w', jmktime(0, 0, 0, $jthismonth, $jday, $jthisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', jmktime(0, 0, 0, $jthismonth, $jday, $jthisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('ztjalali_calendar', $cache, 'calendar'); if ($ztjalali_option['change_jdate_number_to_persian']) { $calendar_output = ztjalali_persian_num($calendar_output); } if ($echo) { /** * Filter the HTML calendar output. * * @since 3.0.0 * * @param string $calendar_output HTML output of the calendar. */ echo apply_filters('ztjalali_calendar', $calendar_output); } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('ztjalali_calendar', $calendar_output); } }
function axiom_get_calendar($onlyFirstLetter = false, $get_month = 0, $get_year = 0, $opt = array()) { global $m, $monthnum, $year, $wp_locale, $posts; if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($get_month) && !empty($get_year)) { $thismonth = '' . zeroise(intval($get_month), 2); $thisyear = '' . intval($get_year); } else { if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { $thisyear = '' . intval(substr($m, 0, 4)); $thismonth = strtotime("+{$w} weeks", "{$thisyear}-01-01"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); // Post types in array $posts_types = explode(',', !empty($opt['post_type']) ? $opt['post_type'] : 'post'); // Translators: Calendar caption: 1: month name, 2: 4-digit year $calendar_caption = _x('%1$s %2$s', 'calendar caption', 'axiom'); $calendar_output = '<table id="wp-calendar-' . str_replace('.', '', mt_rand()) . '" class="wp-calendar"><thead><tr>'; // Get the previous month and year with at least one post $prev = apply_filters('axiom_filter_calendar_get_prev_month', array(), array('posts_types' => $posts_types, 'year' => $thisyear, 'month' => $thismonth, 'last_day' => $last_day)); $prev_month = !empty($prev) ? $prev['month'] : 0; $prev_year = !empty($prev) ? $prev['year'] : 0; $calendar_output .= '<th class="month_prev">'; if ($prev_year + $prev_month > 0) { $calendar_output .= '<a href="#" data-type="' . esc_attr(join(',', $posts_types)) . '" data-year="' . esc_attr($prev_year) . '" data-month="' . esc_attr($prev_month) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'axiom'), $wp_locale->get_month($prev_month), date('Y', mktime(0, 0, 0, $prev_month, 1, $prev_year)))) . '">' . '</a>'; } else { $calendar_output .= ' '; } $calendar_output .= '</th>'; // Get days with posts $posts = apply_filters('axiom_filter_calendar_get_curr_month_posts', array(), array('posts_types' => $posts_types, 'year' => $thisyear, 'month' => $thismonth, 'last_day' => $last_day)); if (count($posts) > (!empty($posts['done']) ? 1 : 0)) { // Get the current month and year $link = apply_filters('axiom_filter_calendar_get_month_link', '', array('posts_types' => $posts_types, 'year' => $thisyear, 'month' => $thismonth, 'last_day' => $last_day)); } else { $link = ''; } $calendar_output .= '<th class="month_cur" colspan="5">' . ($link ? '<a href="' . esc_url($link) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'axiom'), $wp_locale->get_month($thismonth), date('Y', mktime(0, 0, 0, $thismonth, 1, $thisyear)))) . '">' : '') . sprintf($calendar_caption, $wp_locale->get_month($thismonth), '<span>' . date('Y', $unixmonth) . '</span>') . ($link ? '</a>' : '') . '</th>'; // Get the next month and year with at least one post $next = apply_filters('axiom_filter_calendar_get_next_month', array(), array('posts_types' => $posts_types, 'year' => $thisyear, 'month' => $thismonth, 'last_day' => $last_day)); $next_month = !empty($next) ? $next['month'] : 0; $next_year = !empty($next) ? $next['year'] : 0; $calendar_output .= '<th class="month_next">'; if ($next_year + $next_month > 0) { $calendar_output .= '<a href="#" data-type="' . esc_attr(join(',', $posts_types)) . '" data-year="' . esc_attr($next_year) . '" data-month="' . esc_attr($next_month) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'axiom'), $wp_locale->get_month($next_month), date('Y', mktime(0, 0, 0, $next_month, 1, $next_year)))) . '">' . '</a>'; } else { $calendar_output .= ' '; } $calendar_output .= '</th></tr><tr>'; // Show Week days $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = $onlyFirstLetter ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "<th class=\"weekday\" scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= '</tr></thead><tbody><tr>'; // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if ($pad != 0) { $calendar_output .= '<td colspan="' . esc_attr($pad) . '" class="pad"><span class="day_wrap"> </span></td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "</tr><tr>"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td class="today">'; } else { $calendar_output .= '<td class="day">'; } if (!empty($posts[$day])) { $calendar_output .= '<a class="day_wrap" href="' . esc_url(!empty($posts[$day]['link']) ? $posts[$day]['link'] : get_day_link($thisyear, $thismonth, $day)) . '" title="' . esc_attr(is_int($posts[$day]['titles']) ? $posts[$day]['titles'] . ' ' . __('items', 'axiom') : $posts[$day]['titles']) . "\">{$day}</a>"; } else { $calendar_output .= '<span class="day_wrap">' . $day . '</span>'; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= '<td class="pad" colspan="' . esc_attr($pad) . '"><span class="day_wrap"> </span></td>'; } $calendar_output .= "</tr></tbody></table>"; return $calendar_output; }
function getThemeRexCalendar($onlyFirstLetter = false, $get_month = 0, $get_year = 0, $opt = array()) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; // Check additional options $tmp = explode(',', !empty($opt['post_type']) ? $opt['post_type'] : 'post'); $post_type = $post_type_full = ''; $sql_post_type = $sql_post_type_full = ''; $post_type_prepare = 'post'; $post_status_prepare = 'publish'; $post_type_prepare = esc_sql($post_type_prepare); $post_status_prepare = esc_sql($post_status_prepare); $events_present = false; for ($i = 0; $i < count($tmp); $i++) { $tmp[$i] = trim($tmp[$i]); if ($tmp[$i] == 'product' && !function_exists('is_woocommerce')) { continue; } if ($tmp[$i] == 'tribe_events') { if (!class_exists('TribeEvents')) { continue; } $events_present = true; } else { $post_type .= (!empty($post_type) ? ',' : '') . $tmp[$i]; $sql_post_type .= (!empty($sql_post_type) ? "','" : '') . $tmp[$i]; } $post_type_full .= (!empty($post_type_full) ? ',' : '') . $tmp[$i]; $sql_post_type_full .= (!empty($sql_post_type_full) ? "','" : '') . $tmp[$i]; } if (!empty($sql_post_type)) { $sql_post_type = "{$wpdb->posts}.post_type " . (themerex_strpos($sql_post_type, ',') !== false ? 'IN (' : '=') . "'" . $sql_post_type . "'" . (themerex_strpos($sql_post_type, ',') !== false ? ')' : ''); } if (!empty($sql_post_type_full)) { $sql_post_type_full = "{$wpdb->posts}.post_type " . (themerex_strpos($sql_post_type_full, ',') !== false ? 'IN (' : '=') . "'" . $sql_post_type_full . "'" . (themerex_strpos($sql_post_type_full, ',') !== false ? ')' : ''); } // Create Events object (if need) $sql_from = $sql_from_full = $wpdb->posts; if ($events_present) { $events = TribeEvents::instance(); $sql_from = $wpdb->posts . "\n\t\t\t\tINNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} as tribe_event_end_date ON ({$wpdb->posts}.ID = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate')\n\t\t\t"; } $prv = current_user_can('read_private_pages') && current_user_can('read_private_posts') ? " OR {$wpdb->posts}.post_status='private'" : ''; $sql_status = (!empty($prv) ? '(' : '') . "{$wpdb->posts}.post_status='publish'" . (!empty($prv) ? $prv : '') . (!empty($prv) ? ')' : ''); // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var($wpdb->prepare("SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s LIMIT 1", $post_type_prepare, $post_status_prepare)); if (!$gotsome) { $cache[$key] = ''; wp_cache_set('get_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($get_month) && !empty($get_year)) { $thismonth = '' . zeroise(intval($get_month), 2); $thisyear = '' . intval($get_year); } else { if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var($wpdb->prepare("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')")); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); $last_day = esc_sql($last_day); $thismonth = esc_sql($thismonth); $thisyear = esc_sql($thisyear); /// translators: Calendar caption: 1: month name, 2: 4-digit year $calendar_caption = _x('%1$s %2$s', 'calendar caption', 'themerex'); $calendar_output = ' <table id="wp-calendar-' . str_replace('.', '', mt_rand()) . '" class="wp-calendar"> <thead> <tr>'; // Get the previous month and year with at least one post $prev_month = $prev_year = 0; if (!empty($post_type)) { $previous = $wpdb->get_row("SELECT MONTH({$wpdb->posts}.post_date) AS month, YEAR({$wpdb->posts}.post_date) AS year\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tWHERE {$sql_status} AND ({$sql_post_type} AND {$wpdb->posts}.post_date < '" . esc_sql($thisyear) . '-' . esc_sql($thismonth) . "-01')\n\t\t\t\tORDER BY {$wpdb->posts}.post_date DESC\n\t\t\t\tLIMIT 1"); if ($previous) { $prev_month = $previous->month; $prev_year = $previous->year; } } if ($events_present) { $previous = $wpdb->get_row("SELECT MONTH(tribe_event_end_date.meta_value) AS month, YEAR(tribe_event_end_date.meta_value) AS year\n\t\t\t\tFROM {$sql_from}\n\t\t\t\tWHERE {$sql_status} AND ({$wpdb->posts}.post_type = 'tribe_events' AND tribe_event_end_date.meta_value < '" . esc_sql($thisyear) . '-' . esc_sql($thismonth) . "-01')\n\t\t\t\tORDER BY tribe_event_end_date.meta_value DESC\n\t\t\t\tLIMIT 1"); if ($previous && $previous->year . '-' . $previous->month > $prev_year . '-' . $prev_month) { $prev_month = $previous->month; $prev_year = $previous->year; } } // Get the current month and year $calendar_output .= '<th class="curMonth" colspan="7"><a href="' . (empty($post_type) && $events_present ? $events->getLink('month', $thisyear . '-' . $thismonth, null) : get_month_link($thisyear, $thismonth)) . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'themerex'), $wp_locale->get_month($thismonth), date('Y', mktime(0, 0, 0, $thismonth, 1, $thisyear)))) . '">' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), '') . '</a></th>'; // Get the next month and year with at least one post $next_month = $next_year = 0; $sql_date = $events_present && empty($post_type) ? "{$wpdb->postmeta}.meta_value" : "{$wpdb->posts}.post_date"; if (!empty($post_type)) { $next = $wpdb->get_row("SELECT MONTH({$wpdb->posts}.post_date) AS month, YEAR({$wpdb->posts}.post_date) AS year\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tWHERE {$sql_status} AND ({$sql_post_type} AND {$wpdb->posts}.post_date > '" . esc_sql($thisyear) . '-' . esc_sql($thismonth) . "-{$last_day} 23:59:59')\n\t\t\t\tORDER BY {$wpdb->posts}.post_date ASC\n\t\t\t\tLIMIT 1"); if ($next) { $next_month = $next->month; $next_year = $next->year; } } if ($events_present) { $next = $wpdb->get_row("SELECT MONTH({$wpdb->postmeta}.meta_value) AS month, YEAR({$wpdb->postmeta}.meta_value) AS year\n\t\t\t\tFROM {$sql_from}\n\t\t\t\tWHERE {$sql_status} AND ({$wpdb->posts}.post_type = 'tribe_events' AND {$wpdb->postmeta}.meta_key = '_EventStartDate' AND {$wpdb->postmeta}.meta_value > '" . esc_sql($thisyear) . '-' . esc_sql($thismonth) . "-{$last_day} 23:59:59')\n\t\t\t\tORDER BY {$sql_date} ASC\n\t\t\t\tLIMIT 1"); if ($next && $next->year . '-' . $next->month > $next_year . '-' . $next_month) { $next_month = $next->month; $next_year = $next->year; } } $calendar_output .= '</tr><tr>'; // Show Week days $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { //$day_name = $onlyFirstLetter ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $day_name = $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t\t\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH({$sql_date})\n\t\t\tFROM {$sql_from}\n\t\t\tWHERE {$sql_status} AND\n\t\t\t\t(" . (!empty($post_type) ? "({$sql_post_type} AND {$wpdb->posts}.post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' AND {$wpdb->posts}.post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')" : '') . ($events_present ? (!empty($post_type) ? ' OR ' : '') . "\n\t\t\t\t\t\t\t{$wpdb->posts}.post_type = 'tribe_events' AND {$wpdb->postmeta}.meta_key = '_EventStartDate'\n\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t({$wpdb->postmeta}.meta_value >= '{$thisyear}-{$thismonth}-01 00:00:00' AND {$wpdb->postmeta}.meta_value <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\tOR (tribe_event_end_date.meta_value >= '{$thisyear}-{$thismonth}-01 00:00:00' AND tribe_event_end_date.meta_value <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\tOR ({$wpdb->postmeta}.meta_value < '{$thisyear}-{$thismonth}-01' AND tribe_event_end_date.meta_value >= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t" : '') . ")", ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT {$wpdb->posts}.ID, {$wpdb->posts}.post_title, DAYOFMONTH({$sql_date}) as dom\n\t\t\tFROM {$sql_from}\n\t\t\tWHERE {$sql_status} AND\n\t\t\t\t(" . (!empty($post_type) ? "({$sql_post_type} AND {$wpdb->posts}.post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' AND {$wpdb->posts}.post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')" : '') . ($events_present ? (!empty($post_type) ? ' OR ' : '') . "\n\t\t\t\t\t\t\t{$wpdb->posts}.post_type = 'tribe_events' AND {$wpdb->postmeta}.meta_key = '_EventStartDate'\n\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t({$wpdb->postmeta}.meta_value >= '{$thisyear}-{$thismonth}-01 00:00:00' AND {$wpdb->postmeta}.meta_value <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\tOR (tribe_event_end_date.meta_value >= '{$thisyear}-{$thismonth}-01 00:00:00' AND tribe_event_end_date.meta_value <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\tOR ({$wpdb->postmeta}.meta_value < '{$thisyear}-{$thismonth}-01' AND tribe_event_end_date.meta_value >= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59')\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t" : '') . ")"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= ' <td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t\t\t\t</tr>\n\t\t\t\t<tr>"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= ' <td class="today">'; } else { $calendar_output .= ' <td>'; } if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . (empty($post_type) && $events_present ? $events->getLink('day', $thisyear . '-' . $thismonth . '-' . $day, null) : get_day_link($thisyear, $thismonth, $day)) . '" title="' . esc_attr($ak_titles_for_day[$day]) . "\">{$day}</a>"; } else { $calendar_output .= '<span>' . $day . '</span>'; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= ' <td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t\t\t\t</tr>\n\t\t\t</tbody>\n\t\t"; $calendar_output .= '<tfoot> <tr> <th colspan="4" class="prevMonth">'; if ($prev_year + $prev_month > 0) { $calendar_output .= '<div class="left"><a href="#" data-type="' . esc_attr($post_type_full) . '" data-year="' . $prev_year . '" data-month="' . $prev_month . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'themerex'), $wp_locale->get_month($prev_month), date('Y', mktime(0, 0, 0, $prev_month, 1, $prev_year)))) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($prev_month)) . '</a></div>'; } else { $calendar_output .= ' '; } $calendar_output .= ' </th> <th colspan="3" class="nextMonth">'; if ($next_year + $next_month > 0) { $calendar_output .= '<div class="right"><a href="#" data-type="' . esc_attr($post_type_full) . '" data-year="' . $next_year . '" data-month="' . $next_month . '" title="' . esc_attr(sprintf(__('View posts for %1$s %2$s', 'themerex'), $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></div>'; } else { $calendar_output .= ' '; } $calendar_output .= ' </th> </tr> </tfoot> '; $calendar_output .= "</table>"; return $calendar_output; }
function get_future_calendar($thismonth = '', $thisyear = '', $onclick = 1, $initial = true) { global $wpdb, $timedifference, $wp_locale; $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); $add_hours = intval(get_option('gmt_offset')); $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours)); echo '<table id="wp-calendar"> <caption><em>' . $wp_locale->get_month($thismonth) . ' ' . $thisyear . '</em></caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); echo "\n\t\t<th abbr=\"{$wd}\" scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } echo ' </tr> </thead> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("\r\n\t\tSELECT \r\n\t\tDISTINCT DAYOFMONTH(meta_value)\r\n\t\tFROM {$wpdb->posts} \r\n \t\tINNER JOIN {$wpdb->postmeta} \r\n\t\tWHERE MONTH({$wpdb->postmeta}.meta_value) = '{$thismonth}'\r\n\t\tAND YEAR({$wpdb->postmeta}.meta_value) = '{$thisyear}'\r\n\t\tAND {$wpdb->posts}.post_type = 'appointment' \r\n\t\tAND {$wpdb->posts}.post_status = 'publish'\r\n\t\tAND {$wpdb->postmeta}.meta_value > '" . current_time('mysql') . '\'', ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari')) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); //sets the Density Thermometer $ak_posts_for_day = array(); $ak_post_titles = $wpdb->get_results("\r\n\t\tSELECT post_title, DAYOFMONTH(meta_value) as dom \r\n\t\tFROM {$wpdb->posts} \r\n \t\tINNER JOIN {$wpdb->postmeta} \r\n\t\tWHERE YEAR({$wpdb->postmeta}.meta_value) = '{$thisyear}' \r\n\t\tAND MONTH({$wpdb->postmeta}.meta_value) = '{$thismonth}' \r\n\t\tAND {$wpdb->postmeta}.meta_value > '" . current_time('mysql') . "' \r\n\t\tAND {$wpdb->posts}.post_type = 'appointment' AND {$wpdb->posts}.post_status = 'publish'"); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = str_replace('"', '"', wptexturize($ak_post_title->post_title)); } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . str_replace('"', '"', wptexturize($ak_post_title->post_title)); } $ak_posts_for_day["{$ak_post_title->dom}"] += 1; } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { echo "\n\t\t" . '<td colspan="' . $pad . '" class="pad"> </td>'; } //Determines the Density Thermometer colors $thermo = array("#BDFFBE", "#7AFFDE", "#2FEEFF", "#108BFF", "#0E72FF"); $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { echo "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', time() + get_option('gmt_offset') * 3600) && $thismonth == gmdate('m', time() + get_option('gmt_offset') * 3600) && $thisyear == gmdate('Y', time() + get_option('gmt_offset') * 3600)) { echo '<td style="font-weight:bold;">'; } else { echo '<td>'; } if ($onclick == 1) { $onclick1 = 'onclick="fcal_set_date(' . $day . ',' . ($thismonth - 1) . ',' . $thisyear . ')"'; } // any posts on that day? if (in_array($day, $daywithpost)) { //Outputs the Density Thermometer along with the day... echo '<span style="padding:5px;background-color:' . ($ak_posts_for_day[$day] <= Count($thermo) ? $thermo[$ak_posts_for_day[$day] - 1] : $thermo[Count($thermo) - 1]) . ';" title="' . $ak_titles_for_day[$day] . ' ' . $onclick1 . '">' . $day . '</span>'; } else { echo '<span ' . $onclick1 . ' >' . $day . '</span>'; } echo '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { echo "\n\t\t" . '<td class="pad" colspan="' . $pad . '"> </td>'; } echo "\n\t</tr>\n\t</tbody>\n\t</table>"; }
/** * Display calendar with days that have posts as links. * * The calendar is cached, which will be retrieved, if it exists. If there are * no posts for the month, then it will not be displayed. * * @since 1.0.0 * * @global wpdb $wpdb * @global int $m * @global int $monthnum * @global int $year * @global WP_Locale $wp_locale * @global array $posts * * @param bool $initial Optional, default is true. Use initial calendar names. * @param bool $echo Optional, default is true. Set to false for return. * @return string|void String when retrieving. */ function get_calendar($initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $key = md5($m . $monthnum . $year); $cache = wp_cache_get('get_calendar', 'calendar'); if ($cache && is_array($cache) && isset($cache[$key])) { /** This filter is documented in wp-includes/general-template.php */ $output = apply_filters('get_calendar', $cache[$key]); if ($echo) { echo $output; return; } return $output; } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT TOP 1 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'"); if (!$gotsome) { $cache[$key] = ''; wp_cache_set('get_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = (int) $_GET['w']; } // week_begins = 0 stands for Sunday $week_begins = (int) get_option('start_of_week'); $ts = current_time('timestamp'); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = zeroise(intval($monthnum), 2); $thisyear = (int) $year; } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = (int) substr($m, 0, 4); //it seems MySQL's weeks disagree with PHP's $d = ($w - 1) * 7 + 6; $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATEADD(DAY, {$d}, '{$thisyear}0101')), '%m')"); } elseif (!empty($m)) { $thisyear = (int) substr($m, 0, 4); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = zeroise((int) substr($m, 4, 2), 2); } } else { $thisyear = gmdate('Y', $ts); $thismonth = gmdate('m', $ts); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT TOP 1 MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date < '{$thisyear}-{$thismonth}-01'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY YEAR(post_date) DESC, MONTH(post_date) DESC"); $next = $wpdb->get_row("SELECT TOP 1 MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date > '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY YEAR(post_date) ASC, MONTH(post_date) ASC"); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar"> <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; $daywithpost = array(); // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAY(post_date)\n\t\tFROM {$wpdb->posts} WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\tAND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $newrow = false; $daysinmonth = (int) date('t', $unixmonth); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', $ts) && $thismonth == gmdate('m', $ts) && $thisyear == gmdate('Y', $ts)) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? $date_format = date(_x('F j, Y', 'daily archives date format'), strtotime("{$thisyear}-{$thismonth}-{$day}")); $label = sprintf(__('Posts published on %s'), $date_format); $calendar_output .= sprintf('<a href="%s" aria-label="%s">%s</a>', get_day_link($thisyear, $thismonth, $day), esc_attr($label), $day); } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); if ($echo) { /** * Filter the HTML calendar output. * * @since 3.0.0 * * @param string $calendar_output HTML output of the calendar. */ echo apply_filters('get_calendar', $calendar_output); return; } /** This filter is documented in wp-includes/general-template.php */ return apply_filters('get_calendar', $calendar_output); }
function get_custom_calendar($initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; global $wp_object_cache; $cache = array(); $key = md5($m . $monthnum . $year); /*if($cache = get_option('custom_calendar_datas', false)){ if(is_array($cache) && isset($cache[$key])){ if($echo){ echo apply_filters('get_custom_calendar', $cache[$key]); return; }else{ return apply_filters('get_custom_calendar', $cache[$key]); } } }*/ if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! /*if ( !$posts ) { $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'calendar' LIMIT 1"); if ( !$gotsome ) { $cache[ $key ] = ''; wp_cache_set( 'get_custom_calendar', $cache, 'calendar' ); return; } }*/ if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post /*$previous = $wpdb->get_row("SELECT DISTINCT MONTH($wpdb->term_taxonomy.description) AS month, YEAR($wpdb->term_taxonomy.description) AS year FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id WHERE $wpdb->term_taxonomy.description < '$thisyear-$thismonth-01' AND $wpdb->term_taxonomy.taxonomy = 'calendar' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->term_taxonomy.description DESC LIMIT 1"); $next = $wpdb->get_row("SELECT DISTINCT MONTH($wpdb->term_taxonomy.description) AS month, YEAR($wpdb->term_taxonomy.description) AS year FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id WHERE $wpdb->term_taxonomy.description > '$thisyear-$thismonth-01' AND $wpdb->term_taxonomy.taxonomy = 'calendar' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND MONTH( $wpdb->term_taxonomy.description ) != MONTH( '$thisyear-$thismonth-01' ) ORDER BY $wpdb->term_taxonomy.description ASC LIMIT 1");*/ $previous = new DateTime("{$thisyear}-{$thismonth}-01"); $previous->modify('-1 month'); $next = new DateTime("{$thisyear}-{$thismonth}-01"); $next->modify('+1 month'); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<div class="calendar"><table>'; /*if($previous){ $calendar_output .= '<a href="'.get_month_link($previous->year, $previous->month).'" class="ui-datepicker-prev ui-corner-all" title="'.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))).'"><span class="ui-icon ui-icon-circle-triangle-w"><Préc</span></a>'; } if($next){ $calendar_output .= '<a href="'.get_month_link($next->year, $next->month).'" class="ui-datepicker-next ui-corner-all" title="'.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))).'"><span class="ui-icon ui-icon-circle-triangle-e">Suiv></span></a>'; }*/ $calendar_output .= '<caption>' . ucfirst(sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth))) . '</caption>'; $calendar_output .= '<thead><tr><th><span title="Lundi">Lu</span></th><th><span title="Mardi">Ma</span></th><th><span title="Mercredi">Me</span></th><th><span title="Jeudi">Je</span></th><th><span title="Vendredi">Ve</span></th><th class="ui-datepicker-week-end"><span title="Samedi">Sa</span></th><th class="ui-datepicker-week-end"><span title="Dimanche">Di</span></th></tr></thead>'; $calendar_output .= '<tfoot><tr><td colspan="2" class="before">'; $calendar_output .= '<span id="date-' . $previous->format('Y-m') . '">«</span>'; $calendar_output .= '</td><td colspan="3" class="loading"><img src="' . get_bloginfo('template_url') . '/images/loader.gif" style="display:none;height:13px;" alt="chargement" /></td><td colspan="2" class="after">'; $calendar_output .= '<span id="date-' . $next->format('Y-m') . '">»</span>'; $calendar_output .= '</td></tr></tfoot><tbody><tr>'; /* TODO : mettre les jours bien avec le code suivant; $myweek = array(); for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); } foreach ( $myweek as $wd ) { $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; }*/ $sql = "SELECT DISTINCT DAYOFMONTH({$wpdb->term_taxonomy}.description)\n\t\tFROM {$wpdb->posts}\n\t\t\tLEFT JOIN {$wpdb->term_relationships}\n\t\t\t\tON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id\n\t\t\tLEFT JOIN {$wpdb->term_taxonomy} \n\t\t\t\tON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id\n\t\tWHERE MONTH({$wpdb->term_taxonomy}.description) = '{$thismonth}'\n\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\tAND {$wpdb->term_taxonomy}.taxonomy = 'calendar'\n\t\t\tAND YEAR({$wpdb->term_taxonomy}.description) = '{$thisyear}'\n\t\t\tAND {$wpdb->posts}.post_type = 'post'"; $dayswithposts = $wpdb->get_results($sql, ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } $ak_title_separator = "\n"; $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT \n\t\t\t{$wpdb->posts}.ID AS ID, \n\t\t\t{$wpdb->posts}.post_title AS post_title, \n\t\t\tDAYOFMONTH({$wpdb->term_taxonomy}.description) as dom \n\t\tFROM {$wpdb->posts}\n\t\tLEFT JOIN {$wpdb->term_relationships}\n\t\t\tON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id\n\t\tLEFT JOIN {$wpdb->term_taxonomy} \n\t\t\tON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id\n\t\tWHERE YEAR({$wpdb->term_taxonomy}.description) = '{$thisyear}' \n\t\t\tAND {$wpdb->term_taxonomy}.taxonomy = 'calendar'\n\t\t\tAND MONTH({$wpdb->term_taxonomy}.description) = '{$thismonth}' \n\t\t\tAND {$wpdb->term_taxonomy}.description < '" . current_time('mysql') . "' \n\t\t\tAND {$wpdb->posts}.post_type = 'post' \n\t\t\tAND {$wpdb->posts}.post_status = 'publish'"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td class="today">'; } elseif (in_array($day, $daywithpost)) { $calendar_output .= '<td class="has_post">'; } else { $calendar_output .= '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . get_calendar_day_link($thisyear, $thismonth, $day) . "\">{$day} \n\t\t\t\t<span class='calendar_desc'><span class='calendar_title'>{$day} " . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</span>' . nl2br($ak_titles_for_day[$day]) . "</span></a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $calendar_output .= '</div>'; $cache[$key] = $calendar_output; update_option('custom_calendar_datas', $cache); if ($echo) { echo apply_filters('get_custom_calendar', $calendar_output); } else { return apply_filters('get_custom_calendar', $calendar_output); } }
function get_event_calendar($initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $cache = array(); $key = md5($m . $monthnum . $year); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { if ($echo) { /** This filter is documented in wp-includes/general-template.php */ echo apply_filters('get_calendar', $cache[$key]); return; } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('get_calendar', $cache[$key]); } } } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'bf_events' AND post_status = 'publish' LIMIT 1"); if (!$gotsome) { $cache[$key] = ''; wp_cache_set('get_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } if (isset($_GET['calmonth'])) { $monthnum = '' . intval($_GET['calmonth']); } if (isset($_GET['calyear'])) { $year = '' . intval($_GET['calyear']); if ($year < 100) { $year = $year + 2000; } } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are $dt = new DateTime(); if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $dt->setTimezone(new DateTimeZone(get_option('timezone_string'))); $thisyear = $dt->format('Y'); $thismonth = $dt->format('m'); } $dt->setDate($thisyear, $thismonth, 1); $dt->setTime(0, 0, 0); // $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); // $last_day = date('t', $unixmonth); $last_day = $dt->format('t'); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT (FROM_UNIXTIME(`wp_postmeta`.`meta_value`,'%m')) AS month, \r\n (FROM_UNIXTIME(`wp_postmeta`.`meta_value`-(" . get_option('gmt_offset') * 3600 . "),'%y')) AS year\r\n\t\tFROM {$wpdb->postmeta} wp_postmeta\r\n\t\tLEFT JOIN {$wpdb->posts} wp_posts ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` \r\n WHERE `wp_postmeta`.`meta_key` = 'bf_events_startdate'\r\n\t\tAND `wp_postmeta`.`meta_value` < UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-01 00:00:00' )\r\n\t\tAND wp_posts.post_status = 'publish'\r\n\t\t\tORDER BY wp_postmeta.meta_value DESC\r\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT (FROM_UNIXTIME(`wp_postmeta`.`meta_value`,'%m')) AS month, \r\n (FROM_UNIXTIME(`wp_postmeta`.`meta_value`-(" . get_option('gmt_offset') * 3600 . "),'%y')) AS year\r\n\t\tFROM {$wpdb->postmeta} wp_postmeta\r\n\t\tLEFT JOIN {$wpdb->posts} wp_posts ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` \r\n WHERE `wp_postmeta`.`meta_key` = 'bf_events_startdate'\r\n AND `wp_postmeta`.`meta_value` > UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' )\r\n\t\tAND wp_posts.post_status = 'publish'\r\n\t\t\tORDER BY wp_postmeta.meta_value ASC\r\n\t\t\tLIMIT 1"); /* $wpdb->get_results("SELECT (FROM_UNIXTIME(`wp_postmeta`.`meta_value`-(" . get_option( 'gmt_offset' ) * 3600 . "),'%d')) as dom , `wp_postmeta`.`post_id` , `wp_posts`.`ID` , `wp_posts`.`post_title` FROM $wpdb->postmeta wp_postmeta LEFT JOIN $wpdb->posts wp_posts ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` WHERE `wp_postmeta`.`meta_key` = 'bf_events_startdate' AND `wp_posts`.`post_status` = 'publish' AND `wp_postmeta`.`meta_value` >= UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-01 00:00:00' ) + (" . get_option( 'gmt_offset' ) * 3600 . ") AND `wp_postmeta`.`meta_value` <= UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' ) + (" . get_option( 'gmt_offset' ) * 3600 . ")", OBJECT); */ /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar"> <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), $dt->format('Y')) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = true == $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . add_query_arg(array('calyear' => $previous->year, 'calmonth' => $previous->month)) . '" title="' . esc_attr(sprintf(__('View events 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>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . add_query_arg(array('calyear' => $next->year, 'calmonth' => $next->month)) . '" title="' . esc_attr(sprintf(__('View events 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>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; $ak_titles_for_day = array(); $dayswithposts = $wpdb->get_results("SELECT (FROM_UNIXTIME(`wp_postmeta`.`meta_value`,'%e')) as dom , \r\n `wp_postmeta`.`post_id` , `wp_posts`.`ID` , `wp_posts`.`post_title` \r\n\t\tFROM {$wpdb->postmeta} wp_postmeta\r\n\t\tLEFT JOIN {$wpdb->posts} wp_posts ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` \r\n\t\tWHERE `wp_postmeta`.`meta_key` = 'bf_events_startdate'\r\n\t\tAND `wp_posts`.`post_status` = 'publish'\r\n\t\tAND `wp_postmeta`.`meta_value` >= UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-01 00:00:00' )\r\n\t\tAND `wp_postmeta`.`meta_value` <= UNIX_TIMESTAMP( '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' )", OBJECT); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith->dom; $post_title = esc_attr(apply_filters('the_title', $daywith->post_title, $daywith->post_id)); if (empty($ak_titles_for_day[$daywith->dom])) { $ak_titles_for_day[$daywith->dom] = array(); $ak_titles_for_day[$daywith->dom][] = array('title' => $post_title, 'url' => get_permalink($daywith->ID)); } else { $ak_titles_for_day[$daywith->dom][] = array('title' => $post_title, 'url' => get_permalink($daywith->ID)); } } } else { $daywithpost = array(); } // echo print_r($ak_titles_for_day); // See how much we should pad in the beginning $pad = calendar_week_mod($dt->format('w') - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval($dt->format('t')); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? $title_div = "<div>"; if (isset($ak_titles_for_day[$day])) { // this test should not be necessary but prevents some errors foreach ($ak_titles_for_day[$day] as $ak_title) { $title_div .= "<a href='" . $ak_title['url'] . "' title='" . $ak_title['title'] . "'>" . $ak_title['title'] . "</a>"; } $title_div .= "</div>"; $calendar_output .= "<span>{$day}</span>" . $title_div; } } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); if ($echo) { /** * Filter the HTML calendar output. * * @since 3.0.0 * * @param string $calendar_output HTML output of the calendar. */ echo apply_filters('get_calendar', $calendar_output); } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('get_calendar', $calendar_output); } }
/** * Display calendar with days that have posts as links. * * The calendar is cached, which will be retrieved, if it exists. If there are * no posts for the month, then it will not be displayed. * * @since 1.0.0 * * @global wpdb $wpdb * @global int $m * @global int $monthnum * @global int $year * @global WP_Locale $wp_locale * @global array $posts * * @param bool $initial Optional, default is true. Use initial calendar names. * @param bool $echo Optional, default is true. Set to false for return. * @return string|void String when retrieving. */ function get_calendar($initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $key = md5($m . $monthnum . $year); if ($cache = wp_cache_get('get_calendar', 'calendar')) { if (is_array($cache) && isset($cache[$key])) { if ($echo) { /** This filter is documented in wp-includes/general-template.php */ echo apply_filters('get_calendar', $cache[$key]); return; } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('get_calendar', $cache[$key]); } } } if (!is_array($cache)) { $cache = array(); } // Quick check. If we have no posts at all, abort! 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_calendar', $cache, 'calendar'); return; } } if (isset($_GET['w'])) { $w = '' . intval($_GET['w']); } // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = '' . zeroise(intval($monthnum), 2); $thisyear = '' . intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = '' . intval(substr($m, 0, 4)); $d = ($w - 1) * 7 + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')"); } elseif (!empty($m)) { $thisyear = '' . intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date < '{$thisyear}-{$thismonth}-01'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date DESC\n\t\t\tLIMIT 1"); $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_date > '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\t\tORDER BY post_date ASC\n\t\t\tLIMIT 1"); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar"> <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ($wdcount = 0; $wdcount <= 6; $wdcount++) { $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7); } foreach ($myweek as $wd) { $day_name = $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ($previous) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; if ($next) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>'; } else { $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; $daywithpost = array(); // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)\n\t\tFROM {$wpdb->posts} WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'\n\t\tAND post_type = 'post' AND post_status = 'publish'\n\t\tAND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); if ($dayswithposts) { foreach ((array) $dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) { $ak_title_separator = "\n"; } else { $ak_title_separator = ', '; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " . "FROM {$wpdb->posts} " . "WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " . "AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " . "AND post_type = 'post' AND post_status = 'publish'"); if ($ak_post_titles) { foreach ((array) $ak_post_titles as $ak_post_title) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr(apply_filters('the_title', $ak_post_title->post_title, $ak_post_title->ID)); if (empty($ak_titles_for_day['day_' . $ak_post_title->dom])) { $ak_titles_for_day['day_' . $ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["{$ak_post_title->dom}"])) { // first one $ak_titles_for_day["{$ak_post_title->dom}"] = $post_title; } else { $ak_titles_for_day["{$ak_post_title->dom}"] .= $ak_title_separator . $post_title; } } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad"> </td>'; } $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { if (isset($newrow) && $newrow) { $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; } $newrow = false; if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp'))) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; } if (in_array($day, $daywithpost)) { // any posts today? $calendar_output .= '<a href="' . get_day_link($thisyear, $thismonth, $day) . '" title="' . esc_attr($ak_titles_for_day[$day]) . "\">{$day}</a>"; } else { $calendar_output .= $day; } $calendar_output .= '</td>'; if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) { $newrow = true; } } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> </td>'; } $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[$key] = $calendar_output; wp_cache_set('get_calendar', $cache, 'calendar'); if ($echo) { /** * Filter the HTML calendar output. * * @since 3.0.0 * * @param string $calendar_output HTML output of the calendar. */ echo apply_filters('get_calendar', $calendar_output); } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters('get_calendar', $calendar_output); } }
function amr_events_as_calendar($liststyle, $events, $id, $class = '', $initial = true) { /* startingpoint was wp calendar */ global $amr_options, $amr_listtype, $amr_limits, $amrW; global $amr_globaltz; global $change_view_allowed; global $wpdb, $wp_locale; global $amr_calendar_url; global $amr_event_columns; //20151018 $empty = ' '; $link = amr_get_day_link_stem(); // get the daylink stem // --- Note that if months set, then events will have started from beg of month */ $months = 1; $weeks = 2; // as default if (isset($amr_limits['months'])) { $months = $amr_limits['months']; //may need later on if we are going to show multiple boxes on one page $weeks = 0; } else { if (isset($amr_limits['weeks'])) { $weeks = $amr_limits['weeks']; $months = 0; } } // testing //$weeks = 2; // need weeks =2 else miss early ones // Let's figure out when we are $start = amr_newDateTime(); $today_day = $start->format('j'); $today_month = $start->format('m'); $today_year = $start->format('Y'); $start = clone $amr_limits['start']; $thismonth = $start->format('m'); $thisyear = $start->format('Y'); if (!($liststyle === 'weekscalendar')) { $start->setDate($thisyear, $thismonth, 1); } else { $start->setDate($thisyear, $thismonth, $start->format('j')); } // what was asked for in url (in case using small calendar as a selector ) if (!empty($_GET['start']) and is_numeric($_GET['start'])) { $selected_day = substr($_GET['start'], 6, 2); $selected_month = substr($_GET['start'], 4, 2); $selected_year = substr($_GET['start'], 0, 4); } else { $selected_day = $today_day; $selected_month = $today_month; $selected_year = $today_year; } $events = amr_check_for_multiday_events($events); // now have dummy multi day events added and field dummyYMD to use if (!($liststyle === 'weekscalendar')) { $bunchesofevents = amr_get_events_in_months_format($events, $months, $start); } else { $bunchesofevents = amr_get_events_in_weeks_format($events, $weeks, $start); } if ($liststyle === 'weekscalendar') { if (!empty($amr_options['listtypes'][$amr_listtype]['format']['Day'])) { $caption_format = $amr_options['listtypes'][$amr_listtype]['format']['Day']; } else { $caption_format = 'j M'; } } else { if (!empty($amr_options['listtypes'][$amr_listtype]['format']['Month'])) { $caption_format = $amr_options['listtypes'][$amr_listtype]['format']['Month']; } else { $caption_format = 'F,Y'; } } // if ( isset($_GET['w']) ) $w = ''.intval($_GET['w']); /* what sthis for ?*/ // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); if ($liststyle == 'smallcalendar') { // for compatibility with wordpress default $class = ' widget_calendar '; } if (empty($class)) { $class = $liststyle; } else { $class = $class . ' ' . $liststyle . ' '; } if (!empty($amr_limits['show_views']) and $change_view_allowed) { $views = amrical_calendar_views(); } else { $views = ''; } $html = $views; $calendar_output = ''; $multi_output = ''; if (empty($amr_limits['show_month_nav'])) { $navigation = ''; $tfoot = ''; } else { $navigation = amr_calendar_navigation($start, $months, $weeks, $liststyle); // include month year dropdown with links if ($liststyle == 'smallcalendar' and $months < 2) { $tfoot = '<tfoot><tr><td class="calendar_navigation" colspan="7">' . $navigation . '</td></tr></tfoot>'; } else { $tfoot = ''; $html .= '<div class="calendar_navigation">' . $navigation . '</div>'; } } $columns = $amr_event_columns; // 20151018 add so we can use easily//prepare_order_and_sequence ($amr_options['listtypes'][$amr_listtype]['compprop']); //var_dump($columns); if (empty($columns)) { return; } // now do for each month or week------------------------------------------------------------------------------------------------------- if (isset($_GET['debugwks'])) { echo '<br />Bunches of events = ' . count($bunchesofevents) . '<br />'; } foreach ($bunchesofevents as $ym => $bunchevents) { //also for weeks $thismonth = substr($ym, 4, 2); $thisyear = substr($ym, 0, 4); if (!($liststyle === 'weekscalendar')) { $start->setDate($thisyear, $thismonth, 1); } else { $start->setDate($thisyear, $thismonth, $start->format('j')); } if (ICAL_EVENTS_DEBUG) { echo '<br />' . $ym; } if (isset($_GET['debugwks'])) { echo '<br />weeks = ' . $weeks . ' ' . $start->format('c'); } $dayheaders = '<tr class="dayheaders">' . amr_calendar_colheaders($liststyle, $start) . '</tr>'; if ($liststyle === 'weekscalendar') { // then cannot use thead as can only have one thead per table- else is data $calendar_caption = apply_filters('amr_events_table_caption', amr_weeks_caption($start)); if (!empty($calendar_caption)) { $calendar_caption = '<tr class="caption"><th colspan="7">' . $calendar_caption . '</th></tr>'; } $calendar_output .= '<tbody>'; $calendar_output .= $dayheaders; } else { $calendar_caption = apply_filters('amr_events_table_caption', amr_date_i18n($caption_format, $start)); if (!empty($calendar_caption)) { $calendar_caption = '<caption>' . $calendar_caption . '</caption>'; } $calendar_output .= '<table ' . $id . ' class="' . $class . '" >' . $calendar_caption; $calendar_output .= '<thead>' . $dayheaders . '</thead>' . $tfoot . '<tbody>'; } // Get days with events $titles = array(); $eventsfortheday = array(); $dayswithevents = array(); if (ICAL_EVENTS_DEBUG) { echo '<br />Bunch events count=' . count($bunchevents); } if (!empty($bunchevents)) { // NOTE SINGULAR month // get the titles and events for each day $bunchevents = amr_sort_by_two_cols('dummytime', 'MultiDay', $bunchevents); //20140805 foreach ($bunchevents as $event) { // convert eventdate to display timezone now for day of month assignment, other dates will be // converted to display timezone at display time. if (empty($event['EventDate'])) { continue; } // if no date, we cannot display anywhere if (isset($event['dummyYMD'])) { //$month = $event['EventDate']->format('m'); //$month = substr($event['dummyYMD'],4,2); // quicker? //if (isset($_GET['debugwks'])) {echo '<br />Do we need monts=thismonth check?'.$month.' '.$thismonth;} //if ($month == $thismonth) { // this allows to have agenda with more months and events cached //$day = $event['dummyYMD']->format('j'); $day = ltrim(substr($event['dummyYMD'], 6, 2), '0'); // quicker? $dayswithevents[] = $day; // replace with listtype format $title = ''; if (isset($event['SUMMARY'])) { $title = $event['SUMMARY']; } if (is_array($title)) { $title = implode($title); } $titles[$day][] = $title; // $eventsfortheday[$day][] = $event; // } } } } if (isset($dayswithevents)) { $dayswithevents = array_unique($dayswithevents); } if (!($liststyle === 'smallcalendar') or !function_exists('amr_events_customisable_small_calendar_daytitles')) { $daytitles = amr_prepare_day_titles($titles, $liststyle); } // for large hover? unset($titles); //----------------------------------------------------------------------------------- if (!empty($eventsfortheday)) { if (ICAL_EVENTS_DEBUG) { echo ' we have ' . count($eventsfortheday) . ' days of events'; } foreach ($eventsfortheday as $day => $devents) { if (ICAL_EVENTS_DEBUG) { echo '<br />Day =' . $day . ' with ' . count($devents) . ' events '; } $dayhtml[$day] = amr_list_one_days_events($devents, $columns); if (function_exists('amr_events_customisable_small_calendar_daytitles') and $liststyle === 'smallcalendar') { $daytitles[$day] = amr_events_customisable_small_calendar_daytitles($devents, $columns); } //if (isset($_GET['debugwks'])) echo '<br />Day: '.$day.' '.$dayhtml[$day]; } } unset($eventsfortheday); // else echo 'EMPTY events forday'; /* ------See how much we should pad in the beginning */ $week = 1; $calendar_output .= "\n\t" . '<tr class="week week1">'; //----------------------------------------------------------------------------------- if ($liststyle === 'weekscalendar') { //if (isset ($weeks)) { $day1 = $start->format('j'); // set to start of week //The day of the month without leading zeros (1 to 31) //$daysinbunch = $day1+6; $daysinbunch = 7; } else { $pad = calendar_week_mod($start->format('w') - $week_begins); if (0 != $pad) { $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad">' . $empty . '</td>'; } $day1 = 1; $daysinbunch = $start->format('t'); //The number of days in the given month } if (isset($_GET['debugwks'])) { echo '<br />Day 1= ' . $day1; } $newrow = false; $nextdate = new Datetime(); //if cloning dont need tz $nextdate = clone $start; // for ( $day = $day1; $day <= $daysinbunch; ++$day ) { for ($i = 1; $i <= $daysinbunch; $i += 1) { if (isset($_GET['debugwks'])) { echo '<br />i = ' . $i; } // $calendar_output .= amr_handle_each_day ($thisyear, $thismonth, $day, $daytitles, $dayswithevents,$dayhtml); if (isset($newrow) && $newrow) { if ($week > 1) { // then we need to end the previous row $calendar_output .= AMR_NL . '</tr>'; $calendar_output .= AMR_NL . '<tr class="week week' . $week . '">' . AMR_NL; } //else echo 'new row but $week = '.$week; } $newrow = false; $lastinrow = ''; // check if after this we need a new row eg if month calendar// // if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) { if (6 == calendar_week_mod($nextdate->format('w') - $week_begins)) { $newrow = true; $lastinrow = ' endweek'; $week = $week + 1; // helps to balance out the multi month view } /* wp code - can't we do better ? */ //$datestring = $day.'-'.$thismonth.'-'.$thisyear; // must use hyphens for uk english dates, else it goes US //$dow = date('N',strtotime($datestring)); // does not like dates earlier than 1902 $dow = $nextdate->format('N'); $thisyear = $nextdate->format('Y'); $thismonth = $nextdate->format('m'); $day = $nextdate->format('j'); $hasevents = ''; if (!empty($amr_limits['day_links']) and $amr_limits['day_links'] and !empty($daytitles[$day])) { // then we have events for that day, so can link to it $hasevents = ' hasevents '; $daylink = '<a class="daylink" href="' . htmlentities(amr_get_day_link($thisyear, $thismonth, $day, $link)) . '" title="' . $daytitles[$day] . '">' . $day . '</a>'; } else { $daylink = $day; } if ($day == $today_day && $thismonth == $today_month && $thisyear == $today_year) { $today = ' today '; } else { $today = ''; } if ($day == $selected_day && $thismonth == $selected_month && $thisyear == $selected_year) { $selected = ' selected '; } else { $selected = ''; } $calendar_output .= '<td class="day' . $dow . $today . $selected . $hasevents . $lastinrow . '">'; if (!($liststyle === 'weekscalendar')) { $calendar_output .= '<div class="day">' . $daylink . '</div>'; } if (!empty($dayswithevents) and in_array($day, $dayswithevents)) { // any posts today? if (isset($_GET['debugwks'])) { echo '<br />Day=' . $day; } // if (($liststyle == 'largecalendar') if (in_array($liststyle, array('largecalendar', 'weekscalendar')) and !empty($dayhtml[$day])) { $calendar_output .= AMR_NL . $dayhtml[$day]; } } else { $calendar_output .= $empty; //' '; } $calendar_output .= '</td>'; date_modify($nextdate, '+1 day'); } // now check if we need to pad to the end of the week // $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); $pad = 6 - calendar_week_mod($dow - $week_begins); if ($pad != 0 && $pad != 7) { $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '"> ' . '</td>'; } else { $week = $week - 1; } if ($months > 1 and $liststyle == 'smallcalendar') { // pad so that they will all line up nicely in the multiview for ($w = $week; $week <= 5; ++$week) { $calendar_output .= "\n\t" . '</tr><tr><td class="pad" colspan="7" > </td>' . "\n\t"; } } if ($liststyle === 'weekscalendar') { $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t"; } else { $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; } $multi_output .= $calendar_output; $calendar_output = ''; if (isset($weeks)) { date_modify($start, '+7 days'); } else { date_modify($start, '+1 month'); } } // for each bunch (eg month?) if ($liststyle === 'weekscalendar') { // if we are doing weekly, we want only one table so all will line up $multi_output = '<table ' . $id . ' class="' . $class . '" >' . $multi_output . '</table><!-- end weekly table -->'; } $html .= $multi_output; return $html; }