PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.1
Forumax – AI Powered Advanced Community Forum Plugin v2.5.1
2.5.1 2.5.0 2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 All 31 releases
bbp-core / widgets / Forumax_Recent_Topics.php

Forumax_Recent_Topics.php in Forumax – AI Powered Advanced Community Forum Plugin 2.5.1, at widgets/Forumax_Recent_Topics.php

297 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // The loop reads reply counts/freshness meta; load it in one query.
79 'update_post_meta_cache' => true
80 );
81 break;
82
83 // Order by total number of replies
84 case 'popular':
85 $topics_query = array(
86 'post_type' => bbp_get_topic_post_type(),
87 'post_status' => bbp_get_public_topic_statuses(),
88 'post_parent' => $settings['parent_forum'],
89 'posts_per_page' => (int) $settings['max_shown'],
90 'meta_query' => array(
91 array(
92 'key' => '_bbp_reply_count',
93 'type' => 'NUMERIC'
94 )
95 ),
96 'orderby' => 'meta_value_num',
97 'order' => 'DESC',
98 'ignore_sticky_posts' => true,
99 'no_found_rows' => true,
100 'update_post_term_cache' => false,
101 // The loop reads reply counts/freshness meta; load it in one query.
102 'update_post_meta_cache' => true
103 );
104 break;
105
106 // Order by which topic was created most recently
107 case 'newness':
108 default:
109 $topics_query = array(
110 'post_type' => bbp_get_topic_post_type(),
111 'post_status' => bbp_get_public_topic_statuses(),
112 'post_parent' => $settings['parent_forum'],
113 'posts_per_page' => (int) $settings['max_shown'],
114 'orderby' => 'date',
115 'order' => 'DESC',
116 'ignore_sticky_posts' => true,
117 'no_found_rows' => true,
118 'update_post_term_cache' => false,
119 // The loop reads reply counts/freshness meta; load it in one query.
120 'update_post_meta_cache' => true
121 );
122 break;
123 }
124
125 // Note: private and hidden forums will be excluded via the
126 // bbp_pre_get_posts_normalize_forum_visibility action and function.
127 $widget_query = new WP_Query($topics_query);
128
129 // Bail if no topics are found
130 if (!$widget_query->have_posts()) {
131 return;
132 }
133
134 echo $args['before_widget'];
135
136 if (!empty($settings['title'])) {
137 echo $args['before_title'] . $settings['title'] . $args['after_title'];
138 } ?>
139
140 <ul class="frmx-list-unstyled bbp-topics-widget <?php echo esc_attr($settings['order_by']); ?>">
141
142 <?php while ($widget_query->have_posts()):
143
144 $widget_query->the_post();
145 $topic_id = bbp_get_topic_id($widget_query->post->ID);
146 $author_link = bbp_get_topic_author_link(array('post_id' => $topic_id, 'type' => 'both', 'size' => 14));
147 ?>
148
149 <li>
150 <a class="bbp-forum-title" href="<?php bbp_topic_permalink($topic_id); ?>">
151 <?php bbp_topic_title($topic_id); ?>
152 </a>
153
154 <?php if (!empty($author_link)): ?>
155 <?php printf(esc_html_x('by %1$s', 'widgets', 'bbpress'), '<span class="topic-author">' . $author_link . '</span>'); ?>
156 <?php endif; ?>
157
158 <div><?php bbp_topic_last_active_time($topic_id); ?></div>
159 </li>
160
161 <?php endwhile; ?>
162
163 </ul>
164
165 <?php echo $args['after_widget'];
166
167 // Reset the $post global
168 wp_reset_postdata();
169 }
170
171 /**
172 * Update the topic widget options
173 *
174 * @param array $new_instance The new instance options
175 * @param array $old_instance The old instance options
176 * @return array Updated instance
177 */
178 public function update($new_instance = array(), $old_instance = array())
179 {
180 $instance = $old_instance;
181 $instance['title'] = strip_tags($new_instance['title']);
182 $instance['order_by'] = strip_tags($new_instance['order_by']);
183 $instance['parent_forum'] = sanitize_text_field($new_instance['parent_forum']);
184 $instance['max_shown'] = (int) $new_instance['max_shown'];
185
186 // Date
187 $instance['show_date'] = isset($new_instance['show_date'])
188 ? (bool) $new_instance['show_date']
189 : false;
190
191 // Author
192 $instance['show_user'] = isset($new_instance['show_user'])
193 ? (bool) $new_instance['show_user']
194 : false;
195
196 // Force to any
197 if (!empty($instance['parent_forum']) && !is_numeric($instance['parent_forum'])) {
198 $instance['parent_forum'] = 'any';
199 }
200
201 return $instance;
202 }
203
204 /**
205 * Output the topic widget options form
206 *
207 * @param array $instance Instance
208 */
209 public function form($instance = array())
210 {
211
212 // Get widget settings
213 $settings = $this->parse_settings($instance); ?>
214
215 <p>
216 <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:', 'forumax'); ?>
217 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
218 name="<?php echo $this->get_field_name('title'); ?>" type="text"
219 value="<?php echo esc_attr($settings['title']); ?>" />
220 </label>
221 </p>
222
223 <p>
224 <label
225 for="<?php echo $this->get_field_id('max_shown'); ?>"><?php esc_html_e('Maximum topics to show:', 'forumax'); ?>
226 <input class="widefat" id="<?php echo $this->get_field_id('max_shown'); ?>"
227 name="<?php echo $this->get_field_name('max_shown'); ?>" type="text"
228 value="<?php echo esc_attr($settings['max_shown']); ?>" />
229 </label>
230 </p>
231
232 <p>
233 <label
234 for="<?php echo $this->get_field_id('parent_forum'); ?>"><?php esc_html_e('Parent Forum ID:', 'forumax'); ?>
235 <input class="widefat" id="<?php echo $this->get_field_id('parent_forum'); ?>"
236 name="<?php echo $this->get_field_name('parent_forum'); ?>" type="text"
237 value="<?php echo esc_attr($settings['parent_forum']); ?>" />
238 </label>
239
240 <br />
241
242 <small><?php esc_html_e('"0" to show only root - "any" to show all', 'forumax'); ?></small>
243 </p>
244
245 <p>
246 <label for="<?php echo $this->get_field_id('show_date'); ?>">
247 <input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>"
248 name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked(true, $settings['show_date']); ?>
249 value="1" />
250 <?php esc_html_e('Show post date', 'forumax'); ?>
251 </label>
252 </p>
253
254 <p>
255 <label for="<?php echo $this->get_field_id('show_user'); ?>">
256 <input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>"
257 name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked(true, $settings['show_user']); ?>
258 value="1" />
259 <?php esc_html_e('Show topic author', 'forumax'); ?>
260 </label>
261 </p>
262
263 <p>
264 <label for="<?php echo $this->get_field_id('order_by'); ?>"><?php esc_html_e('Order By:', 'forumax'); ?></label>
265 <select class="widefat" name="<?php echo $this->get_field_name('order_by'); ?>"
266 id="<?php echo $this->get_field_id('order_by'); ?>">
267 <option <?php selected($settings['order_by'], 'newness'); ?> value="newness">
268 <?php esc_html_e('Newest Topics', 'forumax'); ?></option>
269 <option <?php selected($settings['order_by'], 'popular'); ?> value="popular">
270 <?php esc_html_e('Popular Topics', 'forumax'); ?></option>
271 <option <?php selected($settings['order_by'], 'freshness'); ?> value="freshness">
272 <?php esc_html_e('Topics With Recent Replies', 'forumax'); ?></option>
273 </select>
274 </p>
275
276 <?php
277 }
278
279 /**
280 * Merge the widget settings into defaults array.
281 *
282 * @param array $instance Instance
283 * @return array Parsed settings
284 */
285 public function parse_settings($instance = array())
286 {
287 return bbp_parse_args($instance, array(
288 'title' => esc_html__('Recent Topics', 'forumax'),
289 'max_shown' => 5,
290 'show_date' => true,
291 'show_user' => true,
292 'parent_forum' => 'any',
293 'order_by' => 'newness'
294 ), 'forumax_topic_widget_settings');
295 }
296 }
297