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 / includes / ajax_actions.php

ajax_actions.php in Forumax – AI Powered Advanced Community Forum Plugin 2.5.1, at includes/ajax_actions.php

654 lines 23.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ajax actions for Forumax
4 *
5 * @package Forumax
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * All Search results
12 */
13 add_action( 'wp_ajax_forumax_search_data_fetch', 'forumax_search_data_fetch' );
14 add_action( 'wp_ajax_nopriv_forumax_search_data_fetch', 'forumax_search_data_fetch' );
15
16 /**
17 * Build the ancestor "breadcrumb" trail for a search result, mirroring the
18 * EazyDocs search UI (e.g. Forum › Topic for a reply).
19 *
20 * @param int $post_id Post ID.
21 * @param string $type Post type of the result.
22 * @return array List of ancestor titles, ordered from top-most down.
23 */
24 function forumax_search_result_breadcrumb( $post_id, $type ) {
25 $crumbs = array();
26
27 if ( 'reply' === $type && function_exists( 'bbp_get_reply_forum_id' ) ) {
28 $forum_id = bbp_get_reply_forum_id( $post_id );
29 $topic_id = bbp_get_reply_topic_id( $post_id );
30 if ( $forum_id ) {
31 $crumbs[] = get_the_title( $forum_id );
32 }
33 if ( $topic_id ) {
34 $crumbs[] = get_the_title( $topic_id );
35 }
36 } elseif ( 'topic' === $type && function_exists( 'bbp_get_topic_forum_id' ) ) {
37 $forum_id = bbp_get_topic_forum_id( $post_id );
38 if ( $forum_id ) {
39 $crumbs[] = get_the_title( $forum_id );
40 }
41 } else {
42 // Generic hierarchy fallback (forums with parents, docs, pages, etc.).
43 foreach ( array_reverse( get_post_ancestors( $post_id ) ) as $ancestor_id ) {
44 $crumbs[] = get_the_title( $ancestor_id );
45 }
46 }
47
48 return array_values( array_filter( $crumbs ) );
49 }
50
51 /**
52 * Fetch search data.
53 */
54 function forumax_search_data_fetch() {
55
56 // Check nonce.
57 check_ajax_referer( 'forumax-nonce', 'nonce' );
58
59 // Sanitize keyword.
60 $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
61 $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : '';
62
63 // Determine which post types to query. An explicit single post type narrows
64 // the search; 'all' (or no value) searches the whole forum: forums, topics
65 // and replies, so content matches from any of them are returned.
66 // Only public post types may be searched; anything else falls back to the
67 // forum-wide search instead of exposing non-public content.
68 $post_type_object = ( ! empty( $post_type ) && 'all' !== $post_type ) ? get_post_type_object( $post_type ) : null;
69
70 if ( $post_type_object && $post_type_object->public ) {
71 $query_post_types = array( $post_type );
72 } elseif ( class_exists( 'bbPress' ) ) {
73 $query_post_types = array( 'forum', 'topic', 'reply' );
74 } else {
75 $query_post_types = array( 'post' );
76 }
77
78 // Determine which statuses to include. bbPress stores closed topics with the
79 // 'closed' post status, which WP_Query's default 'publish'-only behavior skips.
80 // Private/hidden forums are only searchable by users allowed to read them.
81 if ( class_exists( 'bbPress' ) ) {
82 $query_post_status = array( 'publish', 'closed' );
83
84 if ( current_user_can( 'read_private_forums' ) ) {
85 $query_post_status[] = 'private';
86 }
87
88 if ( current_user_can( 'read_hidden_forums' ) ) {
89 $query_post_status[] = 'hidden';
90 }
91 } else {
92 $query_post_status = array( 'publish' );
93 }
94
95 // Force the search to match BOTH the title and the content. This overrides
96 // any other plugin/theme `posts_search` filter that may otherwise restrict
97 // matching to titles only, so content keywords return results too.
98 $bbpc_search_clause = static function ( $search, $wp_query ) use ( $keyword ) {
99 global $wpdb;
100
101 if ( '' === $keyword || ! $wp_query->get( 'bbpc_ajax_search' ) ) {
102 return $search;
103 }
104
105 $like = '%' . $wpdb->esc_like( $keyword ) . '%';
106
107 return $wpdb->prepare(
108 " AND ( {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_content LIKE %s ) ",
109 $like,
110 $like
111 );
112 };
113
114 add_filter( 'posts_search', $bbpc_search_clause, 999, 2 );
115
116 // Main results query.
117 $results = new \WP_Query( array(
118 'post_type' => $query_post_types,
119 'post_status' => $query_post_status,
120 's' => $keyword,
121 'posts_per_page' => 10,
122 'bbpc_ajax_search' => true,
123 'update_post_term_cache' => false,
124 ) );
125
126 remove_filter( 'posts_search', $bbpc_search_clause, 999 );
127
128 $results_count = $results->found_posts;
129
130 if ( $results_count > 0 ) {
131 $nsoresult = 'data-result=';
132 } else {
133 $nsoresult = 'data-noresult=';
134 }
135
136 ?>
137 <div class="tab-item active all-active" <?php echo esc_attr( $nsoresult . 'No-Results-Found' ); ?>></div>
138 <?php
139
140 echo '<div class="bbpc-search-results-wrapper">';
141 echo '<div class="search-results-tab forum show" id="search-forum-results">';
142
143 if ( $results->have_posts() ) :
144 // Group results by post type.
145 $grouped = array();
146 while ( $results->have_posts() ) :
147 $results->the_post();
148 $type = get_post_type();
149 if ( ! isset( $grouped[ $type ] ) ) {
150 $grouped[ $type ] = array();
151 }
152 $grouped[ $type ][] = array(
153 'id' => get_the_ID(),
154 'title' => get_the_title(),
155 'url' => get_the_permalink(),
156 'breadcrumb' => forumax_search_result_breadcrumb( get_the_ID(), $type ),
157 );
158 endwhile;
159 wp_reset_postdata();
160
161 // Render grouped results.
162 $type_labels = array(
163 'forum' => __( 'Forums', 'forumax' ),
164 'topic' => __( 'Topics', 'forumax' ),
165 'reply' => __( 'Replies', 'forumax' ),
166 'docs' => __( 'Documentation', 'forumax' ),
167 'post' => __( 'Posts', 'forumax' ),
168 );
169
170 // Document icon shared by every result row.
171 $result_icon = '<svg class="bbpc-result-icon" width="16" height="16" viewBox="0 0 17 17" fill="currentColor" role="img" aria-hidden="true"><path d="M14.72,0H2.28A2.28,2.28,0,0,0,0,2.28V14.72A2.28,2.28,0,0,0,2.28,17H14.72A2.28,2.28,0,0,0,17,14.72V2.28A2.28,2.28,0,0,0,14.72,0ZM2.28,1H14.72A1.28,1.28,0,0,1,16,2.28V5.33H1V2.28A1.28,1.28,0,0,1,2.28,1ZM1,14.72V6.33H5.33V16H2.28A1.28,1.28,0,0,1,1,14.72ZM14.72,16H6.33V6.33H16v8.39A1.28,1.28,0,0,1,14.72,16Z"></path></svg>';
172
173 // Result type tabs (All / Forums / Topics / Replies), mirroring EazyDocs.
174 ?>
175 <div class="bbpc-result-tabs">
176 <button type="button" class="bbpc-tab active" data-tab="all"><?php esc_html_e( 'All', 'forumax' ); ?></button>
177 <?php foreach ( $grouped as $type => $posts ) :
178 $tab_label = isset( $type_labels[ $type ] ) ? $type_labels[ $type ] : ucfirst( $type );
179 ?>
180 <button type="button" class="bbpc-tab" data-tab="<?php echo esc_attr( $type ); ?>"><?php echo esc_html( $tab_label ); ?></button>
181 <?php endforeach; ?>
182 </div>
183 <?php
184
185 foreach ( $grouped as $type => $posts ) :
186 $label = isset( $type_labels[ $type ] ) ? $type_labels[ $type ] : ucfirst( $type );
187 ?>
188 <div class="bbpc-result-group" data-type="<?php echo esc_attr( $type ); ?>">
189 <div class="bbpc-result-group-label"><?php echo esc_html( $label ); ?></div>
190 <?php foreach ( $posts as $post_item ) : ?>
191 <div class="search-result-item" data-url="<?php echo esc_url( $post_item['url'] ); ?>" onclick="document.location='<?php echo esc_url( $post_item['url'] ); ?>'">
192 <a href="<?php echo esc_url( $post_item['url'] ); ?>" class="bbpc-result-link">
193 <?php echo $result_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup. ?>
194 <span class="bbpc-result-body">
195 <span class="bbpc-result-title"><?php echo esc_html( $post_item['title'] ); ?></span>
196 <span class="bbpc-result-type"><?php echo esc_html( $label ); ?></span>
197 </span>
198 </a>
199 <?php if ( ! empty( $post_item['breadcrumb'] ) ) : ?>
200 <ol class="bbpc-result-breadcrumb">
201 <?php foreach ( $post_item['breadcrumb'] as $crumb ) : ?>
202 <li class="bbpc-breadcrumb-item"><?php echo esc_html( $crumb ); ?></li>
203 <?php endforeach; ?>
204 </ol>
205 <?php endif; ?>
206 </div>
207 <?php endforeach; ?>
208 </div>
209 <?php
210 endforeach;
211 endif;
212
213 echo '</div>';
214 echo '</div>';
215 wp_die();
216 }
217
218 /**
219 * AJAX handler to fetch forums for Gutenberg block editor
220 * Used by the Single Forum block to populate the forum selector dropdown
221 */
222 add_action( 'wp_ajax_bbpc_get_forums', 'forumax_get_forums_ajax' );
223
224 /**
225 * Get forums ajax.
226 */
227 function forumax_get_forums_ajax() {
228 // Verify nonce for security
229 check_ajax_referer( 'forumax-nonce', 'nonce' );
230
231 if ( ! current_user_can( 'edit_posts' ) ) {
232 wp_send_json_error( 'Unauthorized' );
233 }
234
235 // Get all forums
236 $forums = new \WP_Query( [
237 'post_type' => 'forum',
238 'posts_per_page' => -1,
239 'orderby' => 'title',
240 'order' => 'ASC',
241 ] );
242
243 $forum_list = [];
244
245 if ( $forums->have_posts() ) {
246 while ( $forums->have_posts() ) {
247 $forums->the_post();
248 $forum_list[] = [
249 'id' => get_the_ID(),
250 'title' => get_the_title(),
251 ];
252 }
253 wp_reset_postdata();
254 }
255
256 wp_send_json_success( $forum_list );
257 }
258
259
260 /**
261 * Loading Post
262 */
263 add_action( 'wp_ajax_forumax_loading_post', 'forumax_loading_post' );
264 add_action( 'wp_ajax_nopriv_forumax_loading_post', 'forumax_loading_post' );
265
266 /**
267 * Loading forum posts
268 *
269 * @return void
270 */
271 function forumax_loading_post() {
272 // Check nonce
273 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'forumax-nonce' ) ) {
274 wp_die( '-1' );
275 }
276
277 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
278 $post_in = isset( $_POST['a_t_id'] ) ? sanitize_text_field( wp_unslash( $_POST['a_t_id'] ) ) : '';
279 $count = isset( $_POST['count'] ) ? sanitize_text_field( wp_unslash( $_POST['count'] ) ) : '';
280 $parent = isset( $_POST['parent'] ) ? sanitize_text_field( wp_unslash( $_POST['parent'] ) ) : '';
281
282 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
283 $q = [
284 'post_type' => 'topic',
285 'post_parent' => $parent,
286 'order' => 'DESC',
287 'orderby' => 'post_date',
288 'post_status' => 'publish',
289 'posts_per_page' => get_option( '_bbp_topics_per_page', 10 ),
290 'ignore_sticky_posts' => 1,
291 ];
292
293 if ( 'author' === $type ) {
294 $auth_ids = [
295 'author' => $post_in,
296 ];
297 $q = array_merge( $q, $auth_ids );
298 } elseif ( 'tag' === $type ) {
299 $tax_query[] = [
300 'taxonomy' => 'topic-tag',
301 'field' => 'term_id',
302 'terms' => $post_in,
303 ];
304 }
305
306 $tax_query[] = [
307 'taxonomy' => 'post_format',
308 'field' => 'slug',
309 'terms' => [ 'post-format-quote', 'post-format-link' ],
310 'operator' => 'NOT IN',
311 ];
312
313 if ( ! empty( $tax_query ) ) {
314 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
315 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
316 }
317
318 $query = new WP_Query( $q );
319
320 if ( $query->have_posts() ) :
321 echo '<div class="community-posts-wrapper bb-radius">';
322 while ( $query->have_posts() ) : $query->the_post();
323 global $post;
324 $author_id = $post->post_author;
325 $parent_post_id = $parent;
326 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
327 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
328 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
329 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
330 ?>
331 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
332 <div class="post-content">
333 <div class="author-avatar">
334 <?php
335 echo bbp_get_topic_author_link(
336 [
337 'post_id' => get_the_ID(),
338 'type' => 'avatar',
339 'size' => 40,
340 ]
341 );
342 ?>
343 </div>
344 <div class="entry-content">
345 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', esc_url( get_permalink() ) ), '</h3></a>' ); ?>
346 <ul class="meta">
347 <li>
348 <?php echo get_the_post_thumbnail( bbp_get_topic_forum_id(), [ 40, 40 ] ); ?>
349 <a href="<?php echo get_permalink( bbp_get_topic_forum_id() ); ?>">
350 <?php echo get_the_title( bbp_get_topic_forum_id() ); ?>
351 </a>
352 </li>
353 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
354 </ul>
355 </div>
356 </div>
357 <div class="post-meta-wrapper">
358 <ul class="post-meta-info">
359 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
360 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
361 </ul>
362 </div>
363 </div>
364 <?php
365 endwhile;
366 wp_reset_postdata();
367
368 echo '</div>';
369 else :
370 echo '<div class="community-post-error bug">';
371 echo '<div class="error-content">';
372 echo '<svg height="40" class="forumax-error error-icon" viewBox="0 0 24 24" version="1.1" width="40" aria-hidden="true"><path d="M12 7a.75.75 0 01.75.75v4.5a.75.75 0 01-1.5 0v-4.5A.75.75 0 0112 7zm1 9a1 1 0 11-2 0 1 1 0 012 0z"></path><path fill-rule="evenodd" d="M12 1C5.925 1 1 5.925 1 12s4.925 11 11 11 11-4.925 11-11S18.075 1 12 1zM2.5 12a9.5 9.5 0 1119 0 9.5 9.5 0 01-19 0z"></path></svg>';
373 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
374 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
375 echo '</div>';
376 echo '</div>';
377 endif;
378
379 wp_die();
380 }
381
382 /**
383 * Filter posts by status (open/closed)
384 *
385 * @return void
386 */
387 add_action( 'wp_ajax_forumax_open_post', 'forumax_open_post' );
388 add_action( 'wp_ajax_nopriv_forumax_open_post', 'forumax_open_post' );
389
390 /**
391 * Open post.
392 */
393 function forumax_open_post() {
394
395 // Check nonce
396 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'forumax-nonce' ) ) {
397 wp_die( '-1' );
398 }
399
400 $is_queried_obj = is_singular( 'forum' ) ? get_queried_object_id() : false;
401 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
402 $post_in = isset( $_POST['a_t_id'] ) ? sanitize_text_field( wp_unslash( $_POST['a_t_id'] ) ) : '';
403 $count = isset( $_POST['count'] ) ? sanitize_text_field( wp_unslash( $_POST['count'] ) ) : '';
404 $parent = isset( $_POST['parent'] ) ? sanitize_text_field( wp_unslash( $_POST['parent'] ) ) : '';
405 $userid = isset( $_POST['userid'] ) ? sanitize_text_field( wp_unslash( $_POST['userid'] ) ) : '';
406
407 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
408 $q = [
409 'post_type' => 'topic',
410 'post_parent' => $parent,
411 'order' => 'DESC',
412 'orderby' => 'post_date',
413 'posts_per_page' => get_option( '_bbp_topics_per_page', 10 ),
414 'ignore_sticky_posts' => 1,
415 'author' => $userid,
416 ];
417
418 if ( 'open' === $type ) {
419 $status = [
420 'post_status' => 'publish',
421 ];
422 $q = array_merge( $q, $status );
423 } elseif ( 'closed' === $type ) {
424 $status = [
425 'post_status' => 'closed',
426 ];
427 $q = array_merge( $q, $status );
428 }
429
430 $tax_query[] = [
431 'taxonomy' => 'post_format',
432 'field' => 'slug',
433 'terms' => [ 'post-format-quote', 'post-format-link' ],
434 'operator' => 'NOT IN',
435 ];
436
437 if ( ! empty( $tax_query ) ) {
438 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
439 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
440 }
441
442 $query = new WP_Query( $q );
443 if ( $query->have_posts() ) :
444 echo '<div class="community-posts-wrapper bb-radius">';
445 while ( $query->have_posts() ) : $query->the_post();
446 global $post;
447 $author_id = $post->post_author;
448 //$parent_post_id = get_post_meta( get_the_ID(), '_bbp_topic_id', true );
449 $parent_post_id = $parent;
450 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
451 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
452 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
453 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
454 ?>
455
456 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
457 <div class="post-content">
458 <div class="author-avatar">
459 <?php
460 echo bbp_get_topic_author_link(
461 [
462 'post_id' => get_the_ID(),
463 'type' => 'avatar',
464 'size' => 40,
465 ]
466 );
467 ?>
468 </div>
469 <div class="entry-content">
470 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', get_permalink() ), '</h3></a>' ); ?>
471 <ul class="meta">
472 <li>
473 <?php echo get_the_post_thumbnail( bbp_get_topic_forum_id(), [ 40, 40 ] ); ?>
474 <a href="<?php echo get_permalink( bbp_get_topic_forum_id() ); ?>">
475 <?php echo get_the_title( bbp_get_topic_forum_id() ); ?>
476 </a>
477 </li>
478 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
479 </ul>
480 </div>
481 </div>
482 <div class="post-meta-wrapper">
483 <ul class="post-meta-info">
484 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
485 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
486 </ul>
487 </div>
488 </div>
489 <?php
490 endwhile;
491 wp_reset_postdata();
492
493 echo '</div>';
494 else :
495 echo '<div class="community-post-error bug">';
496 echo '<div class="error-content">';
497 echo '<svg height="40" class="forumax-error error-icon" viewBox="0 0 24 24" version="1.1" width="40" aria-hidden="true"><path d="M12 7a.75.75 0 01.75.75v4.5a.75.75 0 01-1.5 0v-4.5A.75.75 0 0112 7zm1 9a1 1 0 11-2 0 1 1 0 012 0z"></path><path fill-rule="evenodd" d="M12 1C5.925 1 1 5.925 1 12s4.925 11 11 11 11-4.925 11-11S18.075 1 12 1zM2.5 12a9.5 9.5 0 1119 0 9.5 9.5 0 01-19 0z"></path></svg>';
498 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
499 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
500 echo '</div>';
501 echo '</div>';
502 endif;
503 wp_die();
504 }
505
506 /**
507 * Sort posts
508 *
509 * @return void
510 */
511 add_action( 'wp_ajax_forumax_loading_sort_post', 'forumax_loading_sort_post' );
512 add_action( 'wp_ajax_nopriv_forumax_loading_sort_post', 'forumax_loading_sort_post' );
513
514 /**
515 * Loading sort post.
516 */
517 function forumax_loading_sort_post() {
518
519 // Check nonce
520 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'forumax-nonce' ) ) {
521 wp_die( '-1' );
522 }
523
524 $sort = isset( $_POST['sort'] ) ? sanitize_text_field( wp_unslash( $_POST['sort'] ) ) : '';
525 $parent = isset( $_POST['parent'] ) ? sanitize_text_field( wp_unslash( $_POST['parent'] ) ) : '';
526
527 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
528 $q = [
529 'post_type' => 'topic',
530 'post_parent' => $parent,
531 'post_status' => 'publish',
532 'posts_per_page' => get_option( '_bbp_topics_per_page', 10 ),
533 'ignore_sticky_posts' => 1,
534 ];
535
536 if ( 'newest_posts' === $sort ) {
537 $newest_posts = [
538 'order' => 'DESC',
539 ];
540 $q = array_merge( $q, $newest_posts );
541 } elseif ( 'oldest_posts' === $sort ) {
542 $oldest_posts = [
543 'order' => 'ASC',
544 ];
545 $q = array_merge( $q, $oldest_posts );
546 } elseif ( 'comment_count' === $sort ) {
547 $comment_count = [
548 'meta_key' => '_bbp_reply_count',
549 'orderby' => 'meta_value_num',
550 'order' => 'DESC',
551 ];
552 $q = array_merge( $q, $comment_count );
553 } elseif ( 'comment_date' === $sort ) {
554 $comment_count = [
555 'meta_key' => '_bbp_reply_count',
556 'meta_type' => 'NUMERIC',
557 'orderby' => 'meta_value_num',
558 'order' => 'ASC',
559 ];
560 $q = array_merge( $q, $comment_count );
561 } elseif ( 'recent_updated_post' === $sort ) {
562 $post_date = [
563 'orderby' => 'post_modified',
564 'order' => 'DESC',
565 ];
566 $q = array_merge( $q, $post_date );
567 } elseif ( 'last_recent_updated_post' === $sort ) {
568 $post_modified = [
569 'orderby' => 'post_modified',
570 'order' => 'ASC',
571 ];
572 $q = array_merge( $q, $post_modified );
573 }
574
575 $tax_query[] = [
576 'taxonomy' => 'post_format',
577 'field' => 'slug',
578 'terms' => [ 'post-format-quote', 'post-format-link' ],
579 'operator' => 'NOT IN',
580 ];
581
582 if ( ! empty( $tax_query ) ) {
583 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
584 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
585 }
586
587 $query = new WP_Query( $q );
588 if ( $query->have_posts() ) :
589 echo '<div class="community-posts-wrapper bb-radius">';
590 while ( $query->have_posts() ) : $query->the_post();
591 global $post;
592
593 $author_id = $post->post_author;
594 $parent_post_id = $parent;
595 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
596 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
597 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
598 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
599 ?>
600 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
601 <div class="post-content">
602 <div class="author-avatar">
603 <?php
604 echo bbp_get_topic_author_link(
605 [
606 'post_id' => get_the_ID(),
607 'type' => 'avatar',
608 'size' => 40,
609 ]
610 );
611 ?>
612 </div>
613 <div class="entry-content">
614 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', esc_url( get_permalink() ) ), '</h3></a>' ); ?>
615 <ul class="meta">
616 <li>
617 <?php
618 if ( get_the_post_thumbnail_url( $parent_post_id ) ) :
619 ?>
620 <img src="<?php echo get_the_post_thumbnail_url( $parent_post_id ); ?>"
621 alt="<?php echo get_the_title( $parent_post_id ); ?>">
622 <?php
623 endif;
624 ?>
625 <a href="<?php echo get_permalink( $parent_post_id ); ?>"> <?php echo get_the_title( $parent_post_id ); ?> </a>
626 </li>
627 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
628 </ul>
629 </div>
630 </div>
631 <div class="post-meta-wrapper">
632 <ul class="post-meta-info">
633 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
634 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
635 </ul>
636 </div>
637 </div>
638 <?php endwhile;
639 wp_reset_postdata();
640
641 echo '</div>';
642 else :
643 echo '<div class="community-post-error bug">';
644 echo '<div class="error-content">';
645 echo '<svg height="40" class="forumax-error error-icon" viewBox="0 0 24 24" version="1.1" width="40" aria-hidden="true"><path d="M12 7a.75.75 0 01.75.75v4.5a.75.75 0 01-1.5 0v-4.5A.75.75 0 0112 7zm1 9a1 1 0 11-2 0 1 1 0 012 0z"></path><path fill-rule="evenodd" d="M12 1C5.925 1 1 5.925 1 12s4.925 11 11 11 11-4.925 11-11S18.075 1 12 1zM2.5 12a9.5 9.5 0 1119 0 9.5 9.5 0 01-19 0z"></path></svg>';
646 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
647 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
648 echo '</div>';
649 echo '</div>';
650 endif;
651
652 wp_die();
653 }
654