PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / trunk
Forumax – AI Powered Advanced Community Forum Plugin vtrunk
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 / Blocks / Render / forums.php

forums.php in Forumax – AI Powered Advanced Community Forum Plugin trunk, at includes/Blocks/Render/forums.php

416 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace admin\Blocks\Render;
3
4 use WP_Query;
5
6 class Forums {
7 public function render_forums( $attributes, $content ) {
8
9 if ( ! wp_script_is( 'bbpc-ajax', 'registered' ) ) {
10 wp_register_script( 'bbpc-ajax', FORUMAX_FRONT_ASS . 'js/ajax.js', array( 'jquery' ), FORUMAX_VERSION, true );
11 }
12 if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) {
13 wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css', array(), FORUMAX_VERSION );
14 }
15
16 wp_enqueue_script( 'bbpc-ajax' );
17 wp_enqueue_style( 'bbpc-el-widgets' );
18
19
20 $settings = $attributes;
21 $ppp = ! empty( $attributes['ppp'] ) ? absint( $attributes['ppp'] ) : 5;
22 $ppp2 = ! empty( $attributes['ppp2'] ) ? absint( $attributes['ppp2'] ) : 10;
23 $order = ! empty( $attributes['order'] ) ? $attributes['order'] : 'ASC';
24 $orderby = ! empty( $attributes['orderby'] ) ? $attributes['orderby'] : 'date';
25 $columns = ! empty( $attributes['columns'] ) ? absint( $attributes['columns'] ) : 3;
26 $forum_image_size = ! empty( $attributes['forum_image_size'] ) ? $attributes['forum_image_size'] : 'full';
27 $show_forum_image = isset( $attributes['show_forum_image'] ) ? (bool) $attributes['show_forum_image'] : true;
28 $show_post_count = isset( $attributes['show_post_count'] ) ? (bool) $attributes['show_post_count'] : true;
29 $show_reply_count = isset( $attributes['show_reply_count'] ) ? (bool) $attributes['show_reply_count'] : false;
30 $show_excerpt = isset( $attributes['show_excerpt'] ) ? (bool) $attributes['show_excerpt'] : false;
31 $is_more_btn = isset( $attributes['is_more_btn'] ) ? (bool) $attributes['is_more_btn'] : true;
32 $more_txt = ! empty( $attributes['more_txt'] ) ? $attributes['more_txt'] : __( 'More Communities', 'bbp-core' );
33 $less_txt = ! empty( $attributes['less_txt'] ) ? $attributes['less_txt'] : __( 'Less Communities', 'bbp-core' );
34 $no_posts_message = ! empty( $attributes['no_posts_message'] ) ? $attributes['no_posts_message'] : __( 'No forums found.', 'bbp-core' );
35 $enable_card_hover = isset( $attributes['enable_card_hover'] ) ? (bool) $attributes['enable_card_hover'] : true;
36 $mobile_columns = ! empty( $attributes['mobile_columns'] ) ? absint( $attributes['mobile_columns'] ) : 1;
37 $hide_image_mobile = isset( $attributes['hide_image_mobile'] ) ? (bool) $attributes['hide_image_mobile'] : false;
38
39 $layout = ! empty( $attributes['layout'] ) ? $attributes['layout'] : 'default';
40 $unique_id = uniqid( 'bbpc-forums-' );
41
42 // Enforce free defaults when Pro is not active.
43 $is_pro_active = function_exists( 'forumax_is_premium' ) && forumax_is_premium();
44 if ( ! $is_pro_active ) {
45 $columns = 3;
46 $mobile_columns = 1;
47 $hide_image_mobile = false;
48 }
49
50
51 $sanitize_css = function ( $value ) {
52 return preg_match( '/^[a-zA-Z0-9#(),.\s%-]+$/', $value ) ? $value : '';
53 };
54
55
56 $query_args = array(
57 'post_type' => 'forum',
58 'posts_per_page' => $ppp,
59 'order' => $order,
60 'orderby' => $orderby,
61 );
62
63 // Filter by IDs (Pro feature).
64 if ( $is_pro_active && ! empty( $attributes['forum_filter_ids'] ) ) {
65 $ids = array_map( 'absint', array_filter( explode( ',', $attributes['forum_filter_ids'] ) ) );
66 if ( ! empty( $ids ) ) {
67 $query_args['post__in'] = $ids;
68 }
69 }
70
71 $forums = new WP_Query( $query_args );
72
73 // ---- Help Center Cards layout ----------------------------------------
74 if ( 'help-center' === $layout ) {
75 $show_excerpt = true; // Always show excerpt in Help Center (matches Elementor template).
76 ob_start();
77 ?>
78 <div id="<?php echo esc_attr( $unique_id ); ?>" class="bbpc-hc-wrap">
79 <?php if ( $forums->have_posts() ) : ?>
80 <div class="bbpc-hc-grid">
81 <?php
82 while ( $forums->have_posts() ) :
83 $forums->the_post();
84 $item_id = get_the_ID();
85 ?>
86 <a href="<?php echo esc_url( get_the_permalink() ); ?>" class="bbpc-hc-card">
87 <?php if ( $show_forum_image && has_post_thumbnail( $item_id ) ) : ?>
88 <div class="bbpc-hc-icon">
89 <?php the_post_thumbnail( array( 48, 48 ) ); ?>
90 </div>
91 <?php endif; ?>
92 <div class="bbpc-hc-body">
93 <h3 class="bbpc-hc-title"><?php echo esc_html( get_the_title() ); ?></h3>
94 <?php
95 $excerpt = get_the_excerpt();
96 if ( $show_excerpt && $excerpt ) :
97 ?>
98 <p class="bbpc-hc-desc"><?php echo esc_html( wp_trim_words( $excerpt, 18, '' ) ); ?></p>
99 <?php endif; ?>
100 <span class="bbpc-hc-count"><?php echo esc_html( bbp_get_forum_topic_count( $item_id ) ); ?> <?php esc_html_e( 'articles', 'bbp-core' ); ?></span>
101 </div>
102 <i class="eicon-arrow-right bbpc-hc-arrow" aria-hidden="true"></i>
103 </a>
104 <?php endwhile; ?>
105 <?php wp_reset_postdata(); ?>
106 </div>
107
108 <?php else : ?>
109 <div class="alert alert-warning"><?php echo esc_html( $no_posts_message ); ?></div>
110 <?php endif; ?>
111
112 <?php
113 $styles = '';
114 $id = '#' . esc_attr( $unique_id );
115
116 // Grid columns.
117 if ( $columns ) {
118 $styles .= "{$id} .bbpc-hc-grid { grid-template-columns: repeat({$columns}, 1fr); }";
119 }
120
121 // Card gap.
122 if ( ! empty( $settings['card_gap'] ) ) {
123 $gap = absint( $settings['card_gap'] );
124 $styles .= "{$id} .bbpc-hc-grid { gap: {$gap}px !important; }";
125 }
126
127 // Card background.
128 if ( ! empty( $settings['card_bg_color'] ) ) {
129 $color = $sanitize_css( $settings['card_bg_color'] );
130 $styles .= "{$id} .bbpc-hc-card { background-color: {$color} !important; }";
131 }
132
133 // Card border radius.
134 if ( ! empty( $settings['card_border_radius'] ) ) {
135 $radius = absint( $settings['card_border_radius'] );
136 $styles .= "{$id} .bbpc-hc-card { border-radius: {$radius}px !important; }";
137 }
138
139 // Title color.
140 if ( ! empty( $settings['title_color'] ) ) {
141 $color = $sanitize_css( $settings['title_color'] );
142 $styles .= "{$id} .bbpc-hc-title { color: {$color} !important; }";
143 }
144
145 // Title hover color.
146 if ( ! empty( $settings['title_hover_color'] ) ) {
147 $color = $sanitize_css( $settings['title_hover_color'] );
148 $styles .= "{$id} .bbpc-hc-card:hover .bbpc-hc-title { color: {$color} !important; }";
149 }
150
151 // Content color.
152 if ( ! empty( $settings['content_color'] ) ) {
153 $color = $sanitize_css( $settings['content_color'] );
154 $styles .= "{$id} .bbpc-hc-desc, {$id} .bbpc-hc-count { color: {$color} !important; }";
155 }
156
157 // Title font size.
158 if ( ! empty( $settings['title_font_size'] ) ) {
159 $size = absint( $settings['title_font_size'] );
160 $styles .= "{$id} .bbpc-hc-title { font-size: {$size}px !important; }";
161 }
162
163 // Meta font size.
164 if ( ! empty( $settings['meta_font_size'] ) ) {
165 $size = absint( $settings['meta_font_size'] );
166 $styles .= "{$id} .bbpc-hc-desc, {$id} .bbpc-hc-count { font-size: {$size}px !important; }";
167 }
168
169 // Hover animation disabled.
170 if ( ! $enable_card_hover ) {
171 $styles .= "{$id} .bbpc-hc-card:hover { background: inherit !important; }";
172 $styles .= "{$id} .bbpc-hc-arrow { opacity: 0 !important; transform: none !important; }";
173 }
174
175 // Responsive: mobile columns.
176 if ( $mobile_columns || $hide_image_mobile ) {
177 $styles .= "@media (max-width: 768px) {";
178 if ( $mobile_columns ) {
179 $styles .= "{$id} .bbpc-hc-grid { grid-template-columns: repeat({$mobile_columns}, 1fr) !important; }";
180 }
181 if ( $hide_image_mobile ) {
182 $styles .= "{$id} .bbpc-hc-icon { display: none !important; }";
183 }
184 $styles .= "}";
185 }
186
187 // Output styles.
188 if ( ! empty( $styles ) ) {
189 echo '<style>' . $styles . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
190 }
191 ?>
192 </div>
193 <?php
194 return ob_get_clean();
195 }
196 // ---- End Help Center Cards layout ------------------------------------
197
198 $query_args2 = $query_args;
199 $query_args2['posts_per_page'] = $ppp2;
200 $query_args2['offset'] = $ppp;
201 $forums2 = new WP_Query( $query_args2 );
202
203 $hover_class = $enable_card_hover ? ' wow fadeInRight' : '';
204
205 $svg_minus = '<svg fill="currentColor" width="16px" height="16px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="icon line-color icon_minus"><line x1="19" y1="12" x2="5" y2="12" style="fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2;"></line></svg>';
206 $svg_plus = '<svg fill="currentColor" width="16px" height="16px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="icon line-color icon_plus"><path d="M5,12H19M12,5V19" style="fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2;"></path></svg>';
207
208 ob_start();
209 ?>
210 <div id="<?php echo esc_attr( $unique_id ); ?>">
211 <?php if ( $forums->have_posts() ) : ?>
212 <div class="communities-boxes">
213 <?php
214 $delay = 0.1;
215 while ( $forums->have_posts() ) :
216 $forums->the_post();
217 $item_id = get_the_ID();
218 ?>
219 <div class="com-box<?php echo esc_attr( $hover_class ); ?>" data-wow-delay="<?php echo esc_attr( $delay ); ?>s">
220 <?php $delay += 0.1; ?>
221 <?php if ( $show_forum_image && get_the_post_thumbnail( get_the_ID(), $forum_image_size ) ) : ?>
222 <div class="icon-container">
223 <?php the_post_thumbnail( $forum_image_size ); ?>
224 </div>
225 <?php endif; ?>
226 <div class="com-box-content">
227 <h3 class="title">
228 <a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
229 </h3>
230 <?php if ( $show_excerpt ) : ?>
231 <p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p>
232 <?php endif; ?>
233 <?php if ( $show_post_count || $show_reply_count ) : ?>
234 <div class="bbpc-forum-meta">
235 <?php if ( $show_post_count ) : ?>
236 <span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span>
237 <?php endif; ?>
238 <?php if ( $show_post_count && $show_reply_count ) : ?>
239 <span class="bbpc-meta-sep"> · </span>
240 <?php endif; ?>
241 <?php if ( $show_reply_count ) : ?>
242 <span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span>
243 <?php endif; ?>
244 </div>
245 <?php endif; ?>
246 </div>
247 </div>
248 <?php endwhile; ?>
249 <?php wp_reset_postdata(); ?>
250 </div>
251
252 <?php if ( $is_more_btn && $forums2->have_posts() ) : ?>
253 <div class="more-communities" data_id="<?php echo esc_attr( $unique_id ); ?>">
254 <a href="#more-category" class="collapse-btn-wrap" data-more="<?php echo esc_attr( $more_txt ); ?>" data-less="<?php echo esc_attr( $less_txt ); ?>">
255 <?php echo esc_html( $more_txt ); ?>
256 <?php echo wp_kses_post( $svg_minus ); ?>
257 <?php echo wp_kses_post( $svg_plus ); ?>
258 </a>
259
260 <div class="collapse-wrap" id="more-category" data_id="<?php echo esc_attr( $unique_id ); ?>">
261 <div class="communities-boxes">
262 <?php
263 while ( $forums2->have_posts() ) :
264 $forums2->the_post();
265 $item_id = get_the_ID();
266 ?>
267 <div class="com-box">
268 <?php if ( $show_forum_image && get_the_post_thumbnail( get_the_ID(), $forum_image_size ) ) : ?>
269 <div class="icon-container">
270 <?php the_post_thumbnail( $forum_image_size ); ?>
271 </div>
272 <?php endif; ?>
273 <div class="com-box-content">
274 <h3 class="title">
275 <a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
276 </h3>
277 <?php if ( $show_excerpt ) : ?>
278 <p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p>
279 <?php endif; ?>
280 <?php if ( $show_post_count || $show_reply_count ) : ?>
281 <div class="bbpc-forum-meta">
282 <?php if ( $show_post_count ) : ?>
283 <span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span>
284 <?php endif; ?>
285 <?php if ( $show_post_count && $show_reply_count ) : ?>
286 <span class="bbpc-meta-sep"> · </span>
287 <?php endif; ?>
288 <?php if ( $show_reply_count ) : ?>
289 <span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span>
290 <?php endif; ?>
291 </div>
292 <?php endif; ?>
293 </div>
294 </div>
295 <?php endwhile; ?>
296 <?php wp_reset_postdata(); ?>
297 </div>
298 </div>
299 </div>
300 <?php endif; ?>
301
302 <?php else : ?>
303 <div class="alert alert-warning"><?php echo esc_html( $no_posts_message ); ?></div>
304 <?php endif; ?>
305 </div>
306
307 <?php
308
309 $styles = '';
310 $id = '#' . esc_attr( $unique_id );
311
312
313 if ( $columns ) {
314 $styles .= "{$id} .communities-boxes { display: grid; grid-template-columns: repeat({$columns}, 1fr); margin: 0; width: 100%; }";
315 $styles .= "{$id} .communities-boxes .com-box { width: 100%; flex: none; margin-bottom: 0; }";
316 }
317
318 // Card gap.
319 if ( ! empty( $settings['card_gap'] ) ) {
320 $gap = absint( $settings['card_gap'] );
321 $styles .= "{$id} .communities-boxes { gap: {$gap}px !important; }";
322 }
323
324 // Card background.
325 if ( ! empty( $settings['card_bg_color'] ) ) {
326 $color = $sanitize_css( $settings['card_bg_color'] );
327 $styles .= "{$id} .com-box { background-color: {$color} !important; }";
328 }
329
330 // Card border radius.
331 if ( ! empty( $settings['card_border_radius'] ) ) {
332 $radius = absint( $settings['card_border_radius'] );
333 $styles .= "{$id} .com-box { border-radius: {$radius}px !important; }";
334 }
335
336 // Title color.
337 if ( ! empty( $settings['title_color'] ) ) {
338 $color = $sanitize_css( $settings['title_color'] );
339 $styles .= "{$id} .com-box-content .title a { color: {$color} !important; }";
340 }
341
342 // Title hover color.
343 if ( ! empty( $settings['title_hover_color'] ) ) {
344 $color = $sanitize_css( $settings['title_hover_color'] );
345 $styles .= "{$id} .com-box-content .title a:hover { color: {$color} !important; }";
346 }
347
348 // Content color.
349 if ( ! empty( $settings['content_color'] ) ) {
350 $color = $sanitize_css( $settings['content_color'] );
351 $styles .= "{$id} .com-box-content .total-post, {$id} .com-box-content .total-reply, {$id} .com-box-content .bbpc-forum-excerpt, {$id} .com-box-content .bbpc-forum-meta { color: {$color} !important; }";
352 }
353
354 // More text color.
355 if ( ! empty( $settings['more_text_color'] ) ) {
356 $color = $sanitize_css( $settings['more_text_color'] );
357 $styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }";
358 $styles .= "{$id} .more-communities .collapse-btn-wrap svg path, {$id} .more-communities .collapse-btn-wrap svg line { stroke: {$color} !important; }";
359 }
360
361 // Button color.
362 if ( ! empty( $settings['btn_color'] ) ) {
363 $color = $sanitize_css( $settings['btn_color'] );
364 $styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }";
365 }
366
367 // Button background.
368 if ( ! empty( $settings['btn_bg_color'] ) ) {
369 $color = $sanitize_css( $settings['btn_bg_color'] );
370 $styles .= "{$id} .collapse-btn-wrap { background-color: {$color} !important; }";
371 }
372
373 // Button border radius.
374 if ( ! empty( $settings['btn_border_radius'] ) ) {
375 $radius = absint( $settings['btn_border_radius'] );
376 $styles .= "{$id} .collapse-btn-wrap { border-radius: {$radius}px !important; }";
377 }
378
379 // Title font size.
380 if ( ! empty( $settings['title_font_size'] ) ) {
381 $size = absint( $settings['title_font_size'] );
382 $styles .= "{$id} .com-box-content .title a { font-size: {$size}px !important; }";
383 }
384
385 // Meta font size.
386 if ( ! empty( $settings['meta_font_size'] ) ) {
387 $size = absint( $settings['meta_font_size'] );
388 $styles .= "{$id} .com-box-content .total-post, {$id} .com-box-content .total-reply, {$id} .com-box-content .bbpc-forum-excerpt, {$id} .com-box-content .bbpc-forum-meta { font-size: {$size}px !important; }";
389 }
390
391 // Hover animation disabled.
392 if ( ! $enable_card_hover ) {
393 $styles .= "{$id} .com-box:hover { transform: none !important; box-shadow: none !important; }";
394 }
395
396 // Responsive: mobile columns.
397 if ( $mobile_columns || $hide_image_mobile ) {
398 $styles .= "@media (max-width: 768px) {";
399 if ( $mobile_columns ) {
400 $styles .= "{$id} .communities-boxes { grid-template-columns: repeat({$mobile_columns}, 1fr) !important; }";
401 $styles .= "{$id} .communities-boxes .com-box { flex: none !important; }";
402 }
403 if ( $hide_image_mobile ) {
404 $styles .= "{$id} .icon-container { display: none !important; }";
405 }
406 $styles .= "}";
407 }
408
409 // Output styles.
410 if ( ! empty( $styles ) ) {
411 echo '<style>' . $styles . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
412 }
413
414 return ob_get_clean();
415 }
416 }