PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.0
Forumax – AI Powered Advanced Community Forum Plugin v2.5.0
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
← All changes | includes/ajax_actions.php +592 -237 1.1.0 → 2.5.0 View file →
@@ -1,286 +1,641 @@
1 1 <?php
2 +/**
3 + * Ajax actions for Forumax
4 + *
5 + * @package Forumax
6 + */
2 7
8 +defined( 'ABSPATH' ) || exit;
9 +
3 10 /**
4 11 * All Search results
5 12 */
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 +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' );
13 15
14 - if ( class_exists( 'EazyDocs' ) || class_exists( 'bbPress' ) ) :
15 -
16 - // All results query
17 - $all_results = new WP_Query( array(
18 - 'post_type' => ['post', 'forum', 'topic', 'docs' ],
19 - 's' => $_POST['keyword'] ?? '',
20 - ) );
21 - $all_results_count = $all_results->found_posts;
22 - if ( $all_results_count > 0 ) {
23 - $all_nsoresult = 'data-result=';
24 - } else {
25 - $all_nsoresult = 'data-noresult=';
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 );
26 32 }
27 -
28 - // Docs query
29 - $docs_query = new WP_Query( array(
30 - 'post_type' => 'docs',
31 - 's' => $_POST['keyword'] ?? '',
32 - ) );
33 - $docs_count = $docs_query->found_posts;
34 - if ( $docs_count > 0 ) {
35 - $docs_nsoresult = 'data-result=';
36 - } else {
37 - $docs_nsoresult = 'data-noresult=';
33 + if ( $topic_id ) {
34 + $crumbs[] = get_the_title( $topic_id );
38 35 }
39 -
40 - // Docs query
41 - $post_query = new WP_Query( array(
42 - 'post_type' => 'post',
43 - 's' => $_POST['keyword'] ?? '',
44 - ) );
45 - $post_count = $post_query->found_posts;
46 - if ( $post_count > 0 ) {
47 - $post_nsoresult = 'data-result=';
48 - } else {
49 - $post_nsoresult = 'data-noresult=';
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 );
50 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 + }
51 47
52 - // Forum query
53 - $forum_query = new WP_Query( array(
54 - 'post_type' => ['forum', 'topic'],
55 - 's' => $_POST['keyword'] ?? '',
56 - ) );
57 - $forum_count = $forum_query->found_posts;
58 - if ( $forum_count > 0 ) {
59 - $forum_nsoresult = 'data-result=';
60 - } else {
61 - $forum_nsoresult = 'data-noresult=';
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;
62 92 }
63 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.
64 162 ?>
65 - <div class="searchbar-tabs">
66 - <button type="button" class="tab-item active all-active" <?php echo esc_attr( $all_nsoresult. 'No-Results-Found' ); ?> onclick="searchAllTab()">
67 - <?php esc_html_e( 'All', 'bbp-core' ); ?>
68 - </button>
69 - <?php
70 - if ( class_exists( 'EazyDocs' ) ) :
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 );
71 167 ?>
72 - <button type="button" id="search-docs" <?php echo esc_attr( $docs_nsoresult. 'No-Results-Found' ); ?> class="tab-item" onclick="searchDocTab()">
73 - <?php esc_html_e( 'Docs', 'bbp-core' ); ?>
74 - </button>
75 - <?php
76 - endif;
77 - ?>
78 - <?php if ( class_exists( 'bbPress' ) ) : ?>
79 - <button type="button" id="search-forum" <?php echo esc_attr( $forum_nsoresult. 'No-Results-Found' ); ?> class="tab-item" onclick="searchForumTab()">
80 - <?php esc_html_e( 'Forum', 'bbp-core' ); ?>
81 - </button>
82 - <?php endif; ?>
83 - <button type="button" id="search-blog" <?php echo esc_attr( $post_nsoresult. 'No-Results-Found' ); ?> class="tab-item" onclick="searchBlogTab()">
84 - <?php esc_html_e( 'Blog', 'bbp-core' ); ?>
85 - </button>
168 + <button type="button" class="bbpc-tab" data-tab="<?php echo esc_attr( $type ); ?>"><?php echo esc_html( $tab_label ); ?></button>
169 + <?php endforeach; ?>
86 170 </div>
87 171 <?php
88 - endif;
89 172
90 - echo '<div class="bbpc-search-results-wrapper">';
91 - // Blog query
92 - $posts = new WP_Query( array(
93 - 'post_type' => 'post',
94 - 's' => $_POST['keyword'] ?? '',
95 - 'posts_per_page' => 5,
96 - ) );
97 - if ( $posts->have_posts() ) :
98 - echo '<div class="search-results-tab post show" id="search-blog-results">';
99 -
100 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Blog Posts', 'bbp-core' ) . '</h2>';
101 - while ( $posts->have_posts() ) : $posts->the_post();
173 + foreach ( $grouped as $type => $posts ) :
174 + $label = isset( $type_labels[ $type ] ) ? $type_labels[ $type ] : ucfirst( $type );
102 175 ?>
103 - <div class="search-result-item">
104 - <a href="<?php the_permalink(); ?>" class="title">
105 - <?php the_title() ?>
106 - </a>
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; ?>
107 196 </div>
108 197 <?php
109 - endwhile;
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 + }
110 241 wp_reset_postdata();
111 - echo '</div>';
112 - endif;
113 -
114 - // Docs query
115 - $posts = new WP_Query( [
116 - 'post_type' => 'docs',
117 - 's' => $_POST['keyword'] ?? '',
118 - 'posts_per_page' => 5,
119 - ]
120 - );
121 - if ( $posts->have_posts() ):
122 - echo '<div class="search-results-tab docs show" id="search-docs-results">';
123 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Documentation', 'bbp-core' ) . '</h2>';
242 + }
124 243
125 - while ( $posts->have_posts() ) : $posts->the_post();
126 - ?>
127 - <div class="search-result-item" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
128 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>" class="title">
129 - <?php the_title() ?>
130 - </a>
131 - </div>
132 - <?php
133 - endwhile;
134 - wp_reset_postdata();
135 - echo '</div>';
136 - endif;
137 -
138 - // Forums query
139 - if ( class_exists( 'bbPress' ) ) :
244 + wp_send_json_success( $forum_list );
245 +}
140 246
141 - echo '<div class="search-results-tab forum show" id="search-forum-results">';
142 -
143 - $forum = new WP_Query( array(
144 - 'post_type' => 'forum',
145 - 's' => $_POST['keyword'] ?? '',
146 - 'posts_per_page' => 5,
147 - ) );
148 247
149 - if ( $forum->have_posts() ) :
150 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Forums', 'bbp-core' ) . '</h2>';
151 - while ( $forum->have_posts() ) : $forum->the_post();
152 - ?>
153 - <div class="search-result-item forum" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
154 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>">
155 - <?php the_title(); ?>
156 - </a>
157 - </div>
158 - <?php
159 - endwhile;
160 - wp_reset_postdata();
161 - endif;
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' );
162 253
163 - $topics = new WP_Query( array(
164 - 'post_type' => 'topic',
165 - 's' => $_POST['keyword'] ?? '',
166 - 'posts_per_page' => 5,
167 - ) );
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 + }
168 264
169 - if ( $topics->have_posts() ) :
170 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Topics', 'bbp-core' ) . '</h2>';
171 - while ( $topics->have_posts() ) : $topics->the_post();
172 - ?>
173 - <div class="search-result-item forum" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
174 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>">
175 - <?php the_title(); ?>
176 - </a>
177 - </div>
178 - <?php
179 - endwhile;
180 - wp_reset_postdata();
181 - endif;
182 - echo '</div>';
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>';
183 365 endif;
184 366
185 - echo '<h5 class="not-found-text">Not Found Result!</h5>';
186 - echo '</div>';
187 - die();
367 + wp_die();
188 368 }
189 369
190 370 /**
191 - * Blog Search results
371 + * Filter posts by status (open/closed)
372 + *
373 + * @return void
192 374 */
193 -add_action( 'wp_ajax_bbpc_search_data_blog', 'bbpc_search_data_blog' );
194 -add_action( 'wp_ajax_nopriv_bbpc_search_data_blog', 'bbpc_search_data_blog' );
195 -function bbpc_search_data_blog() {
375 +add_action( 'wp_ajax_forumax_open_post', 'forumax_open_post' );
376 +add_action( 'wp_ajax_nopriv_forumax_open_post', 'forumax_open_post' );
196 377
197 - $post_type = $_POST['post_type'] ?? '';
198 - $sec_title = '';
378 +/**
379 + * Open post.
380 + */
381 +function forumax_open_post() {
199 382
200 - if ( $post_type == 'post' ) {
201 - $post_name = 'blog';
202 - $sec_title = 'Blog Posts';
203 - } else {
204 - $post_name = 'docs';
205 - $sec_title = 'Documentation';
383 + // Check nonce
384 + if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'forumax-nonce' ) ) {
385 + wp_die( '-1' );
206 386 }
207 -
208 - // Blog query
209 - $blog = new WP_Query( array(
210 - 'post_type' => $post_type,
211 - 's' => $_POST['keyword'] ?? '',
212 - 'posts_per_page' => 5,
213 - ) );
214 - echo '<div class="search-results-tab '.$post_name.' show" id="search-'.$post_name.'-results">';
215 - if ( $blog->have_posts() ) :
216 - echo '<h2 class="bbpc-search-title">' . esc_html__( $sec_title, 'bbp-core' ) . '</h2>';
217 - while ( $blog->have_posts() ) : $blog->the_post();
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;
218 442 ?>
219 - <div class="search-result-item blog" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
220 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>">
221 - <?php the_title(); ?>
222 - </a>
223 - </div>
224 - <?php
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
225 478 endwhile;
226 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>';
227 490 endif;
228 - echo '</div>';
229 - die();
491 + wp_die();
230 492 }
231 493
232 494 /**
233 - * Forum Search results
495 + * Sort posts
496 + *
497 + * @return void
234 498 */
235 -add_action( 'wp_ajax_bbpc_search_data_forum', 'bbpc_search_data_forum' );
236 -add_action( 'wp_ajax_nopriv_bbpc_search_data_forum', 'bbpc_search_data_forum' );
237 -function bbpc_search_data_forum() {
238 -
239 - // Forums query
240 - if ( class_exists( 'bbPress' ) ) :
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' );
241 501
242 - echo '<div class="search-results-tab show" id="search-forum-results">';
243 -
244 - $forum = new WP_Query( array(
245 - 'post_type' => 'forum',
246 - 's' => $_POST['keyword'] ?? '',
247 - 'posts_per_page' => 5,
248 - ) );
502 +/**
503 + * Loading sort post.
504 + */
505 +function forumax_loading_sort_post() {
249 506
250 - if ( $forum->have_posts() ) :
251 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Forums', 'bbp-core' ) . '</h2>';
252 - while ( $forum->have_posts() ) : $forum->the_post();
253 - ?>
254 - <div class="search-result-item forum" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
255 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>">
256 - <?php the_title(); ?>
257 - </a>
258 - </div>
259 - <?php
260 - endwhile;
261 - wp_reset_postdata();
262 - endif;
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 + }
263 511
264 - $topics = new WP_Query( array(
265 - 'post_type' => 'topic',
266 - 's' => $_POST['keyword'] ?? '',
267 - 'posts_per_page' => 5,
268 - ) );
512 + $sort = isset( $_POST['sort'] ) ? sanitize_text_field( wp_unslash( $_POST['sort'] ) ) : '';
513 + $parent = isset( $_POST['parent'] ) ? sanitize_text_field( wp_unslash( $_POST['parent'] ) ) : '';
269 514
270 - if ( $topics->have_posts() ) :
271 - echo '<h2 class="bbpc-search-title">' . esc_html__( 'Topics', 'bbp-core' ) . '</h2>';
272 - while ( $topics->have_posts() ) : $topics->the_post();
273 - ?>
274 - <div class="search-result-item forum" onclick="document.location='<?php echo get_the_permalink(get_the_ID()); ?>'">
275 - <a href="<?php echo get_the_permalink(get_the_ID()); ?>">
276 - <?php the_title(); ?>
277 - </a>
278 - </div>
279 - <?php
280 - endwhile;
281 - wp_reset_postdata();
282 - endif;
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 +
283 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>';
284 638 endif;
285 - die();
286 -}
639 +
640 + wp_die();
641 +}