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_Replies.php

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

230 lines 6.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_Replies
8 * A list of the most recent replies.
9 *
10 * @package Forumax\WpWidgets
11 */
12 class Forumax_Recent_Replies extends WP_Widget
13 {
14 /**
15 * Forumax Recent Replies Widget
16 *
17 * Registers the replies widget
18 */
19 public function __construct()
20 {
21 $widget_ops = apply_filters('forumax_replies_widget_options', array(
22 'classname' => 'forumax_recent_replies_widget widget_display_replies',
23 'description' => esc_html__('A list of the most recent replies.', 'forumax'),
24 'customize_selective_refresh' => true
25 ));
26
27 parent::__construct('forumax_recent_replies', esc_html__('(Forumax) Recent Replies', '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 replies 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_replies_widget_title', $settings['title'], $instance, $this->id_base);
56
57 // Note: private and hidden forums will be excluded via the
58 // bbp_pre_get_posts_normalize_forum_visibility action and function.
59 $widget_query = new WP_Query(array(
60 'post_type' => bbp_get_reply_post_type(),
61 'post_status' => bbp_get_public_reply_statuses(),
62 'posts_per_page' => (int) $settings['max_shown'],
63 'ignore_sticky_posts' => true,
64 'no_found_rows' => true,
65 'update_post_term_cache' => false,
66 // The loop reads each reply's topic/author meta; load it in one query.
67 'update_post_meta_cache' => true
68 ));
69
70 // Bail if no replies are found
71 if (!$widget_query->have_posts()) {
72 return;
73 }
74
75 // Parent topics are used for titles and links; fetch them together.
76 _prime_post_caches(array_unique(wp_list_pluck($widget_query->posts, 'post_parent')), false, true);
77
78 echo $args['before_widget'];
79
80 if (!empty($settings['title'])) {
81 echo $args['before_title'] . $settings['title'] . $args['after_title'];
82 } ?>
83
84 <ul class="bbp-replies-widget frmx-list-unstyled">
85
86 <?php while ($widget_query->have_posts()):
87 $widget_query->the_post(); ?>
88
89 <li>
90
91 <?php
92 // Verify the reply ID
93 $reply_id = bbp_get_reply_id($widget_query->post->ID);
94 $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)) . '">' . esc_html(bbp_get_reply_topic_title($reply_id)) . '</a>';
95 $time = get_the_time('U', $reply_id);
96 $show_date = '<time datetime="' . gmdate('Y-m-d H:i:s', $time) . '">' . esc_html(bbp_get_time_since($time)) . '</time>';
97
98 // Only query user if showing them
99 if (!empty($settings['show_user'])):
100 $author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14));
101 else:
102 $author_link = false;
103 endif;
104
105 // Reply author, link, and timestamp
106 if (!empty($settings['show_date']) && !empty($author_link)):
107
108 // translators: 1: reply author, 2: reply link, 3: reply timestamp
109 printf(esc_html_x('%1$s on %2$s %3$s', 'widgets', 'bbpress'), $author_link, $reply_link, $show_date);
110
111 // Reply link and timestamp
112 elseif (!empty($settings['show_date'])):
113 echo $reply_link . ' ' . $show_date;
114
115 // Reply author and title
116 elseif (!empty($author_link)):
117
118 // translators: 1: reply author, 2: reply link
119 printf(esc_html_x('%1$s on %2$s', 'widgets', 'bbpress'), $author_link, $reply_link);
120
121 // Only the reply title
122 else:
123 echo $reply_link;
124 endif;
125 ?>
126
127 </li>
128
129 <?php endwhile; ?>
130
131 </ul>
132
133 <?php echo $args['after_widget'];
134
135 // Reset the $post global
136 wp_reset_postdata();
137 }
138
139 /**
140 * Update the reply widget options
141 *
142 * @param array $new_instance The new instance options
143 * @param array $old_instance The old instance options
144 * @return array Updated instance
145 */
146 public function update($new_instance = array(), $old_instance = array())
147 {
148 $instance = $old_instance;
149 $instance['title'] = strip_tags($new_instance['title']);
150 $instance['max_shown'] = (int) $new_instance['max_shown'];
151
152 // Date
153 $instance['show_date'] = isset($new_instance['show_date'])
154 ? (bool) $new_instance['show_date']
155 : false;
156
157 // Author
158 $instance['show_user'] = isset($new_instance['show_user'])
159 ? (bool) $new_instance['show_user']
160 : false;
161
162 return $instance;
163 }
164
165 /**
166 * Output the reply widget options form
167 *
168 * @param array $instance Instance
169 */
170 public function form($instance = array())
171 {
172
173 // Get widget settings
174 $settings = $this->parse_settings($instance); ?>
175
176 <p>
177 <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:', 'forumax'); ?>
178 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
179 name="<?php echo $this->get_field_name('title'); ?>" type="text"
180 value="<?php echo esc_attr($settings['title']); ?>" />
181 </label>
182 </p>
183
184 <p>
185 <label
186 for="<?php echo $this->get_field_id('max_shown'); ?>"><?php esc_html_e('Maximum replies to show:', 'forumax'); ?>
187 <input class="widefat" id="<?php echo $this->get_field_id('max_shown'); ?>"
188 name="<?php echo $this->get_field_name('max_shown'); ?>" type="text"
189 value="<?php echo esc_attr($settings['max_shown']); ?>" />
190 </label>
191 </p>
192
193 <p>
194 <label for="<?php echo $this->get_field_id('show_date'); ?>">
195 <input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>"
196 name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked(true, $settings['show_date']); ?>
197 value="1" />
198 <?php esc_html_e('Show post date', 'forumax'); ?>
199 </label>
200 </p>
201
202 <p>
203 <label for="<?php echo $this->get_field_id('show_user'); ?>">
204 <input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>"
205 name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked(true, $settings['show_user']); ?>
206 value="1" />
207 <?php esc_html_e('Show reply author', 'forumax'); ?>
208 </label>
209 </p>
210
211 <?php
212 }
213
214 /**
215 * Merge the widget settings into defaults array.
216 *
217 * @param array $instance Instance
218 * @return array Parsed settings
219 */
220 public function parse_settings($instance = array())
221 {
222 return bbp_parse_args($instance, array(
223 'title' => esc_html__('Recent Replies', 'forumax'),
224 'max_shown' => 5,
225 'show_date' => true,
226 'show_user' => true
227 ), 'forumax_replies_widget_settings');
228 }
229 }
230