| 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 |
} |
| 12 |
|
| 13 |
|
| 14 |
add_action('widgets_init', 'register_pg_widgets'); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
// Exit if accessed directly |
| 19 |
if (!defined('ABSPATH')) |
| 20 |
exit; |
| 21 |
|
| 22 |
/** |
| 23 |
* pg Forum Widget* |
| 24 |
* Adds a widget which displays the forum list |
| 25 |
* |
| 26 |
*/ |
| 27 |
class pg_Forums_Widget extends WP_Widget { |
| 28 |
|
| 29 |
/** |
| 30 |
* primarily the code from the bbpress forum widget ! |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$widget_ops = apply_filters('pg_forums_widget_options', array( |
| 34 |
'classname' => 'widget_display_forums', |
| 35 |
'description' => __('A list of forums with an option to set the parent.', 'private groups') |
| 36 |
)); |
| 37 |
|
| 38 |
parent::__construct(false, __('(Private Groups) Forums List', 'private-groups'), $widget_ops); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register the widget |
| 43 |
* |
| 44 |
* @since bbPress (r3389) |
| 45 |
* |
| 46 |
* @uses register_widget() |
| 47 |
*/ |
| 48 |
public static function register_widget() { |
| 49 |
register_widget('pg_Forums_Widget'); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Displays the output, the forum list |
| 54 |
* |
| 55 |
* @since bbPress (r2653) |
| 56 |
* |
| 57 |
* @param mixed $args Arguments |
| 58 |
* @param array $instance Instance |
| 59 |
* @uses apply_filters() Calls 'bbp_forum_widget_title' with the title |
| 60 |
* @uses get_option() To get the forums per page option |
| 61 |
* @uses current_user_can() To check if the current user can read |
| 62 |
* private() To resety name |
| 63 |
* @uses bbp_has_forums() The main forum loop |
| 64 |
* @uses bbp_forums() To check whether there are more forums available |
| 65 |
* in the loop |
| 66 |
* @uses bbp_the_forum() Loads up the current forum in the loop |
| 67 |
* @uses bbp_forum_permalink() To display the forum permalink |
| 68 |
* @uses bbp_forum_title() To display the forum title |
| 69 |
*/ |
| 70 |
public function widget($args, $instance) { |
| 71 |
|
| 72 |
// Get widget settings |
| 73 |
$settings = $this->parse_settings($instance); |
| 74 |
|
| 75 |
// Typical WordPress filter |
| 76 |
$settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); |
| 77 |
|
| 78 |
// bbPress filter |
| 79 |
$settings['title'] = apply_filters('pg_widget_title', $settings['title'], $instance, $this->id_base); |
| 80 |
|
| 81 |
// Note: private and hidden forums will be excluded via the |
| 82 |
// bbp_pre_get_posts_exclude_forums filter and function. |
| 83 |
$query_data = array( |
| 84 |
'post_type' => bbp_get_forum_post_type(), |
| 85 |
'post_parent' => $settings['parent_forum'], |
| 86 |
'post_status' => bbp_get_public_status_id(), |
| 87 |
'posts_per_page' => get_option('_bbp_forums_per_page', 50), |
| 88 |
'orderby' => 'menu_order', |
| 89 |
'order' => 'ASC' |
| 90 |
); |
| 91 |
|
| 92 |
//PRIVATE GROUPS Get an array of IDs which the current user has permissions to view |
| 93 |
$allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($query_data)); |
| 94 |
// The default forum query with allowed forum ids array added |
| 95 |
$query_data['post__in'] = $allowed_posts; |
| 96 |
|
| 97 |
$widget_query = new WP_Query($query_data); |
| 98 |
|
| 99 |
// Bail if no posts |
| 100 |
if (!$widget_query->have_posts()) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
echo $args['before_widget']; |
| 105 |
if (!empty($settings['title'])) { |
| 106 |
echo $args['before_title'] . $settings['title'] . $args['after_title']; |
| 107 |
} |
| 108 |
?> |
| 109 |
|
| 110 |
<ul> |
| 111 |
|
| 112 |
<?php while ($widget_query->have_posts()) : $widget_query->the_post(); ?> |
| 113 |
|
| 114 |
<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> |
| 115 |
|
| 116 |
<?php endwhile; ?> |
| 117 |
|
| 118 |
</ul> |
| 119 |
|
| 120 |
<?php |
| 121 |
echo $args['after_widget']; |
| 122 |
|
| 123 |
// Reset the $post global |
| 124 |
wp_reset_postdata(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Update the forum widget options |
| 129 |
* |
| 130 |
* @since bbPress (r2653) |
| 131 |
* |
| 132 |
* @param array $new_instance The new instance options |
| 133 |
* @param array $old_instance The old instance options |
| 134 |
*/ |
| 135 |
public function update($new_instance, $old_instance) { |
| 136 |
$instance = $old_instance; |
| 137 |
$instance['title'] = strip_tags($new_instance['title']); |
| 138 |
$instance['parent_forum'] = $new_instance['parent_forum']; |
| 139 |
|
| 140 |
// Force to any |
| 141 |
if (!empty($instance['parent_forum']) && !is_numeric($instance['parent_forum'])) { |
| 142 |
$instance['parent_forum'] = 'any'; |
| 143 |
} |
| 144 |
|
| 145 |
return $instance; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Output the forum widget options form |
| 150 |
* |
| 151 |
* @since bbPress (r2653) |
| 152 |
* |
| 153 |
* @param $instance Instance |
| 154 |
* |
| 155 |
*/ |
| 156 |
public function form($instance) { |
| 157 |
|
| 158 |
// Get widget settings |
| 159 |
$settings = $this->parse_settings($instance); |
| 160 |
?> |
| 161 |
|
| 162 |
<p> |
| 163 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bbpress'); ?> |
| 164 |
<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']); ?>" /> |
| 165 |
</label> |
| 166 |
</p> |
| 167 |
|
| 168 |
<p> |
| 169 |
<label for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php _e('Parent Forum ID:', 'bbpress'); ?> |
| 170 |
<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']); ?>" /> |
| 171 |
</label> |
| 172 |
|
| 173 |
<br /> |
| 174 |
|
| 175 |
<small><?php _e('"0" to show only root - "any" to show all', 'private groups'); ?></small> |
| 176 |
</p> |
| 177 |
|
| 178 |
<?php |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Merge the widget settings into defaults array. |
| 183 |
* |
| 184 |
* @since bbPress (r4802) |
| 185 |
* |
| 186 |
* @param $instance Instance |
| 187 |
* @uses bbp_parse_args() To merge widget settings into defaults |
| 188 |
*/ |
| 189 |
public function parse_settings($instance = array()) { |
| 190 |
return bbp_parse_args($instance, array( |
| 191 |
'title' => __('Forums', 'bbpress'), |
| 192 |
'parent_forum' => 0 |
| 193 |
), 'forum_widget_settings'); |
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* PRIVATE GROUPS Topic Widget |
| 200 |
* |
| 201 |
* Adds a widget which displays the topic list |
| 202 |
* |
| 203 |
* @since bbPress (r2653) |
| 204 |
* |
| 205 |
* @uses WP_Widget |
| 206 |
*/ |
| 207 |
class pg_Topics_Widget extends WP_Widget { |
| 208 |
|
| 209 |
/** |
| 210 |
* bbPress Topic Widget |
| 211 |
* |
| 212 |
* Registers the topic widget |
| 213 |
* |
| 214 |
* @since bbPress (r2653) |
| 215 |
* |
| 216 |
* @uses apply_filters() Calls 'bbp_topics_widget_options' with the |
| 217 |
* widget options |
| 218 |
*/ |
| 219 |
public function __construct() { |
| 220 |
$widget_ops = apply_filters('pg_topics_widget_options', array( |
| 221 |
'classname' => 'widget_display_topics', |
| 222 |
'description' => __('A list of recent topics, sorted by popularity or freshness.', 'private groups') |
| 223 |
)); |
| 224 |
|
| 225 |
parent::__construct(false, __('(Private Groups) Recent Topics', 'private groups'), $widget_ops); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Register the widget |
| 230 |
* |
| 231 |
* @since bbPress (r3389) |
| 232 |
* |
| 233 |
* @uses register_widget() |
| 234 |
*/ |
| 235 |
public static function register_widget() { |
| 236 |
register_widget('pg_Topics_Widget'); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Displays the output, the topic list |
| 241 |
* |
| 242 |
* @since bbPress (r2653) |
| 243 |
* |
| 244 |
* @param mixed $args |
| 245 |
* @param array $instance |
| 246 |
* @uses apply_filters() Calls 'bbp_topic_widget_title' with the title |
| 247 |
* @uses bbp_topic_permalink() To display the topic permalink |
| 248 |
* @uses bbp_topic_title() To display the topic title |
| 249 |
* @uses bbp_get_topic_last_active_time() To get the topic last active |
| 250 |
* time |
| 251 |
* @uses bbp_get_topic_id() To get the topic id |
| 252 |
*/ |
| 253 |
public function widget($args = array(), $instance = array()) { |
| 254 |
|
| 255 |
// Get widget settings |
| 256 |
$settings = $this->parse_settings($instance); |
| 257 |
|
| 258 |
// Typical WordPress filter |
| 259 |
$settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); |
| 260 |
|
| 261 |
// bbPress filter |
| 262 |
$settings['title'] = apply_filters('pg_topic_widget_title', $settings['title'], $instance, $this->id_base); |
| 263 |
|
| 264 |
// How do we want to order our results? |
| 265 |
switch ($settings['order_by']) { |
| 266 |
|
| 267 |
// Order by most recent replies |
| 268 |
case 'freshness' : |
| 269 |
$topics_query = array( |
| 270 |
'post_type' => bbp_get_topic_post_type(), |
| 271 |
'post_parent' => $settings['parent_forum'], |
| 272 |
'posts_per_page' => '50', |
| 273 |
'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), |
| 274 |
'show_stickies' => false, |
| 275 |
'meta_key' => '_bbp_last_active_time', |
| 276 |
'orderby' => 'meta_value', |
| 277 |
'order' => 'DESC', |
| 278 |
); |
| 279 |
break; |
| 280 |
|
| 281 |
// Order by total number of replies |
| 282 |
case 'popular' : |
| 283 |
$topics_query = array( |
| 284 |
'post_type' => bbp_get_topic_post_type(), |
| 285 |
'post_parent' => $settings['parent_forum'], |
| 286 |
'posts_per_page' => '50', |
| 287 |
'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), |
| 288 |
'show_stickies' => false, |
| 289 |
'meta_key' => '_bbp_reply_count', |
| 290 |
'orderby' => 'meta_value', |
| 291 |
'order' => 'DESC' |
| 292 |
); |
| 293 |
break; |
| 294 |
|
| 295 |
// Order by which topic was created most recently |
| 296 |
case 'newness' : |
| 297 |
default : |
| 298 |
$topics_query = array( |
| 299 |
'post_type' => bbp_get_topic_post_type(), |
| 300 |
'post_parent' => $settings['parent_forum'], |
| 301 |
'posts_per_page' => '50', |
| 302 |
'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), |
| 303 |
'show_stickies' => false, |
| 304 |
'order' => 'DESC' |
| 305 |
); |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
//PRIVATE GROUPS Get an array of IDs which the current user has permissions to view |
| 310 |
$allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($topics_query)); |
| 311 |
// The default forum query with allowed forum ids array added |
| 312 |
$topics_query['post__in'] = $allowed_posts; |
| 313 |
//reset the max to be shown |
| 314 |
$topics_query['posts_per_page'] =(int) $settings['max_shown'] ; |
| 315 |
|
| 316 |
// Note: private and hidden forums will be excluded via the |
| 317 |
// bbp_pre_get_posts_exclude_forums filter and function. |
| 318 |
$widget_query = new WP_Query($topics_query); |
| 319 |
|
| 320 |
// Bail if no topics are found |
| 321 |
if (!$widget_query->have_posts()) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
echo $args['before_widget']; |
| 326 |
|
| 327 |
if (!empty($settings['title'])) { |
| 328 |
echo $args['before_title'] . $settings['title'] . $args['after_title']; |
| 329 |
} |
| 330 |
?> |
| 331 |
|
| 332 |
<ul> |
| 333 |
|
| 334 |
<?php |
| 335 |
while ($widget_query->have_posts()) : |
| 336 |
|
| 337 |
$widget_query->the_post(); |
| 338 |
$topic_id = bbp_get_topic_id($widget_query->post->ID); |
| 339 |
$author_link = ''; |
| 340 |
|
| 341 |
// Maybe get the topic author |
| 342 |
if ('on' == $settings['show_user']) : |
| 343 |
$author_link = bbp_get_topic_author_link(array('post_id' => $topic_id, 'type' => 'both', 'size' => 14)); |
| 344 |
endif; |
| 345 |
?> |
| 346 |
|
| 347 |
<li> |
| 348 |
<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> |
| 349 |
|
| 350 |
<?php if (!empty($author_link)) : ?> |
| 351 |
|
| 352 |
<?php printf(_x('by %1$s', 'widgets', 'bbpress'), '<span class="topic-author">' . $author_link . '</span>'); ?> |
| 353 |
|
| 354 |
<?php endif; ?> |
| 355 |
|
| 356 |
<?php if ('on' == $settings['show_date']) : ?> |
| 357 |
|
| 358 |
<div><?php bbp_topic_last_active_time($topic_id); ?></div> |
| 359 |
|
| 360 |
<?php endif; ?> |
| 361 |
|
| 362 |
</li> |
| 363 |
|
| 364 |
<?php endwhile; ?> |
| 365 |
|
| 366 |
</ul> |
| 367 |
|
| 368 |
<?php |
| 369 |
echo $args['after_widget']; |
| 370 |
|
| 371 |
// Reset the $post global |
| 372 |
wp_reset_postdata(); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Update the topic widget options |
| 377 |
* |
| 378 |
* @since bbPress (r2653) |
| 379 |
* |
| 380 |
* @param array $new_instance The new instance options |
| 381 |
* @param array $old_instance The old instance options |
| 382 |
*/ |
| 383 |
public function update($new_instance = array(), $old_instance = array()) { |
| 384 |
$instance = $old_instance; |
| 385 |
$instance['title'] = strip_tags($new_instance['title']); |
| 386 |
$instance['order_by'] = strip_tags($new_instance['order_by']); |
| 387 |
$instance['show_date'] = (bool) $new_instance['show_date']; |
| 388 |
$instance['show_user'] = (bool) $new_instance['show_user']; |
| 389 |
$instance['max_shown'] = (int) $new_instance['max_shown']; |
| 390 |
|
| 391 |
// Force to any |
| 392 |
if (!empty($instance['parent_forum']) || !is_numeric($instance['parent_forum'])) { |
| 393 |
$instance['parent_forum'] = 'any'; |
| 394 |
} else { |
| 395 |
$instance['parent_forum'] = (int) $new_instance['parent_forum']; |
| 396 |
} |
| 397 |
|
| 398 |
return $instance; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Output the topic widget options form |
| 403 |
* |
| 404 |
* @since bbPress (r2653) |
| 405 |
* |
| 406 |
* @param $instance Instance |
| 407 |
* |
| 408 |
*/ |
| 409 |
public function form($instance = array()) { |
| 410 |
|
| 411 |
// Get widget settings |
| 412 |
$settings = $this->parse_settings($instance); |
| 413 |
?> |
| 414 |
|
| 415 |
<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> |
| 416 |
<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> |
| 417 |
|
| 418 |
<p> |
| 419 |
<label for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php _e('Parent Forum ID:', 'bbpress'); ?> |
| 420 |
<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']); ?>" /> |
| 421 |
</label> |
| 422 |
|
| 423 |
<br /> |
| 424 |
|
| 425 |
<small><?php _e('"0" to show only root - "any" to show all', 'bbpress'); ?></small> |
| 426 |
</p> |
| 427 |
|
| 428 |
<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( true, $settings['show_date'] ); ?> value="1" /></label></p> |
| 429 |
<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( true, $settings['show_user'] ); ?> value="1" /></label></p> |
| 430 |
<p> |
| 431 |
<label for="<?php echo $this->get_field_id('order_by'); ?>"><?php _e('Order By:', 'bbpress'); ?></label> |
| 432 |
<select name="<?php echo $this->get_field_name('order_by'); ?>" id="<?php echo $this->get_field_name('order_by'); ?>"> |
| 433 |
<option <?php selected($settings['order_by'], 'newness'); ?> value="newness"><?php _e('Newest Topics', 'bbpress'); ?></option> |
| 434 |
<option <?php selected($settings['order_by'], 'popular'); ?> value="popular"><?php _e('Popular Topics', 'bbpress'); ?></option> |
| 435 |
<option <?php selected($settings['order_by'], 'freshness'); ?> value="freshness"><?php _e('Topics With Recent Replies', 'bbpress'); ?></option> |
| 436 |
</select> |
| 437 |
</p> |
| 438 |
|
| 439 |
<?php |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Merge the widget settings into defaults array. |
| 444 |
* |
| 445 |
* @since bbPress (r4802) |
| 446 |
* |
| 447 |
* @param $instance Instance |
| 448 |
* @uses bbp_parse_args() To merge widget options into defaults |
| 449 |
*/ |
| 450 |
public function parse_settings($instance = array()) { |
| 451 |
return bbp_parse_args($instance, array( |
| 452 |
'title' => __('Recent Topics', 'bbpress'), |
| 453 |
'max_shown' => 5, |
| 454 |
'show_date' => false, |
| 455 |
'show_user' => false, |
| 456 |
'parent_forum' => 'any', |
| 457 |
'order_by' => false |
| 458 |
), 'topic_widget_settings'); |
| 459 |
} |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* PRIVATE GROUPS Replies Widget |
| 465 |
* |
| 466 |
* Adds a widget which displays the replies list |
| 467 |
* |
| 468 |
* |
| 469 |
* @uses WP_Widget |
| 470 |
*/ |
| 471 |
class pg_Replies_Widget extends WP_Widget { |
| 472 |
|
| 473 |
/** |
| 474 |
* pg Replies Widget |
| 475 |
* |
| 476 |
* Registers the replies widget |
| 477 |
* |
| 478 |
* |
| 479 |
* @uses apply_filters() Calls 'bbp_replies_widget_options' with the |
| 480 |
* widget options |
| 481 |
*/ |
| 482 |
public function __construct() { |
| 483 |
$widget_ops = apply_filters('pg_replies_widget_options', array( |
| 484 |
'classname' => 'widget_display_replies', |
| 485 |
'description' => __('A list of the most recent replies.', 'private groups') |
| 486 |
)); |
| 487 |
|
| 488 |
parent::__construct(false, __('(Private Groups) Recent Replies', 'private groups'), $widget_ops); |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Register the widget |
| 493 |
* |
| 494 |
* @since bbPress (r3389) |
| 495 |
* |
| 496 |
* @uses register_widget() |
| 497 |
*/ |
| 498 |
public static function register_widget() { |
| 499 |
register_widget('pg_Replies_Widget'); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Displays the output, the replies list |
| 504 |
* |
| 505 |
* @since bbPress (r2653) |
| 506 |
* |
| 507 |
* @param mixed $args |
| 508 |
* @param array $instance |
| 509 |
* @uses apply_filters() Calls 'bbp_reply_widget_title' with the title |
| 510 |
* @uses bbp_get_reply_author_link() To get the reply author link |
| 511 |
* @uses bbp_get_reply_author() To get the reply author name |
| 512 |
* @uses bbp_get_reply_id() To get the reply id |
| 513 |
* @uses bbp_get_reply_url() To get the reply url |
| 514 |
* @uses bbp_get_reply_excerpt() To get the reply excerpt |
| 515 |
* @uses bbp_get_reply_topic_title() To get the reply topic title |
| 516 |
* @uses get_the_date() To get the date of the reply |
| 517 |
* @uses get_the_time() To get the time of the reply |
| 518 |
*/ |
| 519 |
public function widget($args, $instance) { |
| 520 |
|
| 521 |
// Get widget settings |
| 522 |
$settings = $this->parse_settings($instance); |
| 523 |
|
| 524 |
// Typical WordPress filter |
| 525 |
$settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); |
| 526 |
|
| 527 |
// bbPress filter |
| 528 |
$settings['title'] = apply_filters('pg_replies_widget_title', $settings['title'], $instance, $this->id_base); |
| 529 |
|
| 530 |
// Note: private and hidden forums will be excluded via the |
| 531 |
// bbp_pre_get_posts_exclude_forums filter and function. |
| 532 |
$query_data = array( |
| 533 |
'post_type' => bbp_get_reply_post_type(), |
| 534 |
'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), |
| 535 |
'posts_per_page' => '50', |
| 536 |
|
| 537 |
); |
| 538 |
|
| 539 |
//PRIVATE GROUPS Get an array of IDs which the current user has permissions to view |
| 540 |
$allowed_posts = private_groups_get_permitted_post_ids(new WP_Query($query_data)); |
| 541 |
// The default forum query with allowed forum ids array added |
| 542 |
$query_data['post__in'] = $allowed_posts; |
| 543 |
|
| 544 |
//now set max posts |
| 545 |
$query_data ['posts_per_page'] = (int) $settings['max_shown'] ; |
| 546 |
|
| 547 |
$widget_query = new WP_Query($query_data); |
| 548 |
|
| 549 |
// Bail if no replies |
| 550 |
if (!$widget_query->have_posts()) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
echo $args['before_widget']; |
| 555 |
|
| 556 |
if (!empty($settings['title'])) { |
| 557 |
echo $args['before_title'] . $settings['title'] . $args['after_title']; |
| 558 |
} |
| 559 |
?> |
| 560 |
|
| 561 |
<ul> |
| 562 |
|
| 563 |
<?php while ($widget_query->have_posts()) : $widget_query->the_post(); ?> |
| 564 |
|
| 565 |
<li> |
| 566 |
|
| 567 |
<?php |
| 568 |
// Verify the reply ID |
| 569 |
$reply_id = bbp_get_reply_id($widget_query->post->ID); |
| 570 |
$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>'; |
| 571 |
|
| 572 |
// Only query user if showing them |
| 573 |
if ('on' == $settings['show_user']) : |
| 574 |
$author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14)); |
| 575 |
else : |
| 576 |
$author_link = false; |
| 577 |
endif; |
| 578 |
|
| 579 |
// Reply author, link, and timestamp |
| 580 |
if (( 'on' == $settings['show_date'] ) && !empty($author_link)) : |
| 581 |
|
| 582 |
// translators: 1: reply author, 2: reply link, 3: reply timestamp |
| 583 |
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>'); |
| 584 |
|
| 585 |
// Reply link and timestamp |
| 586 |
elseif ('on' == $settings['show_date']) : |
| 587 |
|
| 588 |
// translators: 1: reply link, 2: reply timestamp |
| 589 |
printf(_x('%1$s %2$s', 'widgets', 'bbpress'), $reply_link, '<div>' . bbp_get_time_since(get_the_time('U')) . '</div>'); |
| 590 |
|
| 591 |
// Reply author and title |
| 592 |
elseif (!empty($author_link)) : |
| 593 |
|
| 594 |
// translators: 1: reply author, 2: reply link |
| 595 |
printf(_x('%1$s on %2$s', 'widgets', 'bbpress'), $author_link, $reply_link); |
| 596 |
|
| 597 |
// Only the reply title |
| 598 |
else : |
| 599 |
|
| 600 |
// translators: 1: reply link |
| 601 |
printf(_x('%1$s', 'widgets', 'bbpress'), $reply_link); |
| 602 |
|
| 603 |
endif; |
| 604 |
?> |
| 605 |
|
| 606 |
</li> |
| 607 |
|
| 608 |
<?php endwhile; ?> |
| 609 |
|
| 610 |
</ul> |
| 611 |
|
| 612 |
<?php |
| 613 |
echo $args['after_widget']; |
| 614 |
|
| 615 |
// Reset the $post global |
| 616 |
wp_reset_postdata(); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Update the reply widget options |
| 621 |
* |
| 622 |
* @since bbPress (r2653) |
| 623 |
* |
| 624 |
* @param array $new_instance The new instance options |
| 625 |
* @param array $old_instance The old instance options |
| 626 |
*/ |
| 627 |
public function update($new_instance = array(), $old_instance = array()) { |
| 628 |
$instance = $old_instance; |
| 629 |
$instance['title'] = strip_tags($new_instance['title']); |
| 630 |
$instance['show_date'] = (bool) $new_instance['show_date']; |
| 631 |
$instance['show_user'] = (bool) $new_instance['show_user']; |
| 632 |
$instance['max_shown'] = (int) $new_instance['max_shown']; |
| 633 |
|
| 634 |
return $instance; |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Output the reply widget options form |
| 639 |
* |
| 640 |
* @since bbPress (r2653) |
| 641 |
* |
| 642 |
* @param $instance Instance |
| 643 |
* |
| 644 |
*/ |
| 645 |
public function form($instance = array()) { |
| 646 |
|
| 647 |
// Get widget settings |
| 648 |
$settings = $this->parse_settings($instance); |
| 649 |
?> |
| 650 |
|
| 651 |
<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> |
| 652 |
<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> |
| 653 |
<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( true, $settings['show_date'] ); ?> value="1" /></label></p> |
| 654 |
<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( true, $settings['show_user'] ); ?> value="1" /></label></p> |
| 655 |
|
| 656 |
<?php |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Merge the widget settings into defaults array. |
| 661 |
* |
| 662 |
* @since bbPress (r4802) |
| 663 |
* |
| 664 |
* @param $instance Instance |
| 665 |
* @uses bbp_parse_args() To merge widget settings into defaults |
| 666 |
*/ |
| 667 |
public function parse_settings($instance = array()) { |
| 668 |
return bbp_parse_args($instance, array( |
| 669 |
'title' => __('Recent Replies', 'private groups'), |
| 670 |
'max_shown' => 5, |
| 671 |
'show_date' => false, |
| 672 |
'show_user' => false |
| 673 |
), 'replies_widget_settings'); |
| 674 |
} |
| 675 |
|
| 676 |
} |
| 677 |
?> |