| 1 |
<?php |
| 2 |
/** |
| 3 |
* Forumax theme helper functions and resources |
| 4 |
*/ |
| 5 |
if ( !class_exists( 'Forumax_Forum_Class' ) ) { |
| 6 |
class Forumax_Forum_Class { |
| 7 |
/** |
| 8 |
* Hold an instance of Forumax_Forum_Class class. |
| 9 |
* @var Forumax_Forum_Class |
| 10 |
*/ |
| 11 |
protected static $instance = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Main Forumax_Forum_Class instance. |
| 15 |
* @return Forumax_Forum_Class - Main instance. |
| 16 |
*/ |
| 17 |
public static function instance() { |
| 18 |
|
| 19 |
if ( null == self::$instance ) { |
| 20 |
self::$instance = new Forumax_Forum_Class(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
/** |
| 26 |
* Adds a reference of this object to $instance, populates default strings, |
| 27 |
* @see __construct |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Authors Name |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function forum_topics_authors() { |
| 39 |
global $wpdb; |
| 40 |
$d_users = []; |
| 41 |
$forumusers = get_users(); |
| 42 |
|
| 43 |
if ( $forumusers ) { |
| 44 |
foreach ( $forumusers as $forumuser ) { |
| 45 |
$post_count = count_user_posts( $forumuser->ID, 'topic' ); |
| 46 |
$d_users[$forumuser->ID] = $post_count; |
| 47 |
} |
| 48 |
arsort( $d_users ); |
| 49 |
$i = 0; |
| 50 |
foreach ( $d_users as $key => $value ) { |
| 51 |
$i++; |
| 52 |
|
| 53 |
$user = get_userdata( $key ); |
| 54 |
$parent_id = get_queried_object_id(); |
| 55 |
//$users_post_count = count_user_posts( $user->ID, 'topic' ); |
| 56 |
$users_post_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $user->ID . "' AND post_type = 'topic' AND post_status = 'publish' AND post_parent = '" . $parent_id . "' " ); |
| 57 |
$author_posts_url = get_author_posts_url( $key ); |
| 58 |
$post_count = $users_post_count; |
| 59 |
if ( $post_count > 0 ) { |
| 60 |
echo '<div class="userlist">'; |
| 61 |
echo '<a class="dropdown-item current-user forumax-data data-auth" data-nonce="' . wp_create_nonce( 'forumax-nonce' ) . '" data-rel="' . $user->display_name . '" data-type="author" data-count="' . $post_count . '" data-parent="' . get_queried_object_id() . '" data-id="' . intval( $user->ID ) . '" href="#' . $user->display_name . '"> |
| 62 |
<img src="' . get_avatar_url( $user->ID ) . '" alt="' . $user->display_name . '">' . $user->display_name . '<span class="count">' . $post_count . '</span> |
| 63 |
</a>'; |
| 64 |
echo '</div>'; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Tag Name |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public static function forum_topics_tags() { |
| 76 |
$args = [ |
| 77 |
'post_type' => 'topic', |
| 78 |
'post_parent' => get_queried_object_id(), |
| 79 |
'post_status' => 'publish', |
| 80 |
'order' => 'ASC', |
| 81 |
'orderby' => 'ID', |
| 82 |
'posts_per_page' => -1, |
| 83 |
]; |
| 84 |
$forumaxtags = new WP_Query( $args ); |
| 85 |
|
| 86 |
if ( $forumaxtags->have_posts() ): |
| 87 |
$i=1; |
| 88 |
while ( $forumaxtags->have_posts() ): $forumaxtags->the_post(); |
| 89 |
global $post; |
| 90 |
$get_tags = get_the_terms( get_the_ID(), 'topic-tag' ); |
| 91 |
if ( $get_tags ) { |
| 92 |
foreach ( $get_tags as $tag ) { |
| 93 |
$post_count = $tag->count; |
| 94 |
echo '<div class="tagList" id="'.$tag->slug.'">'; |
| 95 |
echo '<a class="dropdown-item forumax-data data-tag" data-parent="'.get_queried_object_id().'" data-nonce="'.wp_create_nonce('forumax-nonce').'" data-rel="'.$tag->slug.'" data-type="tag" data-count="' . $post_count . '" data-id="' . intval( $tag->term_id ) . '" href="#' . esc_attr( $tag->slug ) . '"><span class="color-purple"></span>' . $tag->name . '</a>'; |
| 96 |
echo '</div>'; |
| 97 |
} |
| 98 |
} |
| 99 |
$i++; |
| 100 |
endwhile; |
| 101 |
|
| 102 |
endif; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Tag Name |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public static function forum_topics_open_close(): void |
| 111 |
{ |
| 112 |
$is_queried_obj = is_singular('forum') ? get_queried_object_id() : false; |
| 113 |
$user_id = is_singular('forum') ? false : bbp_get_displayed_user_field('ID'); |
| 114 |
|
| 115 |
// Get the active tab from the URL or default to 'open' |
| 116 |
$active_tab = isset($_GET['tab']) && in_array($_GET['tab'], ['open', 'closed']) ? sanitize_text_field($_GET['tab']) : 'open'; |
| 117 |
|
| 118 |
// Query for Open Topics Count |
| 119 |
$open_args = [ |
| 120 |
'post_type' => 'topic', |
| 121 |
'post_parent' => $is_queried_obj, |
| 122 |
'post_status' => 'publish', |
| 123 |
'posts_per_page' => -1, // No pagination for count |
| 124 |
]; |
| 125 |
$open_query = new WP_Query($open_args); |
| 126 |
$open_count = $open_query->found_posts; |
| 127 |
|
| 128 |
// Query for Closed Topics Count |
| 129 |
$closed_args = [ |
| 130 |
'post_type' => 'topic', |
| 131 |
'post_parent' => $is_queried_obj, |
| 132 |
'post_status' => 'closed', |
| 133 |
'posts_per_page' => -1, |
| 134 |
]; |
| 135 |
$closed_query = new WP_Query($closed_args); |
| 136 |
$closed_count = $closed_query->found_posts; |
| 137 |
|
| 138 |
if ( in_array('bbp-user-page', get_body_class()) ) : |
| 139 |
?> |
| 140 |
<ul class="support-total-info"> |
| 141 |
<li class="open-ticket"> |
| 142 |
<?php esc_html_e( 'TOPIC', 'forumax' ); ?> |
| 143 |
</li> |
| 144 |
</ul> |
| 145 |
<ul class="category-menu"> |
| 146 |
<li> <?php esc_html_e( 'Comments', 'forumax' ); ?> </li> |
| 147 |
<li> <?php esc_html_e( 'Favorites', 'forumax' ); ?> </li> |
| 148 |
</ul> |
| 149 |
<?php |
| 150 |
else : |
| 151 |
?> |
| 152 |
<ul class="support-total-info"> |
| 153 |
<li class="open-ticket <?php echo $active_tab === 'open' ? 'active' : ''; ?>"> |
| 154 |
<i class="icon_info_alt"></i> |
| 155 |
<a href="?tab=open" |
| 156 |
class="open-data" |
| 157 |
data-nonce="<?php echo wp_create_nonce( 'forumax-nonce' );?>" |
| 158 |
data-type="open" |
| 159 |
data-userid="<?php echo esc_attr($user_id); ?>" |
| 160 |
data-parent="<?php echo esc_attr($is_queried_obj); ?>"> |
| 161 |
<?php echo esc_html($open_count) . esc_html__(' Open', 'forumax'); ?> |
| 162 |
</a> |
| 163 |
</li> |
| 164 |
<li class="close-ticket <?php echo $active_tab === 'closed' ? 'active' : ''; ?>"> |
| 165 |
<i class="icon_check"></i> |
| 166 |
<a href="?tab=closed" |
| 167 |
class="open-data" |
| 168 |
data-nonce="<?php echo wp_create_nonce( 'forumax-nonce' );?>" |
| 169 |
data-type="closed" |
| 170 |
data-parent="<?php echo esc_attr($is_queried_obj); ?>" |
| 171 |
data-userid="<?php echo esc_attr($user_id); ?>"> |
| 172 |
<?php echo esc_html($closed_count) . esc_html__(' Closed', 'forumax'); ?> |
| 173 |
</a> |
| 174 |
</li> |
| 175 |
</ul> |
| 176 |
<?php |
| 177 |
endif; |
| 178 |
} |
| 179 |
|
| 180 |
function topic_badges() { |
| 181 |
if( bbp_is_topic_sticky(get_the_ID()) ) { ?> |
| 182 |
<span class="badge badge-success"> |
| 183 |
<?php esc_html_e( 'Sticky', 'forumax' ); ?> |
| 184 |
</span> |
| 185 |
<?php } |
| 186 |
if ( bbp_is_topic_closed( get_the_ID() ) ) { ?> |
| 187 |
<span class="badge badge-danger"> |
| 188 |
<?php esc_html_e( 'Closed', 'forumax' ); ?> |
| 189 |
</span> |
| 190 |
<?php } |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get Forumax Forum Class instance |
| 197 |
* Works with or without Forumax theme active |
| 198 |
* |
| 199 |
* @return Forumax_Forum_Class|null |
| 200 |
*/ |
| 201 |
if ( ! function_exists( 'Forumax_Forum' ) ) { |
| 202 |
function Forumax_Forum() { |
| 203 |
if ( class_exists( 'Forumax_Forum_Class' ) ) { |
| 204 |
return Forumax_Forum_Class::instance(); |
| 205 |
} |
| 206 |
return null; |
| 207 |
} |
| 208 |
} |