[wp-trac] [WordPress Trac] #29319: filter dayswithposts in widget calendar

WordPress Trac noreply at wordpress.org
Thu Feb 13 12:00:58 UTC 2020


#29319: filter dayswithposts in widget calendar
-------------------------------------------------+-------------------------
 Reporter:  kkarpieszuk                          |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  5.5
Component:  Widgets                              |     Version:  3.0
 Severity:  normal                               |  Resolution:
 Keywords:  needs-unit-tests has-patch needs-    |     Focuses:
  refresh                                        |
-------------------------------------------------+-------------------------

Comment (by BackuPs):

 Here is the worked out solution using get_posts

 {{{#!php
 <?php
         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', 'theme_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 = (bool) get_posts(array(
                                 'fields'=> 'ids',
                                 'suppress_filters'=> false,
                                 'posts_per_page'=> 1,
                         ));
                         if ( ! $gotsome ) {
                                 $nothing_found='No Calendar Posts Found
 !';
                                 // Make $nothing_found empty to still show
 the calendar even if there are no posts
                                 $nothing_found = apply_filters(
 'get_calendar_nothing_found', $nothing_found );
                                 if (!empty($nothing_found)) {
                                         $cache[ $key ] = $nothing_found;
                                         wp_cache_set( 'get_calendar',
 $cache, 'theme_calendar' );
                                         if ( $echo ) {
                                                 echo $nothing_found;
                                                 return;
                                         } else   return $nothing_found;
                                 }
                         }
                 }

                 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((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%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 = current_time( 'Y' );
                         $thismonth = current_time( 'm' );
                 }

                 $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
                 $last_day = gmdate( 't', $unixmonth );

                 $previous_sheet_posts = get_posts(
                         array(
                                 'post_type' => 'post',
                                 'post_status' => 'publish',
                                 'orderby' => 'date',
                                 'order' => 'DESC',
                                 'posts_per_page' => 1,
                                 'suppress_filters' => false,
                                 'date_query'=> array(
                                         'before' => array(
                                                 'month' =>$thismonth,
                                                 'year'=>$thisyear,
                                         ),
                                 ),
                         )
                 );
                 $next_sheet_posts = get_posts(
                         array(
                                 'post_type' => 'post',
                                 'post_status' => 'publish',
                                 'orderby' => 'date',
                                 'order' => 'ASC',
                                 'posts_per_page' => 1,
                                 'suppress_filters' => false,
                                 'date_query'=> array(
                                         'after' => array(
                                                 'month' =>$thismonth,
                                                 'year'=>$thisyear,
                                         ),
                                 ),
                         )
                 );
                 /* 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 class="calendar-caption">' . sprintf(
                         $calendar_caption,
                         $wp_locale->get_month( $thismonth ),
                         gmdate( 'Y', $unixmonth )
                 ) . '</caption>
                 <thead class="calendar-head">
                 <tr class="calendar-days">';

                 $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 class=\"calendar-
 day\" scope=\"col\" title=\"$wd\">$day_name</th>";
                 }

                 $calendar_output .= '
                 </tr>
                 </thead>

                 <tbody class="calendar-body">
                 <tr>';

                 $posts_of_month_titles = array();
                 $posts_of_month = get_posts(
                         array(
                                 'post_type' => 'post',
                                 'post_status' => 'publish',
                                 'orderby' => 'date',
                                 'order' => 'ASC',
                                 'posts_per_page' => -1,
                                 'suppress_filters' => false,
                                 'date_query'=> array(
                                         'month' =>$thismonth,
                                         'year'=>$thisyear,
                                 ),
                         )
                 );

                 if ( (bool) $posts_of_month ) {
                         foreach ( $posts_of_month as $post ) {
                                 $post_date = date_parse($post->post_date);
                                 $post_day = $post_date['day'];

                                 /** This filter is documented in wp-
 includes/post-template.php */
                                 $post_title = esc_attr( apply_filters(
 'the_title', $post->post_title, $post->ID ) );

                                 if ( ! isset( $posts_of_month_titles[
 $post_day ] ) )
                                 $posts_of_month_titles[ $post_day ] =
 array();

                                 $posts_of_month_titles[ $post_day ][] =
 $post_title;
                         }
                 }
                 // See how much we should pad in the beginning.
                 $pad = calendar_week_mod( gmdate( 'w', $unixmonth ) -
 $week_begins );
                 if ( 0 != $pad ) {
                         $calendar_output .= "\n\t\t".'<td colspan="'.
 esc_attr( $pad ) .'" class="pad"> </td>';
                 }

                 $newrow = false;
                 $daysinmonth = (int) gmdate( '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 ( current_time( 'j' ) == $day &&
                                 current_time( 'm' ) == $thismonth &&
                                 current_time( 'Y' ) == $thisyear ) {
                                 if ( array_key_exists( $day ,
 $posts_of_month_titles ) ) {
                                         $calendar_output .= '<td
 id="today" class="day-has-post">';
                                 } else $calendar_output .= '<td id="today"
 class="calendar-is-today">';
                         } else {
                                 if ( array_key_exists( $day ,
 $posts_of_month_titles ) ) {
                                         $calendar_output .= '<td  class
 ="day-has-post">';
                                 } else $calendar_output .= '<td>';
                         }

                         if ( array_key_exists( $day ,
 $posts_of_month_titles ) ) {
                                 // any posts today?
                                 $date_format = gmdate( _x( 'F j, Y',
 'daily archives date format','' ), strtotime(
 "{$thisyear}-{$thismonth}-{$day}" ) );
                                 /* translators: Post calendar label. %s:
 Date */
                                 $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( gmdate( 'w',
 mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
                                 $newrow = true;
                         }
                 }

                 $pad = 7 - calendar_week_mod( gmdate( 'w', mktime( 0, 0 ,
 0, $thismonth, $day, $thisyear ) ) - $week_begins );
                 if ( 0 != $pad && 7 != $pad ) {
                         $calendar_output .= "\n\t\t".'<td class="pad"
 colspan="'. esc_attr( $pad ) .'"> </td>';
                 }
                 $calendar_output .= "\n\t</tr>\n\t</tbody>";
                 $calendar_output .= "\n\t</table>";

                 $calendar_output .= '<nav aria-label="' . __( 'Previous
 and next months' ) . '" class="calendar-footer">';

                 if ( (bool) $previous_sheet_posts ) {
                         $previous = (object) date_parse(
 $previous_sheet_posts[0]->post_date );
                         $calendar_output .= "\n\t\t" . '<span id="prev"
 class="calendar-prev-next-wrap"><a href="' . get_month_link(
 $previous->year, $previous->month ) . '" class="calendar-prev-
 next">  « ' .
                                 $wp_locale->get_month_abbrev(
 $wp_locale->get_month( $previous->month ) ) .
                         '</a></span>';
                 } else {
                         $calendar_output .= "\n\t\t" . '<span id="prev
 calendar-prev-nex-wrapt"> </span>';
                 }

                 $calendar_output .= "\n\t\t" . '<span
 class="pad"> </span>';

                 if ( (bool) $next_sheet_posts ) {
                         $next = (object) date_parse(
 $next_sheet_posts[0]->post_date );
                         $calendar_output .= "\n\t\t" . '<span id="next"
 class="calendar-prev-next-wrap"><a href="' . get_month_link( $next->year,
 $next->month ) . '" class="calendar-prev-next">' .
                                 $wp_locale->get_month_abbrev(
 $wp_locale->get_month( $next->month ) ) .
                         ' »  </a></span>';
                 } else {
                         $calendar_output .= "\n\t\t" . '<span id="next"
 class="pad calendar-prev-next-wrap"> </span>';
                 }

                 $calendar_output .= '
                 </nav>';

                 $cache[ $key ] = $calendar_output;
                 wp_cache_set( 'get_calendar', $cache, 'theme_calendar' );

                 if ( $echo ) {
                         /**
                          * Filters 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 );
         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/29319#comment:26>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list