Make WordPress Core

Ticket #31085: 31085.diff

File 31085.diff, 3.8 KB (added by sippis, 10 years ago)
  • src/wp-includes/default-widgets.php

     
    705705                /** This filter is documented in wp-includes/default-widgets.php */
    706706                $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    707707
     708                $cpt = isset( $instance['cpt'] ) ? $instance['cpt'] : 'post';
     709
    708710                $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    709711                if ( ! $number )
    710712                        $number = 5;
     
    720722                 * @param array $args An array of arguments used to retrieve the recent posts.
    721723                 */
    722724                $r = new WP_Query( apply_filters( 'widget_posts_args', array(
     725                        'post_type'           => $cpt,
    723726                        'posts_per_page'      => $number,
    724727                        'no_found_rows'       => true,
    725728                        'post_status'         => 'publish',
     
    760763        public function update( $new_instance, $old_instance ) {
    761764                $instance = $old_instance;
    762765                $instance['title'] = strip_tags($new_instance['title']);
     766                $instance['cpt'] = stripslashes($new_instance['cpt']);
    763767                $instance['number'] = (int) $new_instance['number'];
    764768                $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
    765769                $this->flush_widget_cache();
     
    776780        }
    777781
    778782        public function form( $instance ) {
    779                 $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
    780                 $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
    781                 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
     783                $cpt_args = apply_filters( 'widget_posts_cpt_args', array(
     784            'public'    => true,
     785            '_builtin'  => false,
     786        ) );
     787
     788                $title                  = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
     789                $current_cpt    = isset( $instance['cpt'] ) ? $instance['cpt'] : 'post';
     790                $number                 = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
     791                $show_date              = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
     792                $has_cpts       = !empty( get_post_types( $cpt_args ) ) ? true : false;
    782793?>
    783794                <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
    784795                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
    785796
     797                <?php // Show post type selector if site has CPT's
     798        if( apply_filters( 'widget_posts_cpt_selector', $has_cpts ) ): ?>
     799            <p><label for="<?php echo $this->get_field_id('cpt'); ?>"><?php _e('Post type of posts to show:') ?></label>
     800            <select class="widefat" id="<?php echo $this->get_field_id('cpt'); ?>" name="<?php echo $this->get_field_name('cpt'); ?>">
     801                <option value="any" <?php selected('any', $current_cpt) ?>><?php _e( 'All' ); ?></option>
     802                <option value="post" <?php selected('post', $current_cpt) ?>><?php _e( 'Posts' ); ?></option>
     803                <?php foreach ( get_post_types( $cpt_args, 'objects' ) as $post_type => $cpt ) :
     804                    if ( empty($cpt->labels->name) )
     805                        continue;
     806                ?>
     807                    <option value="<?php echo esc_attr($post_type) ?>" <?php selected($post_type, $current_cpt) ?>><?php echo $cpt->labels->name; ?></option>
     808                <?php endforeach; ?>
     809            </select></p>
     810        <?php endif; ?>
     811
    786812                <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
    787813                <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
    788814