PluginProbe
wpForo Forum / 2.2.9
wpForo Forum v2.2.9
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / widgets / RecentTopics.php

RecentTopics.php in wpForo Forum 2.2.9, at widgets/RecentTopics.php

270 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\widgets;
4
5 use WP_Widget;
6
7 class RecentTopics extends WP_Widget {
8 private $default_instance = [];
9 private $orderby_fields = [];
10 private $order_fields = [];
11
12 function __construct() {
13 parent::__construct( 'wpforo_recent_topics', 'wpForo Recent Topics', [ 'description' => 'Your forum\'s recent topics.' ] );
14 $this->init_local_vars();
15 add_action( 'wp_ajax_wpforo_load_ajax_widget_RecentTopics', [ $this, 'load_ajax_widget' ] );
16 add_action( 'wp_ajax_nopriv_wpforo_load_ajax_widget_RecentTopics', [ $this, 'load_ajax_widget' ] );
17 if( is_admin() ) {
18 add_action( 'wp_ajax_wpforo_get_forum_tree', [ $this, 'get_forum_tree' ] );
19 }
20 }
21
22 private function init_local_vars() {
23 $this->default_instance = [
24 'boardid' => 0,
25 'title' => 'Recent Topics',
26 'forumids' => [],
27 'orderby' => 'created',
28 'order' => 'DESC',
29 'count' => 9,
30 'display_avatar' => true,
31 'forumids_filter' => false,
32 'current_forumid_filter' => false,
33 'goto_unread' => false,
34 'refresh_interval' => 0,
35 ];
36 $this->orderby_fields = [
37 'created' => __( 'Created Date', 'wpforo' ),
38 'modified' => __( 'Modified Date', 'wpforo' ),
39 'posts' => __( 'Posts Count', 'wpforo' ),
40 'views' => __( 'Views Count', 'wpforo' ),
41 ];
42 $this->order_fields = [
43 'DESC' => __( 'DESC', 'wpforo' ),
44 'ASC' => __( 'ASC', 'wpforo' ),
45 ];
46 }
47
48 public function get_widget( $instance, $topic_args ) {
49 $is_user_logged_in = (bool) WPF()->current_userid;
50 $topic_args['private'] = ( ! $is_user_logged_in || ! WPF()->usergroup->can( 'aum' ) ) ? 0 : null;;
51 $topic_args['status'] = ( ! $is_user_logged_in || ! WPF()->usergroup->can( 'aum' ) ) ? 0 : null;
52
53 $row_count = (int) wpfval( $topic_args, 'row_count' );
54 $topics = [];
55 $topic_args['offset'] = 0;
56 while( $row_count && count( $topics ) < $row_count ){
57 if( ! ( $_topics = WPF()->topic->get_topics( $topic_args ) ) ) break;
58
59 $topics = array_merge( $topics, $_topics );
60 $topic_args['offset'] += $row_count;
61 }
62 array_splice( $topics, $row_count );
63
64 $print_avatar = $instance['display_avatar'] && wpforo_setting( 'profiles', 'avatars' ) && WPF()->usergroup->can( 'va' );
65
66 $lis = '';
67 foreach( $topics as $topic ) {
68 $topic_url = wpforo_topic( $topic['topicid'], 'url' );
69 $member = wpforo_member( $topic );
70
71 $lis .= sprintf(
72 '<li>
73 <div class="wpforo-list-item">
74 %1$s
75 <div class="wpforo-list-item-right" %2$s>
76 <p class="posttitle">%3$s</p>
77 <p class="postuser">
78 %4$s %5$s <span style="white-space: nowrap;">%6$s</span>
79 </p>
80 </div>
81 <div class="wpf-clear"></div>
82 </div>
83 </li>',
84 ( $print_avatar ? sprintf( '<div class="wpforo-list-item-left">%1$s</div>', wpforo_user_avatar( $member ) ) : '' ),
85 ( !$print_avatar ? 'style="width: 100%"' : '' ),
86 ( wpfval( $instance, 'goto_unread' ) ?
87 wpforo_topic_title( $topic, $topic_url, '{p}{au}{t}{/a}', false ) .
88 ( $topic['topicid'] != wpfval( WPF()->current_object, 'topicid' ) ? wpforo_unread_button( $topic['topicid'], $topic_url, false ) : '' )
89 :
90 wpforo_topic_title( $topic, $topic_url, '{p}{a}{t}{/a}', false )
91 ),
92 wpforo_phrase( 'by', false ),
93 wpforo_member_link( $member, '', 30, '', false ),
94 esc_html( wpforo_date( $topic['created'], 'ago', false ) )
95 );
96 }
97
98 return sprintf( '<ul>%1$s</ul>', $lis );
99 }
100
101 public function load_ajax_widget() {
102 $_POST = wp_unslash( $_POST );
103 $instance = json_decode( (string) wpfval( $_POST, 'instance' ), true );
104 $topic_args = json_decode( (string) wpfval( $_POST, 'topic_args' ), true );
105 wp_send_json_success( [ 'html' => $this->get_widget( $instance, $topic_args ) ] );
106 }
107
108 public function widget( $args, $instance ) {
109 wp_enqueue_script( 'wpforo-widgets-js' );
110 $instance = wpforo_parse_args( $instance, $this->default_instance );
111 if( $instance['current_forumid_filter'] && $instance['boardid'] === WPF()->board->get_current( 'boardid' ) && $current_forumid = wpfval( WPF()->current_object, 'forumid' ) ) $instance['forumids'] = (array) $current_forumid;
112 $data = [
113 'boardid' => $instance['boardid'],
114 'action' => 'wpforo_load_ajax_widget_RecentTopics',
115 'instance' => $instance,
116 'topic_args' => [
117 'forumids' => ( $instance['forumids'] ?: $this->default_instance['forumids'] ),
118 'orderby' => ( key_exists( $instance['orderby'], $this->orderby_fields ) ? $instance['orderby'] : $this->default_instance['orderby'] ),
119 'order' => ( key_exists( $instance['order'], $this->order_fields ) ? $instance['order'] : $this->default_instance['order'] ),
120 'row_count' => ( ( $count = intval( $instance['count'] ) ) ? $count : $this->default_instance['count'] ),
121 ],
122 ];
123 if( WPF()->board->get_current( 'boardid' ) === $instance['boardid'] ) {
124 $html = $this->get_widget( $data['instance'], $data['topic_args'] );
125 $onload = false;
126 }else{
127 $html = '<div style="text-align: center; font-size: 20px;"><i class="fas fa-spinner fa-spin"></i></div>';
128 $onload = true;
129 $data['referer'] = home_url();
130 }
131 $json = json_encode( $data );
132 echo $args['before_widget'] . '<div id="wpf-widget-recent-replies" class="wpforo-widget-wrap">';
133 if( ! empty( $instance['title'] ) ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
134 echo '<div class="wpforo-widget-content wpforo-ajax-widget ' . ( ! $onload ? 'wpforo-ajax-widget-onload-false' : '' ) . '" data-json="' . esc_attr( $json ) . '">' . $html . '</div></div>' . $args['after_widget'];
135 }
136
137 public function form( $instance ) {
138 $instance = wpforo_parse_args( $instance, $this->default_instance );
139 $title = (string) $instance['title'];
140 $boardid = (int) $instance['boardid'];
141 $selected = array_unique( array_filter( array_map( 'intval', (array) $instance['forumids'] ) ) );
142 $orderby = (string) $instance['orderby'];
143 $order = (string) $instance['order'];
144 $count = (int) $instance['count'];
145 $display_avatar = (bool) $instance['display_avatar'];
146 $forumids_filter = (bool) $instance['forumids_filter'];
147 $current_forumid_filter = (bool) $instance['current_forumid_filter'];
148 $goto_unread = (bool) $instance['goto_unread'];
149 $refresh_interval = (int) $instance['refresh_interval'];
150 WPF()->change_board( $boardid );
151 ?>
152 <style>
153 .wpf-wdg-wrapper .wpf_wdg_forumids_wrap {
154 display: none !important;
155 }
156
157 .wpf-wdg-wrapper input.wpf_wdg_forumids_filter_1:checked ~ .wpf_wdg_forumids_wrap {
158 display: block !important;
159 }
160 </style>
161 <div class="wpf-wdg-wrapper">
162 <p>
163 <label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title', 'wpforo' ); ?>:</label>
164 <input id="<?php echo $this->get_field_id( 'title' ) ?>" class="widefat"
165 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
166 value="<?php echo esc_attr( $title ); ?>">
167 </p>
168 <p>
169 <label for="<?php echo $this->get_field_id( 'boardid' ) ?>"></label>
170 <select id="<?php echo $this->get_field_id( 'boardid' ) ?>" class="wpf_wdg_boardid" name="<?php echo esc_attr( $this->get_field_name( 'boardid' ) ); ?>">
171 <?php echo WPF()->board->dropdown( $boardid ) ?>
172 </select>
173 </p>
174 <div>
175 <span><?php _e( 'Filter by forums', 'wpforo' ); ?> :</span>
176
177 <label for="<?php echo $this->get_field_id( 'forumids_filter' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label>
178 <input id="<?php echo $this->get_field_id( 'forumids_filter' ) ?>_1" value="1" class="wpf_wdg_forumids_filter_1" name="<?php echo esc_attr( $this->get_field_name( 'forumids_filter' ) ); ?>" <?php checked( $forumids_filter ); ?> type="radio">
179
180 <label for="<?php echo $this->get_field_id( 'forumids_filter' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label>
181 <input id="<?php echo $this->get_field_id( 'forumids_filter' ) ?>_0" value="0" class="wpf_wdg_forumids_filter_0" name="<?php echo esc_attr( $this->get_field_name( 'forumids_filter' ) ); ?>" <?php checked( $forumids_filter, false ); ?> type="radio">
182
183 <div class="wpf_wdg_forumids_wrap">
184 <label for="<?php echo $this->get_field_id( 'forumids' ) ?>"></label>
185 <select id="<?php echo $this->get_field_id( 'forumids' ) ?>" class="wpf_wdg_forumids" name="<?php echo esc_attr( $this->get_field_name( 'forumids' ) ); ?>[]" multiple>
186 <?php WPF()->forum->tree( 'select_box', false, $selected ) ?>
187 </select>
188 </div>
189 </div>
190 <p>
191 <span><?php _e( 'Autofilter by current forum', 'wpforo' ); ?> : </span>
192
193 <label for="<?php echo $this->get_field_id( 'current_forumid_filter' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label>
194 <input id="<?php echo $this->get_field_id( 'current_forumid_filter' ) ?>_1" name="<?php echo esc_attr( $this->get_field_name( 'current_forumid_filter' ) ); ?>" value="1" <?php checked( $current_forumid_filter ); ?> type="radio">
195
196 <label for="<?php echo $this->get_field_id( 'current_forumid_filter' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label>
197 <input id="<?php echo $this->get_field_id( 'current_forumid_filter' ) ?>_0" name="<?php echo esc_attr( $this->get_field_name( 'current_forumid_filter' ) ); ?>" value="0" <?php checked( $current_forumid_filter, false ); ?> type="radio">
198 </p>
199 <p>
200 <label for="<?php echo $this->get_field_id( 'orderby' ) ?>"><?php _e( 'Order by', 'wpforo' ); ?>:</label>
201 <select name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>"
202 id="<?php echo $this->get_field_id( 'orderby' ) ?>">
203 <?php foreach( $this->orderby_fields as $orderby_key => $orderby_field ) : ?>
204 <option value="<?php echo $orderby_key; ?>"<?php echo( $orderby_key == $orderby ? ' selected' : '' ); ?>><?php echo $orderby_field; ?></option>
205 <?php endforeach; ?>
206 </select>
207 <label>
208 <select name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
209 <?php foreach( $this->order_fields as $order_key => $order_field ) : ?>
210 <option value="<?php echo $order_key; ?>"<?php echo( $order_key == $order ? ' selected' : '' ); ?>><?php echo $order_field; ?></option>
211 <?php endforeach; ?>
212 </select>
213 </label>
214 </p>
215 <p>
216 <label for="<?php echo $this->get_field_id( 'count' ) ?>"><?php _e( 'Number of Items', 'wpforo' ); ?></label>&nbsp;
217 <input id="<?php echo $this->get_field_id( 'count' ) ?>" type="number" min="1" style="width: 53px;" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" value="<?php echo esc_attr( $count ); ?>">
218 </p>
219 <p>
220 <span><?php _e( 'Display with avatars', 'wpforo' ); ?> : </span>
221
222 <label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label>
223 <input id="<?php echo $this->get_field_id( 'display_avatar' ) ?>_1" value="1" <?php checked( $display_avatar ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'display_avatar' ) ); ?>">
224
225 <label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label>
226 <input id="<?php echo $this->get_field_id( 'display_avatar' ) ?>_0" value="0" <?php checked( $display_avatar, false ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'display_avatar' ) ); ?>">
227 </p>
228 <p>
229 <span><?php _e( 'Refer topics to first unread post', 'wpforo' ); ?> : </span>
230
231 <label for="<?php echo $this->get_field_id( 'goto_unread' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label>
232 <input id="<?php echo $this->get_field_id( 'goto_unread' ) ?>_1" <?php checked( $goto_unread ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'goto_unread' ) ); ?>">
233
234 <label for="<?php echo $this->get_field_id( 'goto_unread' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label>
235 <input id="<?php echo $this->get_field_id( 'goto_unread' ) ?>_0" <?php checked( $goto_unread, false ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'goto_unread' ) ); ?>">
236 </p>
237 <p>
238 <label for="<?php echo $this->get_field_id( 'refresh_interval' ) ?>"><?php _e( 'Auto Refresh Interval Seconds', 'wpforo' ); ?></label>&nbsp;
239 <input id="<?php echo $this->get_field_id( 'refresh_interval' ) ?>" type="number" min="0" style="display: inline-block; width: 53px;" name="<?php echo esc_attr( $this->get_field_name( 'refresh_interval' ) ); ?>" value="<?php echo esc_attr( $refresh_interval ); ?>">
240 <span style="color: #ccc"><?php _e( 'Set 0 to disable autorefresh', 'wpforo' ) ?></span>
241 </p>
242 </div>
243 <?php
244 }
245
246 public function update( $new_instance, $old_instance ) {
247 $new_instance = wpforo_parse_args( $new_instance, $this->default_instance );
248 $instance = [];
249 $instance['title'] = strip_tags( (string) $new_instance['title'] );
250 $instance['boardid'] = (int) $new_instance['boardid'];
251 $instance['forumids_filter'] = (bool) (int) $new_instance['forumids_filter'];
252 $instance['forumids'] = array_unique( array_filter( array_map( 'intval', (array) $new_instance['forumids'] ) ) );
253 $instance['orderby'] = ( ! empty( $new_instance['orderby'] ) && key_exists( $new_instance['orderby'], $this->orderby_fields ) ) ? $new_instance['orderby'] : $this->default_instance['orderby'];
254 $instance['order'] = ( ! empty( $new_instance['order'] ) && key_exists( $new_instance['order'], $this->order_fields ) ) ? $new_instance['order'] : $this->default_instance['order'];
255 $instance['count'] = (int) $new_instance['count'];
256 $instance['display_avatar'] = (bool) (int) $new_instance['display_avatar'];
257 $instance['current_forumid_filter'] = (bool) (int) $new_instance['current_forumid_filter'];
258 $instance['goto_unread'] = (bool) (int) $new_instance['goto_unread'];
259 $instance['refresh_interval'] = (int) $new_instance['refresh_interval'];
260
261 return $instance;
262 }
263
264 public function get_forum_tree( ) {
265 ob_start();
266 WPF()->forum->tree( 'select_box', false, [] );
267 wp_send_json_success( [ 'html' => ob_get_clean() ] );
268 }
269 }
270