PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 1.3.1
Forumax – AI Powered Advanced Community Forum Plugin v1.3.1
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 / includes / features / bbp_solved_topic.php

bbp_solved_topic.php in Forumax – AI Powered Advanced Community Forum Plugin 1.3.1, at includes/features/bbp_solved_topic.php

419 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace features;
3
4 class bbp_solved_topic {
5
6 /**
7 * The capability required to view private posts.
8 *
9 * @since 1.0
10 *
11 * @var string $capability
12 */
13 public $capability = 'moderate';
14
15 /*
16 --------------------------------------------*
17 * Constructor
18 *--------------------------------------------*/
19
20 /**
21 * Initializes the plugin by setting filters, and administration functions.
22 */
23 function __construct() {
24 add_action( 'plugins_loaded', [ $this, 'filter_capability' ] );
25
26 // show the checkboxes
27 add_action( 'bbp_theme_before_reply_form_submit_wrapper', [ $this, 'reply_checkbox' ] );
28 add_action( 'bbp_theme_before_topic_form_submit_wrapper', [ $this, 'topic_checkbox' ] );
29
30 // save the reply state
31 add_action( 'bbp_new_reply', [ $this, 'update_reply' ], 0, 6 );
32 add_action( 'bbp_edit_reply', [ $this, 'update_reply' ], 0, 6 );
33
34 // save the topic state
35 add_action( 'bbp_new_topic', [ $this, 'update_topic' ], 0, 6 );
36 add_action( 'bbp_edit_topic', [ $this, 'update_topic' ], 0, 6 );
37
38 // reply content filter
39 add_filter( 'bbp_get_reply_excerpt', [ $this, 'reply_content_filter' ], 999, 2 );
40 add_filter( 'bbp_get_reply_content', [ $this, 'reply_content_filter' ], 999, 2 );
41
42 add_filter( 'bbp_theme_before_topic_title', [ $this, 'solved_badge' ], 999, 2 );
43 add_filter( 'bbp_topic_admin_links', [ $this, 'add_topic_admin_link' ], 10, 2 );
44
45 // add a class name indicating the read status
46 add_filter( 'post_class', [ $this, 'reply_post_class' ] );
47 }
48
49 /**
50 * Admin links
51 */
52 public function add_topic_admin_link( $links, $topic_id ) {
53
54 $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() );
55 if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) {
56 if ( isset( $_GET['unsolve_topic'] ) ) {
57 $this->unsolve_topic( bbp_get_topic_id() );
58 $links['solved'] = '<a href="' . bbp_get_topic_permalink() . '?solve_topic">' . esc_html__( 'Mark as solved', 'ama-core' ) . '</a>';
59 } elseif ( isset( $_GET['solve_topic'] ) ) {
60 $this->solve_topic( bbp_get_topic_id() );
61 $links['unsolved'] = '<a href="' . bbp_get_topic_permalink() . '?unsolve_topic">' . esc_html__( 'Mark as unsolved', 'ama-core' ) . '</a>';
62 } else {
63 if ( $this->is_solved( bbp_get_topic_id() ) ) {
64 $links['unsolved'] = '<a href="' . bbp_get_topic_permalink() . '?unsolve_topic">' . esc_html__( 'Mark as unsolved', 'ama-core' ) . '</a>';
65 } else {
66 $links['solved'] = '<a href="' . bbp_get_topic_permalink() . '?solve_topic">' . esc_html__( 'Mark as solved', 'ama-core' ) . '</a>';
67 }
68 }
69 }
70 return $links;
71 }
72
73 public function solve_topic( $topic_id = 0 ) {
74 $topic_author = bbp_get_topic_author_id( $topic_id );
75 if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) {
76 update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' );
77 $redirect = remove_query_arg( [ 'solve_topic' ] );
78 wp_safe_redirect( $redirect );
79 } else {
80 wp_die( esc_html__( 'You do not have the permission to do that!', 'ama-core' ) );
81 }
82
83 }
84
85 public function unsolve_topic( $topic_id = 0 ) {
86 $topic_author = bbp_get_topic_author_id( $topic_id );
87 if ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) {
88 delete_post_meta( $topic_id, '_bbp_topic_is_solved' );
89 $redirect = remove_query_arg( [ 'unsolve_topic' ] );
90 wp_safe_redirect( $redirect );
91 } else {
92 wp_die( esc_html__( 'You do not have the permission to do that!', 'ama-core' ) );
93 }
94 }
95
96 /**
97 * Called during the plugins_loaded action to filter the capability
98 * required to view solved topics.
99 *
100 * @since 1.0
101 *
102 * @return void
103 */
104 public function filter_capability() {
105 $this->capability = apply_filters( 'bbp_solved_topic_capability', $this->capability );
106 }
107
108 /**
109 * Solved topic badge
110 *
111 * @since 1.0
112 *
113 * @return void
114 */
115 public function solved_badge() {
116 $topic_id = bbp_get_topic_id();
117 if ( $this->is_solved( $topic_id ) ) {
118 echo '<i class="fa fa-check-circle text-success"></i>';
119 }
120 }
121
122 /**
123 * Outputs the reply checkbox
124 *
125 * @since 1.0
126 *
127 * @return void
128 */
129 public function reply_checkbox() {
130 $has_best_answer = $this->has_best_answer( bbp_get_topic_id() );
131 $is_best_answer = $this->is_best_answer( bbp_get_reply_id() );
132 if ( bbp_is_reply_edit() && ( current_user_can( $this->capability ) ) ) {
133 if ( ( ! $has_best_answer ) || ( $is_best_answer ) ) {
134 ?>
135 <p>
136 <input name="bbp_best_answer" id="bbp_best_answer" type="checkbox"<?php checked( '1', $is_best_answer ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" />
137 <label for="bbp_best_answer"><?php esc_html_e( 'Set this reply as Best Answer.', 'ama-core' ); ?></label>
138 </p>
139 <?php
140 }
141 }
142 }
143
144 /**
145 * Outputs the topic checkbox
146 *
147 * @since 1.0
148 *
149 * @return void
150 */
151 public function topic_checkbox() {
152 $topic_author = bbp_get_topic_author_id( bbp_get_topic_id() );
153 if ( bbp_is_topic_edit() && ( ( current_user_can( $this->capability ) ) || ( $topic_author == get_current_user_id() ) ) ) {
154 ?>
155 <p>
156 <input name="bbp_solved_topic" id="bbp_solved_topic" type="checkbox"<?php checked( '1', $this->is_solved( bbp_get_topic_id() ) ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" />
157 <label for="bbp_solved_topic"><?php esc_html_e( 'Set this post as solved.', 'ama-core' ); ?></label>
158 </p>
159 <?php
160 }
161
162 }
163
164 /**
165 * Stores the private state on reply creation and edit
166 *
167 * @since 1.0
168 *
169 * @param $reply_id int The ID of the reply
170 * @param $topic_id int The ID of the topic the reply belongs to
171 * @param $forum_id int The ID of the forum the topic belongs to
172 * @param $anonymous_data bool Are we posting as an anonymous user?
173 * @param $author_id int The ID of user creating the reply, or the ID of the replie's author during edit
174 * @param $is_edit bool Are we editing a reply?
175 *
176 * @return void
177 */
178 public function update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
179
180 if ( isset( $_POST['bbp_best_answer'] ) ) {
181 $get_topic_meta = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true );
182 if ( ! $get_topic_meta ) {
183 update_post_meta( $topic_id, '_bbp_topic_has_best_answer', '1' );
184 }
185 update_post_meta( $reply_id, '_bbp_reply_is_best_answer', '1' );
186 } else {
187 delete_post_meta( $reply_id, '_bbp_reply_is_best_answer' );
188 delete_post_meta( $topic_id, '_bbp_topic_has_best_answer' );
189 }
190
191 }
192
193 /**
194 * Stores the private state on topic creation and edit
195 *
196 * @since 1.0
197 *
198 * @param $topic_id int The ID of the topic
199 * @param $is_edit bool Are we editing a topic?
200 *
201 * @return void
202 */
203 public function update_topic( $topic_id = 0, $is_edit = false ) {
204
205 if ( isset( $_POST['bbp_solved_topic'] ) ) {
206 update_post_meta( $topic_id, '_bbp_topic_is_solved', '1' );
207 } else {
208 delete_post_meta( $topic_id, '_bbp_topic_is_solved' );
209 }
210
211 }
212
213
214 /**
215 * Determines if a reply is marked as solved
216 *
217 * @since 1.0
218 *
219 * @param $reply_id int The ID of the reply
220 *
221 * @return bool
222 */
223 public function is_best_answer( $reply_id = 0 ) {
224
225 $retval = false;
226
227 // Checking a specific reply id
228 if ( ! empty( $reply_id ) ) {
229 $reply = bbp_get_reply( $reply_id );
230 $reply_id = ! empty( $reply ) ? $reply->ID : 0;
231
232 // Using the global reply id
233 } elseif ( bbp_get_reply_id() ) {
234 $reply_id = bbp_get_reply_id();
235
236 // Use the current post id
237 } elseif ( ! bbp_get_reply_id() ) {
238 $reply_id = get_the_ID();
239 }
240
241 if ( ! empty( $reply_id ) ) {
242 $retval = get_post_meta( $reply_id, '_bbp_reply_is_best_answer', true );
243 }
244
245 return (bool) apply_filters( 'bbp_reply_is_best_answer', (bool) $retval, $reply_id );
246 }
247
248 /**
249 * Determines if a reply is marked as solved
250 *
251 * @since 1.0
252 *
253 * @param $reply_id int The ID of the reply
254 *
255 * @return bool
256 */
257 public function is_solved( $topic_id = 0 ) {
258
259 $retval = false;
260
261 // Checking a specific reply id.
262 if ( ! empty( $topic_id ) ) {
263 $topic = bbp_get_topic( $topic_id );
264 $topic_id = ! empty( $topic ) ? $topic->ID : 0;
265
266 // Using the global reply id.
267 } elseif ( bbp_get_topic_id() ) {
268 $topic_id = bbp_get_topic_id();
269
270 // Use the current post id.
271 } elseif ( ! bbp_get_topic_id() ) {
272 $topic_id = get_the_ID();
273 }
274
275 if ( ! empty( $topic_id ) ) {
276 $retval = get_post_meta( $topic_id, '_bbp_topic_is_solved', true );
277 }
278
279 return (bool) apply_filters( 'bbp_topic_is_solved', (bool) $retval, $topic_id );
280 }
281
282 /**
283 * Determines if a reply is marked as solved
284 *
285 * @since 1.0
286 *
287 * @param $reply_id int The ID of the reply
288 *
289 * @return bool
290 */
291 public function has_best_answer( $topic_id = 0 ) {
292
293 // if bbpress is not active, return the classes
294
295 if ( ! function_exists( 'bbp_get_topic_id' ) ) {
296 return false;
297 }
298
299 $retval = false;
300
301 // Checking a specific reply id.
302 if ( ! empty( $topic_id ) ) {
303 $topic = bbp_get_topic( $topic_id );
304 $topic_id = ! empty( $topic ) ? $topic->ID : 0;
305
306 // Using the global reply id.
307 } elseif ( bbp_get_topic_id() ) {
308 $topic_id = bbp_get_topic_id();
309
310 // Use the current post id.
311 } elseif ( ! bbp_get_topic_id() ) {
312 $topic_id = get_the_ID();
313 }
314
315 if ( ! empty( $topic_id ) ) {
316 $retval = get_post_meta( $topic_id, '_bbp_topic_has_best_answer', true );
317 }
318
319 return (bool) apply_filters( 'bbp_topic_has_best_answer', (bool) $retval, $topic_id );
320 }
321
322
323 /**
324 * Reply content filter
325 *
326 * @since 1.0
327 *
328 * @param $content string The content of the reply
329 * @param $reply_id int The ID of the reply
330 *
331 * @return string
332 */
333 public function reply_content_filter( $content = '', $reply_id = 0 ) {
334
335 if ( empty( $reply_id ) ) {
336 $reply_id = bbp_get_reply_id( $reply_id );
337 }
338
339 $topic_id = bbp_get_topic_id();
340
341 if ( $this->is_best_answer( $reply_id ) ) {
342 $content = '<div class="solved-topic-bar" id="best-answer"><span class="badge"><i class="icon_check"></i> ' . esc_html__( 'Best Answer', 'ama-core' ) . '</span></div>' . $content;
343 }
344
345 return $content;
346 }
347
348 /**
349 * Topic content filter
350 *
351 * @since 1.0
352 *
353 * @param $content string The content of the topic
354 * @param $topic_id int The ID of the reply
355 *
356 * @return string
357 */
358 public function topic_content_filter( $content = '', $topic_id = 0 ) {
359
360 if ( empty( $topic_id ) ) {
361 $topic_id = bbp_get_topic_id( $topic_id );
362 }
363
364 $open = '';
365 $close = '';
366 $resolved = '';
367 $has_best_answer = '';
368
369 if ( $this->is_solved( $topic_id ) || $this->has_best_answer( $topic_id ) ) {
370 $open = '<div class="solved-topic-bar">';
371 $close = '</div>';
372 }
373
374 if ( $this->is_solved( $topic_id ) ) {
375 $resolved = '<span class="accepted-ans-mark"><i class="icon_check_alt"></i> ' . esc_html__( 'Resolved', 'ama-core' ) . '</span>';
376 }
377
378 $content = $content . $open . $resolved . $has_best_answer . $close;
379
380 return $content;
381 }
382
383 /**
384 * Adds a new class to replies that are marked as solved
385 *
386 * @since 1.0
387 *
388 * @param $classes array An array of current class names
389 *
390 * @return bool
391 */
392 public function reply_post_class( $classes ) {
393
394 // if bbpress is not active, return the classes
395 if ( ! function_exists( 'bbp_get_reply_id' ) ) {
396 return $classes;
397 }
398
399 $reply_id = bbp_get_reply_id();
400
401 // Only apply the class to replies.
402 if ( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) {
403 return $classes;
404 }
405
406 if ( $this->is_best_answer( $reply_id ) ) {
407 $classes[] = 'best-answer';
408 }
409
410 return $classes;
411 }
412 }
413
414 // Instantiate the class.
415 $GLOBALS['bbp_solved_topic'] = new bbp_solved_topic();
416
417 function bbp_solved_topic(): bbp_solved_topic {
418 return new bbp_solved_topic();
419 }