PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.0.0
Forumax – AI Powered Advanced Community Forum Plugin v2.0.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 2.1.0 All 29 releases
bbp-core / includes / ajax_actions.php

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

565 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * All Search results
5 */
6 add_action( 'wp_ajax_forumax_search_data_fetch', 'forumax_search_data_fetch' );
7 add_action( 'wp_ajax_nopriv_forumax_search_data_fetch', 'forumax_search_data_fetch' );
8 function forumax_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', 'forumax' ) . '</h2>';
63 while ( $forum->have_posts() ) : $forum->the_post();
64 ?>
65 <div class="search-result-item forum" onclick="document.location='<?php echo esc_url( get_the_permalink(get_the_ID()) ); ?>'">
66 <a href="<?php echo esc_url( 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', 'forumax' ) . '</h2>';
83 while ( $topics->have_posts() ) : $topics->the_post();
84 ?>
85 <div class="search-result-item forum" onclick="document.location='<?php echo esc_url( get_the_permalink(get_the_ID()) ); ?>'">
86 <a href="<?php echo esc_url( 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">' . esc_html__( 'Not Found Result!', 'forumax' ) . '</h5>';
98 echo '</div>';
99 die();
100 }
101
102 /**
103 * Forum Search results
104 */
105 add_action( 'wp_ajax_forumax_search_data_forum', 'forumax_search_data_forum' );
106 add_action( 'wp_ajax_nopriv_forumax_search_data_forum', 'forumax_search_data_forum' );
107 function forumax_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', 'forumax' ) . '</h2>';
122 while ( $forum->have_posts() ) : $forum->the_post();
123 ?>
124 <div class="search-result-item forum" onclick="document.location='<?php echo esc_url( get_the_permalink(get_the_ID()) ); ?>'">
125 <a href="<?php echo esc_url( 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', 'forumax' ) . '</h2>';
142 while ( $topics->have_posts() ) : $topics->the_post();
143 ?>
144 <div class="search-result-item forum" onclick="document.location='<?php echo esc_url( get_the_permalink(get_the_ID()) ); ?>'">
145 <a href="<?php echo esc_url( 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 }
157
158 /**
159 * AJAX handler to fetch forums for Gutenberg block editor
160 * Used by the Single Forum block to populate the forum selector dropdown
161 */
162 add_action( 'wp_ajax_bbpc_get_forums', 'forumax_get_forums_ajax' );
163 add_action( 'wp_ajax_nopriv_bbpc_get_forums', 'forumax_get_forums_ajax' );
164 function forumax_get_forums_ajax() {
165 // Get all forums
166 $forums = new \WP_Query( array(
167 'post_type' => 'forum',
168 'posts_per_page' => -1,
169 'orderby' => 'title',
170 'order' => 'ASC',
171 ) );
172
173 $forum_list = array();
174
175 if ( $forums->have_posts() ) {
176 while ( $forums->have_posts() ) {
177 $forums->the_post();
178 $forum_list[] = array(
179 'id' => get_the_ID(),
180 'title' => get_the_title(),
181 );
182 }
183 wp_reset_postdata();
184 }
185
186 wp_send_json_success( $forum_list );
187 }
188
189
190 /**
191 * Loading Post
192 *
193 * @return string
194 */
195 add_action( 'wp_ajax_forumax_loading_post', 'forumax_loading_post' );
196 add_action( 'wp_ajax_nopriv_forumax_loading_post', 'forumax_loading_post' );
197
198 /**
199 * Loading forum posts
200 */
201 function forumax_loading_post() {
202 global $wpdb;
203
204 $nonce = sanitize_text_field( $_POST['nonce'] );
205 $type = sanitize_text_field( $_POST['type'] );
206 $post_in = sanitize_text_field( $_POST['a_t_id'] );
207 $count = sanitize_text_field( $_POST['count'] );
208 $parent = sanitize_text_field( $_POST['parent'] );
209 if ( ! wp_verify_nonce( $nonce, 'forumax-nonce' ) ) {
210 die( '-1' );
211 }
212 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
213 $q = [
214 'post_type' => 'topic',
215 'post_parent' => $parent,
216 'order' => 'DESC',
217 'orderby' => 'post_date',
218 'post_status' => 'publish',
219 'posts_per_page' => - 1,
220 'ignore_sticky_posts' => 1,
221 ];
222 if ( $type == 'author' ) {
223 $auth_ids = [
224 'author' => $post_in,
225 ];
226 $q = array_merge( $q, $auth_ids );
227 } elseif ( $type == 'tag' ) {
228 $tax_query[] = [
229 'taxonomy' => 'topic-tag',
230 'field' => 'term_id',
231 'terms' => $post_in,
232 ];
233 }
234 $tax_query[] = [
235 'taxonomy' => 'post_format',
236 'field' => 'slug',
237 'terms' => [ 'post-format-quote', 'post-format-link' ],
238 'operator' => 'NOT IN',
239 ];
240 if ( ! empty( $tax_query ) ) {
241 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
242 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
243 }
244 $query = new WP_Query( $q );
245
246 if ( $query->have_posts() ):
247 echo '<div class="community-posts-wrapper bb-radius">';
248 while ( $query->have_posts() ): $query->the_post();
249 global $post;
250 $author_id = $post->post_author;
251 $parent_post_id = $parent;
252 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
253 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
254 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
255 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
256 ?>
257 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
258 <div class="post-content">
259 <div class="author-avatar">
260 <?php
261 echo bbp_get_topic_author_link(
262 array(
263 'post_id' => get_the_ID(),
264 'type' => 'avatar',
265 'size' => 40
266 )
267 );
268 ?>
269 </div>
270 <div class="entry-content">
271 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', esc_url( get_permalink() ) ), '</h3></a>' ); ?>
272 <ul class="meta">
273 <li>
274 <?php echo get_the_post_thumbnail( bbp_get_topic_forum_id(), array( 40, 40 ) ); ?>
275 <a href="<?php echo get_permalink( bbp_get_topic_forum_id() ); ?>">
276 <?php echo get_the_title( bbp_get_topic_forum_id() ); ?>
277 </a>
278 </li>
279 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
280 </ul>
281 </div>
282 </div>
283 <div class="post-meta-wrapper">
284 <ul class="post-meta-info">
285 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
286 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
287 </ul>
288 </div>
289 </div>
290 <?php
291 endwhile;
292 wp_reset_postdata();
293
294 echo '</div>';
295 else:
296 echo '<div class="community-post-error bug">';
297 echo '<div class="error-content">';
298 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>';
299 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
300 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
301 echo '</div>';
302 echo '</div>';
303 endif;
304
305 die;
306 }
307
308
309
310 /**
311 * Loading Post
312 *
313 * @return string
314 */
315 add_action( 'wp_ajax_forumax_open_post', 'forumax_open_post' );
316 add_action( 'wp_ajax_nopriv_forumax_open_post', 'forumax_open_post' );
317
318 function forumax_open_post() {
319 global $wpdb;
320
321 $is_queried_obj = is_singular( 'forum' ) ? get_queried_object_id() : false;
322 $nonce = sanitize_text_field( $_POST['nonce'] );
323 $type = sanitize_text_field( $_POST['type'] );
324 $post_in = sanitize_text_field( $_POST['a_t_id'] );
325 $count = sanitize_text_field( $_POST['count'] );
326 $parent = sanitize_text_field( $_POST['parent'] );
327 $userid = sanitize_text_field( $_POST['userid'] );
328
329 if ( ! wp_verify_nonce( $nonce, 'forumax-nonce' ) ) {
330 die( '-1' );
331 }
332
333 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
334 $q = [
335 'post_type' => 'topic',
336 'post_parent' => $parent,
337 'order' => 'DESC',
338 'orderby' => 'post_date',
339 'posts_per_page' => get_option( '_bbp_topics_per_page', 10 ),
340 'ignore_sticky_posts' => 1,
341 'author' => $userid
342 ];
343 if ( $type == 'open' ) {
344 $status = [
345 'post_status' => 'publish',
346 ];
347 $q = array_merge( $q, $status );
348 } elseif ( $type == 'closed' ) {
349 $status = [
350 'post_status' => 'closed',
351 ];
352 $q = array_merge( $q, $status );
353 }
354 $tax_query[] = [
355 'taxonomy' => 'post_format',
356 'field' => 'slug',
357 'terms' => [ 'post-format-quote', 'post-format-link' ],
358 'operator' => 'NOT IN',
359 ];
360 if ( ! empty( $tax_query ) ) {
361 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
362 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
363 }
364 $query = new WP_Query( $q );
365 if ( $query->have_posts() ):
366 echo '<div class="community-posts-wrapper bb-radius">';
367 while ( $query->have_posts() ): $query->the_post();
368 global $post;
369 $author_id = $post->post_author;
370 //$parent_post_id = get_post_meta( get_the_ID(), '_bbp_topic_id', true );
371 $parent_post_id = $parent;
372 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
373 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
374 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
375 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
376 ?>
377
378 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
379 <div class="post-content">
380 <div class="author-avatar">
381 <?php
382 echo bbp_get_topic_author_link(
383 array(
384 'post_id' => get_the_ID(),
385 'type' => 'avatar',
386 'size' => 40
387 )
388 );
389 ?>
390 </div>
391 <div class="entry-content">
392 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', get_permalink() ), '</h3></a>' ); ?>
393 <ul class="meta">
394 <li>
395 <?php echo get_the_post_thumbnail( bbp_get_topic_forum_id(), array( 40, 40 ) ); ?>
396 <a href="<?php echo get_permalink( bbp_get_topic_forum_id() ); ?>">
397 <?php echo get_the_title( bbp_get_topic_forum_id() ); ?>
398 </a>
399 </li>
400 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
401 </ul>
402 </div>
403 </div>
404 <div class="post-meta-wrapper">
405 <ul class="post-meta-info">
406 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
407 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
408 </ul>
409 </div>
410 </div>
411 <?php
412 endwhile;
413 wp_reset_postdata();
414
415 echo '</div>';
416 else:
417 echo '<div class="community-post-error bug">';
418 echo '<div class="error-content">';
419 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>';
420 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
421 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
422 echo '</div>';
423 echo '</div>';
424 endif;
425 die;
426 }
427
428 add_action( 'wp_ajax_forumax_loading_sort_post', 'forumax_loading_sort_post' );
429 add_action( 'wp_ajax_nopriv_forumax_loading_sort_post', 'forumax_loading_sort_post' );
430
431 function forumax_loading_sort_post() {
432 global $wpdb;
433
434 $nonce = sanitize_text_field( $_POST['nonce'] );
435 $sort = sanitize_text_field( $_POST['sort'] );
436 $parent = sanitize_text_field( $_POST['parent'] );
437
438 if ( ! wp_verify_nonce( $nonce, 'forumax-nonce' ) ) {
439 die( '-1' );
440 }
441
442 $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
443 $q = [
444 'post_type' => 'topic',
445 'post_parent' => $parent,
446 'post_status' => 'publish',
447 'posts_per_page' => get_option( '_bbp_topics_per_page', 10 ),
448 'ignore_sticky_posts' => 1,
449 ];
450 if ( $sort == 'newest_posts' ) {
451 $newest_posts = [
452 'order' => 'DESC',
453 ];
454 $q = array_merge( $q, $newest_posts );
455 } elseif ( $sort == 'oldest_posts' ) {
456 $oldest_posts = [
457 'order' => 'ASC',
458 ];
459 $q = array_merge( $q, $oldest_posts );
460 } elseif ( $sort == 'comment_count' ) {
461 $comment_count = [
462 'meta_key' => '_bbp_reply_count',
463 'orderby' => 'meta_value_num',
464 'order' => 'DESC',
465 ];
466 $q = array_merge( $q, $comment_count );
467 } elseif ( $sort == 'comment_date' ) {
468 $comment_count = [
469 'meta_key' => '_bbp_reply_count',
470 'meta_type' => 'NUMERIC',
471 'orderby' => 'meta_value_num',
472 'order' => 'ASC',
473 ];
474 $q = array_merge( $q, $comment_count );
475 } elseif ( $sort == 'recent_updated_post' ) {
476 $post_date = [
477 'orderby' => 'post_modified',
478 'order' => 'DESC',
479 ];
480 $q = array_merge( $q, $post_date );
481 } elseif ( $sort == 'last_recent_updated_post' ) {
482 $post_modified = [
483 'orderby' => 'post_modified',
484 'order' => 'ASC',
485 ];
486 $q = array_merge( $q, $post_modified );
487 }
488 $tax_query[] = [
489 'taxonomy' => 'post_format',
490 'field' => 'slug',
491 'terms' => [ 'post-format-quote', 'post-format-link' ],
492 'operator' => 'NOT IN',
493 ];
494 if ( ! empty( $tax_query ) ) {
495 $tax_query = array_merge( [ 'relation' => 'AND' ], $tax_query );
496 $q = array_merge( $q, [ 'tax_query' => $tax_query ] );
497 }
498 $query = new WP_Query( $q );
499 if ( $query->have_posts() ):
500 echo '<div class="community-posts-wrapper bb-radius">';
501 while ( $query->have_posts() ): $query->the_post();
502 global $post;
503
504 $author_id = $post->post_author;
505 $parent_post_id = $parent;
506 $favoriters = get_post_meta( get_the_ID(), '_bbp_favorite', true );
507 $favorite_count = ! empty( $favoriters ) ? $favoriters[0] : '0';
508 $get_reply = get_post_meta( get_the_ID(), '_bbp_reply_count', true );
509 $_reply_count = isset( $get_reply ) && ! empty( $get_reply ) ? $get_reply : 0;
510 ?>
511 <div class="community-post style-two <?php the_author_meta( 'user_nicename', $author_id ); ?>">
512 <div class="post-content">
513 <div class="author-avatar">
514 <?php
515 echo bbp_get_topic_author_link(
516 array(
517 'post_id' => get_the_ID(),
518 'type' => 'avatar',
519 'size' => 40
520 )
521 );
522 ?>
523 </div>
524 <div class="entry-content">
525 <?php the_title( sprintf( '<a href="%s" rel="bookmark"> <h3 class="post-title">', esc_url( get_permalink() ) ), '</h3></a>' ); ?>
526 <ul class="meta">
527 <li>
528 <?php
529 if ( get_the_post_thumbnail_url( $parent_post_id ) ) :
530 ?>
531 <img src="<?php echo get_the_post_thumbnail_url( $parent_post_id ); ?>"
532 alt="<?php echo get_the_title( $parent_post_id ); ?>">
533 <?php
534 endif;
535 ?>
536 <a href="<?php echo get_permalink( $parent_post_id ); ?>"> <?php echo get_the_title( $parent_post_id ); ?> </a>
537 </li>
538 <li><i class="icon_clock_alt"></i> <?php bbp_topic_post_date( get_the_ID() ); ?> </li>
539 </ul>
540 </div>
541 </div>
542 <div class="post-meta-wrapper">
543 <ul class="post-meta-info">
544 <li><a href="#"><i class="icon_chat_alt"></i><?php echo esc_html( $_reply_count ); ?></a></li>
545 <li><a href="#"><i class="icon_star"></i><?php echo esc_html( $favorite_count ); ?></a></li>
546 </ul>
547 </div>
548 </div>
549 <?php endwhile;
550 wp_reset_postdata();
551
552 echo '</div>';
553 else:
554 echo '<div class="community-post-error bug">';
555 echo '<div class="error-content">';
556 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>';
557 echo '<h3 class="error">' . esc_html__( 'Oops! No results matched your search.', 'forumax' ) . '</h3>';
558 echo '<p class="error">' . esc_html__( 'You could search again.', 'forumax' ) . '</p>';
559 echo '</div>';
560 echo '</div>';
561 endif;
562
563 die;
564 }
565