| 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_FRONT_ASS . 'css/el-widgets.css' ); |
| 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 |
$unique_id = uniqid( 'bbpc-forums-' ); |
| 40 |
|
| 41 |
// Enforce free defaults when Pro is not active. |
| 42 |
$is_pro_active = function_exists( 'forumax_is_premium' ) && forumax_is_premium(); |
| 43 |
if ( ! $is_pro_active ) { |
| 44 |
$columns = 3; |
| 45 |
$mobile_columns = 1; |
| 46 |
$hide_image_mobile = false; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
$sanitize_css = function ( $value ) { |
| 51 |
return preg_match( '/^[a-zA-Z0-9#(),.\s%-]+$/', $value ) ? $value : ''; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
$query_args = array( |
| 56 |
'post_type' => 'forum', |
| 57 |
'posts_per_page' => $ppp, |
| 58 |
'order' => $order, |
| 59 |
'orderby' => $orderby, |
| 60 |
); |
| 61 |
|
| 62 |
// Filter by IDs (Pro feature). |
| 63 |
if ( $is_pro_active && ! empty( $attributes['forum_filter_ids'] ) ) { |
| 64 |
$ids = array_map( 'absint', array_filter( explode( ',', $attributes['forum_filter_ids'] ) ) ); |
| 65 |
if ( ! empty( $ids ) ) { |
| 66 |
$query_args['post__in'] = $ids; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$forums = new WP_Query( $query_args ); |
| 71 |
|
| 72 |
|
| 73 |
$query_args2 = $query_args; |
| 74 |
$query_args2['posts_per_page'] = $ppp2; |
| 75 |
$query_args2['offset'] = $ppp; |
| 76 |
$forums2 = new WP_Query( $query_args2 ); |
| 77 |
|
| 78 |
|
| 79 |
$hover_class = $enable_card_hover ? ' wow fadeInRight' : ''; |
| 80 |
|
| 81 |
|
| 82 |
$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>'; |
| 83 |
$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>'; |
| 84 |
|
| 85 |
ob_start(); |
| 86 |
?> |
| 87 |
<div id="<?php echo esc_attr( $unique_id ); ?>"> |
| 88 |
<?php if ( $forums->have_posts() ) : ?> |
| 89 |
<div class="communities-boxes"> |
| 90 |
<?php |
| 91 |
while ( $forums->have_posts() ) : |
| 92 |
$forums->the_post(); |
| 93 |
$item_id = get_the_ID(); |
| 94 |
?> |
| 95 |
<div class="com-box<?php echo esc_attr( $hover_class ); ?>" data-wow-delay="0.5s"> |
| 96 |
<?php if ( $show_forum_image ) : ?> |
| 97 |
<div class="icon-container"> |
| 98 |
<?php the_post_thumbnail( $forum_image_size ); ?> |
| 99 |
</div> |
| 100 |
<?php endif; ?> |
| 101 |
<div class="com-box-content"> |
| 102 |
<h3 class="title"> |
| 103 |
<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a> |
| 104 |
</h3> |
| 105 |
<?php if ( $show_excerpt ) : ?> |
| 106 |
<p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p> |
| 107 |
<?php endif; ?> |
| 108 |
<?php if ( $show_post_count || $show_reply_count ) : ?> |
| 109 |
<div class="bbpc-forum-meta"> |
| 110 |
<?php if ( $show_post_count ) : ?> |
| 111 |
<span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span> |
| 112 |
<?php endif; ?> |
| 113 |
<?php if ( $show_post_count && $show_reply_count ) : ?> |
| 114 |
<span class="bbpc-meta-sep"> · </span> |
| 115 |
<?php endif; ?> |
| 116 |
<?php if ( $show_reply_count ) : ?> |
| 117 |
<span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span> |
| 118 |
<?php endif; ?> |
| 119 |
</div> |
| 120 |
<?php endif; ?> |
| 121 |
</div> |
| 122 |
</div> |
| 123 |
<?php endwhile; ?> |
| 124 |
<?php wp_reset_postdata(); ?> |
| 125 |
</div> |
| 126 |
|
| 127 |
<?php if ( $is_more_btn && $forums2->have_posts() ) : ?> |
| 128 |
<div class="more-communities" data_id="<?php echo esc_attr( $unique_id ); ?>"> |
| 129 |
<a href="#more-category" class="collapse-btn-wrap" data-more="<?php echo esc_attr( $more_txt ); ?>" data-less="<?php echo esc_attr( $less_txt ); ?>"> |
| 130 |
<?php echo esc_html( $more_txt ); ?> |
| 131 |
<?php echo wp_kses_post( $svg_minus ); ?> |
| 132 |
<?php echo wp_kses_post( $svg_plus ); ?> |
| 133 |
</a> |
| 134 |
|
| 135 |
<div class="collapse-wrap" id="more-category" data_id="<?php echo esc_attr( $unique_id ); ?>"> |
| 136 |
<div class="communities-boxes"> |
| 137 |
<?php |
| 138 |
while ( $forums2->have_posts() ) : |
| 139 |
$forums2->the_post(); |
| 140 |
$item_id = get_the_ID(); |
| 141 |
?> |
| 142 |
<div class="com-box"> |
| 143 |
<?php if ( $show_forum_image ) : ?> |
| 144 |
<div class="icon-container"> |
| 145 |
<?php the_post_thumbnail( $forum_image_size ); ?> |
| 146 |
</div> |
| 147 |
<?php endif; ?> |
| 148 |
<div class="com-box-content"> |
| 149 |
<h3 class="title"> |
| 150 |
<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a> |
| 151 |
</h3> |
| 152 |
<?php if ( $show_excerpt ) : ?> |
| 153 |
<p class="bbpc-forum-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?></p> |
| 154 |
<?php endif; ?> |
| 155 |
<?php if ( $show_post_count || $show_reply_count ) : ?> |
| 156 |
<div class="bbpc-forum-meta"> |
| 157 |
<?php if ( $show_post_count ) : ?> |
| 158 |
<span class="total-post"><?php bbp_forum_topic_count( $item_id ); ?> <?php esc_html_e( 'Posts', 'bbp-core' ); ?></span> |
| 159 |
<?php endif; ?> |
| 160 |
<?php if ( $show_post_count && $show_reply_count ) : ?> |
| 161 |
<span class="bbpc-meta-sep"> · </span> |
| 162 |
<?php endif; ?> |
| 163 |
<?php if ( $show_reply_count ) : ?> |
| 164 |
<span class="total-reply"><?php bbp_forum_reply_count( $item_id ); ?> <?php esc_html_e( 'Replies', 'bbp-core' ); ?></span> |
| 165 |
<?php endif; ?> |
| 166 |
</div> |
| 167 |
<?php endif; ?> |
| 168 |
</div> |
| 169 |
</div> |
| 170 |
<?php endwhile; ?> |
| 171 |
<?php wp_reset_postdata(); ?> |
| 172 |
</div> |
| 173 |
</div> |
| 174 |
</div> |
| 175 |
<?php endif; ?> |
| 176 |
|
| 177 |
<?php else : ?> |
| 178 |
<div class="alert alert-warning"><?php echo esc_html( $no_posts_message ); ?></div> |
| 179 |
<?php endif; ?> |
| 180 |
</div> |
| 181 |
|
| 182 |
<?php |
| 183 |
|
| 184 |
$styles = ''; |
| 185 |
$id = '#' . esc_attr( $unique_id ); |
| 186 |
|
| 187 |
|
| 188 |
if ( $columns ) { |
| 189 |
$styles .= "{$id} .communities-boxes { display: grid !important; grid-template-columns: repeat({$columns}, 1fr) !important; margin: 0 !important; width: 100% !important; }"; |
| 190 |
$styles .= "{$id} .communities-boxes .com-box { width: 100% !important; flex: none !important; margin-bottom: 0 !important; }"; |
| 191 |
} |
| 192 |
|
| 193 |
// Card gap. |
| 194 |
if ( ! empty( $settings['card_gap'] ) ) { |
| 195 |
$gap = absint( $settings['card_gap'] ); |
| 196 |
$styles .= "{$id} .communities-boxes { gap: {$gap}px !important; }"; |
| 197 |
} |
| 198 |
|
| 199 |
// Card background. |
| 200 |
if ( ! empty( $settings['card_bg_color'] ) ) { |
| 201 |
$color = $sanitize_css( $settings['card_bg_color'] ); |
| 202 |
$styles .= "{$id} .com-box { background-color: {$color} !important; }"; |
| 203 |
} |
| 204 |
|
| 205 |
// Card border radius. |
| 206 |
if ( ! empty( $settings['card_border_radius'] ) ) { |
| 207 |
$radius = absint( $settings['card_border_radius'] ); |
| 208 |
$styles .= "{$id} .com-box { border-radius: {$radius}px !important; }"; |
| 209 |
} |
| 210 |
|
| 211 |
// Title color. |
| 212 |
if ( ! empty( $settings['title_color'] ) ) { |
| 213 |
$color = $sanitize_css( $settings['title_color'] ); |
| 214 |
$styles .= "{$id} .com-box-content .title a { color: {$color} !important; }"; |
| 215 |
} |
| 216 |
|
| 217 |
// Title hover color. |
| 218 |
if ( ! empty( $settings['title_hover_color'] ) ) { |
| 219 |
$color = $sanitize_css( $settings['title_hover_color'] ); |
| 220 |
$styles .= "{$id} .com-box-content .title a:hover { color: {$color} !important; }"; |
| 221 |
} |
| 222 |
|
| 223 |
// Content color. |
| 224 |
if ( ! empty( $settings['content_color'] ) ) { |
| 225 |
$color = $sanitize_css( $settings['content_color'] ); |
| 226 |
$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; }"; |
| 227 |
} |
| 228 |
|
| 229 |
// More text color. |
| 230 |
if ( ! empty( $settings['more_text_color'] ) ) { |
| 231 |
$color = $sanitize_css( $settings['more_text_color'] ); |
| 232 |
$styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }"; |
| 233 |
$styles .= "{$id} .more-communities .collapse-btn-wrap svg path, {$id} .more-communities .collapse-btn-wrap svg line { stroke: {$color} !important; }"; |
| 234 |
} |
| 235 |
|
| 236 |
// Button color. |
| 237 |
if ( ! empty( $settings['btn_color'] ) ) { |
| 238 |
$color = $sanitize_css( $settings['btn_color'] ); |
| 239 |
$styles .= "{$id} .collapse-btn-wrap { color: {$color} !important; }"; |
| 240 |
} |
| 241 |
|
| 242 |
// Button background. |
| 243 |
if ( ! empty( $settings['btn_bg_color'] ) ) { |
| 244 |
$color = $sanitize_css( $settings['btn_bg_color'] ); |
| 245 |
$styles .= "{$id} .collapse-btn-wrap { background-color: {$color} !important; }"; |
| 246 |
} |
| 247 |
|
| 248 |
// Button border radius. |
| 249 |
if ( ! empty( $settings['btn_border_radius'] ) ) { |
| 250 |
$radius = absint( $settings['btn_border_radius'] ); |
| 251 |
$styles .= "{$id} .collapse-btn-wrap { border-radius: {$radius}px !important; }"; |
| 252 |
} |
| 253 |
|
| 254 |
// Title font size. |
| 255 |
if ( ! empty( $settings['title_font_size'] ) ) { |
| 256 |
$size = absint( $settings['title_font_size'] ); |
| 257 |
$styles .= "{$id} .com-box-content .title a { font-size: {$size}px !important; }"; |
| 258 |
} |
| 259 |
|
| 260 |
// Meta font size. |
| 261 |
if ( ! empty( $settings['meta_font_size'] ) ) { |
| 262 |
$size = absint( $settings['meta_font_size'] ); |
| 263 |
$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; }"; |
| 264 |
} |
| 265 |
|
| 266 |
// Hover animation disabled. |
| 267 |
if ( ! $enable_card_hover ) { |
| 268 |
$styles .= "{$id} .com-box:hover { transform: none !important; box-shadow: none !important; }"; |
| 269 |
} |
| 270 |
|
| 271 |
// Responsive: mobile columns. |
| 272 |
if ( $mobile_columns || $hide_image_mobile ) { |
| 273 |
$styles .= "@media (max-width: 768px) {"; |
| 274 |
if ( $mobile_columns ) { |
| 275 |
$styles .= "{$id} .communities-boxes { grid-template-columns: repeat({$mobile_columns}, 1fr) !important; }"; |
| 276 |
$styles .= "{$id} .communities-boxes .com-box { flex: none !important; }"; |
| 277 |
} |
| 278 |
if ( $hide_image_mobile ) { |
| 279 |
$styles .= "{$id} .icon-container { display: none !important; }"; |
| 280 |
} |
| 281 |
$styles .= "}"; |
| 282 |
} |
| 283 |
|
| 284 |
// Output styles. |
| 285 |
if ( ! empty( $styles ) ) { |
| 286 |
echo '<style>' . $styles . '</style>'; |
| 287 |
} |
| 288 |
|
| 289 |
return ob_get_clean(); |
| 290 |
} |
| 291 |
} |