Advertisement
Guest User

Fixed custom get_calendar function for custom post types

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