Ticket #34093: 34093-7.diff
File 34093-7.diff, 8.3 KB (added by , 8 years ago) |
---|
-
src/wp-includes/general-template.php
1863 1863 * no posts for the month, then it will not be displayed. 1864 1864 * 1865 1865 * @since 1.0.0 1866 * @since 4.7 arguments changed to accept an associative array $args. 1867 * @since 4.7 `get_calendar_args` filter added. 1866 1868 * 1867 1869 * @global wpdb $wpdb 1868 1870 * @global int $m … … 1871 1873 * @global WP_Locale $wp_locale 1872 1874 * @global array $posts 1873 1875 * 1874 * @param bool $initial Optional, default is true. Use initial calendar names. 1875 * @param bool $echo Optional, default is true. Set to false for return. 1876 * @return string|void String when retrieving. 1876 * @param array $args{ 1877 * Optional. An array of arguments. 1878 * @type bool $initial. Use initial calendar names. Default true. 1879 * @type bool $echo. Should we echo the calendar. Default true. 1880 * @type string $post_type. The post type to be used on the calendar. Default `post`. 1881 * } 1882 * 1883 * @return string|void String when $args['echo'] is set to true. 1877 1884 */ 1878 function get_calendar( $ initial = true, $echo = true) {1885 function get_calendar( $args = array() ) { 1879 1886 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 1880 1887 1888 $defaults = array( 1889 'initial' => true, 1890 'echo' => true, 1891 'post_type' => 'post', 1892 ); 1893 1894 if ( ! is_array( $args ) ) { 1895 $backwards_compatible_args = func_get_args(); 1896 1897 if ( isset( $backwards_compatible_args[0] ) && is_bool( $backwards_compatible_args[0] ) ){ 1898 $defaults['initial'] = $backwards_compatible_args[0]; 1899 } 1900 1901 if ( isset( $backwards_compatible_args[1] ) && is_bool( $backwards_compatible_args[1] ) ){ 1902 $defaults['echo'] = $backwards_compatible_args[1]; 1903 } 1904 } 1905 1906 /** 1907 * Filter the `get_calander` function arguments before they are used. 1908 * 1909 * @since 4.7 1910 * @param array $args 1911 */ 1912 $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults) ); 1913 1881 1914 $key = md5( $m . $monthnum . $year ); 1882 1915 $cache = wp_cache_get( 'get_calendar', 'calendar' ); 1883 1916 1884 1917 if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) { 1885 1918 /** This filter is documented in wp-includes/general-template.php */ 1886 $output = apply_filters( 'get_calendar', $cache[ $key ] );1919 $output = apply_filters( 'get_calendar', $cache[ $key ], $args ); 1887 1920 1888 if ( $ echo) {1921 if ( $args['echo'] ) { 1889 1922 echo $output; 1890 1923 return; 1891 1924 } … … 1897 1930 $cache = array(); 1898 1931 } 1899 1932 1933 $post_type = $args['post_type']; 1934 if ( ! post_type_exists( $post_type ) ) { 1935 // set the post type back to 'post' if the filter value is not a valid post type 1936 $post_type = 'post'; 1937 } 1938 1900 1939 // Quick check. If we have no posts at all, abort! 1901 1940 if ( ! $posts ) { 1902 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); 1941 $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ); 1942 $gotsome = $wpdb->get_var( $prepared_query ); 1903 1943 if ( ! $gotsome ) { 1904 1944 $cache[ $key ] = ''; 1905 1945 wp_cache_set( 'get_calendar', $cache, 'calendar' ); … … 1940 1980 $last_day = date( 't', $unixmonth ); 1941 1981 1942 1982 // Get the next and previous month and year with at least one post 1943 $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year1983 $previous_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1944 1984 FROM $wpdb->posts 1945 1985 WHERE post_date < '$thisyear-$thismonth-01' 1946 AND post_type = 'post'AND post_status = 'publish'1986 AND post_type = %s AND post_status = 'publish' 1947 1987 ORDER BY post_date DESC 1948 LIMIT 1"); 1949 $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1988 LIMIT 1" , $post_type); 1989 $previous = $wpdb->get_row( $previous_prepared_query ); 1990 1991 $next_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1950 1992 FROM $wpdb->posts 1951 1993 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' 1952 AND post_type = 'post' AND post_status = 'publish' 1953 ORDER BY post_date ASC 1954 LIMIT 1"); 1994 AND post_type = %s AND post_status = 'publish' 1995 ORDER BY post_date ASC 1996 LIMIT 1", $post_type ); 1997 $next = $wpdb->get_row( $next_prepared_query ); 1955 1998 1956 1999 /* translators: Calendar caption: 1: month name, 2: 4-digit year */ 1957 2000 $calendar_caption = _x('%1$s %2$s', 'calendar caption'); … … 1971 2014 } 1972 2015 1973 2016 foreach ( $myweek as $wd ) { 1974 $day_name = $ initial? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );2017 $day_name = $args['initial'] ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); 1975 2018 $wd = esc_attr( $wd ); 1976 2019 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 1977 2020 } … … 2011 2054 $daywithpost = array(); 2012 2055 2013 2056 // Get days with posts 2014 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)2057 $dayswithposts_prepared_query = $wpdb->prepare( "SELECT DISTINCT DAYOFMONTH(post_date) 2015 2058 FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' 2016 AND post_type = 'post' AND post_status = 'publish' 2017 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); 2059 AND post_type = %s AND post_status = 'publish' 2060 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", $post_type); 2061 $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query , ARRAY_N); 2062 2018 2063 if ( $dayswithposts ) { 2019 2064 foreach ( (array) $dayswithposts as $daywith ) { 2020 2065 $daywithpost[] = $daywith[0]; … … 2073 2118 $cache[ $key ] = $calendar_output; 2074 2119 wp_cache_set( 'get_calendar', $cache, 'calendar' ); 2075 2120 2076 if ( $echo ) { 2077 /** 2078 * Filters the HTML calendar output. 2079 * 2080 * @since 3.0.0 2081 * 2082 * @param string $calendar_output HTML output of the calendar. 2083 */ 2084 echo apply_filters( 'get_calendar', $calendar_output ); 2121 /** 2122 * Filters the HTML calendar output. 2123 * 2124 * @since 3.0.0 2125 * @since 4.7.0 new argument $args added. 2126 * 2127 * @param string $calendar_output HTML output of the calendar. 2128 * @param array $args passed into get_calender function. $args also filtered earlier in get_calender(). 2129 */ 2130 $calendar_output = apply_filters( 'get_calendar', $calendar_output, $args ); 2131 2132 if ( $args['echo'] ) { 2133 echo $calendar_output; 2085 2134 return; 2086 2135 } 2087 /** This filter is documented in wp-includes/general-template.php */ 2088 return apply_filters( 'get_calendar', $calendar_output );2136 2137 return $calendar_output; 2089 2138 } 2090 2139 2091 2140 /** -
tests/phpunit/tests/functions/getCalendar.php
1 <?php 2 /* 3 $defaults = array( 4 'initial' => true, 5 'echo' => true, 6 'post_type' => 'post', 7 ); 8 */ 9 class Tests_Get_Calendar extends WP_UnitTestCase { 10 function setUp() { 11 parent::setUp(); 12 } 13 public static function wpSetUpBeforeClass( $factory ) { 14 $factory->post->create_many( 1, array( 'post_type' => 'post', 'post_author' => '1' ) ); 15 } 16 17 public function test_get_calendar_echo() { 18 $expected['echo'] = '<table id="wp-calendar">'; 19 ob_start(); 20 get_calendar( array( 'echo' => true ) ); 21 $function_output = ob_get_clean(); 22 $this->assertContains( $expected['echo'], $function_output ); 23 } 24 25 public function test_get_calendar_initial() { 26 $expected['initial_false'] = '<th scope="col" title="Monday">Mon</th>'; 27 ob_start(); 28 get_calendar( array( 'initial' => false ) ); 29 $initial_false_function_output = ob_get_clean(); 30 $this->assertContains( $expected['initial_false'], $initial_false_function_output ); 31 32 //invalidate the cache before the second function call 33 wp_cache_delete( 'get_calendar', 'calendar' ); 34 35 $expected['initial_true'] = '<th scope="col" title="Monday">M</th>'; 36 ob_start(); 37 get_calendar( array( 'initial' => true ) ); 38 $initial_true_function_output = ob_get_clean(); 39 $this->assertContains( $expected['initial_true'], $initial_true_function_output ); 40 } 41 }