WordPress.org

Make WordPress Core

Ticket #16173: calendar.diff

File calendar.diff, 7.3 KB (added by GautamGupta, 9 years ago)

TODO: Something in the place of archive links

  • wp-includes/default-widgets.php

     
    332332        }
    333333
    334334        function widget( $args, $instance ) {
    335                 extract($args);
    336                 $title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title'], $instance, $this->id_base);
     335                extract( $args );
     336
     337                $title             = apply_filters( 'widget_title', empty( $instance['title'] ) ? ' ' : $instance['title'], $instance, $this->id_base );
     338                $current_post_type = $this->_get_current_post_type( $instance );
     339
    337340                echo $before_widget;
     341
    338342                if ( $title )
    339                         echo $before_title . $title . $after_title;
    340                 echo '<div id="calendar_wrap">';
    341                 get_calendar();
    342                 echo '</div>';
     343                        echo $before_title . $title . $after_title; ?>
     344
     345                <div id="calendar_wrap">
     346
     347                        <?php get_calendar( true, true, $current_post_type ); ?>
     348
     349                </div>
     350
     351                <?php
     352               
    343353                echo $after_widget;
    344354        }
    345355
    346356        function update( $new_instance, $old_instance ) {
    347                 $instance = $old_instance;
    348                 $instance['title'] = strip_tags($new_instance['title']);
     357                $instance              = $old_instance;
     358                $instance['title']     = strip_tags( $new_instance['title'] );
     359                $instance['post_type'] = stripslashes( $new_instance['post_type'] );
    349360
    350361                return $instance;
    351362        }
    352363
    353364        function form( $instance ) {
    354                 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    355                 $title = strip_tags($instance['title']);
    356 ?>
    357                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    358                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
    359 <?php
     365                $instance          = wp_parse_args( (array) $instance, array( 'title' => '' ) );
     366                $title             = strip_tags( $instance['title'] );
     367                $current_post_type = $this->_get_current_post_type( $instance );
     368                ?>
     369
     370                <p>
     371                        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
     372                        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
     373                </p>
     374
     375                <p>
     376                        <label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Post Type:' ); ?></label>
     377                        <select class="widefat" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name('post_type'); ?>">
     378
     379                                <?php foreach ( get_post_types( array(), 'objects' ) as $post_type ) :
     380                                        if ( !post_type_supports( $post_type->name, 'calendar' ) )
     381                                                continue;
     382                                        ?>
     383
     384                                        <option value="<?php echo esc_attr( $post_type->name ); ?>"<?php selected( $post_type->name, $current_post_type ); ?>><?php echo $post_type->label; ?></option>
     385
     386                                <?php endforeach; ?>
     387
     388                        </select>
     389                </p>
     390
     391                <?php
    360392        }
     393
     394        function _get_current_post_type( $instance ) {
     395                if ( !empty( $instance['post_type'] ) && post_type_exists( $instance['post_type'] ) )
     396                        return $instance['post_type'];
     397
     398                return 'post';
     399        }
    361400}
    362401
    363402/**
  • wp-includes/general-template.php

     
    10711071 *
    10721072 * @param bool $initial Optional, default is true. Use initial calendar names.
    10731073 * @param bool $echo Optional, default is true. Set to false for return.
     1074 * @param string $post_type Optional, default is 'post'. Use to get a calendar
     1075 *                           of a custom post type
    10741076 */
    1075 function get_calendar($initial = true, $echo = true) {
     1077function get_calendar( $initial = true, $echo = true, $post_type = 'post' ) {
    10761078        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    10771079
     1080        // Check if the post type exists
     1081        if ( empty( $post_type ) || !post_type_exists( 'post' ) )
     1082                return false;
     1083
    10781084        $cache = array();
    10791085        $key = md5( $m . $monthnum . $year );
    10801086        if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
     
    10931099
    10941100        // Quick check. If we have no posts at all, abort!
    10951101        if ( !$posts ) {
    1096                 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
     1102                $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = 'publish' LIMIT 1");
    10971103                if ( !$gotsome ) {
    10981104                        $cache[ $key ] = '';
    10991105                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
     
    11341140        $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    11351141                FROM $wpdb->posts
    11361142                WHERE post_date < '$thisyear-$thismonth-01'
    1137                 AND post_type = 'post' AND post_status = 'publish'
     1143                AND post_type = '$post_type' AND post_status = 'publish'
    11381144                        ORDER BY post_date DESC
    11391145                        LIMIT 1");
    11401146        $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    11411147                FROM $wpdb->posts
    11421148                WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    1143                 AND post_type = 'post' AND post_status = 'publish'
     1149                AND post_type = '$post_type' AND post_status = 'publish'
    11441150                        ORDER BY post_date ASC
    11451151                        LIMIT 1");
    11461152
     
    11941200        // Get days with posts
    11951201        $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    11961202                FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    1197                 AND post_type = 'post' AND post_status = 'publish'
     1203                AND post_type = '$post_type' AND post_status = 'publish'
    11981204                AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
    11991205        if ( $dayswithposts ) {
    12001206                foreach ( (array) $dayswithposts as $daywith ) {
     
    12141220                ."FROM $wpdb->posts "
    12151221                ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    12161222                ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    1217                 ."AND post_type = 'post' AND post_status = 'publish'"
     1223                ."AND post_type = '$post_type' AND post_status = 'publish'"
    12181224        );
    12191225        if ( $ak_post_titles ) {
    12201226                foreach ( (array) $ak_post_titles as $ak_post_title ) {
  • wp-includes/post.php

     
    2626                'hierarchical' => false,
    2727                'rewrite' => false,
    2828                'query_var' => false,
    29                 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
     29                'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats', 'calendar' ),
    3030        ) );
    3131
    3232        register_post_type( 'page', array(
     
    12091209 *
    12101210 * All features are directly associated with a functional area of the edit screen, such as the
    12111211 * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
    1212  * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
     1212 * 'excerpt', 'page-attributes', 'thumbnail', 'calendar' and 'custom-fields'.
    12131213 *
    12141214 * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
     1215 * the 'calendar' feature will show the post type in the select box in the calendar widget,
    12151216 * and the 'comments' feature dicates whether the comments count will show on the edit screen.
    12161217 *
    12171218 * @since 3.0.0