PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.4.0
Forumax – AI Powered Advanced Community Forum Plugin v2.4.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 2.0.0 2.1.0 All 29 releases
bbp-core / widgets / Forumax_Recent_Topics.php

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

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