PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.4.3
Forumax – AI Powered Advanced Community Forum Plugin v2.4.3
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_Replies.php

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

226 lines 6.4 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 'update_post_meta_cache' => false
67 ));
68
69 // Bail if no replies are found
70 if (!$widget_query->have_posts()) {
71 return;
72 }
73
74 echo $args['before_widget'];
75
76 if (!empty($settings['title'])) {
77 echo $args['before_title'] . $settings['title'] . $args['after_title'];
78 } ?>
79
80 <ul class="bbp-replies-widget frmx-list-unstyled">
81
82 <?php while ($widget_query->have_posts()):
83 $widget_query->the_post(); ?>
84
85 <li>
86
87 <?php
88 // Verify the reply ID
89 $reply_id = bbp_get_reply_id($widget_query->post->ID);
90 $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>';
91 $time = get_the_time('U', $reply_id);
92 $show_date = '<time datetime="' . gmdate('Y-m-d H:i:s', $time) . '">' . esc_html(bbp_get_time_since($time)) . '</time>';
93
94 // Only query user if showing them
95 if (!empty($settings['show_user'])):
96 $author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14));
97 else:
98 $author_link = false;
99 endif;
100
101 // Reply author, link, and timestamp
102 if (!empty($settings['show_date']) && !empty($author_link)):
103
104 // translators: 1: reply author, 2: reply link, 3: reply timestamp
105 printf(esc_html_x('%1$s on %2$s %3$s', 'widgets', 'bbpress'), $author_link, $reply_link, $show_date);
106
107 // Reply link and timestamp
108 elseif (!empty($settings['show_date'])):
109 echo $reply_link . ' ' . $show_date;
110
111 // Reply author and title
112 elseif (!empty($author_link)):
113
114 // translators: 1: reply author, 2: reply link
115 printf(esc_html_x('%1$s on %2$s', 'widgets', 'bbpress'), $author_link, $reply_link);
116
117 // Only the reply title
118 else:
119 echo $reply_link;
120 endif;
121 ?>
122
123 </li>
124
125 <?php endwhile; ?>
126
127 </ul>
128
129 <?php echo $args['after_widget'];
130
131 // Reset the $post global
132 wp_reset_postdata();
133 }
134
135 /**
136 * Update the reply widget options
137 *
138 * @param array $new_instance The new instance options
139 * @param array $old_instance The old instance options
140 * @return array Updated instance
141 */
142 public function update($new_instance = array(), $old_instance = array())
143 {
144 $instance = $old_instance;
145 $instance['title'] = strip_tags($new_instance['title']);
146 $instance['max_shown'] = (int) $new_instance['max_shown'];
147
148 // Date
149 $instance['show_date'] = isset($new_instance['show_date'])
150 ? (bool) $new_instance['show_date']
151 : false;
152
153 // Author
154 $instance['show_user'] = isset($new_instance['show_user'])
155 ? (bool) $new_instance['show_user']
156 : false;
157
158 return $instance;
159 }
160
161 /**
162 * Output the reply widget options form
163 *
164 * @param array $instance Instance
165 */
166 public function form($instance = array())
167 {
168
169 // Get widget settings
170 $settings = $this->parse_settings($instance); ?>
171
172 <p>
173 <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title:', 'forumax'); ?>
174 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
175 name="<?php echo $this->get_field_name('title'); ?>" type="text"
176 value="<?php echo esc_attr($settings['title']); ?>" />
177 </label>
178 </p>
179
180 <p>
181 <label
182 for="<?php echo $this->get_field_id('max_shown'); ?>"><?php esc_html_e('Maximum replies to show:', 'forumax'); ?>
183 <input class="widefat" id="<?php echo $this->get_field_id('max_shown'); ?>"
184 name="<?php echo $this->get_field_name('max_shown'); ?>" type="text"
185 value="<?php echo esc_attr($settings['max_shown']); ?>" />
186 </label>
187 </p>
188
189 <p>
190 <label for="<?php echo $this->get_field_id('show_date'); ?>">
191 <input type="checkbox" id="<?php echo $this->get_field_id('show_date'); ?>"
192 name="<?php echo $this->get_field_name('show_date'); ?>" <?php checked(true, $settings['show_date']); ?>
193 value="1" />
194 <?php esc_html_e('Show post date', 'forumax'); ?>
195 </label>
196 </p>
197
198 <p>
199 <label for="<?php echo $this->get_field_id('show_user'); ?>">
200 <input type="checkbox" id="<?php echo $this->get_field_id('show_user'); ?>"
201 name="<?php echo $this->get_field_name('show_user'); ?>" <?php checked(true, $settings['show_user']); ?>
202 value="1" />
203 <?php esc_html_e('Show reply author', 'forumax'); ?>
204 </label>
205 </p>
206
207 <?php
208 }
209
210 /**
211 * Merge the widget settings into defaults array.
212 *
213 * @param array $instance Instance
214 * @return array Parsed settings
215 */
216 public function parse_settings($instance = array())
217 {
218 return bbp_parse_args($instance, array(
219 'title' => esc_html__('Recent Replies', 'forumax'),
220 'max_shown' => 5,
221 'show_date' => true,
222 'show_user' => true
223 ), 'forumax_replies_widget_settings');
224 }
225 }
226