WordPress.org

Make WordPress Core

Changeset 34463


Ignore:
Timestamp:
09/23/2015 11:56:05 PM (5 years ago)
Author:
wonderboymusic
Message:

Calendar Widget: remove the title attribute for days and replace with aria-label. Spruce up some of this nasty code. Delete unnecessary bits.

Props wonderboymusic, elusiveunit, 5um17.
Fixes #30297.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r34348 r34463  
    16121612 * @return string|void String when retrieving.
    16131613 */
    1614 function get_calendar($initial = true, $echo = true) {
     1614function get_calendar( $initial = true, $echo = true ) {
    16151615    global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    16161616
    16171617    $key = md5( $m . $monthnum . $year );
    1618     if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
    1619         if ( is_array($cache) && isset( $cache[ $key ] ) ) {
    1620             if ( $echo ) {
    1621                 /** This filter is documented in wp-includes/general-template.php */
    1622                 echo apply_filters( 'get_calendar', $cache[$key] );
    1623                 return;
    1624             } else {
    1625                 /** This filter is documented in wp-includes/general-template.php */
    1626                 return apply_filters( 'get_calendar', $cache[$key] );
    1627             }
     1618    $cache = wp_cache_get( 'get_calendar', 'calendar' );
     1619
     1620    if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
     1621        /** This filter is documented in wp-includes/general-template.php */
     1622        $output = apply_filters( 'get_calendar', $cache[ $key ] );
     1623
     1624        if ( $echo ) {
     1625            echo $output;
     1626            return;
    16281627        }
    1629     }
    1630 
    1631     if ( !is_array($cache) )
     1628
     1629        return $output;
     1630    }
     1631
     1632    if ( ! is_array( $cache ) ) {
    16321633        $cache = array();
     1634    }
    16331635
    16341636    // Quick check. If we have no posts at all, abort!
    1635     if ( !$posts ) {
     1637    if ( ! $posts ) {
    16361638        $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
    1637         if ( !$gotsome ) {
     1639        if ( ! $gotsome ) {
    16381640            $cache[ $key ] = '';
    16391641            wp_cache_set( 'get_calendar', $cache, 'calendar' );
     
    16421644    }
    16431645
    1644     if ( isset($_GET['w']) )
    1645         $w = ''.intval($_GET['w']);
    1646 
     1646    if ( isset( $_GET['w'] ) ) {
     1647        $w = (int) $_GET['w'];
     1648    }
    16471649    // week_begins = 0 stands for Sunday
    1648     $week_begins = intval(get_option('start_of_week'));
     1650    $week_begins = (int) get_option( 'start_of_week' );
     1651    $ts = current_time( 'timestamp' );
    16491652
    16501653    // Let's figure out when we are
    1651     if ( !empty($monthnum) && !empty($year) ) {
    1652         $thismonth = ''.zeroise(intval($monthnum), 2);
    1653         $thisyear = ''.intval($year);
    1654     } elseif ( !empty($w) ) {
     1654    if ( ! empty( $monthnum ) && ! empty( $year ) ) {
     1655        $thismonth = zeroise( intval( $monthnum ), 2 );
     1656        $thisyear = (int) $year;
     1657    } elseif ( ! empty( $w ) ) {
    16551658        // We need to get the month from MySQL
    1656         $thisyear = ''.intval(substr($m, 0, 4));
    1657         $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
     1659        $thisyear = (int) substr( $m, 0, 4 );
     1660        //it seems MySQL's weeks disagree with PHP's
     1661        $d = ( ( $w - 1 ) * 7 ) + 6;
    16581662        $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
    1659     } elseif ( !empty($m) ) {
    1660         $thisyear = ''.intval(substr($m, 0, 4));
    1661         if ( strlen($m) < 6 )
    1662                 $thismonth = '01';
    1663         else
    1664                 $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
     1663    } elseif ( ! empty( $m ) ) {
     1664        $thisyear = (int) substr( $m, 0, 4 );
     1665        if ( strlen( $m ) < 6 ) {
     1666            $thismonth = '01';
     1667        } else {
     1668            $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
     1669        }
    16651670    } else {
    1666         $thisyear = gmdate('Y', current_time('timestamp'));
    1667         $thismonth = gmdate('m', current_time('timestamp'));
    1668     }
    1669 
    1670     $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
    1671     $last_day = date('t', $unixmonth);
     1671        $thisyear = gmdate( 'Y', $ts );
     1672        $thismonth = gmdate( 'm', $ts );
     1673    }
     1674
     1675    $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
     1676    $last_day = date( 't', $unixmonth );
    16721677
    16731678    // Get the next and previous month and year with at least one post
     
    16881693    $calendar_caption = _x('%1$s %2$s', 'calendar caption');
    16891694    $calendar_output = '<table id="wp-calendar">
    1690     <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
     1695    <caption>' . sprintf(
     1696        $calendar_caption,
     1697        $wp_locale->get_month( $thismonth ),
     1698        date( 'Y', $unixmonth )
     1699    ) . '</caption>
    16911700    <thead>
    16921701    <tr>';
     
    16941703    $myweek = array();
    16951704
    1696     for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
    1697         $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
     1705    for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
     1706        $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
    16981707    }
    16991708
    17001709    foreach ( $myweek as $wd ) {
    1701         $day_name = $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
    1702         $wd = esc_attr($wd);
     1710        $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
     1711        $wd = esc_attr( $wd );
    17031712        $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    17041713    }
     
    17121721
    17131722    if ( $previous ) {
    1714         $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
     1723        $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
     1724            $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
     1725        '</a></td>';
    17151726    } else {
    17161727        $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
     
    17201731
    17211732    if ( $next ) {
    1722         $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)) . ' &raquo;</a></td>';
     1733        $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
     1734            $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
     1735        ' &raquo;</a></td>';
    17231736    } else {
    17241737        $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
     
    17451758    }
    17461759
    1747     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
    1748         $ak_title_separator = "\n";
    1749     else
    1750         $ak_title_separator = ', ';
    1751 
    1752     $ak_titles_for_day = array();
    1753     $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    1754         ."FROM $wpdb->posts "
    1755         ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    1756         ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    1757         ."AND post_type = 'post' AND post_status = 'publish'"
    1758     );
    1759     if ( $ak_post_titles ) {
    1760         foreach ( (array) $ak_post_titles as $ak_post_title ) {
    1761 
    1762                 /** This filter is documented in wp-includes/post-template.php */
    1763                 $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
    1764 
    1765                 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
    1766                     $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
    1767                 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
    1768                     $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
    1769                 else
    1770                     $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
     1760    // See how much we should pad in the beginning
     1761    $pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins );
     1762    if ( 0 != $pad ) {
     1763        $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad">&nbsp;</td>';
     1764    }
     1765
     1766    $newrow = false;
     1767    $daysinmonth = (int) date( 't', $unixmonth );
     1768
     1769    for ( $day = 1; $day <= $daysinmonth; ++$day ) {
     1770        if ( isset($newrow) && $newrow ) {
     1771            $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    17711772        }
    1772     }
    1773 
    1774     // See how much we should pad in the beginning
    1775     $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
    1776     if ( 0 != $pad )
    1777         $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
    1778 
    1779     $daysinmonth = intval(date('t', $unixmonth));
    1780     for ( $day = 1; $day <= $daysinmonth; ++$day ) {
    1781         if ( isset($newrow) && $newrow )
    1782             $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    17831773        $newrow = false;
    17841774
    1785         if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
     1775        if ( $day == gmdate( 'j', $ts ) &&
     1776            $thismonth == gmdate( 'm', $ts ) &&
     1777            $thisyear == gmdate( 'Y', $ts ) ) {
    17861778            $calendar_output .= '<td id="today">';
    1787         else
     1779        } else {
    17881780            $calendar_output .= '<td>';
    1789 
    1790         if ( in_array($day, $daywithpost) ) // any posts today?
    1791                 $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
    1792         else
     1781        }
     1782
     1783        if ( in_array( $day, $daywithpost ) ) {
     1784            // any posts today?
     1785            $date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
     1786            $label = sprintf( __( 'Posts published on %s' ), $date_format );
     1787            $calendar_output .= sprintf(
     1788                '<a href="%s" aria-label="%s">%s</a>',
     1789                get_day_link( $thisyear, $thismonth, $day ),
     1790                esc_attr( $label ),
     1791                $day
     1792            );
     1793        } else {
    17931794            $calendar_output .= $day;
     1795        }
    17941796        $calendar_output .= '</td>';
    17951797
    1796         if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
     1798        if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
    17971799            $newrow = true;
    1798     }
    1799 
    1800     $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
    1801     if ( $pad != 0 && $pad != 7 )
    1802         $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
    1803 
     1800        }
     1801    }
     1802
     1803    $pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins );
     1804    if ( $pad != 0 && $pad != 7 ) {
     1805        $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr( $pad ) .'">&nbsp;</td>';
     1806    }
    18041807    $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
    18051808
     
    18161819         */
    18171820        echo apply_filters( 'get_calendar', $calendar_output );
    1818     } else {
    1819         /** This filter is documented in wp-includes/general-template.php */
    1820         return apply_filters( 'get_calendar', $calendar_output );
    1821     }
    1822 
     1821        return;
     1822    }
     1823    /** This filter is documented in wp-includes/general-template.php */
     1824    return apply_filters( 'get_calendar', $calendar_output );
    18231825}
    18241826
Note: See TracChangeset for help on using the changeset viewer.