Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. /* code to put in your functions.php file */
  2.  
  3. function widget_customCalendar($args) {
  4. extract($args);
  5. echo $before_widget;
  6. echo get_calendar_custom(7); /* 7 is the category id you want to limit posts to */
  7. echo $after_widget;
  8. }
  9.  
  10. if ( function_exists('register_sidebar_widget') ) {
  11. register_sidebar_widget('Customized Calendar', 'widget_customCalendar');
  12. }
  13.  
  14. function get_calendar_custom($catid,$initial = true) {
  15. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  16.  
  17.  
  18. $key = md5( $m . $monthnum . $year );
  19. if ( $cache = wp_cache_get( 'get_calendar_custom', 'calendar_custom' ) ) {
  20. if ( isset( $cache[ $key ] ) ) {
  21. echo $cache[ $key ];
  22. return;
  23. }
  24. }
  25.  
  26.  
  27. ob_start();
  28. // Quick check. If we have no posts at all, abort!
  29. if ( !$posts ) {
  30. $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
  31. if ( !$gotsome )
  32. return;
  33. }
  34.  
  35. if ( isset($_GET['w']) )
  36. $w = ''.intval($_GET['w']);
  37.  
  38. // week_begins = 0 stands for Sunday
  39. $week_begins = intval(get_option('start_of_week'));
  40.  
  41. // Let's figure out when we are
  42. if ( !empty($monthnum) && !empty($year) ) {
  43. $thismonth = ''.zeroise(intval($monthnum), 2);
  44. $thisyear = ''.intval($year);
  45. } elseif ( !empty($w) ) {
  46. // We need to get the month from MySQL
  47. $thisyear = ''.intval(substr($m, 0, 4));
  48. $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
  49. $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')");
  50. } elseif ( !empty($m) ) {
  51. $thisyear = ''.intval(substr($m, 0, 4));
  52. if ( strlen($m) < 6 )
  53. $thismonth = '01';
  54. else
  55. $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
  56. } else {
  57. $thisyear = gmdate('Y', current_time('timestamp'));
  58. $thismonth = gmdate('m', current_time('timestamp'));
  59. }
  60.  
  61. $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
  62.  
  63. // Get the next and previous month and year with at least one post
  64. $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
  65. FROM $wpdb->posts
  66. LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  67. LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  68.  
  69. WHERE post_date < '$thisyear-$thismonth-01'
  70.  
  71. AND $wpdb->term_taxonomy.term_id IN ($catid)
  72. AND $wpdb->term_taxonomy.taxonomy = 'category'
  73.  
  74. AND post_type = 'post' AND post_status = 'publish'
  75. ORDER BY post_date DESC
  76. LIMIT 1");
  77.  
  78. $next = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
  79. FROM $wpdb->posts
  80. LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  81. LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  82.  
  83. WHERE post_date > '$thisyear-$thismonth-01'
  84.  
  85. AND $wpdb->term_taxonomy.term_id IN ($catid)
  86. AND $wpdb->term_taxonomy.taxonomy = 'category'
  87.  
  88. AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
  89. AND post_type = 'post' AND post_status = 'publish'
  90. ORDER BY post_date ASC
  91. LIMIT 1");
  92.  
  93. echo '<div id="calendar_wrap">
  94. <table id="wp-calendar" summary="' . __('Calendar') . '">
  95. <caption>' . sprintf(_c('%1$s %2$s|Used as a calendar caption'), $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
  96. <thead>
  97. <tr>';
  98.  
  99. $myweek = array();
  100.  
  101. for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
  102. $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
  103. }
  104.  
  105. foreach ( $myweek as $wd ) {
  106. $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
  107. echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">$day_name</th>";
  108. }
  109.  
  110. echo '
  111. </tr>
  112. </thead>
  113.  
  114. <tfoot>
  115. <tr>';
  116.  
  117. if ( $previous ) {
  118. echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($previous->month) . '" colspan="3" id="prev"><a href="' .
  119. get_month_link($previous->year, $previous->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month),
  120. date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
  121. } else {
  122. echo "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
  123. }
  124.  
  125. echo "\n\t\t".'<td class="pad">&nbsp;</td>';
  126.  
  127. if ( $next ) {
  128. echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($next->month) . '" colspan="3" id="next"><a href="' .
  129. get_month_link($next->year, $next->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month),
  130. date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
  131. } else {
  132. echo "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
  133. }
  134.  
  135. echo '
  136. </tr>
  137. </tfoot>
  138.  
  139. <tbody>
  140. <tr>';
  141.  
  142. // Get days with posts
  143. $dyp_sql = "SELECT DISTINCT DAYOFMONTH(post_date)
  144. FROM $wpdb->posts
  145.  
  146. LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  147. LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  148.  
  149. WHERE MONTH(post_date) = '$thismonth'
  150.  
  151. AND $wpdb->term_taxonomy.term_id IN ($catid)
  152. AND $wpdb->term_taxonomy.taxonomy = 'category'
  153.  
  154. AND YEAR(post_date) = '$thisyear'
  155. AND post_type = 'post' AND post_status = 'publish'
  156. AND post_date < '" . current_time('mysql') . "'";
  157.  
  158. $dayswithposts = $wpdb->get_results($dyp_sql, ARRAY_N);
  159.  
  160. if ( $dayswithposts ) {
  161. foreach ( (array) $dayswithposts as $daywith ) {
  162. $daywithpost[] = $daywith[0];
  163. }
  164. } else {
  165. $daywithpost = array();
  166. }
  167.  
  168. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false)
  169. $ak_title_separator = "\n";
  170. else
  171. $ak_title_separator = ', ';
  172.  
  173. $ak_titles_for_day = array();
  174. $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
  175. ."FROM $wpdb->posts "
  176.  
  177. ."LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) "
  178. ."LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "
  179.  
  180. ."WHERE YEAR(post_date) = '$thisyear' "
  181.  
  182. ."AND $wpdb->term_taxonomy.term_id IN ($catid) "
  183. ."AND $wpdb->term_taxonomy.taxonomy = 'category' "
  184.  
  185. ."AND MONTH(post_date) = '$thismonth' "
  186. ."AND post_date < '".current_time('mysql')."' "
  187. ."AND post_type = 'post' AND post_status = 'publish'"
  188. );
  189. if ( $ak_post_titles ) {
  190. foreach ( (array) $ak_post_titles as $ak_post_title ) {
  191.  
  192. $post_title = apply_filters( "the_title", $ak_post_title->post_title );
  193. $post_title = str_replace('"', '"', wptexturize( $post_title ));
  194.  
  195. if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
  196. $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
  197. if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
  198. $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
  199. else
  200. $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
  201. }
  202. }
  203.  
  204.  
  205. // See how much we should pad in the beginning
  206. $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
  207. if ( 0 != $pad )
  208. echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>';
  209.  
  210. $daysinmonth = intval(date('t', $unixmonth));
  211. for ( $day = 1; $day <= $daysinmonth; ++$day ) {
  212. if ( isset($newrow) && $newrow )
  213. echo "\n\t</tr>\n\t<tr>\n\t\t";
  214. $newrow = false;
  215.  
  216. 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)) )
  217. echo '<td id="today">';
  218. else
  219. echo '<td>';
  220.  
  221. if ( in_array($day, $daywithpost) ) // any posts today?
  222. echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>";
  223. else
  224. echo $day;
  225. echo '</td>';
  226.  
  227. if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
  228. $newrow = true;
  229. }
  230.  
  231. $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
  232. if ( $pad != 0 && $pad != 7 )
  233. echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
  234.  
  235. echo "\n\t</tr>\n\t</tbody>\n\t</table></div>";
  236.  
  237. $output = ob_get_contents();
  238. ob_end_clean();
  239. echo $output;
  240. $cache[ $key ] = $output;
  241. wp_cache_set( 'get_calendar_custom', $cache, 'calendar_custom' );
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement