Make WordPress Core


Ignore:
Timestamp:
03/03/2025 07:54:34 PM (20 months ago)
Author:
audrasjb
Message:

Widgets: Add post type support to get_calendar() function.

This changeset updates the get_calendar() function to allow post type filtering via the $post_type parameter, with backard compatibility for previous params. It also updates the related get_calendar_args and get_calendar hooks accordingly.

Props sebastianpisula, swissspidy, dwainm, moxie, sukhendu2002, audrasjb, mukesh27.
Fixes #34093.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r59836 r59908  
    22342234 *
    22352235 * @since 1.0.0
     2236 * @since 6.8.0 New argument $args added, with backward compatibility.
    22362237 *
    22372238 * @global wpdb      $wpdb      WordPress database abstraction object.
    … …  
    22422243 * @global array     $posts
    22432244 *
    2244  * @param bool $initial Optional. Whether to use initial calendar names. Default true.
    2245  * @param bool $display Optional. Whether to display the calendar output. Default true.
     2245 * @param array $args {
     2246 *     Optional. Arguments for the `get_calendar` function.
     2247 *
     2248 *     @type bool $initial   Whether to use initial calendar names. Default true.
     2249 *     @type bool $display   Whether to display the calendar output. Default true.
     2250 *     @type string $post_type Optional. Post type. Default 'post'.
     2251 * }
    22462252 * @return void|string Void if `$display` argument is true, calendar HTML if `$display` is false.
    22472253 */
    2248 function get_calendar( $initial = true, $display = true ) {
     2254function get_calendar( $args = array() ) {
    22492255        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
     2256
     2257        $defaults = array(
     2258                'initial'   => true,
     2259                'display'   => true,
     2260                'post_type' => 'post',
     2261        );
     2262
     2263        $original_args = func_get_args();
     2264        $args          = array();
     2265
     2266        if ( ! empty( $original_args ) ) {
     2267                if ( ! is_array( $original_args[0] ) ) {
     2268                        if ( isset( $original_args[0] ) && is_bool( $original_args[0] ) ) {
     2269                                $defaults['initial'] = $original_args[0];
     2270                        }
     2271                        if ( isset( $original_args[1] ) && is_bool( $original_args[1] ) ) {
     2272                                $defaults['display'] = $original_args[1];
     2273                        }
     2274                } else {
     2275                        $args = $original_args[0];
     2276                }
     2277        }
     2278
     2279        /**
     2280         * Filter the `get_calendar` function arguments before they are used.
     2281         *
     2282         * @since 6.8.0
     2283         *
     2284         * @param array $args {
     2285         *     Optional. Arguments for the `get_calendar` function.
     2286         *
     2287         *     @type bool $initial   Whether to use initial calendar names. Default true.
     2288         *     @type bool $display   Whether to display the calendar output. Default true.
     2289         *     @type string $post_type Optional. Post type. Default 'post'.
     2290         * }
     2291         * @return array The arguments for the `get_calendar` function.
     2292         */
     2293        $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults ) );
    22502294
    22512295        $key   = md5( $m . $monthnum . $year );
    … …  
    22542298        if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
    22552299                /** This filter is documented in wp-includes/general-template.php */
    2256                 $output = apply_filters( 'get_calendar', $cache[ $key ] );
    2257 
    2258                 if ( $display ) {
     2300                $output = apply_filters( 'get_calendar', $cache[ $key ], $args );
     2301
     2302                if ( $args['display'] ) {
    22592303                        echo $output;
    22602304                        return;
    … …  
    22682312        }
    22692313
     2314        $post_type = $args['post_type'];
     2315        if ( ! post_type_exists( $post_type ) ) {
     2316                $post_type = 'post';
     2317        }
     2318
    22702319        // Quick check. If we have no posts at all, abort!
    22712320        if ( ! $posts ) {
    2272                 $gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" );
     2321                $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type );
     2322                $gotsome        = $wpdb->get_var( $prepared_query );
    22732323                if ( ! $gotsome ) {
    22742324                        $cache[ $key ] = '';
    … …  
    23102360
    23112361        // Get the next and previous month and year with at least one post.
    2312         $previous = $wpdb->get_row(
     2362        $previous_prepared_query = $wpdb->prepare(
    23132363                "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    23142364                FROM $wpdb->posts
    23152365                WHERE post_date < '$thisyear-$thismonth-01'
    2316                 AND post_type = 'post' AND post_status = 'publish'
     2366                AND post_type = %s AND post_status = 'publish'
    23172367                ORDER BY post_date DESC
    2318                 LIMIT 1"
     2368                LIMIT 1",
     2369                $post_type
    23192370        );
    2320         $next     = $wpdb->get_row(
     2371        $previous                = $wpdb->get_row( $previous_prepared_query );
     2372
     2373        $next_prepared_query = $wpdb->prepare(
    23212374                "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    23222375                FROM $wpdb->posts
    23232376                WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    2324                 AND post_type = 'post' AND post_status = 'publish'
     2377                AND post_type = %s AND post_status = 'publish'
    23252378                ORDER BY post_date ASC
    2326                 LIMIT 1"
     2379                LIMIT 1",
     2380                $post_type
    23272381        );
     2382        $next                = $wpdb->get_row( $next_prepared_query );
    23282383
    23292384        /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */
    … …  
    23452400
    23462401        foreach ( $myweek as $wd ) {
    2347                 $day_name         = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
     2402                $day_name         = $args['initial'] ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
    23482403                $wd               = esc_attr( $wd );
    23492404                $calendar_output .= "\n\t\t<th scope=\"col\" aria-label=\"$wd\">$day_name</th>";
    … …  
    23592414
    23602415        // Get days with posts.
    2361         $dayswithposts = $wpdb->get_results(
     2416        $dayswithposts_prepared_query = $wpdb->prepare(
    23622417                "SELECT DISTINCT DAYOFMONTH(post_date)
    23632418                FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    2364                 AND post_type = 'post' AND post_status = 'publish'
     2419                AND post_type = %s AND post_status = 'publish'
    23652420                AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'",
    2366                 ARRAY_N
     2421                $post_type
    23672422        );
     2423        $dayswithposts                = $wpdb->get_results( $dayswithposts_prepared_query, ARRAY_N );
    23682424
    23692425        if ( $dayswithposts ) {
    … …  
    24532509        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    24542510
    2455         if ( $display ) {
    2456                 /**
    2457                  * Filters the HTML calendar output.
    2458                  *
    2459                  * @since 3.0.0
    2460                  *
    2461                  * @param string $calendar_output HTML output of the calendar.
    2462                  */
    2463                 echo apply_filters( 'get_calendar', $calendar_output );
     2511        /**
     2512         * Filters the HTML calendar output.
     2513         *
     2514         * @since 3.0.0
     2515         * @since 6.8.0 New argument $args added, with backward compatibility.
     2516         *
     2517         * @param string $calendar_output HTML output of the calendar.
     2518         * @param array  $args {
     2519         *     Optional. Array of display arguments.
     2520         *
     2521         *     @type bool $initial   Whether to use initial calendar names. Default true.
     2522         *     @type bool $display   Whether to display the calendar output. Default true.
     2523         *     @type string $post_type Optional. Post type. Default 'post'.
     2524         * }
     2525         */
     2526        $calendar_output = apply_filters( 'get_calendar', $calendar_output, $args );
     2527
     2528        if ( $args['display'] ) {
     2529                echo $calendar_output;
    24642530                return;
    24652531        }
    2466         /** This filter is documented in wp-includes/general-template.php */
    2467         return apply_filters( 'get_calendar', $calendar_output );
     2532
     2533        return $calendar_output;
    24682534}
    24692535
Note: See TracChangeset for help on using the changeset viewer.