| 1 |
<?php |
| 2 |
namespace Forumax\WpWidgets; |
| 3 |
use WP_Widget; |
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Forumax_Recent_Topics |
| 8 |
* A list of recent topics, sorted by: newness, popularity, or recent replies. |
| 9 |
* |
| 10 |
* @package Forumax\WpWidgets |
| 11 |
*/ |
| 12 |
class Forumax_Recent_Topics extends WP_Widget |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Forumax Recent Topics Widget |
| 16 |
* |
| 17 |
* Registers the topic widget |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$widget_ops = apply_filters('forumax_topics_widget_options', array( |
| 22 |
'classname' => 'forumax_recent_topics_widget widget_display_topics', |
| 23 |
'description' => esc_html__('A list of recent topics, sorted by: newness, popularity, or recent replies.', 'forumax'), |
| 24 |
'customize_selective_refresh' => true |
| 25 |
)); |
| 26 |
|
| 27 |
parent::__construct('forumax_recent_topics', esc_html__('(Forumax) Recent Topics', 'forumax'), $widget_ops); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register the widget |
| 32 |
*/ |
| 33 |
public static function register_widget() |
| 34 |
{ |
| 35 |
register_widget(__CLASS__); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Displays the output, the topic list |
| 40 |
* |
| 41 |
* @param array $args Arguments |
| 42 |
* @param array $instance Instance |
| 43 |
*/ |
| 44 |
public function widget($args = array(), $instance = array()) |
| 45 |
{ |
| 46 |
wp_enqueue_style('bbpc-forum'); |
| 47 |
|
| 48 |
// Get widget settings |
| 49 |
$settings = $this->parse_settings($instance); |
| 50 |
|
| 51 |
// Typical WordPress filter |
| 52 |
$settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); |
| 53 |
|
| 54 |
// Forumax filter |
| 55 |
$settings['title'] = apply_filters('forumax_topic_widget_title', $settings['title'], $instance, $this->id_base); |
| 56 |
|
| 57 |
// How do we want to order our results? |
| 58 |
switch ($settings['order_by']) { |
| 59 |
|
| 60 |
// Order by most recent replies |
| 61 |
case 'freshness': |
| 62 |
$topics_query = array( |
| 63 |
'post_type' => bbp_get_topic_post_type(), |
| 64 |
'post_status' => bbp_get_public_topic_statuses(), |
| 65 |
'post_parent' => $settings['parent_forum'], |
| 66 |
'posts_per_page' => (int) $settings['max_shown'], |
| 67 |
'meta_query' => array( |
| 68 |
array( |
| 69 |
'key' => '_bbp_last_active_time', |
| 70 |
'type' => 'DATETIME' |
| 71 |
) |
| 72 |
), |
| 73 |
'orderby' => 'meta_value', |
| 74 |
'order' => 'DESC', |
| 75 |
'ignore_sticky_posts' => true, |
| 76 |
'no_found_rows' => true, |
| 77 |
'update_post_term_cache' => false, |
| 78 |
'update_post_meta_cache' => false |
| 79 |
); |
| 80 |
break; |
| 81 |
|
| 82 |
// Order by total number of replies |
| 83 |
case 'popular': |
| 84 |
$topics_query = array( |
| 85 |
'post_type' => bbp_get_topic_post_type(), |
| 86 |
'post_status' => bbp_get_public_topic_statuses(), |
| 87 |
'post_parent' => $settings['parent_forum'], |
| 88 |
'posts_per_page' => (int) $settings['max_shown'], |
| 89 |
'meta_query' => array( |
| 90 |
array( |
| 91 |
'key' => '_bbp_reply_count', |
| 92 |
'type' => 'NUMERIC' |
| 93 |
) |
| 94 |
), |
| 95 |
'orderby' => 'meta_value_num', |
| 96 |
'order' => 'DESC', |
| 97 |
'ignore_sticky_posts' => true, |
| 98 |
'no_found_rows' => true, |
| 99 |
'update_post_term_cache' => false, |
| 100 |
'update_post_meta_cache' => false |
| 101 |
); |
| 102 |
break; |
| 103 |
|
| 104 |
// Order by which topic was created most recently |
| 105 |
case 'newness': |
| 106 |
default: |
| 107 |
$topics_query = array( |
| 108 |
'post_type' => bbp_get_topic_post_type(), |
| 109 |
'post_status' => bbp_get_public_topic_statuses(), |
| 110 |
'post_parent' => $settings['parent_forum'], |
| 111 |
'posts_per_page' => (int) $settings['max_shown'], |
| 112 |
'orderby' => 'date', |
| 113 |
'order' => 'DESC', |
| 114 |
'ignore_sticky_posts' => true, |
| 115 |
'no_found_rows' => true, |
| 116 |
'update_post_term_cache' => false, |
| 117 |
'update_post_meta_cache' => false |
| 118 |
); |
| 119 |
break; |
| 120 |
} |
| 121 |
|
| 122 |
// Note: private and hidden forums will be excluded via the |
| 123 |
// bbp_pre_get_posts_normalize_forum_visibility action and function. |
| 124 |
$widget_query = new WP_Query($topics_query); |
| 125 |
|
| 126 |
// Bail if no topics are found |
| 127 |
if (!$widget_query->have_posts()) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
echo $args['before_widget']; |
| 132 |
|
| 133 |
if (!empty($settings['title'])) { |
| 134 |
echo $args['before_title'] . $settings['title'] . $args['after_title']; |
| 135 |
} ?> |
| 136 |
|
| 137 |
<ul class="frmx-list-unstyled bbp-topics-widget <?php echo esc_attr($settings['order_by']); ?>"> |
| 138 |
|
| 139 |
<?php while ($widget_query->have_posts()): |
| 140 |
|
| 141 |
$widget_query->the_post(); |
| 142 |
$topic_id = bbp_get_topic_id($widget_query->post->ID); |
| 143 |
$author_link = bbp_get_topic_author_link(array('post_id' => $topic_id, 'type' => 'both', 'size' => 14)); |
| 144 |
?> |
| 145 |
|
| 146 |
<li> |
| 147 |
<a class="bbp-forum-title" href="<?php bbp_topic_permalink($topic_id); ?>"> |
| 148 |
<?php bbp_topic_title($topic_id); ?> |
| 149 |
</a> |
| 150 |
|
| 151 |
<?php if (!empty($author_link)): ?> |
| 152 |
<?php printf(esc_html_x('by %1$s', 'widgets', 'bbpress'), '<span class="topic-author">' . $author_link . '</span>'); ?> |
| 153 |
<?php endif; ?> |
| 154 |
|
| 155 |
<div><?php bbp_topic_last_active_time($topic_id); ?></div> |
| 156 |
</li> |
| 157 |
|
| 158 |
<?php endwhile; ?> |
| 159 |
|
| 160 |
</ul> |
| 161 |
|
| 162 |
<?php echo $args['after_widget']; |
| 163 |
|
| 164 |
// Reset the $post global |
| 165 |
wp_reset_postdata(); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Update the topic widget options |
| 170 |
* |
| 171 |
* @param array $new_instance The new instance options |
| 172 |
* @param array $old_instance The old instance options |
| 173 |
* @return array Updated instance |
| 174 |
*/ |
| 175 |
public function update($new_instance = array(), $old_instance = array()) |
| 176 |
{ |
| 177 |
$instance = $old_instance; |
| 178 |
$instance['title'] = strip_tags($new_instance['title']); |
| 179 |
$instance['order_by'] = strip_tags($new_instance['order_by']); |
| 180 |
$instance['parent_forum'] = sanitize_text_field($new_instance['parent_forum']); |
| 181 |
$instance['max_shown'] = (int) $new_instance['max_shown']; |
| 182 |
|
| 183 |
// Date |
| 184 |
$instance['show_date'] = isset($new_instance['show_date']) |
| 185 |
? (bool) $new_instance['show_date'] |
| 186 |
: false; |
| 187 |
|
| 188 |
// Author |
| 189 |
$instance['show_user'] = isset($new_instance['show_user']) |
| 190 |
? (bool) $new_instance['show_user'] |
| 191 |
: false; |
| 192 |
|
| 193 |
// Force to any |
| 194 |
if (!empty($instance['parent_forum']) && !is_numeric($instance['parent_forum'])) { |
| 195 |
$instance['parent_forum'] = 'any'; |
| 196 |
} |
| 197 |
|
| 198 |
return $instance; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Output the topic widget options form |
| 203 |
* |
| 204 |
* @param array $instance Instance |
| 205 |
*/ |
| 206 |
public function form($instance = array()) |
| 207 |
{ |
| 208 |
|
| 209 |
// Get widget settings |
| 210 |
$settings = $this->parse_settings($instance); ?> |
| 211 |
|
| 212 |
<p> |
| 213 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:', 'forumax'); ?> |
| 214 |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
| 215 |
name="<?php echo $this->get_field_name('title'); ?>" type="text" |
| 216 |
value="<?php echo esc_attr($settings['title']); ?>" /> |
| 217 |
</label> |
| 218 |
</p> |
| 219 |
|
| 220 |
<p> |
| 221 |
<label |
| 222 |
for="<?php echo $this->get_field_id('max_shown'); ?>"><?php esc_html_e('Maximum topics to show:', 'forumax'); ?> |
| 223 |
<input class="widefat" id="<?php echo $this->get_field_id('max_shown'); ?>" |
| 224 |
name="<?php echo $this->get_field_name('max_shown'); ?>" type="text" |
| 225 |
value="<?php echo esc_attr($settings['max_shown']); ?>" /> |
| 226 |
</label> |
| 227 |
</p> |
| 228 |
|
| 229 |
<p> |
| 230 |
<label |
| 231 |
for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php esc_html_e('Parent Forum ID:', 'forumax'); ?> |
| 232 |
<input class="widefat" id="<?php echo $this->get_field_id('parent_forum'); ?>" |
| 233 |
name="<?php echo $this->get_field_name('parent_forum'); ?>" type="text" |
| 234 |
value="<?php echo esc_attr($settings['parent_forum']); ?>" /> |
| 235 |
</label> |
| 236 |
|
| 237 |
<br /> |
| 238 |
|
| 239 |
<small><?php esc_html_e('"0" to show only root - "any" to show all', 'forumax'); ?></small> |
| 240 |
</p> |
| 241 |
|
| 242 |
<p> |
| 243 |
<label for="<?php echo $this->get_field_id('show_date'); ?>"> |
| 244 |
<input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>" |
| 245 |
name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked(true, $settings['show_date']); ?> |
| 246 |
value="1" /> |
| 247 |
<?php esc_html_e('Show post date', 'forumax'); ?> |
| 248 |
</label> |
| 249 |
</p> |
| 250 |
|
| 251 |
<p> |
| 252 |
<label for="<?php echo $this->get_field_id('show_user'); ?>"> |
| 253 |
<input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>" |
| 254 |
name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked(true, $settings['show_user']); ?> |
| 255 |
value="1" /> |
| 256 |
<?php esc_html_e('Show topic author', 'forumax'); ?> |
| 257 |
</label> |
| 258 |
</p> |
| 259 |
|
| 260 |
<p> |
| 261 |
<label for="<?php echo $this->get_field_id('order_by'); ?>"><?php esc_html_e('Order By:', 'forumax'); ?></label> |
| 262 |
<select class="widefat" name="<?php echo $this->get_field_name('order_by'); ?>" |
| 263 |
id="<?php echo $this->get_field_id('order_by'); ?>"> |
| 264 |
<option <?php selected($settings['order_by'], 'newness'); ?> value="newness"> |
| 265 |
<?php esc_html_e('Newest Topics', 'forumax'); ?></option> |
| 266 |
<option <?php selected($settings['order_by'], 'popular'); ?> value="popular"> |
| 267 |
<?php esc_html_e('Popular Topics', 'forumax'); ?></option> |
| 268 |
<option <?php selected($settings['order_by'], 'freshness'); ?> value="freshness"> |
| 269 |
<?php esc_html_e('Topics With Recent Replies', 'forumax'); ?></option> |
| 270 |
</select> |
| 271 |
</p> |
| 272 |
|
| 273 |
<?php |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Merge the widget settings into defaults array. |
| 278 |
* |
| 279 |
* @param array $instance Instance |
| 280 |
* @return array Parsed settings |
| 281 |
*/ |
| 282 |
public function parse_settings($instance = array()) |
| 283 |
{ |
| 284 |
return bbp_parse_args($instance, array( |
| 285 |
'title' => esc_html__('Recent Topics', 'forumax'), |
| 286 |
'max_shown' => 5, |
| 287 |
'show_date' => true, |
| 288 |
'show_user' => true, |
| 289 |
'parent_forum' => 'any', |
| 290 |
'order_by' => 'newness' |
| 291 |
), 'forumax_topic_widget_settings'); |
| 292 |
} |
| 293 |
} |
| 294 |
|