WordPress.org

Make WordPress Core

Ticket #34093: 34093-4.diff

File 34093-4.diff, 5.4 KB (added by dwainm, 5 years ago)

Adding array args parameter with backwards compatibly for older argument types. Changing the filter to be applied to all args and not just the post type. Allow post type to be submitted as an argument. Also laying the foundation allowing for other arguments to be added in future.

  • src/wp-includes/general-template.php

     
    18631863 * no posts for the month, then it will not be displayed.
    18641864 *
    18651865 * @since 1.0.0
     1866 * @since 4.7 new filter
    18661867 *
    18671868 * @global wpdb      $wpdb
    18681869 * @global int       $m
     
    18711872 * @global WP_Locale $wp_locale
    18721873 * @global array     $posts
    18731874 *
    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.
    18771882 */
    1878 function get_calendar( $initial = true, $echo = true ) {
     1883function get_calendar( $args = array() ) {
    18791884        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    18801885
     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
    18811906        $key = md5( $m . $monthnum . $year );
    18821907        $cache = wp_cache_get( 'get_calendar', 'calendar' );
    18831908
     
    18851910                /** This filter is documented in wp-includes/general-template.php */
    18861911                $output = apply_filters( 'get_calendar', $cache[ $key ] );
    18871912
    1888                 if ( $echo ) {
     1913                if ( $args['echo'] ) {
    18891914                        echo $output;
    18901915                        return;
    18911916                }
     
    18971922                $cache = array();
    18981923        }
    18991924
     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
    19001938        // Quick check. If we have no posts at all, abort!
    19011939        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 );
    19031942                if ( ! $gotsome ) {
    19041943                        $cache[ $key ] = '';
    19051944                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
     
    19401979        $last_day = date( 't', $unixmonth );
    19411980
    19421981        // 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 year
     1982        $previous_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    19441983                FROM $wpdb->posts
    19451984                WHERE post_date < '$thisyear-$thismonth-01'
    1946                 AND post_type = 'post' AND post_status = 'publish'
     1985                AND post_type = %s AND post_status = 'publish'
    19471986                        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
    19501991                FROM $wpdb->posts
    19511992                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 );
    19551997
    19561998        /* translators: Calendar caption: 1: month name, 2: 4-digit year */
    19571999        $calendar_caption = _x('%1$s %2$s', 'calendar caption');
     
    19712013        }
    19722014
    19732015        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 );
    19752017                $wd = esc_attr( $wd );
    19762018                $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    19772019        }
     
    20112053        $daywithpost = array();
    20122054
    20132055        // 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)
    20152057                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
    20182062        if ( $dayswithposts ) {
    20192063                foreach ( (array) $dayswithposts as $daywith ) {
    20202064                        $daywithpost[] = $daywith[0];
     
    20732117        $cache[ $key ] = $calendar_output;
    20742118        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    20752119
    2076         if ( $echo ) {
     2120        if ( $args['echo'] ) {
    20772121                /**
    20782122                 * Filters the HTML calendar output.
    20792123                 *