Ticket #34093: 34093-4.diff
| File 34093-4.diff, 5.4 KB (added by , 10 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 new filter 1866 1867 * 1867 1868 * @global wpdb $wpdb 1868 1869 * @global int $m … … 1871 1872 * @global WP_Locale $wp_locale 1872 1873 * @global array $posts 1873 1874 * 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. 1875 * @param array $args{ 1876 * @type bool $initial defaults to true 1877 * @type bool $echo defaults to true 1878 * @type string $post_type defaults to post 1879 * } 1880 * 1881 *@return string|void String when retrieving. 1877 1882 */ 1878 function get_calendar( $ initial = true, $echo = true) {1883 function get_calendar( $args = array() ) { 1879 1884 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 1880 1885 1886 $defaults = array( 1887 'initial' => true, 1888 'echo' => true, 1889 'post_type' => 'post', 1890 ); 1891 1892 if ( ! is_array( $args ) ) { 1893 $backwards_compatible_args = func_get_args(); 1894 1895 if ( isset( $backwards_compatible_args[0] ) && is_bool( $backwards_compatible_args[0] ) ){ 1896 $defaults['initial'] = $backwards_compatible_args[0]; 1897 } 1898 1899 if ( isset( $backwards_compatible_args[1] ) && is_bool( $backwards_compatible_args[1] ) ){ 1900 $defaults['echo'] = $backwards_compatible_args[1]; 1901 } 1902 } 1903 1904 $args = wp_parse_args( $args, $defaults); 1905 1881 1906 $key = md5( $m . $monthnum . $year ); 1882 1907 $cache = wp_cache_get( 'get_calendar', 'calendar' ); 1883 1908 … … 1885 1910 /** This filter is documented in wp-includes/general-template.php */ 1886 1911 $output = apply_filters( 'get_calendar', $cache[ $key ] ); 1887 1912 1888 if ( $ echo) {1913 if ( $args['echo'] ) { 1889 1914 echo $output; 1890 1915 return; 1891 1916 } … … 1897 1922 $cache = array(); 1898 1923 } 1899 1924 1925 /** 1926 * Filter the `get_calander` function arguments before they are used.. 1927 * 1928 * @since 4.7 1929 * @param array $args 1930 */ 1931 $post_type = apply_filters( 'get_calendar_args', $args ); 1932 1933 if ( ! post_type_exists( $post_type ) ) { 1934 // set the post type back to 'post' if the filter value is not a valid post type 1935 $post_type = 'post'; 1936 } 1937 1900 1938 // Quick check. If we have no posts at all, abort! 1901 1939 if ( ! $posts ) { 1902 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); 1940 $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ); 1941 $gotsome = $wpdb->get_var( $prepared_query ); 1903 1942 if ( ! $gotsome ) { 1904 1943 $cache[ $key ] = ''; 1905 1944 wp_cache_set( 'get_calendar', $cache, 'calendar' ); … … 1940 1979 $last_day = date( 't', $unixmonth ); 1941 1980 1942 1981 // 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 year1982 $previous_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1944 1983 FROM $wpdb->posts 1945 1984 WHERE post_date < '$thisyear-$thismonth-01' 1946 AND post_type = 'post'AND post_status = 'publish'1985 AND post_type = %s AND post_status = 'publish' 1947 1986 ORDER BY post_date DESC 1948 LIMIT 1"); 1949 $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1987 LIMIT 1" , $post_type); 1988 $previous = $wpdb->get_row( $previous_prepared_query ); 1989 1990 $next_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1950 1991 FROM $wpdb->posts 1951 1992 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"); 1993 AND post_type = %s AND post_status = 'publish' 1994 ORDER BY post_date ASC 1995 LIMIT 1", $post_type ); 1996 $next = $wpdb->get_row( $next_prepared_query ); 1955 1997 1956 1998 /* translators: Calendar caption: 1: month name, 2: 4-digit year */ 1957 1999 $calendar_caption = _x('%1$s %2$s', 'calendar caption'); … … 1971 2013 } 1972 2014 1973 2015 foreach ( $myweek as $wd ) { 1974 $day_name = $ initial? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );2016 $day_name = $args['initial'] ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); 1975 2017 $wd = esc_attr( $wd ); 1976 2018 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 1977 2019 } … … 2011 2053 $daywithpost = array(); 2012 2054 2013 2055 // Get days with posts 2014 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)2056 $dayswithposts_prepared_query = $wpdb->prepare( "SELECT DISTINCT DAYOFMONTH(post_date) 2015 2057 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); 2058 AND post_type = %s AND post_status = 'publish' 2059 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", $post_type); 2060 $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query , ARRAY_N); 2061 2018 2062 if ( $dayswithposts ) { 2019 2063 foreach ( (array) $dayswithposts as $daywith ) { 2020 2064 $daywithpost[] = $daywith[0]; … … 2073 2117 $cache[ $key ] = $calendar_output; 2074 2118 wp_cache_set( 'get_calendar', $cache, 'calendar' ); 2075 2119 2076 if ( $ echo) {2120 if ( $args['echo'] ) { 2077 2121 /** 2078 2122 * Filters the HTML calendar output. 2079 2123 *