PluginProbe
bbPress / 2.2
bbPress v2.2
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / common / widgets.php

widgets.php in bbPress 2.2, at includes/common/widgets.php

864 lines 28.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Widgets
5 *
6 * Contains the forum list, topic list, reply list and login form widgets.
7 *
8 * @package bbPress
9 * @subpackage Widgets
10 */
11
12 // Exit if accessed directly
13 if ( !defined( 'ABSPATH' ) ) exit;
14
15 /**
16 * bbPress Login Widget
17 *
18 * Adds a widget which displays the login form
19 *
20 * @since bbPress (r2827)
21 *
22 * @uses WP_Widget
23 */
24 class BBP_Login_Widget extends WP_Widget {
25
26 /**
27 * bbPress Login Widget
28 *
29 * Registers the login widget
30 *
31 * @since bbPress (r2827)
32 *
33 * @uses apply_filters() Calls 'bbp_login_widget_options' with the
34 * widget options
35 */
36 public function __construct() {
37 $widget_ops = apply_filters( 'bbp_login_widget_options', array(
38 'classname' => 'bbp_widget_login',
39 'description' => __( 'A simple login form with optional links to sign-up and lost password pages.', 'bbpress' )
40 ) );
41
42 parent::__construct( false, __( '(bbPress) Login Widget', 'bbpress' ), $widget_ops );
43 }
44
45 /**
46 * Register the widget
47 *
48 * @since bbPress (r3389)
49 *
50 * @uses register_widget()
51 */
52 public static function register_widget() {
53 register_widget( 'BBP_Login_Widget' );
54 }
55
56 /**
57 * Displays the output, the login form
58 *
59 * @since bbPress (r2827)
60 *
61 * @param mixed $args Arguments
62 * @param array $instance Instance
63 * @uses apply_filters() Calls 'bbp_login_widget_title' with the title
64 * @uses get_template_part() To get the login/logged in form
65 */
66 public function widget( $args, $instance ) {
67 extract( $args );
68
69 $title = apply_filters( 'bbp_login_widget_title', $instance['title'] );
70 $register = apply_filters( 'bbp_login_widget_register', $instance['register'] );
71 $lostpass = apply_filters( 'bbp_login_widget_lostpass', $instance['lostpass'] );
72
73 echo $before_widget;
74
75 if ( !empty( $title ) )
76 echo $before_title . $title . $after_title;
77
78 if ( !is_user_logged_in() ) : ?>
79
80 <form method="post" action="<?php bbp_wp_login_action( array( 'context' => 'login_post' ) ); ?>" class="bbp-login-form">
81 <fieldset>
82 <legend><?php _e( 'Log In', 'bbpress' ); ?></legend>
83
84 <div class="bbp-username">
85 <label for="user_login"><?php _e( 'Username', 'bbpress' ); ?>: </label>
86 <input type="text" name="log" value="<?php bbp_sanitize_val( 'user_login', 'text' ); ?>" size="20" id="user_login" tabindex="<?php bbp_tab_index(); ?>" />
87 </div>
88
89 <div class="bbp-password">
90 <label for="user_pass"><?php _e( 'Password', 'bbpress' ); ?>: </label>
91 <input type="password" name="pwd" value="<?php bbp_sanitize_val( 'user_pass', 'password' ); ?>" size="20" id="user_pass" tabindex="<?php bbp_tab_index(); ?>" />
92 </div>
93
94 <div class="bbp-remember-me">
95 <input type="checkbox" name="rememberme" value="forever" <?php checked( bbp_get_sanitize_val( 'rememberme', 'checkbox' ), true, true ); ?> id="rememberme" tabindex="<?php bbp_tab_index(); ?>" />
96 <label for="rememberme"><?php _e( 'Remember Me', 'bbpress' ); ?></label>
97 </div>
98
99 <div class="bbp-submit-wrapper">
100
101 <?php do_action( 'login_form' ); ?>
102
103 <button type="submit" name="user-submit" id="user-submit" tabindex="<?php bbp_tab_index(); ?>" class="button submit user-submit"><?php _e( 'Log In', 'bbpress' ); ?></button>
104
105 <?php bbp_user_login_fields(); ?>
106
107 </div>
108
109 <?php if ( !empty( $register ) || !empty( $lostpass ) ) : ?>
110
111 <div class="bbp-login-links">
112
113 <?php if ( !empty( $register ) ) : ?>
114
115 <a href="<?php echo esc_url( $register ); ?>" title="<?php esc_attr_e( 'Register', 'bbpress' ); ?>" class="bbp-register-link"><?php _e( 'Register', 'bbpress' ); ?></a>
116
117 <?php endif; ?>
118
119 <?php if ( !empty( $lostpass ) ) : ?>
120
121 <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>
122
123 <?php endif; ?>
124
125 </div>
126
127 <?php endif; ?>
128
129 </fieldset>
130 </form>
131
132 <?php else : ?>
133
134 <div class="bbp-logged-in">
135 <a href="<?php bbp_user_profile_url( bbp_get_current_user_id() ); ?>" class="submit user-submit"><?php echo get_avatar( bbp_get_current_user_id(), '40' ); ?></a>
136 <h4><?php bbp_user_profile_link( bbp_get_current_user_id() ); ?></h4>
137
138 <?php bbp_logout_link(); ?>
139 </div>
140
141 <?php endif;
142
143 echo $after_widget;
144 }
145
146 /**
147 * Update the login widget options
148 *
149 * @since bbPress (r2827)
150 *
151 * @param array $new_instance The new instance options
152 * @param array $old_instance The old instance options
153 */
154 public function update( $new_instance, $old_instance ) {
155 $instance = $old_instance;
156 $instance['title'] = strip_tags( $new_instance['title'] );
157 $instance['register'] = esc_url( $new_instance['register'] );
158 $instance['lostpass'] = esc_url( $new_instance['lostpass'] );
159
160 return $instance;
161 }
162
163 /**
164 * Output the login widget options form
165 *
166 * @since bbPress (r2827)
167 *
168 * @param $instance Instance
169 * @uses BBP_Login_Widget::get_field_id() To output the field id
170 * @uses BBP_Login_Widget::get_field_name() To output the field name
171 */
172 public function form( $instance ) {
173
174 // Form values
175 $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
176 $register = !empty( $instance['register'] ) ? esc_attr( $instance['register'] ) : '';
177 $lostpass = !empty( $instance['lostpass'] ) ? esc_attr( $instance['lostpass'] ) : '';
178
179 ?>
180
181 <p>
182 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
183 <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>
184 </p>
185
186 <p>
187 <label for="<?php echo $this->get_field_id( 'register' ); ?>"><?php _e( 'Register URI:', 'bbpress' ); ?>
188 <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>
189 </p>
190
191 <p>
192 <label for="<?php echo $this->get_field_id( 'lostpass' ); ?>"><?php _e( 'Lost Password URI:', 'bbpress' ); ?>
193 <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>
194 </p>
195
196 <?php
197 }
198 }
199
200 /**
201 * bbPress Views Widget
202 *
203 * Adds a widget which displays the view list
204 *
205 * @since bbPress (r3020)
206 *
207 * @uses WP_Widget
208 */
209 class BBP_Views_Widget extends WP_Widget {
210
211 /**
212 * bbPress View Widget
213 *
214 * Registers the view widget
215 *
216 * @since bbPress (r3020)
217 *
218 * @uses apply_filters() Calls 'bbp_views_widget_options' with the
219 * widget options
220 */
221 public function __construct() {
222 $widget_ops = apply_filters( 'bbp_views_widget_options', array(
223 'classname' => 'widget_display_views',
224 'description' => __( 'A list of registered optional topic views.', 'bbpress' )
225 ) );
226
227 parent::__construct( false, __( '(bbPress) Topic Views List', 'bbpress' ), $widget_ops );
228 }
229
230 /**
231 * Register the widget
232 *
233 * @since bbPress (r3389)
234 *
235 * @uses register_widget()
236 */
237 public static function register_widget() {
238 register_widget( 'BBP_Views_Widget' );
239 }
240
241 /**
242 * Displays the output, the view list
243 *
244 * @since bbPress (r3020)
245 *
246 * @param mixed $args Arguments
247 * @param array $instance Instance
248 * @uses apply_filters() Calls 'bbp_view_widget_title' with the title
249 * @uses bbp_get_views() To get the views
250 * @uses bbp_view_url() To output the view url
251 * @uses bbp_view_title() To output the view title
252 */
253 public function widget( $args, $instance ) {
254
255 // Only output widget contents if views exist
256 if ( bbp_get_views() ) :
257
258 extract( $args );
259
260 $title = apply_filters( 'bbp_view_widget_title', $instance['title'] );
261
262 echo $before_widget;
263 echo $before_title . $title . $after_title; ?>
264
265 <ul>
266
267 <?php foreach ( bbp_get_views() as $view => $args ) : ?>
268
269 <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>
270
271 <?php endforeach; ?>
272
273 </ul>
274
275 <?php echo $after_widget;
276
277 endif;
278 }
279
280 /**
281 * Update the view widget options
282 *
283 * @since bbPress (r3020)
284 *
285 * @param array $new_instance The new instance options
286 * @param array $old_instance The old instance options
287 */
288 public function update( $new_instance, $old_instance ) {
289 $instance = $old_instance;
290 $instance['title'] = strip_tags( $new_instance['title'] );
291
292 return $instance;
293 }
294
295 /**
296 * Output the view widget options form
297 *
298 * @since bbPress (r3020)
299 *
300 * @param $instance Instance
301 * @uses BBP_Views_Widget::get_field_id() To output the field id
302 * @uses BBP_Views_Widget::get_field_name() To output the field name
303 */
304 public function form( $instance ) {
305 $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>
306
307 <p>
308 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
309 <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; ?>" />
310 </label>
311 </p>
312
313 <?php
314 }
315 }
316
317 /**
318 * bbPress Forum Widget
319 *
320 * Adds a widget which displays the forum list
321 *
322 * @since bbPress (r2653)
323 *
324 * @uses WP_Widget
325 */
326 class BBP_Forums_Widget extends WP_Widget {
327
328 /**
329 * bbPress Forum Widget
330 *
331 * Registers the forum widget
332 *
333 * @since bbPress (r2653)
334 *
335 * @uses apply_filters() Calls 'bbp_forums_widget_options' with the
336 * widget options
337 */
338 public function __construct() {
339 $widget_ops = apply_filters( 'bbp_forums_widget_options', array(
340 'classname' => 'widget_display_forums',
341 'description' => __( 'A list of forums with an option to set the parent.', 'bbpress' )
342 ) );
343
344 parent::__construct( false, __( '(bbPress) Forums List', 'bbpress' ), $widget_ops );
345 }
346
347 /**
348 * Register the widget
349 *
350 * @since bbPress (r3389)
351 *
352 * @uses register_widget()
353 */
354 public static function register_widget() {
355 register_widget( 'BBP_Forums_Widget' );
356 }
357
358 /**
359 * Displays the output, the forum list
360 *
361 * @since bbPress (r2653)
362 *
363 * @param mixed $args Arguments
364 * @param array $instance Instance
365 * @uses apply_filters() Calls 'bbp_forum_widget_title' with the title
366 * @uses get_option() To get the forums per page option
367 * @uses current_user_can() To check if the current user can read
368 * private() To resety name
369 * @uses bbp_has_forums() The main forum loop
370 * @uses bbp_forums() To check whether there are more forums available
371 * in the loop
372 * @uses bbp_the_forum() Loads up the current forum in the loop
373 * @uses bbp_forum_permalink() To display the forum permalink
374 * @uses bbp_forum_title() To display the forum title
375 */
376 public function widget( $args, $instance ) {
377 extract( $args );
378
379 $title = apply_filters( 'bbp_forum_widget_title', $instance['title'] );
380 $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : '0';
381
382 // Note: private and hidden forums will be excluded via the
383 // bbp_pre_get_posts_exclude_forums filter and function.
384 $widget_query = new WP_Query( array(
385 'post_parent' => $parent_forum,
386 'post_type' => bbp_get_forum_post_type(),
387 'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),
388 'orderby' => 'menu_order',
389 'order' => 'ASC'
390 ) );
391
392 if ( $widget_query->have_posts() ) :
393
394 echo $before_widget;
395 echo $before_title . $title . $after_title; ?>
396
397 <ul>
398
399 <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
400
401 <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>
402
403 <?php endwhile; ?>
404
405 </ul>
406
407 <?php echo $after_widget;
408
409 // Reset the $post global
410 wp_reset_postdata();
411
412 endif;
413 }
414
415 /**
416 * Update the forum widget options
417 *
418 * @since bbPress (r2653)
419 *
420 * @param array $new_instance The new instance options
421 * @param array $old_instance The old instance options
422 */
423 public function update( $new_instance, $old_instance ) {
424 $instance = $old_instance;
425 $instance['title'] = strip_tags( $new_instance['title'] );
426 $instance['parent_forum'] = $new_instance['parent_forum'];
427
428 // Force to any
429 if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) {
430 $instance['parent_forum'] = 'any';
431 }
432
433 return $instance;
434 }
435
436 /**
437 * Output the forum widget options form
438 *
439 * @since bbPress (r2653)
440 *
441 * @param $instance Instance
442 * @uses BBP_Forums_Widget::get_field_id() To output the field id
443 * @uses BBP_Forums_Widget::get_field_name() To output the field name
444 */
445 public function form( $instance ) {
446 $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
447 $parent_forum = !empty( $instance['parent_forum'] ) ? esc_attr( $instance['parent_forum'] ) : '0'; ?>
448
449 <p>
450 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?>
451 <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; ?>" />
452 </label>
453 </p>
454
455 <p>
456 <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?>
457 <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; ?>" />
458 </label>
459
460 <br />
461
462 <small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
463 </p>
464
465 <?php
466 }
467 }
468
469 /**
470 * bbPress Topic Widget
471 *
472 * Adds a widget which displays the topic list
473 *
474 * @since bbPress (r2653)
475 *
476 * @uses WP_Widget
477 */
478 class BBP_Topics_Widget extends WP_Widget {
479
480 /**
481 * bbPress Topic Widget
482 *
483 * Registers the topic widget
484 *
485 * @since bbPress (r2653)
486 *
487 * @uses apply_filters() Calls 'bbp_topics_widget_options' with the
488 * widget options
489 */
490 public function __construct() {
491 $widget_ops = apply_filters( 'bbp_topics_widget_options', array(
492 'classname' => 'widget_display_topics',
493 'description' => __( 'A list of recent topics, sorted by popularity or freshness.', 'bbpress' )
494 ) );
495
496 parent::__construct( false, __( '(bbPress) Recent Topics', 'bbpress' ), $widget_ops );
497 }
498
499 /**
500 * Register the widget
501 *
502 * @since bbPress (r3389)
503 *
504 * @uses register_widget()
505 */
506 public static function register_widget() {
507 register_widget( 'BBP_Topics_Widget' );
508 }
509
510 /**
511 * Displays the output, the topic list
512 *
513 * @since bbPress (r2653)
514 *
515 * @param mixed $args
516 * @param array $instance
517 * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title
518 * @uses bbp_topic_permalink() To display the topic permalink
519 * @uses bbp_topic_title() To display the topic title
520 * @uses bbp_get_topic_last_active_time() To get the topic last active
521 * time
522 * @uses bbp_get_topic_id() To get the topic id
523 */
524 public function widget( $args, $instance ) {
525
526 extract( $args );
527
528 $title = apply_filters( 'bbp_topic_widget_title', $instance['title'] );
529 $max_shown = !empty( $instance['max_shown'] ) ? (int) $instance['max_shown'] : 5;
530 $show_date = !empty( $instance['show_date'] ) ? 'on' : false;
531 $show_user = !empty( $instance['show_user'] ) ? 'on' : false;
532 $parent_forum = !empty( $instance['parent_forum'] ) ? $instance['parent_forum'] : 'any';
533 $order_by = !empty( $instance['order_by'] ) ? $instance['order_by'] : false;
534
535 // How do we want to order our results?
536 switch ( $order_by ) {
537
538 // Order by most recent replies
539 case 'freshness' :
540 $topics_query = array(
541 'author' => 0,
542 'post_type' => bbp_get_topic_post_type(),
543 'post_parent' => $parent_forum,
544 'posts_per_page' => $max_shown,
545 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
546 'show_stickes' => false,
547 'meta_key' => '_bbp_last_active_time',
548 'orderby' => 'meta_value',
549 'order' => 'DESC',
550 'meta_query' => array( bbp_exclude_forum_ids( 'meta_query' ) )
551 );
552 break;
553
554 // Order by total number of replies
555 case 'popular' :
556 $topics_query = array(
557 'author' => 0,
558 'post_type' => bbp_get_topic_post_type(),
559 'post_parent' => $parent_forum,
560 'posts_per_page' => $max_shown,
561 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
562 'show_stickes' => false,
563 'meta_key' => '_bbp_reply_count',
564 'orderby' => 'meta_value',
565 'order' => 'DESC',
566 'meta_query' => array( bbp_exclude_forum_ids( 'meta_query' ) )
567 );
568 break;
569
570 // Order by which topic was created most recently
571 case 'newness' :
572 default :
573 $topics_query = array(
574 'author' => 0,
575 'post_type' => bbp_get_topic_post_type(),
576 'post_parent' => $parent_forum,
577 'posts_per_page' => $max_shown,
578 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
579 'show_stickes' => false,
580 'order' => 'DESC',
581 'meta_query' => array( bbp_exclude_forum_ids( 'meta_query' ) )
582 );
583 break;
584 }
585
586 // Note: private and hidden forums will be excluded via the
587 // bbp_pre_get_posts_exclude_forums filter and function.
588 $widget_query = new WP_Query( $topics_query );
589
590 // Topics exist
591 if ( $widget_query->have_posts() ) :
592
593 echo $before_widget;
594 echo $before_title . $title . $after_title; ?>
595
596 <ul>
597
598 <?php while ( $widget_query->have_posts() ) :
599
600 $widget_query->the_post();
601 $topic_id = bbp_get_topic_id( $widget_query->post->ID );
602 $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) ); ?>
603
604 <li>
605 <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>
606
607 <?php if ( 'on' == $show_user ) : ?>
608
609 <?php printf( _x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
610
611 <?php endif; ?>
612
613 <?php if ( 'on' == $show_date ) : ?>
614
615 <div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
616
617 <?php endif; ?>
618
619 </li>
620
621 <?php endwhile; ?>
622
623 </ul>
624
625 <?php echo $after_widget;
626
627 // Reset the $post global
628 wp_reset_postdata();
629
630 endif;
631 }
632
633 /**
634 * Update the topic widget options
635 *
636 * @since bbPress (r2653)
637 *
638 * @param array $new_instance The new instance options
639 * @param array $old_instance The old instance options
640 */
641 public function update( $new_instance, $old_instance ) {
642 $instance = $old_instance;
643 $instance['title'] = strip_tags( $new_instance['title'] );
644 $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
645 $instance['show_date'] = strip_tags( $new_instance['show_date'] );
646 $instance['show_user'] = strip_tags( $new_instance['show_user'] );
647 $instance['order_by'] = strip_tags( $new_instance['order_by'] );
648
649 return $instance;
650 }
651
652 /**
653 * Output the topic widget options form
654 *
655 * @since bbPress (r2653)
656 *
657 * @param $instance Instance
658 * @uses BBP_Topics_Widget::get_field_id() To output the field id
659 * @uses BBP_Topics_Widget::get_field_name() To output the field name
660 */
661 public function form( $instance ) {
662 $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
663 $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
664 $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
665 $show_user = !empty( $instance['show_user'] ) ? esc_attr( $instance['show_user'] ) : '';
666 $order_by = !empty( $instance['order_by'] ) ? esc_attr( $instance['order_by'] ) : ''; ?>
667
668 <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>
669 <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>
670 <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>
671 <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>
672
673 <p>
674 <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e( 'Order By:', 'bbpress' ); ?></label>
675 <select name="<?php echo $this->get_field_name( 'order_by' ); ?>" id="<?php echo $this->get_field_name( 'order_by' ); ?>">
676 <option <?php selected( $order_by, 'newness' ); ?> value="newness"><?php _e( 'Newest Topics', 'bbpress' ); ?></option>
677 <option <?php selected( $order_by, 'popular' ); ?> value="popular"><?php _e( 'Popular Topics', 'bbpress' ); ?></option>
678 <option <?php selected( $order_by, 'freshness' ); ?> value="freshness"><?php _e( 'Topics With Recent Replies', 'bbpress' ); ?></option>
679 </select>
680 </p>
681
682 <?php
683 }
684 }
685
686 /**
687 * bbPress Replies Widget
688 *
689 * Adds a widget which displays the replies list
690 *
691 * @since bbPress (r2653)
692 *
693 * @uses WP_Widget
694 */
695 class BBP_Replies_Widget extends WP_Widget {
696
697 /**
698 * bbPress Replies Widget
699 *
700 * Registers the replies widget
701 *
702 * @since bbPress (r2653)
703 *
704 * @uses apply_filters() Calls 'bbp_replies_widget_options' with the
705 * widget options
706 */
707 public function __construct() {
708 $widget_ops = apply_filters( 'bbp_replies_widget_options', array(
709 'classname' => 'widget_display_replies',
710 'description' => __( 'A list of the most recent replies.', 'bbpress' )
711 ) );
712
713 parent::__construct( false, __( '(bbPress) Recent Replies', 'bbpress' ), $widget_ops );
714 }
715
716 /**
717 * Register the widget
718 *
719 * @since bbPress (r3389)
720 *
721 * @uses register_widget()
722 */
723 public static function register_widget() {
724 register_widget( 'BBP_Replies_Widget' );
725 }
726
727 /**
728 * Displays the output, the replies list
729 *
730 * @since bbPress (r2653)
731 *
732 * @param mixed $args
733 * @param array $instance
734 * @uses apply_filters() Calls 'bbp_reply_widget_title' with the title
735 * @uses bbp_get_reply_author_link() To get the reply author link
736 * @uses bbp_get_reply_author() To get the reply author name
737 * @uses bbp_get_reply_id() To get the reply id
738 * @uses bbp_get_reply_url() To get the reply url
739 * @uses bbp_get_reply_excerpt() To get the reply excerpt
740 * @uses bbp_get_reply_topic_title() To get the reply topic title
741 * @uses get_the_date() To get the date of the reply
742 * @uses get_the_time() To get the time of the reply
743 */
744 public function widget( $args, $instance ) {
745
746 extract( $args );
747
748 $title = apply_filters( 'bbp_replies_widget_title', $instance['title'] );
749 $max_shown = !empty( $instance['max_shown'] ) ? $instance['max_shown'] : '5';
750 $show_date = !empty( $instance['show_date'] ) ? 'on' : false;
751 $show_user = !empty( $instance['show_user'] ) ? 'on' : false;
752 $post_types = !empty( $instance['post_type'] ) ? array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) : bbp_get_reply_post_type();
753
754 // Note: private and hidden forums will be excluded via the
755 // bbp_pre_get_posts_exclude_forums filter and function.
756 $widget_query = new WP_Query( array(
757 'post_type' => $post_types,
758 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
759 'posts_per_page' => $max_shown,
760 'meta_query' => array( bbp_exclude_forum_ids( 'meta_query' ) )
761 ) );
762
763 // Get replies and display them
764 if ( $widget_query->have_posts() ) :
765
766 echo $before_widget;
767 echo $before_title . $title . $after_title; ?>
768
769 <ul>
770
771 <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
772
773 <li>
774
775 <?php
776
777 $reply_id = bbp_get_reply_id( $widget_query->post->ID );
778 $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply_id, 'type' => 'both', 'size' => 14 ) );
779 $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>';
780
781 // Reply author, link, and timestamp
782 if ( ( 'on' == $show_date ) && ( 'on' == $show_user ) ) :
783
784 // translators: 1: reply author, 2: reply link, 3: reply timestamp
785 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>' );
786
787 // Reply link and timestamp
788 elseif ( $show_date == 'on' ) :
789
790 // translators: 1: reply link, 2: reply timestamp
791 printf( _x( '%1$s %2$s', 'widgets', 'bbpress' ), $reply_link, '<div>' . bbp_get_time_since( get_the_time( 'U' ) ) . '</div>' );
792
793 // Reply author and title
794 elseif ( $show_user == 'on' ) :
795
796 // translators: 1: reply author, 2: reply link
797 printf( _x( '%1$s on %2$s', 'widgets', 'bbpress' ), $author_link, $reply_link );
798
799 // Only the reply title
800 else :
801
802 // translators: 1: reply link
803 printf( _x( '%1$s', 'widgets', 'bbpress' ), $reply_link );
804
805 endif;
806
807 ?>
808
809 </li>
810
811 <?php endwhile; ?>
812
813 </ul>
814
815 <?php echo $after_widget;
816
817 // Reset the $post global
818 wp_reset_postdata();
819
820 endif;
821 }
822
823 /**
824 * Update the reply widget options
825 *
826 * @since bbPress (r2653)
827 *
828 * @param array $new_instance The new instance options
829 * @param array $old_instance The old instance options
830 */
831 public function update( $new_instance, $old_instance ) {
832 $instance = $old_instance;
833 $instance['title'] = strip_tags( $new_instance['title'] );
834 $instance['max_shown'] = strip_tags( $new_instance['max_shown'] );
835 $instance['show_date'] = strip_tags( $new_instance['show_date'] );
836 $instance['show_user'] = strip_tags( $new_instance['show_user'] );
837
838 return $instance;
839 }
840
841 /**
842 * Output the reply widget options form
843 *
844 * @since bbPress (r2653)
845 *
846 * @param $instance Instance
847 * @uses BBP_Replies_Widget::get_field_id() To output the field id
848 * @uses BBP_Replies_Widget::get_field_name() To output the field name
849 */
850 public function form( $instance ) {
851 $title = !empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
852 $max_shown = !empty( $instance['max_shown'] ) ? esc_attr( $instance['max_shown'] ) : '';
853 $show_date = !empty( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
854 $show_user = !empty( $instance['show_user'] ) ? esc_attr( $instance['show_user'] ) : ''; ?>
855
856 <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>
857 <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>
858 <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>
859 <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>
860
861 <?php
862 }
863 }
864