PluginProbe
Private groups / trunk
Private groups vtrunk
trunk 1.5 1.6 1.7 1.8 1.8.1 1.9.1 1.9.2 2.0 2.0.1 2.2 2.3 2.4 2.5 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 116 releases
bbp-private-groups / includes / pg_forum_widgets.php

pg_forum_widgets.php in Private groups trunk, at includes/pg_forum_widgets.php

1,125 lines 43.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 /**
5 * Register the filtered PG Widgets
6 */
7 function register_pg_widgets() {
8 register_widget("pg_Forums_Widget");
9 register_widget("pg_Topics_Widget");
10 register_widget("pg_Replies_Widget");
11 register_widget("pg_Activity_Widget");
12
13 }
14
15
16 add_action('widgets_init', 'register_pg_widgets');
17
18
19
20 // Exit if accessed directly
21 if (!defined('ABSPATH'))
22 exit;
23
24 /**
25 * pg Forum Widget*
26 * Adds a widget which displays the forum list
27 *
28 */
29 class pg_Forums_Widget extends WP_Widget {
30
31 /**
32 * primarily the code from the bbpress forum widget !
33 */
34 public function __construct() {
35 $widget_ops = apply_filters('pg_forums_widget_options', array(
36 'classname' => 'widget_display_forums',
37 'description' => __('A list of forums with an option to set the parent.', 'bbp-private groups')
38 ));
39
40 parent::__construct(false, __('(PG) Forums List', 'bbp-private-groups'), $widget_ops);
41 }
42
43 /**
44 * Register the widget
45 *
46 * @since bbPress (r3389)
47 *
48 * @uses register_widget()
49 */
50 public static function register_widget() {
51 register_widget('pg_Forums_Widget');
52 }
53
54 /**
55 * Displays the output, the forum list
56 *
57 * @since bbPress (r2653)
58 *
59 * @param mixed $args Arguments
60 * @param array $instance Instance
61 * @uses apply_filters() Calls 'bbp_forum_widget_title' with the title
62 * @uses get_option() To get the forums per page option
63 * @uses current_user_can() To check if the current user can read
64 * private() To resety name
65 * @uses bbp_has_forums() The main forum loop
66 * @uses bbp_forums() To check whether there are more forums available
67 * in the loop
68 * @uses bbp_the_forum() Loads up the current forum in the loop
69 * @uses bbp_forum_permalink() To display the forum permalink
70 * @uses bbp_forum_title() To display the forum title
71 */
72 public function widget($args, $instance) {
73
74 // Get widget settings
75 $settings = $this->parse_settings($instance);
76
77 // Typical WordPress filter
78 $settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base);
79
80 // bbPress filter
81 $settings['title'] = apply_filters('pg_widget_title', $settings['title'], $instance, $this->id_base);
82
83 //see if we have multiple forums
84 //check if it's any and if so do nothing
85 if ($settings['parent_forum'] == 'any' ) {}
86 //then test if it's not number (either single forum or 0 for root) - if it is, then that's also ok, so don't do furher tests
87 elseif ( !is_numeric( $settings['parent_forum'] ) ) {
88 //otherwise it is a list of forums (or rubbish!) so we need to create an array of forum_list
89 $forum_list = explode(",",$settings['parent_forum']);
90 $settings['parent_forum'] = '' ; // to nullify it
91 }
92 //it's a single forum so again do nothing
93 else {}
94
95
96
97 //so now we have the forum(s) in either parent_forum or an array called $forum_list
98
99
100 // Note: private and hidden forums will be excluded via the
101 // bbp_pre_get_posts_exclude_forums filter and function.
102 $query_data = array(
103 'post_type' => bbp_get_forum_post_type(),
104 'post_parent' => $settings['parent_forum'] ,
105 'post_status' => bbp_get_public_status_id(),
106 'posts_per_page' => get_option('_bbp_forums_per_page', 50),
107 'orderby' => 'menu_order',
108 'order' => 'ASC'
109 );
110
111 echo $args['before_widget'];
112
113 if (!empty($settings['title'])) {
114 echo $args['before_title'] . $settings['title'] . $args['after_title'];
115 }
116 //if forum visibility is switched off or if it is switched on, AND set for this forum, then we can skip the private groups filtering, otherwise we do it !
117 global $rpg_settingsf ;
118 if ( (empty($rpg_settingsf['set_forum_visibility'])) || (!empty($rpg_settingsf['set_forum_visibility']) && empty ($settings['forum_visibility'] )) ){
119
120 //PRIVATE GROUPS Get an array of IDs which the current user has permissions to view
121 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($query_data));
122
123 // now we have $allowed posts, so if we have a $forum_list (multiple forums), then we need to only have forums that are common to both (intersect)
124 if (!empty($forum_list)) {
125 $allowed_posts = array_intersect($allowed_posts, $forum_list);
126 }
127
128 //then we can create the post__in data
129 $query_data['post__in'] = $allowed_posts;
130
131 //if no allowed posts for this user display message and bail
132
133 if ( empty( $allowed_posts )) {
134 echo '<ul><li>' ;
135 _e('No Forums', 'bbp-private-groups') ;
136 echo '</li></ul>' ;
137 echo $args['after_widget'];
138 return ;
139 }
140 }
141 elseif (!empty($forum_list)) {
142 $query_data['post__in'] = $forum_list ;
143 }
144
145
146 $widget_query = new WP_Query($query_data);
147
148
149 // Bail if no posts
150 if (!$widget_query->have_posts()) {
151 echo '<ul><li>' ;
152 _e('No Forums', 'bbp-private-groups') ;
153 echo '</li></ul>' ;
154 echo $args['after_widget'];
155 return;
156 }
157
158
159 ?>
160
161 <ul>
162
163 <?php while ($widget_query->have_posts()) : $widget_query->the_post(); ?>
164
165 <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>
166
167 <?php endwhile; ?>
168
169 </ul>
170
171 <?php
172 echo $args['after_widget'];
173
174 // Reset the $post global
175 wp_reset_postdata();
176 }
177
178 /**
179 * Update the forum widget options
180 *
181 * @since bbPress (r2653)
182 *
183 * @param array $new_instance The new instance options
184 * @param array $old_instance The old instance options
185 */
186 public function update($new_instance, $old_instance) {
187 $instance = $old_instance;
188 $instance['title'] = strip_tags($new_instance['title']);
189 $instance['parent_forum'] = $new_instance['parent_forum'];
190 $instance['forum_visibility'] = $new_instance['forum_visibility'];
191
192 // if parent_forum's blank, then Force to any
193 if ( empty( $instance['parent_forum'] ) && ($instance['parent_forum'] !== '0')) {
194 $instance['parent_forum'] = 'any';
195 }
196
197 return $instance;
198 }
199
200 /**
201 * Output the forum widget options form
202 *
203 * @since bbPress (r2653)
204 *
205 * @param $instance Instance
206 *
207 */
208 public function form($instance) {
209
210 // Get widget settings
211 $settings = $this->parse_settings($instance);
212 ?>
213
214 <p>
215 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bbp-private-groups'); ?>
216 <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']); ?>" />
217 </label>
218 </p>
219
220 <p>
221 <label for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php _e('From Forum ID(s):', 'bbp-private-groups'); ?>
222 <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']); ?>" />
223 </label>
224
225 <br />
226
227 <small><?php _e('"0" to show only top level - "any" to show all', 'bbp-private-groups'); ?></small>
228 <small><?php _e( 'a single forum eg "2921" - or forums seperated by commas eg "2921,2922"', 'bbp-private-groups' ); ?></small>
229 </p>
230 <?php
231 global $rpg_settingsf ;
232 if (!empty($rpg_settingsf['set_forum_visibility'])) {
233 ?>
234 <p>
235 <label for="<?php echo $this->get_field_id( 'forum_visibility' ); ?>">
236 <?php _e( 'Show visible forums', 'bbp-private-groups' ); ?>
237 <input type="checkbox" id="<?php echo $this->get_field_id( 'forum_visibility' ); ?>" name="<?php echo $this->get_field_name( 'forum_visibility' ); ?>" <?php checked( true, $settings['forum_visibility'] ); ?> value="1" />
238 </label>
239
240 <br />
241 <small><?php _e('You have forum visibility set', 'bbp-private-groups'); ?></small>
242 <br />
243 <small><?php _e( 'Check this box to show forums in this widget under forum visibility.', 'bbp-private-groups' ); ?></small>
244 <small><?php _e( 'Leave unchecked to only show forums user has access to', 'bbp-private-groups' ); ?></small>
245 </p>
246 <?php
247 }
248 }
249
250 /**
251 * Merge the widget settings into defaults array.
252 *
253 * @since bbPress (r4802)
254 *
255 * @param $instance Instance
256 * @uses bbp_parse_args() To merge widget settings into defaults
257 */
258 public function parse_settings($instance = array()) {
259 return bbp_parse_args($instance, array(
260 'title' => __('Forums', 'bbp-private-groups'),
261 'parent_forum' => 0,
262 'forum_visibility' => false
263 ), 'forum_widget_settings');
264 }
265
266 }
267
268 /**
269 * PRIVATE GROUPS Topic Widget
270 *
271 * Adds a widget which displays the topic list
272 *
273 * @since bbPress (r2653)
274 *
275 * @uses WP_Widget
276 */
277 class pg_Topics_Widget extends WP_Widget {
278
279 /**
280 * bbPress Topic Widget
281 *
282 * Registers the topic widget
283 *
284 * @since bbPress (r2653)
285 *
286 * @uses apply_filters() Calls 'bbp_topics_widget_options' with the
287 * widget options
288 */
289 public function __construct() {
290 $widget_ops = apply_filters('pg_topics_widget_options', array(
291 'classname' => 'widget_display_topics',
292 'description' => __('A list of recent topics, sorted by popularity or freshness.', 'bbp-private-groups')
293 ));
294
295 parent::__construct(false, __('(PG) Recent Topics', 'bbp-private-groups'), $widget_ops);
296 }
297
298 /**
299 * Register the widget
300 *
301 * @since bbPress (r3389)
302 *
303 * @uses register_widget()
304 */
305 public static function register_widget() {
306 register_widget('pg_Topics_Widget');
307 }
308
309 /**
310 * Displays the output, the topic list
311 *
312 * @since bbPress (r2653)
313 *
314 * @param mixed $args
315 * @param array $instance
316 * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title
317 * @uses bbp_topic_permalink() To display the topic permalink
318 * @uses bbp_topic_title() To display the topic title
319 * @uses bbp_get_topic_last_active_time() To get the topic last active
320 * time
321 * @uses bbp_get_topic_id() To get the topic id
322 */
323 public function widget($args = array(), $instance = array()) {
324
325 // Get widget settings
326 $settings = $this->parse_settings($instance);
327
328 // Typical WordPress filter
329 $settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base);
330
331 // bbPress filter
332 $settings['title'] = apply_filters('pg_topic_widget_title', $settings['title'], $instance, $this->id_base);
333
334 // How do we want to order our results?
335 switch ($settings['order_by']) {
336
337 // Order by most recent replies
338 case 'freshness' :
339 $topics_query = array(
340 'post_type' => bbp_get_topic_post_type(),
341 'post_parent' => $settings['parent_forum'],
342 'posts_per_page' => (int) $settings['max_shown'],
343 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()),
344 'show_stickies' => false,
345 'meta_key' => '_bbp_last_active_time',
346 'orderby' => 'meta_value',
347 'order' => 'DESC',
348 );
349 break;
350
351 // Order by total number of replies
352 case 'popular' :
353 $topics_query = array(
354 'post_type' => bbp_get_topic_post_type(),
355 'post_parent' => $settings['parent_forum'],
356 'posts_per_page' => (int) $settings['max_shown'],
357 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()),
358 'show_stickies' => false,
359 'meta_key' => '_bbp_reply_count',
360 'orderby' => 'meta_value',
361 'order' => 'DESC'
362 );
363 break;
364
365 // Order by which topic was created most recently
366 case 'newness' :
367 default :
368 $topics_query = array(
369 'post_type' => bbp_get_topic_post_type(),
370 'post_parent' => $settings['parent_forum'],
371 'posts_per_page' => (int) $settings['max_shown'],
372 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()),
373 'show_stickies' => false,
374 'order' => 'DESC'
375 );
376 break;
377 }
378
379 //PRIVATE GROUPS Get an array of IDs which the current user has permissions to view
380 //set posts per page to 200 to ensure we get a full list
381 $topics_query['posts_per_page'] =200;
382 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($topics_query));
383 if (empty ($allowed_posts)) $allowed_posts[] = -1 ;
384 // The default forum query with allowed forum ids array added
385 $topics_query['post__in'] = $allowed_posts;
386 //reset the max to be shown
387 $topics_query['posts_per_page'] =(int) $settings['max_shown'] ;
388
389 // Note: private and hidden forums will be excluded via the
390 // bbp_pre_get_posts_exclude_forums filter and function.
391 $widget_query = new WP_Query($topics_query);
392
393 // Bail if no topics are found
394 if (!$widget_query->have_posts()) {
395 return;
396 }
397
398 echo $args['before_widget'];
399
400 if (!empty($settings['title'])) {
401 echo $args['before_title'] . $settings['title'] . $args['after_title'];
402 }
403 //if no allowed posts for this user display message and bail
404 if ( empty( $allowed_posts )) {
405 echo '<ul><li>' ;
406 _e('No Topics', 'bbp-private-groups') ;
407 echo '</li></ul>' ;
408 return ;
409 }
410 ?>
411
412 <ul>
413
414 <?php
415 while ($widget_query->have_posts()) :
416
417 $widget_query->the_post();
418 $topic_id = bbp_get_topic_id($widget_query->post->ID);
419 $author_link = '';
420
421 // Maybe get the topic author
422 if ('on' == $settings['show_user']) :
423 $author_link = bbp_get_topic_author_link(array('post_id' => $topic_id, 'type' => 'both', 'size' => 14));
424 endif;
425 ?>
426
427 <li>
428 <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>
429
430 <?php if (!empty($author_link)) : ?>
431
432 <?php printf(_x('by %1$s', 'widgets', 'bbp-private-groups'), '<span class="topic-author">' . $author_link . '</span>'); ?>
433
434 <?php endif; ?>
435
436 <?php if ('on' == $settings['show_date']) : ?>
437
438 <div><?php bbp_topic_last_active_time($topic_id); ?></div>
439
440 <?php endif; ?>
441
442 </li>
443
444 <?php endwhile; ?>
445
446 </ul>
447
448 <?php
449 echo $args['after_widget'];
450
451 // Reset the $post global
452 wp_reset_postdata();
453 }
454
455 /**
456 * Update the topic widget options
457 *
458 * @since bbPress (r2653)
459 *
460 * @param array $new_instance The new instance options
461 * @param array $old_instance The old instance options
462 */
463 public function update($new_instance = array(), $old_instance = array()) {
464 $instance = $old_instance;
465 $instance['title'] = strip_tags($new_instance['title']);
466 $instance['order_by'] = strip_tags($new_instance['order_by']);
467 $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
468 $instance['show_date'] = (bool) $new_instance['show_date'];
469 $instance['show_user'] = (bool) $new_instance['show_user'];
470 $instance['max_shown'] = (int) $new_instance['max_shown'];
471
472 // Force to any
473 if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) {
474 $instance['parent_forum'] = 'any';
475 }
476
477
478 return $instance;
479 }
480
481 /**
482 * Output the topic widget options form
483 *
484 * @since bbPress (r2653)
485 *
486 * @param $instance Instance
487 *
488 */
489 public function form($instance = array()) {
490
491 // Get widget settings
492 $settings = $this->parse_settings($instance);
493 ?>
494
495 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bbp-private-groups'); ?> <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>
496 <p><label for="<?php echo $this->get_field_id('max_shown'); ?>"><?php _e('Maximum topics to show:', 'bbp-private-groups'); ?> <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>
497
498 <p>
499 <label for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php _e('Parent Forum ID:', 'bbp-private-groups'); ?>
500 <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']); ?>" />
501 </label>
502
503 <br />
504
505 <small><?php _e('"0" to show only root - "any" to show all', 'bbp-private-groups'); ?></small>
506 </p>
507
508 <p><label for="<?php echo $this->get_field_id('show_date'); ?>"><?php _e('Show post date:', 'bbp-private-groups'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>" name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked( true, $settings['show_date'] ); ?> value="1" /></label></p>
509 <p><label for="<?php echo $this->get_field_id('show_user'); ?>"><?php _e('Show topic author:', 'bbp-private-groups'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>" name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
510 <p>
511 <label for="<?php echo $this->get_field_id('order_by'); ?>"><?php _e('Order By:', 'bbp-private-groups'); ?></label>
512 <select name="<?php echo $this->get_field_name('order_by'); ?>" id="<?php echo $this->get_field_name('order_by'); ?>">
513 <option <?php selected($settings['order_by'], 'newness'); ?> value="newness"><?php _e('Newest Topics', 'bbp-private-groups'); ?></option>
514 <option <?php selected($settings['order_by'], 'popular'); ?> value="popular"><?php _e('Popular Topics', 'bbp-private-groups'); ?></option>
515 <option <?php selected($settings['order_by'], 'freshness'); ?> value="freshness"><?php _e('Topics With Recent Replies', 'bbp-private-groups'); ?></option>
516 </select>
517 </p>
518
519 <?php
520 }
521
522 /**
523 * Merge the widget settings into defaults array.
524 *
525 * @since bbPress (r4802)
526 *
527 * @param $instance Instance
528 * @uses bbp_parse_args() To merge widget options into defaults
529 */
530 public function parse_settings($instance = array()) {
531 return bbp_parse_args($instance, array(
532 'title' => __('Recent Topics', 'bbp-private-groups'),
533 'max_shown' => 5,
534 'show_date' => false,
535 'show_user' => false,
536 'parent_forum' => 'any',
537 'order_by' => false
538 ), 'topic_widget_settings');
539 }
540
541 }
542 /********************************************/
543 /**
544 * PRIVATE GROUPS Replies Widget
545 *
546 * Adds a widget which displays the replies list
547 *
548 *
549 * @uses WP_Widget
550 */
551 class pg_Replies_Widget extends WP_Widget {
552
553 /**
554 * pg Replies Widget
555 *
556 * Registers the replies widget
557 *
558 *
559 * @uses apply_filters() Calls 'bbp_replies_widget_options' with the
560 * widget options
561 */
562 public function __construct() {
563 $widget_ops = apply_filters('pg_replies_widget_options', array(
564 'classname' => 'widget_display_replies',
565 'description' => __('A list of the most recent replies.', 'bbp-private-groups')
566 ));
567
568 parent::__construct(false, __('(PG) Recent Replies', 'bbp-private-groups'), $widget_ops);
569 }
570
571 /**
572 * Register the widget
573 *
574 * @since bbPress (r3389)
575 *
576 * @uses register_widget()
577 */
578 public static function register_widget() {
579 register_widget('pg_Replies_Widget');
580 }
581
582 /**
583 * Displays the output, the replies list
584 *
585 * @since bbPress (r2653)
586 *
587 * @param mixed $args
588 * @param array $instance
589 * @uses apply_filters() Calls 'bbp_reply_widget_title' with the title
590 * @uses bbp_get_reply_author_link() To get the reply author link
591 * @uses bbp_get_reply_author() To get the reply author name
592 * @uses bbp_get_reply_id() To get the reply id
593 * @uses bbp_get_reply_url() To get the reply url
594 * @uses bbp_get_reply_excerpt() To get the reply excerpt
595 * @uses bbp_get_reply_topic_title() To get the reply topic title
596 * @uses get_the_date() To get the date of the reply
597 * @uses get_the_time() To get the time of the reply
598 */
599 public function widget($args, $instance) {
600
601 // Get widget settings
602 $settings = $this->parse_settings($instance);
603
604 // Typical WordPress filter
605 $settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base);
606
607 // bbPress filter
608 $settings['title'] = apply_filters('pg_replies_widget_title', $settings['title'], $instance, $this->id_base);
609
610 // Note: private and hidden forums will be excluded via the
611 // bbp_pre_get_posts_exclude_forums filter and function.
612 $query_data = array(
613 'post_type' => bbp_get_reply_post_type(),
614 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()),
615 'posts_per_page' => '50',
616
617 );
618
619 //PRIVATE GROUPS Get an array of IDs which the current user has permissions to view
620 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($query_data));
621 // The default forum query with allowed forum ids array added
622 $query_data['post__in'] = $allowed_posts;
623
624 //now set max posts
625 $query_data ['posts_per_page'] = (int) $settings['max_shown'] ;
626
627 $widget_query = new WP_Query($query_data);
628
629 // Bail if no replies
630 if (!$widget_query->have_posts()) {
631 return;
632 }
633
634 echo $args['before_widget'];
635
636 if (!empty($settings['title'])) {
637 echo $args['before_title'] . $settings['title'] . $args['after_title'];
638 }
639
640 //if no allowed posts for this user display message and bail
641 if ( empty( $allowed_posts )) {
642 echo '<ul><li>' ;
643 _e('No Replies', 'bbp-private-groups') ;
644 echo '</li></ul>' ;
645 echo $args['after_widget'];
646 return ;
647 }
648 ?>
649
650 <ul>
651
652 <?php while ($widget_query->have_posts()) : $widget_query->the_post(); ?>
653
654 <li>
655
656 <?php
657 // Verify the reply ID
658 $reply_id = bbp_get_reply_id($widget_query->post->ID);
659 $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>';
660
661 // Only query user if showing them
662 if ('on' == $settings['show_user']) :
663 $author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14));
664 else :
665 $author_link = false;
666 endif;
667
668 // Reply author, link, and timestamp
669 if (( 'on' == $settings['show_date'] ) && !empty($author_link)) :
670
671 // translators: 1: reply author, 2: reply link, 3: reply timestamp
672 printf(_x('%1$s on %2$s %3$s', 'widgets', 'bbp-private-groups'), $author_link, $reply_link, '<div>' . bbp_get_time_since(get_the_time('U')) . '</div>');
673
674 // Reply link and timestamp
675 elseif ('on' == $settings['show_date']) :
676
677 // translators: 1: reply link, 2: reply timestamp
678 printf(_x('%1$s %2$s', 'widgets', 'bbp-private-groups'), $reply_link, '<div>' . bbp_get_time_since(get_the_time('U')) . '</div>');
679
680 // Reply author and title
681 elseif (!empty($author_link)) :
682
683 // translators: 1: reply author, 2: reply link
684 printf(_x('%1$s on %2$s', 'widgets', 'bbp-private-groups'), $author_link, $reply_link);
685
686 // Only the reply title
687 else :
688
689 // translators: 1: reply link
690 printf(_x('%1$s', 'widgets', 'bbp-private-groups'), $reply_link);
691
692 endif;
693 ?>
694
695 </li>
696
697 <?php endwhile; ?>
698
699 </ul>
700
701 <?php
702 echo $args['after_widget'];
703
704 // Reset the $post global
705 wp_reset_postdata();
706 }
707
708 /**
709 * Update the reply widget options
710 *
711 * @since bbPress (r2653)
712 *
713 * @param array $new_instance The new instance options
714 * @param array $old_instance The old instance options
715 */
716 public function update($new_instance = array(), $old_instance = array()) {
717 $instance = $old_instance;
718 $instance['title'] = strip_tags($new_instance['title']);
719 $instance['show_date'] = (bool) $new_instance['show_date'];
720 $instance['show_user'] = (bool) $new_instance['show_user'];
721 $instance['max_shown'] = (int) $new_instance['max_shown'];
722
723 return $instance;
724 }
725
726 /**
727 * Output the reply widget options form
728 *
729 * @since bbPress (r2653)
730 *
731 * @param $instance Instance
732 *
733 */
734 public function form($instance = array()) {
735
736 // Get widget settings
737 $settings = $this->parse_settings($instance);
738 ?>
739
740 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bbp-private-groups'); ?> <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>
741 <p><label for="<?php echo $this->get_field_id('max_shown'); ?>"><?php _e('Maximum replies to show:', 'bbp-private-groups'); ?> <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>
742 <p><label for="<?php echo $this->get_field_id('show_date'); ?>"><?php _e('Show post date:', 'bbp-private-groups'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>" name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked( true, $settings['show_date'] ); ?> value="1" /></label></p>
743 <p><label for="<?php echo $this->get_field_id('show_user'); ?>"><?php _e('Show reply author:', 'bbp-private-groups'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>" name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
744
745 <?php
746 }
747
748 /**
749 * Merge the widget settings into defaults array.
750 *
751 * @since bbPress (r4802)
752 *
753 * @param $instance Instance
754 * @uses bbp_parse_args() To merge widget settings into defaults
755 */
756 public function parse_settings($instance = array()) {
757 return bbp_parse_args($instance, array(
758 'title' => __('Recent Replies', 'bbp-private-groups'),
759 'max_shown' => 5,
760 'show_date' => false,
761 'show_user' => false
762 ), 'replies_widget_settings');
763 }
764
765 }
766
767
768 /*******************************************latest activity widget*/
769
770 class pg_Activity_Widget extends WP_Widget {
771
772 /**
773 * bbPress Topic Widget
774 *
775 * Registers the topic widget
776 *
777 * @since bbPress (r2653)
778 *
779 * @uses apply_filters() Calls 'bbp_topics_widget_options' with the
780 * widget options
781 */
782 public function __construct() {
783 $widget_ops = apply_filters( 'pg_topics_widget_options', array(
784 'classname' => 'widget_display_topics',
785 'description' => __( 'A list of latest activity, sorted by popularity or freshness with latest author.', 'bbp-private-groups' )
786 ) );
787
788 parent::__construct( false, __( '(PG) Latest Activity', 'bbp-private-groups' ), $widget_ops );
789 }
790
791 /**
792 * Register the widget
793 *
794 * @since bbPress (r3389)
795 *
796 * @uses register_widget()
797 */
798 public static function register_widget() {
799 register_widget( 'pg_Activity_Widget' );
800 }
801
802 /**
803 * Displays the output, the topic list
804 *
805 * @since bbPress (r2653)
806 *
807 * @param mixed $args
808 * @param array $instance
809 * @uses apply_filters() Calls 'bbp_topic_widget_title' with the title
810 * @uses bbp_topic_permalink() To display the topic permalink
811 * @uses bbp_topic_title() To display the topic title
812 * @uses bbp_get_topic_last_active_time() To get the topic last active
813 * time
814 * @uses bbp_get_topic_id() To get the topic id
815 */
816 public function widget( $args = array(), $instance = array() ) {
817
818 // Get widget settings
819 $settings = $this->parse_settings( $instance );
820
821 // Typical WordPress filter
822 $settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base );
823
824 // bbPress filter
825 $settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base );
826
827
828 //see if we have multiple forums
829 //check if it's any and if so set post_parent__in
830 if ($settings['parent_forum'] == 'any' ) $settings['post_parent__in'] =''; //to set up a null post parent in
831 //then test if it's nit number (either single forum or 0 for root) - if it is, then that's also ok, so don't do furher tests
832 elseif ( !is_numeric( $settings['parent_forum'] ) ) {
833 //otherwise it is a list of forums (or rubbish!) so we need to create a post_parent_in array
834 $settings['post_parent__in'] = explode(",",$settings['parent_forum']);
835 $settings['parent_forum'] = '' ; // to nullify it
836 }
837 //it's a single forum so
838 else $settings['post_parent__in'] =''; //to set up a null post parent in
839
840
841
842 //so now we have the forum(s) in either parent_forum or post_parent__in
843
844
845 // How do we want to order our results?
846 switch ( $settings['order_by'] ) {
847
848 // Order by most recent replies
849 case 'freshness' :
850 $topics_query = array(
851 'post_type' => bbp_get_topic_post_type(),
852 'post_parent' => $settings['parent_forum'],
853 'post_parent__in' => $settings['post_parent__in'],
854 'posts_per_page' => (int) $settings['max_shown'],
855 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
856 'ignore_sticky_posts' => true,
857 'no_found_rows' => true,
858 'meta_key' => '_bbp_last_active_time',
859 'orderby' => 'meta_value',
860 'order' => 'DESC',
861 );
862 break;
863
864 // Order by total number of replies
865 case 'popular' :
866 $topics_query = array(
867 'post_type' => bbp_get_topic_post_type(),
868 'post_parent' => $settings['parent_forum'],
869 'post_parent__in' => $settings['post_parent__in'],
870 'posts_per_page' => (int) $settings['max_shown'],
871 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
872 'ignore_sticky_posts' => true,
873 'no_found_rows' => true,
874 'meta_key' => '_bbp_reply_count',
875 'orderby' => 'meta_value',
876 'order' => 'DESC'
877 );
878 break;
879
880 // Order by which topic was created most recently
881 case 'newness' :
882 default :
883 $topics_query = array(
884 'post_type' => bbp_get_topic_post_type(),
885 'post_parent' => $settings['parent_forum'],
886 'post_parent__in' => $settings['post_parent__in'],
887 'posts_per_page' => (int) $settings['max_shown'],
888 'post_status' => array( bbp_get_public_status_id(), bbp_get_closed_status_id() ),
889 'ignore_sticky_posts' => true,
890 'no_found_rows' => true,
891 'order' => 'DESC'
892 );
893 break;
894 }
895
896 //PRIVATE GROUPS Get an array of IDs which the current user has permissions to view
897 //set posts per page to 200 to ensure we get a full list
898 $topics_query['posts_per_page'] =200;
899 $allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($topics_query));
900 if (empty ($allowed_posts)) $allowed_posts[] = -1 ;
901 // The default forum query with allowed forum ids array added
902 $topics_query['post__in'] = $allowed_posts;
903 //reset the max to be shown
904 $topics_query['posts_per_page'] =(int) $settings['max_shown'] ;
905
906
907 // Note: private and hidden forums will be excluded via the
908 // bbp_pre_get_posts_normalize_forum_visibility action and function.
909 $widget_query = new WP_Query( $topics_query );
910 // Bail if no topics are found
911 if ( ! $widget_query->have_posts() ) {
912 return;
913 }
914
915
916 echo $args['before_widget'];
917
918 if ( !empty( $settings['title'] ) ) {
919 echo $args['before_title'] . $settings['title'] . $args['after_title'];
920 }
921
922 //if no allowed posts for this user display message and bail
923 if ( empty( $allowed_posts )) {
924 echo '<ul><li>' ;
925 _e('No activity', 'bbp-private-groups') ;
926 echo '</li></ul>' ;
927 echo $args['after_widget'];
928 return ;
929 }
930
931 ?>
932 <ul>
933
934 <?php while ( $widget_query->have_posts() ) :
935
936
937 $widget_query->the_post();
938 $topic_id = bbp_get_topic_id( $widget_query->post->ID );
939 $author_link = '';
940
941 //check if this topic has a reply
942 $reply = get_post_meta( $topic_id, '_bbp_last_reply_id',true);
943
944 // Maybe get the topic author
945 if ( ! empty( $settings['show_user'] ) ) {
946 //do we display avatar?
947 if (!empty ($settings['hide_avatar'])) $type='name' ;
948 else $type='both' ;
949 //if no reply the author
950 if (empty ($reply)) $author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => $type, 'size' => 14 ) );
951 //if has a reply then get the author of the reply
952 else $author_link = bbp_get_reply_author_link( array( 'post_id' => $reply, 'type' => $type, 'size' => 14 ) );
953 } ?>
954
955 <li>
956 <?php
957 //if no replies set the link to the topic
958 if (empty ($reply)) {?>
959 <a class="bbp-forum-title" href="<?php bbp_topic_permalink( $topic_id ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
960 <?php }
961 //if replies then set link to the latest reply
962 else {
963 echo '<a class="bbp-reply-topic-title" href="' . esc_url( bbp_get_reply_url( $reply ) ) . '" title="' . esc_attr( bbp_get_reply_excerpt( $reply, 50 ) ) . '">' . bbp_get_reply_topic_title( $reply ) . '</a>';
964 } ?>
965
966 <?php if ( ! empty( $author_link ) ) : ?>
967 <div class = "pg-activity-author">
968 <?php
969
970 if (empty($reply)) {
971 echo '<span class="pg-la-text">' ;
972 printf( _x( 'topic by %1$s', 'widgets', 'bbp-private-groups' ), '<span class="topic-author">' . $author_link . '</span>' );
973 }
974 else {
975 echo '<span class="pg-la-text">' ;
976 printf( _x( 'reply by %1$s', 'widgets', 'bbp-private-groups' ), '<span class="topic-author">' . $author_link . '</span>' );
977 } ?>
978 </div>
979 <?php endif; ?>
980
981
982 <?php if ( ! empty( $settings['show_freshness'] ) ) : ?>
983 <?php $output = bbp_get_topic_last_active_time( $topic_id ) ;
984 //shorten freshness?
985 if ( ! empty( $settings['shorten_freshness'] ) ) $output = preg_replace( '/, .*[^ago]/', ' ', $output ); ?>
986 <div class = "pg-activity-freshness"><?php
987 echo '<span class="pg-la-freshness">'.$output. '</span>' ;
988 ?></div>
989
990 <?php endif; ?>
991
992 <?php if ( ! empty( $settings['show_forum'] ) ) : ?>
993 <div class = "pg-activity-forum">
994 <?php
995 $forum = bbp_get_topic_forum_id($topic_id);
996 $forum1 = bbp_get_forum_title($forum) ;
997 $forum2 = esc_url( bbp_get_forum_permalink( $forum )) ;
998 echo '<span class="pg-la-text">' ;
999 _e ( 'in ', 'bbp-private-groups' ) ;
1000 echo '</span>' ; ?>
1001 <a class="bbp-forum-title" href="<?php echo $forum2; ?>"><?php echo $forum1 ; ?></a>
1002 </div>
1003 <?php endif; ?>
1004
1005
1006
1007
1008
1009 </li>
1010
1011 <?php endwhile; ?>
1012
1013 </ul>
1014
1015 <?php echo $args['after_widget'];
1016
1017 // Reset the $post global
1018 wp_reset_postdata();
1019 }
1020
1021 /**
1022 * Update the topic widget options
1023 *
1024 * @since bbPress (r2653)
1025 *
1026 * @param array $new_instance The new instance options
1027 * @param array $old_instance The old instance options
1028 */
1029 public function update( $new_instance = array(), $old_instance = array() ) {
1030 $instance = $old_instance;
1031 $instance['title'] = strip_tags( $new_instance['title'] );
1032 $instance['order_by'] = strip_tags( $new_instance['order_by'] );
1033 $instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
1034 $instance['show_freshness'] = (bool) $new_instance['show_freshness'];
1035 $instance['show_user'] = (bool) $new_instance['show_user'];
1036 $instance['show_forum'] = (bool) $new_instance['show_forum'];
1037 $instance['max_shown'] = (int) $new_instance['max_shown'];
1038 $instance['shorten_freshness'] = (int) $new_instance['shorten_freshness'];
1039 $instance['hide_avatar'] = (int) $new_instance['hide_avatar'];
1040
1041 // if parent_forum's blank, then Force to any
1042 if ( empty( $instance['parent_forum'] ) ) {
1043 $instance['parent_forum'] = 'any';
1044 }
1045
1046 return $instance;
1047 }
1048
1049 /**
1050 * Output the topic widget options form
1051 *
1052 * @since bbPress (r2653)
1053 *
1054 * @param $instance Instance
1055 * @uses BBP_Topics_Widget::get_field_id() To output the field id
1056 * @uses BBP_Topics_Widget::get_field_name() To output the field name
1057 */
1058 public function form( $instance = array() ) {
1059
1060 // Get widget settings
1061 $settings = $this->parse_settings( $instance ); ?>
1062
1063 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbp-private-groups' ); ?> <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>
1064 <p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum topics to show:', 'bbp-private-groups' ); ?> <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>
1065
1066 <p>
1067 <label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbp-private-groups' ); ?>
1068 <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'] ); ?>" />
1069 </label>
1070
1071 <br />
1072
1073 <small><?php _e( '"0" to show only root - "any" to show all', 'bbp-private-groups' ); ?></small>
1074 <small><?php _e( 'a single forum eg "2921" - or forums seperated by commas eg "2921,2922"', 'bbp-private-groups' ); ?></small>
1075 </p>
1076
1077 <p><label for="<?php echo $this->get_field_id( 'show_freshness' ); ?>"><?php _e( 'Show Freshness:', 'bbp-private-groups' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_freshness' ); ?>" name="<?php echo $this->get_field_name( 'show_freshness' ); ?>" <?php checked( true, $settings['show_freshness'] ); ?> value="1" /></label></p>
1078 <p><label for="<?php echo $this->get_field_id( 'shorten_freshness' ); ?>"><?php _e( 'Shorten freshness:', 'bbp-private-groups' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'shorten_freshness' ); ?>" name="<?php echo $this->get_field_name( 'shorten_freshness' ); ?>" <?php checked( true, $settings['shorten_freshness'] ); ?> value="1" /></label></p>
1079 <p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show topic author:', 'bbp-private-groups' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
1080 <p><label for="<?php echo $this->get_field_id( 'hide_avatar' ); ?>"><?php _e( 'Hide Avatar', 'bbp-private-groups' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'hide_avatar' ); ?>" name="<?php echo $this->get_field_name( 'hide_avatar' ); ?>" <?php checked( true, $settings['hide_avatar'] ); ?> value="1" /></label></p>
1081 <p><label for="<?php echo $this->get_field_id( 'show_forum' ); ?>"><?php _e( 'Show Forum:', 'bbp-private-groups' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_forum' ); ?>" name="<?php echo $this->get_field_name( 'show_forum' ); ?>" <?php checked( true, $settings['show_forum'] ); ?> value="1" /></label></p>
1082
1083 <p>
1084 <label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e( 'Order By:', 'bbp-private-groups' ); ?></label>
1085 <select name="<?php echo $this->get_field_name( 'order_by' ); ?>" id="<?php echo $this->get_field_name( 'order_by' ); ?>">
1086 <option <?php selected( $settings['order_by'], 'freshness' ); ?> value="freshness"><?php _e( 'Topics With Recent Replies', 'bbp-private-groups' ); ?></option>
1087 <option <?php selected( $settings['order_by'], 'newness' ); ?> value="newness"><?php _e( 'Newest Topics', 'bbp-private-groups' ); ?></option>
1088 <option <?php selected( $settings['order_by'], 'popular' ); ?> value="popular"><?php _e( 'Popular Topics', 'bbp-private-groups' ); ?></option>
1089
1090 </select>
1091 </p>
1092
1093 <?php
1094 }
1095
1096 /**
1097 * Merge the widget settings into defaults array.
1098 *
1099 * @since bbPress (r4802)
1100 *
1101 * @param $instance Instance
1102 * @uses bbp_parse_args() To merge widget options into defaults
1103 */
1104 public function parse_settings( $instance = array() ) {
1105 return bbp_parse_args( $instance, array(
1106 'title' => __( 'Latest Activity', 'bbp-private-groups' ),
1107 'max_shown' => 5,
1108 'show_date' => false,
1109 'show_user' => false,
1110 'parent_forum' => 'any',
1111 'show_freshness' => false,
1112 'shorten_freshness' => false,
1113 'hide_avatar' => false,
1114 'show_forum' => false,
1115 'order_by' => false
1116 ), 'topic_widget_settings' );
1117 }
1118 }
1119
1120
1121
1122
1123
1124
1125 ?>