| 1 |
<?php |
| 2 |
namespace admin\Blocks\Render; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
class Single_Forum { |
| 7 |
/** |
| 8 |
* Render the single forum Gutenberg block. |
| 9 |
* |
| 10 |
* Supports 4 style variations, visibility toggles, typography, |
| 11 |
* spacing, color controls, responsive mobile settings, and columns. |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block content. |
| 15 |
* @return string Rendered block HTML. |
| 16 |
*/ |
| 17 |
public function render_single_forum($attributes, $content) |
| 18 |
{ |
| 19 |
// Helper to sanitize CSS color values (hex and rgba). |
| 20 |
$sanitize_css_color = function ($color) { |
| 21 |
$color = trim((string) $color); |
| 22 |
if (empty($color)) { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
if (preg_match('/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/', $color)) { |
| 26 |
return $color; |
| 27 |
} |
| 28 |
if (preg_match('/^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+(?:\s*,\s*(0|1|0?\.\d+))?\s*\)$/', $color)) { |
| 29 |
return $color; |
| 30 |
} |
| 31 |
return ''; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
$settings = $attributes; |
| 36 |
$settings['style'] = ! empty( $attributes['style'] ) ? $attributes['style'] : '1'; |
| 37 |
|
| 38 |
// Enforce free defaults: styles 3 and 4 are Pro-only. |
| 39 |
$is_pro_active = function_exists( 'forumax_is_premium' ) && forumax_is_premium(); |
| 40 |
if ( ! $is_pro_active && in_array( $settings['style'], array( '3', '4' ), true ) ) { |
| 41 |
$settings['style'] = '1'; |
| 42 |
} |
| 43 |
$settings['ppp'] = ! empty( $attributes['ppp'] ) ? $attributes['ppp'] : 3; |
| 44 |
$settings['order'] = ! empty( $attributes['order'] ) ? $attributes['order'] : 'ASC'; |
| 45 |
$settings['word_length'] = ! empty( $attributes['word_length'] ) ? $attributes['word_length'] : 12; |
| 46 |
$settings['read_more'] = ! empty( $attributes['read_more'] ) ? $attributes['read_more'] : 'View All'; |
| 47 |
$settings['columns'] = ! empty( $attributes['columns'] ) ? (int) $attributes['columns'] : 1; |
| 48 |
|
| 49 |
// Visibility toggles (default true). |
| 50 |
$settings['show_voice'] = isset( $attributes['show_voice'] ) ? (bool) $attributes['show_voice'] : true; |
| 51 |
$settings['show_replies'] = isset( $attributes['show_replies'] ) ? (bool) $attributes['show_replies'] : true; |
| 52 |
$settings['show_freshness'] = isset( $attributes['show_freshness'] ) ? (bool) $attributes['show_freshness'] : true; |
| 53 |
$settings['show_excerpt'] = isset( $attributes['show_excerpt'] ) ? (bool) $attributes['show_excerpt'] : true; |
| 54 |
$settings['show_view_all'] = isset( $attributes['show_view_all'] ) ? (bool) $attributes['show_view_all'] : true; |
| 55 |
$settings['show_cover_badges'] = isset( $attributes['show_cover_badges'] ) ? (bool) $attributes['show_cover_badges'] : true; |
| 56 |
|
| 57 |
// Enforce visibility and responsive defaults when Pro is not active. |
| 58 |
if ( ! $is_pro_active ) { |
| 59 |
$settings['show_voice'] = true; |
| 60 |
$settings['show_replies'] = true; |
| 61 |
$settings['show_freshness'] = true; |
| 62 |
$settings['show_excerpt'] = true; |
| 63 |
$settings['show_view_all'] = true; |
| 64 |
$settings['show_cover_badges'] = true; |
| 65 |
} |
| 66 |
|
| 67 |
// Spacing. |
| 68 |
$settings['row_padding'] = isset( $attributes['row_padding'] ) ? (int) $attributes['row_padding'] : 16; |
| 69 |
$settings['row_gap'] = isset( $attributes['row_gap'] ) ? (int) $attributes['row_gap'] : 10; |
| 70 |
$settings['cover_height'] = isset( $attributes['cover_height'] ) ? (int) $attributes['cover_height'] : 250; |
| 71 |
$settings['border_radius'] = isset( $attributes['border_radius'] ) ? (int) $attributes['border_radius'] : 8; |
| 72 |
|
| 73 |
// Typography. |
| 74 |
$settings['title_font_size'] = ! empty( $attributes['title_font_size'] ) ? (int) $attributes['title_font_size'] : ''; |
| 75 |
$settings['meta_font_size'] = ! empty( $attributes['meta_font_size'] ) ? (int) $attributes['meta_font_size'] : ''; |
| 76 |
$settings['title_font_weight'] = ! empty( $attributes['title_font_weight'] ) ? $attributes['title_font_weight'] : ''; |
| 77 |
|
| 78 |
// Cover overlay. |
| 79 |
$settings['cover_overlay_color'] = ! empty( $attributes['cover_overlay_color'] ) |
| 80 |
? $sanitize_css_color( $attributes['cover_overlay_color'] ) |
| 81 |
: 'rgba(28, 38, 56, 0.5)'; |
| 82 |
|
| 83 |
// Responsive. |
| 84 |
$settings['hide_voice_mobile'] = isset( $attributes['hide_voice_mobile'] ) ? (bool) $attributes['hide_voice_mobile'] : true; |
| 85 |
$settings['hide_replies_mobile'] = isset( $attributes['hide_replies_mobile'] ) ? (bool) $attributes['hide_replies_mobile'] : false; |
| 86 |
$settings['hide_user_mobile'] = isset( $attributes['hide_user_mobile'] ) ? (bool) $attributes['hide_user_mobile'] : true; |
| 87 |
|
| 88 |
// Re-enforce responsive defaults for free users. |
| 89 |
if ( ! $is_pro_active ) { |
| 90 |
$settings['hide_voice_mobile'] = true; |
| 91 |
$settings['hide_replies_mobile'] = false; |
| 92 |
$settings['hide_user_mobile'] = true; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
$title_color = $sanitize_css_color( ! empty( $attributes['title_color'] ) ? $attributes['title_color'] : '' ); |
| 97 |
$title_hover_color = $sanitize_css_color( ! empty( $attributes['title_hover_color'] ) ? $attributes['title_hover_color'] : '' ); |
| 98 |
$excerpt_color = $sanitize_css_color( ! empty( $attributes['excerpt_color'] ) ? $attributes['excerpt_color'] : '' ); |
| 99 |
$meta_color = $sanitize_css_color( ! empty( $attributes['meta_color'] ) ? $attributes['meta_color'] : '' ); |
| 100 |
$bg_color = $sanitize_css_color( ! empty( $attributes['bg_color'] ) ? $attributes['bg_color'] : '' ); |
| 101 |
|
| 102 |
|
| 103 |
$forum_id = ! empty( $settings['forum_id'] ) ? $settings['forum_id'] : ''; |
| 104 |
|
| 105 |
$cover_image = isset( $settings['cover_image'] ) ? $settings['cover_image'] : ''; |
| 106 |
$url = ''; |
| 107 |
if ( is_array( $cover_image ) && isset( $cover_image['url'] ) ) { |
| 108 |
$url = $cover_image['url']; |
| 109 |
} elseif ( is_object( $cover_image ) && isset( $cover_image->url ) ) { |
| 110 |
$url = $cover_image->url; |
| 111 |
} |
| 112 |
$post_thumbnail_url = ! empty( $url ) ? $url : get_the_post_thumbnail_url( $forum_id ); |
| 113 |
|
| 114 |
$topics = new \WP_Query( array( |
| 115 |
'post_type' => bbp_get_topic_post_type(), |
| 116 |
'order' => $settings['order'], |
| 117 |
'posts_per_page' => $settings['ppp'], |
| 118 |
'post_parent' => $forum_id, |
| 119 |
) ); |
| 120 |
|
| 121 |
$unique_id = uniqid( 'bbpc-single-forum-' ); |
| 122 |
|
| 123 |
|
| 124 |
if ( ! wp_style_is( 'bbpc-el-widgets', 'registered' ) ) { |
| 125 |
wp_register_style( 'bbpc-el-widgets', FORUMAX_STYLES . 'frontend/el-widgets.css', array(), FORUMAX_VERSION ); |
| 126 |
} |
| 127 |
wp_enqueue_style( 'bbpc-el-widgets' ); |
| 128 |
|
| 129 |
|
| 130 |
ob_start(); |
| 131 |
echo '<div id="' . esc_attr( $unique_id ) . '" class="bbpc-single-forum-block">'; |
| 132 |
|
| 133 |
if ( $forum_id ) { |
| 134 |
$style_num = in_array( $settings['style'], array( '1', '2', '3', '4' ), true ) ? $settings['style'] : '1'; |
| 135 |
$template_path = __DIR__ . '/../../Elementor/inc/single-forum/single-forum-' . $style_num . '.php'; |
| 136 |
if ( file_exists( $template_path ) ) { |
| 137 |
include $template_path; |
| 138 |
} |
| 139 |
} else { |
| 140 |
?> |
| 141 |
<div class="bbpc-sf-empty-placeholder" style="border:2px dashed #ccc;border-radius:8px;padding:40px 20px;text-align:center;color:#888;"> |
| 142 |
<span class="dashicons dashicons-admin-post" style="font-size:48px;width:48px;height:48px;margin-bottom:10px;display:block;margin-left:auto;margin-right:auto;"></span> |
| 143 |
<p style="font-size:15px;margin:0;"><?php esc_html_e( 'Please select a forum from the block settings.', 'forumax' ); ?></p> |
| 144 |
</div> |
| 145 |
<?php |
| 146 |
} |
| 147 |
|
| 148 |
echo '</div>'; |
| 149 |
|
| 150 |
|
| 151 |
$row_padding = (int) $settings['row_padding']; |
| 152 |
$row_gap = (int) $settings['row_gap']; |
| 153 |
$cover_height = (int) $settings['cover_height']; |
| 154 |
$border_radius = (int) $settings['border_radius']; |
| 155 |
?> |
| 156 |
<style> |
| 157 |
|
| 158 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents { |
| 159 |
padding: <?php echo esc_attr( $row_padding ); ?>px 30px !important; |
| 160 |
margin: <?php echo esc_attr( $row_gap ); ?>px 0 !important; |
| 161 |
border-radius: <?php echo esc_attr( $border_radius ); ?>px !important; |
| 162 |
} |
| 163 |
#<?php echo esc_attr( $unique_id ); ?> .forum-thumbnail { |
| 164 |
min-height: <?php echo esc_attr( $cover_height ); ?>px !important; |
| 165 |
} |
| 166 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card { |
| 167 |
border-radius: <?php echo esc_attr( $border_radius ); ?>px !important; |
| 168 |
overflow: hidden; |
| 169 |
} |
| 170 |
#<?php echo esc_attr( $unique_id ); ?> .forum-thumbnail { |
| 171 |
border-radius: <?php echo esc_attr( $border_radius ); ?>px <?php echo esc_attr( $border_radius ); ?>px 0 0 !important; |
| 172 |
} |
| 173 |
<?php if ( $settings['style'] === '3' ) : ?> |
| 174 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item { |
| 175 |
padding: <?php echo esc_attr( $row_padding ); ?>px 20px !important; |
| 176 |
margin-bottom: <?php echo esc_attr( $row_gap ); ?>px !important; |
| 177 |
border-radius: <?php echo esc_attr( $border_radius ); ?>px !important; |
| 178 |
} |
| 179 |
<?php endif; ?> |
| 180 |
<?php if ( $settings['style'] === '4' ) : ?> |
| 181 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid { |
| 182 |
border-radius: <?php echo esc_attr( $border_radius ); ?>px !important; |
| 183 |
} |
| 184 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid-topic { |
| 185 |
padding: <?php echo esc_attr( $row_padding ); ?>px 24px !important; |
| 186 |
} |
| 187 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid-info { |
| 188 |
min-height: <?php echo esc_attr( $cover_height ); ?>px !important; |
| 189 |
} |
| 190 |
<?php endif; ?> |
| 191 |
|
| 192 |
|
| 193 |
<?php if ( $title_color ) : ?> |
| 194 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3, |
| 195 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3 a, |
| 196 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3, |
| 197 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3 a, |
| 198 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title a, |
| 199 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 a { |
| 200 |
color: <?php echo esc_attr( $title_color ); ?> !important; |
| 201 |
} |
| 202 |
<?php endif; ?> |
| 203 |
|
| 204 |
<?php if ( $title_hover_color ) : ?> |
| 205 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3:hover, |
| 206 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3 a:hover, |
| 207 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title h3:hover a, |
| 208 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3:hover, |
| 209 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3 a:hover, |
| 210 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3:hover a, |
| 211 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title a:hover, |
| 212 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3:hover, |
| 213 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 a:hover, |
| 214 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3:hover a { |
| 215 |
color: <?php echo esc_attr( $title_hover_color ); ?> !important; |
| 216 |
} |
| 217 |
<?php endif; ?> |
| 218 |
|
| 219 |
<?php if ( $excerpt_color ) : ?> |
| 220 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents .title p, |
| 221 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-body, |
| 222 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-excerpt, |
| 223 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic p { |
| 224 |
color: <?php echo esc_attr( $excerpt_color ); ?> !important; |
| 225 |
} |
| 226 |
<?php endif; ?> |
| 227 |
|
| 228 |
<?php if ( $meta_color ) : ?> |
| 229 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice, |
| 230 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics, |
| 231 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date, |
| 232 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date a, |
| 233 |
#<?php echo esc_attr( $unique_id ); ?> .topic-heading .icon-text, |
| 234 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-meta span, |
| 235 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-meta span { |
| 236 |
color: <?php echo esc_attr( $meta_color ); ?> !important; |
| 237 |
} |
| 238 |
<?php endif; ?> |
| 239 |
|
| 240 |
<?php if ( $bg_color ) : ?> |
| 241 |
#<?php echo esc_attr( $unique_id ); ?> .forum-with-topics .topic-table .topic-contents, |
| 242 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-body, |
| 243 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item, |
| 244 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic { |
| 245 |
background-color: <?php echo esc_attr( $bg_color ); ?> !important; |
| 246 |
} |
| 247 |
<?php endif; ?> |
| 248 |
|
| 249 |
|
| 250 |
<?php if ( $settings['title_font_size'] ) : ?> |
| 251 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3, |
| 252 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3 a, |
| 253 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3, |
| 254 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title, |
| 255 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 { |
| 256 |
font-size: <?php echo esc_attr( $settings['title_font_size'] ); ?>px !important; |
| 257 |
} |
| 258 |
<?php endif; ?> |
| 259 |
|
| 260 |
<?php if ( $settings['title_font_weight'] ) : ?> |
| 261 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .title h3, |
| 262 |
#<?php echo esc_attr( $unique_id ); ?> .forum-card .card-title h3, |
| 263 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-title, |
| 264 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-topic h3 { |
| 265 |
font-weight: <?php echo esc_attr( $settings['title_font_weight'] ); ?> !important; |
| 266 |
} |
| 267 |
<?php endif; ?> |
| 268 |
|
| 269 |
<?php if ( $settings['meta_font_size'] ) : ?> |
| 270 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice, |
| 271 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics, |
| 272 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user .user-info .date, |
| 273 |
#<?php echo esc_attr( $unique_id ); ?> .topic-heading .icon-text, |
| 274 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-list-item .bbpc-sf-list-meta, |
| 275 |
#<?php echo esc_attr( $unique_id ); ?> .bbpc-sf-grid .bbpc-sf-grid-meta { |
| 276 |
font-size: <?php echo esc_attr( $settings['meta_font_size'] ); ?>px !important; |
| 277 |
} |
| 278 |
<?php endif; ?> |
| 279 |
|
| 280 |
|
| 281 |
@media all and (max-width: 560px) { |
| 282 |
<?php if ( $settings['hide_voice_mobile'] ) : ?> |
| 283 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .voice, |
| 284 |
#<?php echo esc_attr( $unique_id ); ?> .topic-heading .voice { |
| 285 |
display: none !important; |
| 286 |
} |
| 287 |
<?php endif; ?> |
| 288 |
<?php if ( $settings['hide_replies_mobile'] ) : ?> |
| 289 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .topics, |
| 290 |
#<?php echo esc_attr( $unique_id ); ?> .topic-heading .replies { |
| 291 |
display: none !important; |
| 292 |
} |
| 293 |
<?php endif; ?> |
| 294 |
<?php if ( $settings['hide_user_mobile'] ) : ?> |
| 295 |
#<?php echo esc_attr( $unique_id ); ?> .topic-contents .user, |
| 296 |
#<?php echo esc_attr( $unique_id ); ?> .topic-heading .freshness { |
| 297 |
display: none !important; |
| 298 |
} |
| 299 |
<?php endif; ?> |
| 300 |
} |
| 301 |
</style> |
| 302 |
<?php |
| 303 |
|
| 304 |
return ob_get_clean(); |
| 305 |
} |
| 306 |
} |