Skip to:
Content

bbPress.org

Changeset 4806


Ignore:
Timestamp:
03/15/2013 08:59:15 AM (12 years ago)
Author:
johnjamesjacoby
Message:

Audit up widget code:

  • Add missing UI for some options.
  • Introduce methods for parsing widget arguments.
  • General code clean up.
  • Props alex-ye.
  • Fixes #2250.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/common/widgets.php

    r4579 r4806  
    6464     * @uses get_template_part() To get the login/logged in form
    6565     */
    66     public function widget( $args, $instance ) {
     66    public function widget( $args = array(), $instance = array() ) {
     67
     68        // Get widget settings
     69        $settings = $this->parse_settings( $instance );
    6770
    6871        // Typical WordPress filter
    69         $title    = apply_filters( 'widget_title',              $instance['title'],    $instance, $this->id_base );
     72        $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base );
    7073
    7174        // bbPress filters
    72         $title    = apply_filters( 'bbp_login_widget_title',    $instance['title'],    $instance, $this->id_base );
    73         $register = apply_filters( 'bbp_login_widget_register', $instance['register'], $instance, $this->id_base );
    74         $lostpass = apply_filters( 'bbp_login_widget_lostpass', $instance['lostpass'], $instance, $this->id_base );
     75        $settings['title']    = apply_filters( 'bbp_login_widget_title',    $settings['title'],    $instance, $this->id_base );
     76        $settings['register'] = apply_filters( 'bbp_login_widget_register', $settings['register'], $instance, $this->id_base );
     77        $settings['lostpass'] = apply_filters( 'bbp_login_widget_lostpass', $settings['lostpass'], $instance, $this->id_base );
    7578
    7679        echo $args['before_widget'];
    7780
    78         if ( !empty( $title ) )
    79             echo $args['before_title'] . $title . $args['after_title'];
     81        if ( !empty( $settings['title'] ) ) {
     82            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     83        }
    8084
    8185        if ( !is_user_logged_in() ) : ?>
     
    110114                    </div>
    111115
    112                     <?php if ( !empty( $register ) || !empty( $lostpass ) ) : ?>
     116                    <?php if ( !empty( $settings['register'] ) || !empty( $settings['lostpass'] ) ) : ?>
    113117
    114118                        <div class="bbp-login-links">
    115119
    116                             <?php if ( !empty( $register ) ) : ?>
    117 
    118                                 <a href="<?php echo esc_url( $register ); ?>" title="<?php esc_attr_e( 'Register', 'bbpress' ); ?>" class="bbp-register-link"><?php _e( 'Register', 'bbpress' ); ?></a>
     120                            <?php if ( !empty( $settings['register'] ) ) : ?>
     121
     122                                <a href="<?php echo esc_url( $settings['register'] ); ?>" title="<?php esc_attr_e( 'Register', 'bbpress' ); ?>" class="bbp-register-link"><?php _e( 'Register', 'bbpress' ); ?></a>
    119123
    120124                            <?php endif; ?>
    121125
    122                             <?php if ( !empty( $lostpass ) ) : ?>
    123 
    124                                 <a href="<?php echo esc_url( $lostpass ); ?>" title="<?php esc_attr_e( 'Lost Password', 'bbpress' ); ?>" class="bbp-lostpass-link"><?php _e( 'Lost Password', 'bbpress' ); ?></a>
     126                            <?php if ( !empty( $settings['lostpass'] ) ) : ?>
     127
     128                                <a href="<?php echo esc_url( $settings['lostpass'] ); ?>" title="<?php esc_attr_e( 'Lost Password', 'bbpress' ); ?>" class="bbp-lostpass-link"><?php _e( 'Lost Password', 'bbpress' ); ?></a>
    125129
    126130                            <?php endif; ?>
     
    173177     * @uses BBP_Login_Widget::get_field_name() To output the field name
    174178     */
    175     public function form( $instance ) {
    176 
    177         // Form values
    178         $title    = !empty( $instance['title'] )    ? esc_attr( $instance['title'] )    : '';
    179         $register = !empty( $instance['register'] ) ? esc_attr( $instance['register'] ) : '';
    180         $lostpass = !empty( $instance['lostpass'] ) ? esc_attr( $instance['lostpass'] ) : '';
    181 
    182         ?>
     179    public function form( $instance = array() ) {
     180
     181        // Get widget settings
     182        $settings = $this->parse_settings( $instance ); ?>
    183183
    184184        <p>
    185185            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
    186             <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; ?>" /></label>
     186            <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( $settings['title'] ); ?>" /></label>
    187187        </p>
    188188
    189189        <p>
    190190            <label for="<?php echo $this->get_field_id( 'register' ); ?>"><?php _e( 'Register URI:', 'bbpress' ); ?>
    191             <input class="widefat" id="<?php echo $this->get_field_id( 'register' ); ?>" name="<?php echo $this->get_field_name( 'register' ); ?>" type="text" value="<?php echo $register; ?>" /></label>
     191            <input class="widefat" id="<?php echo $this->get_field_id( 'register' ); ?>" name="<?php echo $this->get_field_name( 'register' ); ?>" type="text" value="<?php echo esc_url( $settings['register'] ); ?>" /></label>
    192192        </p>
    193193
    194194        <p>
    195195            <label for="<?php echo $this->get_field_id( 'lostpass' ); ?>"><?php _e( 'Lost Password URI:', 'bbpress' ); ?>
    196             <input class="widefat" id="<?php echo $this->get_field_id( 'lostpass' ); ?>" name="<?php echo $this->get_field_name( 'lostpass' ); ?>" type="text" value="<?php echo $lostpass; ?>" /></label>
     196            <input class="widefat" id="<?php echo $this->get_field_id( 'lostpass' ); ?>" name="<?php echo $this->get_field_name( 'lostpass' ); ?>" type="text" value="<?php echo esc_url( $settings['lostpass'] ); ?>" /></label>
    197197        </p>
    198198
    199199        <?php
     200    }
     201
     202    /**
     203     * Merge the widget settings into defaults array.
     204     *
     205     * @since bbPress (r4802)
     206     *
     207     * @param $instance Instance
     208     * @uses bbp_parse_args() To merge widget settings into defaults
     209     */
     210    public function parse_settings( $instance = array() ) {
     211        return bbp_parse_args( $instance, array(
     212            'title'    => '',
     213            'register' => '',
     214            'lostpass' => ''
     215        ), 'login_widget_settings' );
    200216    }
    201217}
     
    254270     * @uses bbp_view_title() To output the view title
    255271     */
    256     public function widget( $args, $instance ) {
     272    public function widget( $args = array(), $instance = array() ) {
    257273
    258274        // Only output widget contents if views exist
    259         if ( bbp_get_views() ) :
    260 
    261             // Typical WordPress filter
    262             $title = apply_filters( 'widget_title',          $instance['title'], $instance, $this->id_base );
    263 
    264             // bbPress filter
    265             $title = apply_filters( 'bbp_view_widget_title', $instance['title'], $instance, $this->id_base );
    266 
    267             echo $args['before_widget'];
    268             echo $args['before_title'] . $title . $args['after_title']; ?>
    269 
    270             <ul>
    271 
    272                 <?php foreach ( array_keys( bbp_get_views() ) as $view ) : ?>
    273 
    274                     <li><a class="bbp-view-title" href="<?php bbp_view_url( $view ); ?>" title="<?php bbp_view_title( $view ); ?>"><?php bbp_view_title( $view ); ?></a></li>
    275 
    276                 <?php endforeach; ?>
    277 
    278             </ul>
    279 
    280             <?php echo $args['after_widget'];
    281 
    282         endif;
     275        if ( ! bbp_get_views() ) {
     276            return;
     277        }
     278
     279        // Get widget settings
     280        $settings = $this->parse_settings( $instance );
     281
     282        // Typical WordPress filter
     283        $settings['title'] = apply_filters( 'widget_title',          $settings['title'], $instance, $this->id_base );
     284
     285        // bbPress filter
     286        $settings['title'] = apply_filters( 'bbp_view_widget_title', $settings['title'], $instance, $this->id_base );
     287
     288        echo $args['before_widget'];
     289
     290        if ( !empty( $settings['title'] ) ) {
     291            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     292        } ?>
     293
     294        <ul>
     295
     296            <?php foreach ( array_keys( bbp_get_views() ) as $view ) : ?>
     297
     298                <li><a class="bbp-view-title" href="<?php bbp_view_url( $view ); ?>" title="<?php bbp_view_title( $view ); ?>"><?php bbp_view_title( $view ); ?></a></li>
     299
     300            <?php endforeach; ?>
     301
     302        </ul>
     303
     304        <?php echo $args['after_widget'];
    283305    }
    284306
     
    291313     * @param array $old_instance The old instance options
    292314     */
    293     public function update( $new_instance, $old_instance ) {
     315    public function update( $new_instance = array(), $old_instance = array() ) {
    294316        $instance          = $old_instance;
    295317        $instance['title'] = strip_tags( $new_instance['title'] );
     
    307329     * @uses BBP_Views_Widget::get_field_name() To output the field name
    308330     */
    309     public function form( $instance ) {
    310         $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>
     331    public function form( $instance = array() ) {
     332
     333        // Get widget settings
     334        $settings = $this->parse_settings( $instance ); ?>
    311335
    312336        <p>
    313337            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
    314                 <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; ?>" />
     338                <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( $settings['title'] ); ?>" />
    315339            </label>
    316340        </p>
    317341
    318342        <?php
     343    }
     344
     345    /**
     346     * Merge the widget settings into defaults array.
     347     *
     348     * @since bbPress (r4802)
     349     *
     350     * @param $instance Instance
     351     * @uses bbp_parse_args() To merge widget settings into defaults
     352     */
     353    public function parse_settings( $instance = array() ) {
     354        return bbp_parse_args( $instance, array(
     355            'title' => ''
     356        ), 'view_widget_settings' );
    319357    }
    320358}
     
    371409    public function widget( $args, $instance ) {
    372410
     411        // Get widget settings
     412        $settings = $this->parse_settings( $instance );
     413
    373414        // Typical WordPress filter
    374         $title = apply_filters( 'widget_title',            $instance['title'], $instance, $this->id_base );
     415        $settings['title'] = apply_filters( 'widget_title',            $settings['title'], $instance, $this->id_base );
    375416
    376417        // bbPress filter
    377         $title = apply_filters( 'bbp_search_widget_title', $instance['title'], $instance, $this->id_base );
     418        $settings['title'] = apply_filters( 'bbp_search_widget_title', $settings['title'], $instance, $this->id_base );
    378419
    379420        echo $args['before_widget'];
    380         echo $args['before_title'] . $title . $args['after_title'];
     421
     422        if ( !empty( $settings['title'] ) ) {
     423            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     424        }
    381425
    382426        bbp_get_template_part( 'form', 'search' );
     
    410454     */
    411455    public function form( $instance ) {
    412         $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>
     456
     457        // Get widget settings
     458        $settings = $this->parse_settings( $instance ); ?>
    413459
    414460        <p>
    415461            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
    416                 <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; ?>" />
     462                <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( $settings['title'] ); ?>" />
    417463            </label>
    418464        </p>
    419465
    420466        <?php
     467    }
     468
     469    /**
     470     * Merge the widget settings into defaults array.
     471     *
     472     * @since bbPress (r4802)
     473     *
     474     * @param $instance Instance
     475     * @uses bbp_parse_args() To merge widget settings into defaults
     476     */
     477    public function parse_settings( $instance = array() ) {
     478        return bbp_parse_args( $instance, array(
     479            'title' => ''
     480        ), 'search_widget_settings' );
    421481    }
    422482}
     
    483543    public function widget( $args, $instance ) {
    484544
     545        // Get widget settings
     546        $settings = $this->parse_settings( $instance );
     547
    485548        // Typical WordPress filter
    486         $title        = apply_filters( 'widget_title',           $instance['title'], $instance, $this->id_base );
     549        $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
    487550
    488551        // bbPress filter
    489         $title        = apply_filters( 'bbp_forum_widget_title', $instance['title'], $instance, $this->id_base );
    490         $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : '0';
     552        $settings['title'] = apply_filters( 'bbp_forum_widget_title', $settings['title'], $instance, $this->id_base );
    491553
    492554        // Note: private and hidden forums will be excluded via the
    493555        // bbp_pre_get_posts_exclude_forums filter and function.
    494556        $widget_query = new WP_Query( array(
    495             'post_parent'    => $parent_forum,
     557            'post_parent'    => $settings['parent_forum'],
    496558            'post_type'      => bbp_get_forum_post_type(),
    497559            'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),
     
    500562        ) );
    501563
    502         if ( $widget_query->have_posts() ) :
    503 
    504             echo $args['before_widget'];
    505             echo $args['before_title'] . $title . $args['after_title']; ?>
    506 
    507             <ul>
    508 
    509                 <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
    510 
    511                     <li><a class="bbp-forum-title" href="<?php bbp_forum_permalink( $widget_query->post->ID ); ?>" title="<?php bbp_forum_title( $widget_query->post->ID ); ?>"><?php bbp_forum_title( $widget_query->post->ID ); ?></a></li>
    512 
    513                 <?php endwhile; ?>
    514 
    515             </ul>
    516 
    517             <?php echo $args['after_widget'];
    518 
    519             // Reset the $post global
    520             wp_reset_postdata();
    521 
    522         endif;
     564        // Bail if no posts
     565        if ( ! $widget_query->have_posts() ) {
     566            return;
     567        }
     568
     569        echo $args['before_widget'];
     570
     571        if ( !empty( $settings['title'] ) ) {
     572            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     573        } ?>
     574
     575        <ul>
     576
     577            <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
     578
     579                <li><a class="bbp-forum-title" href="<?php bbp_forum_permalink( $widget_query->post->ID ); ?>" title="<?php bbp_forum_title( $widget_query->post->ID ); ?>"><?php bbp_forum_title( $widget_query->post->ID ); ?></a></li>
     580
     581            <?php endwhile; ?>
     582
     583        </ul>
     584
     585        <?php echo $args['after_widget'];
     586
     587        // Reset the $post global
     588        wp_reset_postdata();
    523589    }
    524590
     
    554620     */
    555621    public function form( $instance ) {
    556         $title        = !empty( $instance['title']        ) ? esc_attr( $instance['title']        ) : '';
    557         $parent_forum = !empty( $instance['parent_forum'] ) ? esc_attr( $instance['parent_forum'] ) : '0'; ?>
     622
     623        // Get widget settings
     624        $settings = $this->parse_settings( $instance ); ?>
    558625
    559626        <p>
    560627            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
    561                 <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; ?>" />
     628                <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( $settings['title'] ); ?>" />
    562629            </label>
    563630        </p>
     
    565632        <p>
    566633            <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?>
    567                 <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo $parent_forum; ?>" />
     634                <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
    568635            </label>
    569636
     
    574641
    575642        <?php
     643    }
     644
     645    /**
     646     * Merge the widget settings into defaults array.
     647     *
     648     * @since bbPress (r4802)
     649     *
     650     * @param $instance Instance
     651     * @uses bbp_parse_args() To merge widget settings into defaults
     652     */
     653    public function parse_settings( $instance = array() ) {
     654        return bbp_parse_args( $instance, array(
     655            'title'        => '',
     656            'parent_forum' => 0
     657        ), 'forum_widget_settings' );
    576658    }
    577659}
     
    632714     * @uses bbp_get_topic_id() To get the topic id
    633715     */
    634     public function widget( $args, $instance ) {
     716    public function widget( $args = array(), $instance = array() ) {
     717
     718        // Get widget settings
     719        $settings = $this->parse_settings( $instance );
    635720
    636721        // Typical WordPress filter
    637         $title        = apply_filters( 'widget_title',           $instance['title'], $instance, $this->id_base );
     722        $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
    638723
    639724        // bbPress filter
    640         $title        = apply_filters( 'bbp_topic_widget_title', $instance['title'], $instance, $this->id_base );
    641         $max_shown    = !empty( $instance['max_shown']    ) ? (int) $instance['max_shown'] : 5;
    642         $show_date    = !empty( $instance['show_date']    ) ? 'on'                         : false;
    643         $show_user    = !empty( $instance['show_user']    ) ? 'on'                         : false;
    644         $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum']    : 'any';
    645         $order_by     = !empty( $instance['order_by']     ) ? $instance['order_by']        : false;
     725        $settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base );
    646726
    647727        // How do we want to order our results?
    648         switch ( $order_by ) {
     728        switch ( $settings['order_by'] ) {
    649729
    650730            // Order by most recent replies
    651731            case 'freshness' :
    652732                $topics_query = array(
    653                     'author'         => 0,
    654733                    'post_type'      => bbp_get_topic_post_type(),
    655                     'post_parent'    => $parent_forum,
    656                     'posts_per_page' => $max_shown,
    657                     'post_status'    => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
    658                     'show_stickes'   => false,
     734                    'post_parent'    => $settings['parent_forum'],
     735                    'posts_per_page' => (int) $settings['max_shown'],
     736                    'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     737                    'show_stickies'  => false,
    659738                    'meta_key'       => '_bbp_last_active_time',
    660739                    'orderby'        => 'meta_value',
    661740                    'order'          => 'DESC',
    662                     'meta_query'     => array( bbp_exclude_forum_ids( 'meta_query' ) )
    663741                );
    664742                break;
     
    667745            case 'popular' :
    668746                $topics_query = array(
    669                     'author'         => 0,
    670747                    'post_type'      => bbp_get_topic_post_type(),
    671                     'post_parent'    => $parent_forum,
    672                     'posts_per_page' => $max_shown,
    673                     'post_status'    => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
    674                     'show_stickes'   => false,
     748                    'post_parent'    => $settings['parent_forum'],
     749                    'posts_per_page' => (int) $settings['max_shown'],
     750                    'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     751                    'show_stickies'  => false,
    675752                    'meta_key'       => '_bbp_reply_count',
    676753                    'orderby'        => 'meta_value',
    677                     'order'          => 'DESC',
    678                     'meta_query'     => array( bbp_exclude_forum_ids( 'meta_query' ) )
     754                    'order'          => 'DESC'
    679755                );
    680756                break;
     
    684760            default :
    685761                $topics_query = array(
    686                     'author'         => 0,
    687762                    'post_type'      => bbp_get_topic_post_type(),
    688                     'post_parent'    => $parent_forum,
    689                     'posts_per_page' => $max_shown,
    690                     'post_status'    => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
    691                     'show_stickes'   => false,
    692                     'order'          => 'DESC',
    693                     'meta_query'     => array( bbp_exclude_forum_ids( 'meta_query' ) )
     763                    'post_parent'    => $settings['parent_forum'],
     764                    'posts_per_page' => (int) $settings['max_shown'],
     765                    'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     766                    'show_stickies'  => false,
     767                    'order'          => 'DESC'
    694768                );
    695769                break;
     
    700774        $widget_query = new WP_Query( $topics_query );
    701775
    702         // Topics exist
    703         if ( $widget_query->have_posts() ) :
    704 
    705             echo $args['before_widget'];
    706             echo $args['before_title'] . $title . $args['after_title']; ?>
    707 
    708             <ul>
    709 
    710                 <?php while ( $widget_query->have_posts() ) :
    711 
    712                     $widget_query->the_post();
    713                     $topic_id    = bbp_get_topic_id( $widget_query->post->ID );
    714                     $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) ); ?>
    715 
    716                     <li>
    717                         <a class="bbp-forum-title" href="<?php bbp_topic_permalink( $topic_id ); ?>" title="<?php bbp_topic_title( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
    718 
    719                         <?php if ( 'on' == $show_user ) : ?>
    720 
    721                             <?php printf( _x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
    722 
    723                         <?php endif; ?>
    724 
    725                         <?php if ( 'on' == $show_date ) : ?>
    726 
    727                             <div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
    728 
    729                         <?php endif; ?>
    730 
    731                     </li>
    732 
    733                 <?php endwhile; ?>
    734 
    735             </ul>
    736 
    737             <?php echo $args['after_widget'];
    738 
    739             // Reset the $post global
    740             wp_reset_postdata();
    741 
    742         endif;
     776        // Bail if no topics are found
     777        if ( ! $widget_query->have_posts() ) {
     778            return;
     779        }
     780
     781        echo $args['before_widget'];
     782
     783        if ( !empty( $settings['title'] ) ) {
     784            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     785        } ?>
     786
     787        <ul>
     788
     789            <?php while ( $widget_query->have_posts() ) :
     790
     791                $widget_query->the_post();
     792                $topic_id    = bbp_get_topic_id( $widget_query->post->ID );
     793                $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) ); ?>
     794
     795                <li>
     796                    <a class="bbp-forum-title" href="<?php echo esc_url( bbp_get_topic_permalink( $topic_id ) ); ?>" title="<?php echo esc_attr( bbp_get_topic_title( $topic_id ) ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
     797
     798                    <?php if ( 'on' == $settings['show_user'] ) : ?>
     799
     800                        <?php printf( _x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
     801
     802                    <?php endif; ?>
     803
     804                    <?php if ( 'on' == $settings['show_date'] ) : ?>
     805
     806                        <div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
     807
     808                    <?php endif; ?>
     809
     810                </li>
     811
     812            <?php endwhile; ?>
     813
     814        </ul>
     815
     816        <?php echo $args['after_widget'];
     817
     818        // Reset the $post global
     819        wp_reset_postdata();
    743820    }
    744821
     
    751828     * @param array $old_instance The old instance options
    752829     */
    753     public function update( $new_instance, $old_instance ) {
     830    public function update( $new_instance = array(), $old_instance = array() ) {
    754831        $instance              = $old_instance;
    755         $instance['title']     = strip_tags( $new_instance['title']     );
    756         $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
    757         $instance['show_date'] = strip_tags( $new_instance['show_date'] );
    758         $instance['show_user'] = strip_tags( $new_instance['show_user'] );
    759         $instance['order_by']  = strip_tags( $new_instance['order_by']  );
     832        $instance['title']     = strip_tags( $new_instance['title'] );
     833        $instance['order_by']  = strip_tags( $new_instance['order_by'] );
     834        $instance['show_date'] = (bool) $new_instance['show_date'];
     835        $instance['show_user'] = (bool) $new_instance['show_user'];
     836        $instance['max_shown'] = (int) $new_instance['max_shown'];
     837
     838        // Force to any
     839        if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) {
     840            $instance['parent_forum'] = 'any';
     841        } else {
     842            $instance['parent_forum'] = (int) $new_instance['parent_forum'];
     843        }
    760844
    761845        return $instance;
     
    771855     * @uses BBP_Topics_Widget::get_field_name() To output the field name
    772856     */
    773     public function form( $instance ) {
    774         $title     = !empty( $instance['title']     ) ? esc_attr( $instance['title']     ) : '';
    775         $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
    776         $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
    777         $show_user = !empty( $instance['show_user'] ) ? esc_attr( $instance['show_user'] ) : '';
    778         $order_by  = !empty( $instance['order_by']  ) ? esc_attr( $instance['order_by']  ) : ''; ?>
    779 
    780         <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php _e( 'Title:',                  'bbpress' ); ?> <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; ?>" /></label></p>
    781         <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum topics to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo $max_shown; ?>" /></label></p>
    782         <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:',         'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( 'on', $show_date ); ?> /></label></p>
    783         <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show topic author:',      'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( 'on', $show_user ); ?> /></label></p>
     857    public function form( $instance = array() ) {
     858
     859        // Get widget settings
     860        $settings = $this->parse_settings( $instance ); ?>
     861
     862        <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php _e( 'Title:',                  'bbpress' ); ?> <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( $settings['title'] ); ?>" /></label></p>
     863        <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum topics to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo esc_attr( $settings['max_shown'] ); ?>" /></label></p>
     864
     865        <p>
     866            <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?>
     867                <input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
     868            </label>
     869
     870            <br />
     871
     872            <small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
     873        </p>
     874
     875        <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:',    'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( 'on', $settings['show_date'] ); ?> /></label></p>
     876        <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show topic author:', 'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( 'on', $settings['show_user'] ); ?> /></label></p>
    784877
    785878        <p>
    786879            <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e( 'Order By:',        'bbpress' ); ?></label>
    787880            <select name="<?php echo $this->get_field_name( 'order_by' ); ?>" id="<?php echo $this->get_field_name( 'order_by' ); ?>">
    788                 <option <?php selected( $order_by, 'newness' );   ?> value="newness"><?php _e( 'Newest Topics',                'bbpress' ); ?></option>
    789                 <option <?php selected( $order_by, 'popular' );   ?> value="popular"><?php _e( 'Popular Topics',               'bbpress' ); ?></option>
    790                 <option <?php selected( $order_by, 'freshness' ); ?> value="freshness"><?php _e( 'Topics With Recent Replies', 'bbpress' ); ?></option>
     881                <option <?php selected( $settings['order_by'], 'newness' );   ?> value="newness"><?php _e( 'Newest Topics',                'bbpress' ); ?></option>
     882                <option <?php selected( $settings['order_by'], 'popular' );   ?> value="popular"><?php _e( 'Popular Topics',               'bbpress' ); ?></option>
     883                <option <?php selected( $settings['order_by'], 'freshness' ); ?> value="freshness"><?php _e( 'Topics With Recent Replies', 'bbpress' ); ?></option>
    791884            </select>
    792885        </p>
    793886
    794887        <?php
     888    }
     889
     890    /**
     891     * Merge the widget settings into defaults array.
     892     *
     893     * @since bbPress (r4802)
     894     *
     895     * @param $instance Instance
     896     * @uses bbp_parse_args() To merge widget options into defaults
     897     */
     898    public function parse_settings( $instance = array() ) {
     899        return bbp_parse_args( $instance, array(
     900            'title'        => '',
     901            'max_shown'    => 5,
     902            'show_date'    => false,
     903            'show_user'    => false,
     904            'parent_forum' => 'any',
     905            'order_by'     => false
     906        ), 'topic_widget_settings' );
    795907    }
    796908}
     
    848960     * @uses bbp_get_template_part() To get the content-forum-statistics template
    849961     */
    850     public function widget( $args, $instance ) {
    851 
    852         $title = apply_filters( 'widget_title',           $instance['title'], $instance, $this->id_base );
    853         $title = apply_filters( 'bbp_stats_widget_title', $instance['title'], $instance, $this->id_base );
     962    public function widget( $args = array(), $instance = array() ) {
     963
     964        // Get widget settings
     965        $settings = $this->parse_settings( $instance );
     966
     967        // Typical WordPress filter
     968        $settings['title'] = apply_filters( 'widget_title',           $settings['title'], $instance, $this->id_base );
     969
     970        // bbPress widget title filter
     971        $settings['title'] = apply_filters( 'bbp_stats_widget_title', $settings['title'], $instance, $this->id_base );
    854972
    855973        echo $args['before_widget'];
    856         echo $args['before_title'] . $title . $args['after_title'];
     974
     975        if ( !empty( $settings['title'] ) ) {
     976            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     977        }
    857978
    858979        bbp_get_template_part( 'content', 'statistics' );
     
    8881009     */
    8891010    public function form( $instance ) {
    890         $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>
     1011
     1012        // Get widget settings
     1013        $settings = $this->parse_settings( $instance ); ?>
    8911014
    8921015        <p>
    8931016            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
    894                 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
    895                    name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>"/>
     1017                <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( $settings['title'] ); ?>"/>
    8961018            </label>
    8971019        </p>
    8981020
    8991021    <?php
     1022    }
     1023
     1024    /**
     1025     * Merge the widget settings into defaults array.
     1026     *
     1027     * @since bbPress (r4802)
     1028     *
     1029     * @param $instance Instance
     1030     * @uses bbp_parse_args() To merge widget settings into defaults
     1031     */
     1032    public function parse_settings( $instance = array() ) {
     1033        return bbp_parse_args( $instance, array(
     1034            'title' => ''
     1035        ),
     1036        'stats_widget_settings' );
    9001037    }
    9011038}
     
    9611098    public function widget( $args, $instance ) {
    9621099
     1100        // Get widget settings
     1101        $settings = $this->parse_settings( $instance );
     1102
    9631103        // Typical WordPress filter
    964         $title      = apply_filters( 'widget_title',             $instance['title'], $instance, $this->id_base );
     1104        $settings['title'] = apply_filters( 'widget_title',             $settings['title'], $instance, $this->id_base );
    9651105
    9661106        // bbPress filter
    967         $title      = apply_filters( 'bbp_replies_widget_title', $instance['title'], $instance, $this->id_base );
    968         $max_shown  = !empty( $instance['max_shown'] ) ? $instance['max_shown'] : '5';
    969         $show_date  = !empty( $instance['show_date'] ) ? 'on'                   : false;
    970         $show_user  = !empty( $instance['show_user'] ) ? 'on'                   : false;
    971         $post_types = !empty( $instance['post_type'] ) ? array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) : bbp_get_reply_post_type();
     1107        $settings['title'] = apply_filters( 'bbp_replies_widget_title', $settings['title'], $instance, $this->id_base );
    9721108
    9731109        // Note: private and hidden forums will be excluded via the
    9741110        // bbp_pre_get_posts_exclude_forums filter and function.
    9751111        $widget_query = new WP_Query( array(
    976             'post_type'      => $post_types,
    977             'post_status'    => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
    978             'posts_per_page' => $max_shown,
    979             'meta_query'     => array( bbp_exclude_forum_ids( 'meta_query' ) )
     1112            'post_type'      => bbp_get_reply_post_type(),
     1113            'post_status'    => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
     1114            'posts_per_page' => (int) $settings['max_shown']
    9801115        ) );
    9811116
    982         // Get replies and display them
    983         if ( $widget_query->have_posts() ) :
    984 
    985             echo $args['before_widget'];
    986             echo $args['before_title'] . $title . $args['after_title']; ?>
    987 
    988             <ul>
    989 
    990                 <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
    991 
    992                     <li>
    993 
    994                         <?php
    995 
    996                         $reply_id    = bbp_get_reply_id( $widget_query->post->ID );
    997                         $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) );
    998                         $reply_link  = '<a class="bbp-reply-topic-title" href="' . esc_url( bbp_get_reply_url( $reply_id ) ) . '" title="' . bbp_get_reply_excerpt( $reply_id, 50 ) . '">' . bbp_get_reply_topic_title( $reply_id ) . '</a>';
    999 
    1000                         // Reply author, link, and timestamp
    1001                         if ( ( 'on' == $show_date ) && ( 'on' == $show_user ) ) :
    1002 
    1003                             // translators: 1: reply author, 2: reply link, 3: reply timestamp
    1004                             printf( _x( '%1$s on %2$s %3$s', 'widgets', 'bbpress' ), $author_link, $reply_link, '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>' );
    1005 
    1006                         // Reply link and timestamp
    1007                         elseif ( $show_date == 'on' ) :
    1008 
    1009                             // translators: 1: reply link, 2: reply timestamp
    1010                             printf( _x( '%1$s %2$s',         'widgets', 'bbpress' ), $reply_link,  '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>'              );
    1011 
    1012                         // Reply author and title
    1013                         elseif ( $show_user == 'on' ) :
    1014 
    1015                             // translators: 1: reply author, 2: reply link
    1016                             printf( _x( '%1$s on %2$s',      'widgets', 'bbpress' ), $author_link, $reply_link                                                                 );
    1017 
    1018                         // Only the reply title
    1019                         else :
    1020 
    1021                             // translators: 1: reply link
    1022                             printf( _x( '%1$s',              'widgets', 'bbpress' ), $reply_link                                                                               );
    1023 
    1024                         endif;
    1025 
    1026                         ?>
    1027 
    1028                     </li>
    1029 
    1030                 <?php endwhile; ?>
    1031 
    1032             </ul>
    1033 
    1034             <?php echo $args['after_widget'];
    1035 
    1036             // Reset the $post global
    1037             wp_reset_postdata();
    1038 
    1039         endif;
     1117        // Bail if no replies
     1118        if ( ! $widget_query->have_posts() ) {
     1119            return;
     1120        }
     1121
     1122        echo $args['before_widget'];
     1123
     1124        if ( !empty( $settings['title'] ) ) {
     1125            echo $args['before_title'] . $settings['title'] . $args['after_title'];
     1126        } ?>
     1127
     1128        <ul>
     1129
     1130            <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
     1131
     1132                <li>
     1133
     1134                    <?php
     1135
     1136                    $reply_id    = bbp_get_reply_id( $widget_query->post->ID );
     1137                    $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) );
     1138                    $reply_link  = '<a class="bbp-reply-topic-title" href="' . esc_url( bbp_get_reply_url( $reply_id ) ) . '" title="' . esc_attr( bbp_get_reply_excerpt( $reply_id, 50 ) ) . '">' . bbp_get_reply_topic_title( $reply_id ) . '</a>';
     1139
     1140                    // Reply author, link, and timestamp
     1141                    if ( ( 'on' == $settings['show_date'] ) && ( 'on' == $settings['show_user'] ) ) :
     1142
     1143                        // translators: 1: reply author, 2: reply link, 3: reply timestamp
     1144                        printf( _x( '%1$s on %2$s %3$s', 'widgets', 'bbpress' ), $author_link, $reply_link, '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>' );
     1145
     1146                    // Reply link and timestamp
     1147                    elseif ( $settings['show_date'] == 'on' ) :
     1148
     1149                        // translators: 1: reply link, 2: reply timestamp
     1150                        printf( _x( '%1$s %2$s',         'widgets', 'bbpress' ), $reply_link,  '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>'              );
     1151
     1152                    // Reply author and title
     1153                    elseif ( $settings['show_user'] == 'on' ) :
     1154
     1155                        // translators: 1: reply author, 2: reply link
     1156                        printf( _x( '%1$s on %2$s',      'widgets', 'bbpress' ), $author_link, $reply_link                                                                 );
     1157
     1158                    // Only the reply title
     1159                    else :
     1160
     1161                        // translators: 1: reply link
     1162                        printf( _x( '%1$s',              'widgets', 'bbpress' ), $reply_link                                                                               );
     1163
     1164                    endif;
     1165
     1166                    ?>
     1167
     1168                </li>
     1169
     1170            <?php endwhile; ?>
     1171
     1172        </ul>
     1173
     1174        <?php echo $args['after_widget'];
     1175
     1176        // Reset the $post global
     1177        wp_reset_postdata();
    10401178    }
    10411179
     
    10481186     * @param array $old_instance The old instance options
    10491187     */
    1050     public function update( $new_instance, $old_instance ) {
     1188    public function update( $new_instance = array(), $old_instance = array() ) {
    10511189        $instance              = $old_instance;
    1052         $instance['title']     = strip_tags( $new_instance['title']     );
    1053         $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
    1054         $instance['show_date'] = strip_tags( $new_instance['show_date'] );
    1055         $instance['show_user'] = strip_tags( $new_instance['show_user'] );
     1190        $instance['title']     = strip_tags( $new_instance['title'] );
     1191        $instance['show_date'] = (bool) $new_instance['show_date'];
     1192        $instance['show_user'] = (bool) $new_instance['show_user'];
     1193        $instance['max_shown'] = (int) $new_instance['max_shown'];
    10561194
    10571195        return $instance;
     
    10671205     * @uses BBP_Replies_Widget::get_field_name() To output the field name
    10681206     */
    1069     public function form( $instance ) {
    1070         $title     = !empty( $instance['title']     ) ? esc_attr( $instance['title']     ) : '';
    1071         $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
    1072         $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
    1073         $show_user = !empty( $instance['show_user'] ) ? esc_attr( $instance['show_user'] ) : ''; ?>
    1074 
    1075         <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php _e( 'Title:',                   'bbpress' ); ?> <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; ?>" /></label></p>
    1076         <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum replies to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo $max_shown; ?>" /></label></p>
    1077         <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:',          'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( 'on', $show_date ); ?> /></label></p>
    1078         <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show reply author:',       'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( 'on', $show_user ); ?> /></label></p>
     1207    public function form( $instance = array() ) {
     1208
     1209        // Get widget settings
     1210        $settings = $this->parse_settings( $instance ); ?>
     1211
     1212        <p><label for="<?php echo $this->get_field_id( 'title'     ); ?>"><?php _e( 'Title:',                   'bbpress' ); ?> <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( $settings['title'] ); ?>" /></label></p>
     1213        <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum replies to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo esc_attr( $settings['max_shown'] ); ?>" /></label></p>
     1214        <p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:',          'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( 'on', $settings['show_date'] ); ?> /></label></p>
     1215        <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show reply author:',       'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( 'on', $settings['show_user'] ); ?> /></label></p>
    10791216
    10801217        <?php
    10811218    }
     1219
     1220    /**
     1221     * Merge the widget settings into defaults array.
     1222     *
     1223     * @since bbPress (r4802)
     1224     *
     1225     * @param $instance Instance
     1226     * @uses bbp_parse_args() To merge widget settings into defaults
     1227     */
     1228    public function parse_settings( $instance = array() ) {
     1229        return bbp_parse_args( $instance , array(
     1230            'title'     => '',
     1231            'max_shown' => 5,
     1232            'show_date' => false,
     1233            'show_user' => false
     1234        ),
     1235        'replies_widget_settings' );
     1236    }
    10821237}
Note: See TracChangeset for help on using the changeset viewer.