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