| 1 |
<?php |
| 2 |
// Handle 'Agree' vote for logged-in users |
| 3 |
function forumax_handle_agree() { |
| 4 |
check_ajax_referer('forumax-nonce', 'nonce'); |
| 5 |
|
| 6 |
$topic_id = intval($_POST['topic_id']); |
| 7 |
$user_id = get_current_user_id(); |
| 8 |
|
| 9 |
if ( ! $user_id ) { |
| 10 |
wp_send_json_error('User not logged in.'); |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
// Check if the user has already voted |
| 15 |
$user_vote = get_user_meta($user_id, '_bbpc_vote_' . $topic_id, true); |
| 16 |
|
| 17 |
if ($user_vote === 'agree') { |
| 18 |
// If user has already agreed, remove the agree vote |
| 19 |
$agree_count = get_post_meta($topic_id, '_bbpc_agree_count', true) ?: 0; |
| 20 |
update_post_meta($topic_id, '_bbpc_agree_count', --$agree_count); |
| 21 |
|
| 22 |
// Remove the vote tracking |
| 23 |
delete_user_meta($user_id, '_bbpc_vote_' . $topic_id); |
| 24 |
} else { |
| 25 |
// If user had previously disagreed, remove 'disagree' first |
| 26 |
if ($user_vote === 'disagree') { |
| 27 |
$disagree_count = get_post_meta($topic_id, '_bbpc_disagree_count', true) ?: 0; |
| 28 |
update_post_meta($topic_id, '_bbpc_disagree_count', --$disagree_count); |
| 29 |
} |
| 30 |
|
| 31 |
// Add agree vote |
| 32 |
$agree_count = get_post_meta($topic_id, '_bbpc_agree_count', true) ?: 0; |
| 33 |
update_post_meta($topic_id, '_bbpc_agree_count', ++$agree_count); |
| 34 |
|
| 35 |
// Track the new vote as 'agree' |
| 36 |
update_user_meta($user_id, '_bbpc_vote_' . $topic_id, 'agree'); |
| 37 |
} |
| 38 |
|
| 39 |
wp_send_json_success(array( |
| 40 |
'agree_count' => get_post_meta($topic_id, '_bbpc_agree_count', true), |
| 41 |
'disagree_count' => get_post_meta($topic_id, '_bbpc_disagree_count', true) |
| 42 |
)); |
| 43 |
} |
| 44 |
add_action('wp_ajax_bbpc_agree', 'forumax_handle_agree'); |
| 45 |
|
| 46 |
|
| 47 |
// Handle 'Disagree' vote for logged-in users |
| 48 |
function forumax_handle_disagree() { |
| 49 |
check_ajax_referer('forumax-nonce', 'nonce'); |
| 50 |
|
| 51 |
$topic_id = intval($_POST['topic_id']); |
| 52 |
$user_id = get_current_user_id(); |
| 53 |
|
| 54 |
if (!$user_id) { |
| 55 |
wp_send_json_error('User not logged in.'); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Check if the user has already voted |
| 60 |
$user_vote = get_user_meta($user_id, '_bbpc_vote_' . $topic_id, true); |
| 61 |
|
| 62 |
if ($user_vote === 'disagree') { |
| 63 |
// If user has already disagreed, remove the disagree vote |
| 64 |
$disagree_count = get_post_meta($topic_id, '_bbpc_disagree_count', true) ?: 0; |
| 65 |
update_post_meta($topic_id, '_bbpc_disagree_count', --$disagree_count); |
| 66 |
|
| 67 |
// Remove the vote tracking |
| 68 |
delete_user_meta($user_id, '_bbpc_vote_' . $topic_id); |
| 69 |
} else { |
| 70 |
// If user had previously agreed, remove 'agree' first |
| 71 |
if ($user_vote === 'agree') { |
| 72 |
$agree_count = get_post_meta($topic_id, '_bbpc_agree_count', true) ?: 0; |
| 73 |
update_post_meta($topic_id, '_bbpc_agree_count', --$agree_count); |
| 74 |
} |
| 75 |
|
| 76 |
// Add disagree vote |
| 77 |
$disagree_count = get_post_meta($topic_id, '_bbpc_disagree_count', true) ?: 0; |
| 78 |
update_post_meta($topic_id, '_bbpc_disagree_count', ++$disagree_count); |
| 79 |
|
| 80 |
// Track the new vote as 'disagree' |
| 81 |
update_user_meta($user_id, '_bbpc_vote_' . $topic_id, 'disagree'); |
| 82 |
} |
| 83 |
|
| 84 |
wp_send_json_success(array( |
| 85 |
'agree_count' => get_post_meta($topic_id, '_bbpc_agree_count', true), |
| 86 |
'disagree_count' => get_post_meta($topic_id, '_bbpc_disagree_count', true) |
| 87 |
)); |
| 88 |
} |
| 89 |
add_action('wp_ajax_bbpc_disagree', 'forumax_handle_disagree'); |
| 90 |
|
| 91 |
|
| 92 |
// Shortcode to display votings |
| 93 |
function forumax_display_geo_votings( $atts ) { |
| 94 |
$atts = shortcode_atts(array( |
| 95 |
'type' => 'liked', |
| 96 |
), $atts); |
| 97 |
ob_start(); |
| 98 |
|
| 99 |
$user_id = get_current_user_id(); |
| 100 |
$topic_id = []; |
| 101 |
$agree_voting = []; |
| 102 |
$disagree_voting = []; |
| 103 |
|
| 104 |
if ($user_id) { |
| 105 |
// Fetch topics where the user has voted "agree" |
| 106 |
$args = array( |
| 107 |
'post_type' => 'topic', |
| 108 |
'posts_per_page' => -1 |
| 109 |
); |
| 110 |
$bbpc_geo_votings = new WP_Query($args); |
| 111 |
if ($bbpc_geo_votings->have_posts()) { |
| 112 |
while ($bbpc_geo_votings->have_posts()) : $bbpc_geo_votings->the_post(); |
| 113 |
$topic_id[] = get_the_ID(); |
| 114 |
endwhile; |
| 115 |
wp_reset_postdata(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
foreach ( $topic_id as $topic_ids ) { |
| 120 |
$voting_type = get_user_meta($user_id, '_bbpc_vote_' . $topic_ids, true); |
| 121 |
if ( $voting_type == 'agree' ) { |
| 122 |
$agree_voting[] = $topic_ids; |
| 123 |
} |
| 124 |
if ( $voting_type == 'disagree' ) { |
| 125 |
$disagree_voting[] = $topic_ids; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$single_agree_voting = ''; |
| 130 |
$single_disagree_voting = ''; |
| 131 |
|
| 132 |
if ( count($agree_voting) === 1 ) { |
| 133 |
$single_agree_voting = 'single-topic-wrap'; |
| 134 |
} |
| 135 |
if ( count($disagree_voting) === 1 ) { |
| 136 |
$single_disagree_voting = 'single-topic-wrap'; |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! empty ( $agree_voting ) || ! empty ( $disagree_voting ) ) { |
| 140 |
if ( $atts['type'] == 'liked' && ! empty( $agree_voting ) ) { |
| 141 |
|
| 142 |
echo '<div class="bbpc-voting-heading">' . esc_html__( 'Liked Topics', 'forumax' ) . '</div>'; |
| 143 |
|
| 144 |
echo '<div class="bbpc-voting-liked-wrap '. esc_attr( $single_agree_voting ) . '">'; |
| 145 |
foreach ( $agree_voting as $agree_voting_id ) { |
| 146 |
$agree_count = get_post_meta($agree_voting_id, '_bbpc_agree_count', true) ?: 0; |
| 147 |
?> |
| 148 |
<div class="bbpc-voting-liked"> |
| 149 |
<a href="<?php echo esc_url( get_the_permalink($agree_voting_id) ); ?>"> |
| 150 |
<?php echo esc_html( get_the_title($agree_voting_id) ); ?> |
| 151 |
<span class="bbpc-agree-disagree-counter-wrap"> |
| 152 |
<span id="bbpc-agree-count-<?php echo esc_attr($agree_voting_id); ?>"> |
| 153 |
<img src="<?php echo esc_url( FORUMAX_IMG . 'icon/thumbs-up.svg' ); ?>" alt="<?php esc_attr_e( 'Thumbs up icon', 'forumax' ) ?>"/> |
| 154 |
<?php echo esc_html( $agree_count );?> |
| 155 |
</span> |
| 156 |
</span> |
| 157 |
</a> |
| 158 |
</div> |
| 159 |
<?php |
| 160 |
} |
| 161 |
echo '</div>'; |
| 162 |
} |
| 163 |
|
| 164 |
if ( $atts['type'] == 'disliked' && ! empty( $disagree_voting ) ) { |
| 165 |
echo '<div class="bbpc-voting-heading">' . esc_html__( 'Disliked Topics', 'forumax' ) . '</div>'; |
| 166 |
|
| 167 |
echo '<div class="bbpc-voting-liked-wrap '. esc_attr( $single_disagree_voting ) . '">'; |
| 168 |
foreach ( $disagree_voting as $disagree_voting_id ) { |
| 169 |
|
| 170 |
$disagree_count = get_post_meta($disagree_voting_id, '_bbpc_disagree_count', true) ?: 0; |
| 171 |
?> |
| 172 |
|
| 173 |
<div class="bbpc-voting-liked"> |
| 174 |
<a href="<?php echo esc_url( get_the_permalink($disagree_voting_id) ); ?>"><?php echo esc_html( get_the_title($disagree_voting_id) ); ?> |
| 175 |
|
| 176 |
<span class="bbpc-agree-disagree-counter-wrap"> |
| 177 |
<span id="bbpc-disagree-count-<?php echo esc_attr($disagree_voting_id); ?>"> |
| 178 |
<img src="<?php echo esc_url( FORUMAX_IMG . 'icon/thumbs-down.svg' ); ?>" alt="<?php esc_attr_e( 'Thumbs down icon', 'forumax' ) ?>"/> |
| 179 |
<?php echo esc_html( $disagree_count );?> |
| 180 |
</span> |
| 181 |
</span> |
| 182 |
</a> |
| 183 |
</div> |
| 184 |
<?php |
| 185 |
} |
| 186 |
echo '</div>'; |
| 187 |
} |
| 188 |
} else { |
| 189 |
echo "<div class='bbpc-no-voting-wrap'>" . esc_html__( "You don't have votes!", 'forumax' ) . "</div>"; |
| 190 |
} |
| 191 |
|
| 192 |
wp_enqueue_style( 'bbpc' ); |
| 193 |
wp_enqueue_style('bbpc-pro-frontend'); |
| 194 |
|
| 195 |
return ob_get_clean(); |
| 196 |
} |
| 197 |
add_shortcode('bbpc_geo_votings', 'forumax_display_geo_votings'); |
| 198 |
|
| 199 |
// Add Agree/Disagree buttons content |
| 200 |
add_action( 'bbp_theme_after_topic_content', function() { |
| 201 |
if ( class_exists( 'BBP_Core' ) ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
// Set default values if options are not set |
| 206 |
$reaction_condition = forumax_get_opt( 'reaction_display_condition', 'always' ); |
| 207 |
$topic_conditional_count = 0; |
| 208 |
|
| 209 |
// Check if the reaction condition is based on reply count |
| 210 |
if ( $reaction_condition === 'has_replies' ) { |
| 211 |
$topic_conditional_count = absint( forumax_get_opt( 'reaction_display_condition_count', 0 ) ); |
| 212 |
} |
| 213 |
|
| 214 |
// Check if topic meets the reply count condition for showing reactions |
| 215 |
if ( forumax_get_reply_count() >= $topic_conditional_count ) { |
| 216 |
|
| 217 |
// Handle login URL if user is not logged in |
| 218 |
$login_url = ''; |
| 219 |
if ( ! is_user_logged_in() ) { |
| 220 |
$login_url = 'data-login="' . esc_url( wp_login_url( get_permalink() ) ) . '"'; |
| 221 |
} |
| 222 |
|
| 223 |
// Enqueue the voting script |
| 224 |
wp_enqueue_script( 'bbpc-voting' ); |
| 225 |
|
| 226 |
$has_same_topic = false; |
| 227 |
|
| 228 |
// Check if the user is premium and the same topic voting is enabled |
| 229 |
if ( function_exists('forumax_is_premium') && forumax_is_premium() && function_exists('forumax_get_opt') && forumax_get_opt('same_topic_voting') == true ) { |
| 230 |
$has_same_topic = true; |
| 231 |
echo '<div class="bbpc-footer-actions">'; |
| 232 |
} |
| 233 |
|
| 234 |
// Begin outputting the agree/disagree buttons |
| 235 |
?> |
| 236 |
<div class="bbpc-agree-disagree-buttons"> |
| 237 |
<?php |
| 238 |
|
| 239 |
// Fetch the current user ID and topic ID |
| 240 |
$user_id = get_current_user_id(); |
| 241 |
$topic_id = get_the_ID(); |
| 242 |
$topic_id = absint( $topic_id ); // Sanitize the topic ID |
| 243 |
|
| 244 |
// Determine if the user has voted (using user meta for logged-in users or cookies for guests) |
| 245 |
$user_vote = ''; |
| 246 |
if ( $user_id ) { |
| 247 |
$user_vote = get_user_meta( $user_id, '_bbpc_vote_' . $topic_id, true ); |
| 248 |
} |
| 249 |
|
| 250 |
// Get vote counts (agree/disagree) |
| 251 |
$agree_count = absint( get_post_meta( $topic_id, '_bbpc_agree_count', true ) ?: 0 ); |
| 252 |
$disagree_count = absint( get_post_meta( $topic_id, '_bbpc_disagree_count', true ) ?: 0 ); |
| 253 |
|
| 254 |
// Determine which button is active based on the user's vote |
| 255 |
$agree_active_class = ( $user_vote === 'agree' ) ? 'active' : ''; |
| 256 |
$disagree_active_class = ( $user_vote === 'disagree' ) ? 'active' : ''; |
| 257 |
|
| 258 |
// Output Agree button |
| 259 |
echo '<button ' . wp_kses_post( $login_url ) . ' class="bbpc-agree-button ' . esc_attr( $agree_active_class ) . '" data-type="like" data-topic="' . esc_attr( $topic_id ) . '">' . esc_html__( 'Agree', 'forumax' ) . '</button>'; |
| 260 |
echo '<span class="bbpc-reactions-btn-counter" id="bbpc-agree-count-' . esc_attr( $topic_id ) . '">' . esc_html( $agree_count ) . '</span>'; |
| 261 |
|
| 262 |
// Output Disagree button |
| 263 |
echo '<button ' . wp_kses_post( $login_url ) . ' class="bbpc-disagree-button ' . esc_attr( $disagree_active_class ) . '" data-type="dislike" data-topic="' . esc_attr( $topic_id ) . '">' . esc_html__( 'Disagree', 'forumax' ) . '</button>'; |
| 264 |
echo '<span class="bbpc-reactions-btn-counter" id="bbpc-disagree-count-' . esc_attr( $topic_id ) . '">' . esc_html( $disagree_count ) . '</span>'; |
| 265 |
|
| 266 |
echo '</div>'; // Close agree-disagree-buttons div |
| 267 |
} |
| 268 |
}, 98 ); |
| 269 |
|