WordPress.org

Make WordPress Core

Ticket #34093: 34093-5.diff

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

Fixed bug where args are being used before filter. Also fix bug where args filter was assigning to $post_type variable and not $args.

  • 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 arguments changed to accept an associative array.
    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        /**
     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
    18811912        $key = md5( $m . $monthnum . $year );
    18821913        $cache = wp_cache_get( 'get_calendar', 'calendar' );
    18831914
     
    18851916                /** This filter is documented in wp-includes/general-template.php */
    18861917                $output = apply_filters( 'get_calendar', $cache[ $key ] );
    18871918
    1888                 if ( $echo ) {
     1919                if ( $args['echo'] ) {
    18891920                        echo $output;
    18901921                        return;
    18911922                }
     
    18971928                $cache = array();
    18981929        }
    18991930
     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
    19001937        // Quick check. If we have no posts at all, abort!
    19011938        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 );
    19031941                if ( ! $gotsome ) {
    19041942                        $cache[ $key ] = '';
    19051943                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
     
    19401978        $last_day = date( 't', $unixmonth );
    19411979
    19421980        // 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
     1981        $previous_prepared_query = $wpdb->prepare( "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    19441982                FROM $wpdb->posts
    19451983                WHERE post_date < '$thisyear-$thismonth-01'
    1946                 AND post_type = 'post' AND post_status = 'publish'
     1984                AND post_type = %s AND post_status = 'publish'
    19471985                        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
    19501990                FROM $wpdb->posts
    19511991                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 );
    19551996
    19561997        /* translators: Calendar caption: 1: month name, 2: 4-digit year */
    19571998        $calendar_caption = _x('%1$s %2$s', 'calendar caption');
     
    19712012        }
    19722013
    19732014        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 );
    19752016                $wd = esc_attr( $wd );
    19762017                $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    19772018        }
     
    20112052        $daywithpost = array();
    20122053
    20132054        // 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)
    20152056                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
    20182061        if ( $dayswithposts ) {
    20192062                foreach ( (array) $dayswithposts as $daywith ) {
    20202063                        $daywithpost[] = $daywith[0];
     
    20732116        $cache[ $key ] = $calendar_output;
    20742117        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    20752118
    2076         if ( $echo ) {
     2119        if ( $args['echo'] ) {
    20772120                /**
    20782121                 * Filters the HTML calendar output.
    20792122                 *