Changeset 34463
- Timestamp:
- 09/23/2015 11:56:05 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/general-template.php
r34348 r34463 1612 1612 * @return string|void String when retrieving. 1613 1613 */ 1614 function get_calendar( $initial = true, $echo = true) {1614 function get_calendar( $initial = true, $echo = true ) { 1615 1615 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 1616 1616 1617 1617 $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; 1628 1627 } 1629 } 1630 1631 if ( !is_array($cache) ) 1628 1629 return $output; 1630 } 1631 1632 if ( ! is_array( $cache ) ) { 1632 1633 $cache = array(); 1634 } 1633 1635 1634 1636 // Quick check. If we have no posts at all, abort! 1635 if ( ! $posts ) {1637 if ( ! $posts ) { 1636 1638 $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 ) { 1638 1640 $cache[ $key ] = ''; 1639 1641 wp_cache_set( 'get_calendar', $cache, 'calendar' ); … … 1642 1644 } 1643 1645 1644 if ( isset( $_GET['w']) )1645 $w = ''.intval($_GET['w']);1646 1646 if ( isset( $_GET['w'] ) ) { 1647 $w = (int) $_GET['w']; 1648 } 1647 1649 // 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' ); 1649 1652 1650 1653 // 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 ) ) { 1655 1658 // 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; 1658 1662 $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 } 1665 1670 } 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 ); 1672 1677 1673 1678 // Get the next and previous month and year with at least one post … … 1688 1693 $calendar_caption = _x('%1$s %2$s', 'calendar caption'); 1689 1694 $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> 1691 1700 <thead> 1692 1701 <tr>'; … … 1694 1703 $myweek = array(); 1695 1704 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 ); 1698 1707 } 1699 1708 1700 1709 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 ); 1703 1712 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 1704 1713 } … … 1712 1721 1713 1722 if ( $previous ) { 1714 $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>'; 1723 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">« ' . 1724 $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) . 1725 '</a></td>'; 1715 1726 } else { 1716 1727 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; … … 1720 1731 1721 1732 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)) . ' »</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 ' »</a></td>'; 1723 1736 } else { 1724 1737 $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; … … 1745 1758 } 1746 1759 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"> </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"; 1771 1772 } 1772 }1773 1774 // See how much we should pad in the beginning1775 $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"> </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";1783 1773 $newrow = false; 1784 1774 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 ) ) { 1786 1778 $calendar_output .= '<td id="today">'; 1787 else1779 } else { 1788 1780 $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 { 1793 1794 $calendar_output .= $day; 1795 } 1794 1796 $calendar_output .= '</td>'; 1795 1797 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 ) ) { 1797 1799 $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) .'"> </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 ) .'"> </td>'; 1806 } 1804 1807 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; 1805 1808 … … 1816 1819 */ 1817 1820 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 ); 1823 1825 } 1824 1826
Note: See TracChangeset
for help on using the changeset viewer.