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