| @@ -3,10 +3,10 @@ | ||
| 3 | 3 | exit; // Exit if accessed directly. |
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | // Vote Buttons and Score hooks. |
| 7 | -add_action( 'bbp_theme_after_topic_author_details', 'bbp_voting_buttons' ); // ✅ New Hook | |
| 8 | -add_action( 'bbp_theme_after_reply_author_details', 'bbp_voting_buttons' ); | |
| 7 | +add_action( 'bbp_theme_after_topic_content', 'bbp_voting_buttons' ); | |
| 8 | +add_action( 'forumax_reply_avatar_voting', 'bbp_voting_buttons' ); | |
| 9 | 9 | add_action( 'bbp_voting_cpt', 'bbp_voting_buttons', 10, 1 ); |
| 10 | 10 | |
| 11 | 11 | function bbp_voting_buttons( $post_obj = false ) { |
| 12 | 12 | // Don't show voting buttons on user profile pages |
| @@ -19,53 +19,65 @@ | ||
| 19 | 19 | $reply_post_type = bbp_get_reply_post_type(); |
| 20 | 20 | $this_post_type = ''; |
| 21 | 21 | $post = null; |
| 22 | 22 | |
| 23 | - // Get post object based on context | |
| 24 | - if ( $current_action === 'bbp_voting_cpt' ) { | |
| 25 | - if ( ! $post_obj ) return; | |
| 23 | + // Prefer explicitly passed post object (custom hooks/CPT usage). | |
| 24 | + if ( $post_obj instanceof WP_Post ) { | |
| 26 | 25 | $post = $post_obj; |
| 27 | - } else { | |
| 26 | + $this_post_type = get_post_type( $post->ID ); | |
| 27 | + } | |
| 28 | + | |
| 29 | + // Fallback to action-based context detection. | |
| 30 | + if ( empty( $post ) ) { | |
| 31 | + if ( $current_action === 'bbp_voting_cpt' ) { | |
| 32 | + if ( ! $post_obj ) return; | |
| 33 | + $post = $post_obj; | |
| 34 | + $this_post_type = get_post_type( $post->ID ); | |
| 35 | + } else { | |
| 28 | 36 | // Determine post type from current action |
| 29 | 37 | if ( in_array( $current_action, [ |
| 30 | 38 | 'bbp_theme_before_topic_title', |
| 31 | 39 | 'bbp_template_before_lead_topic', |
| 32 | - 'bbp_theme_after_topic_author_details' | |
| 40 | + 'bbp_theme_after_topic_author_details', | |
| 41 | + 'bbp_theme_after_topic_content', | |
| 33 | 42 | ] ) ) { |
| 34 | 43 | $this_post_type = $topic_post_type; |
| 35 | 44 | $post = bbpress()->topic_query->post; |
| 36 | 45 | } elseif ( in_array( $current_action, [ |
| 37 | 46 | 'bbp_theme_before_reply_content', |
| 38 | - 'bbp_theme_after_reply_author_details' | |
| 47 | + 'bbp_theme_after_reply_author_details', | |
| 48 | + 'forumax_reply_avatar_voting', | |
| 49 | + 'bbp_theme_after_reply_content', | |
| 39 | 50 | ] ) ) { |
| 40 | 51 | $this_post_type = bbp_voting_get_current_post_type(); |
| 41 | 52 | $post = bbpress()->reply_query->post; |
| 42 | 53 | } |
| 54 | + } | |
| 55 | + } | |
| 43 | 56 | |
| 44 | - // Exit if unknown context or no post | |
| 45 | - if ( empty( $this_post_type ) || empty( $post ) ) return; | |
| 57 | + // Exit if unknown context or no post | |
| 58 | + if ( empty( $this_post_type ) || empty( $post ) ) return; | |
| 46 | 59 | |
| 47 | - // Check forum settings | |
| 48 | - $forum_id = ($this_post_type === $topic_post_type) | |
| 49 | - ? bbp_get_topic_forum_id( $post->ID ) | |
| 50 | - : bbp_get_reply_forum_id( $post->ID ); | |
| 60 | + // Check forum settings | |
| 61 | + $forum_id = ($this_post_type === $topic_post_type) | |
| 62 | + ? bbp_get_topic_forum_id( $post->ID ) | |
| 63 | + : bbp_get_reply_forum_id( $post->ID ); | |
| 51 | 64 | |
| 52 | - // Check if voting is allowed on this forum | |
| 53 | - if ( ! apply_filters( 'bbp_voting_allowed_on_forum', true, $forum_id ) ) return; | |
| 65 | + // Check if voting is allowed on this forum | |
| 66 | + if ( ! apply_filters( 'bbp_voting_allowed_on_forum', true, $forum_id ) ) return; | |
| 54 | 67 | |
| 55 | - // Get post-specific settings | |
| 56 | - $post_setting = ($this_post_type === $topic_post_type) | |
| 57 | - ? get_post_meta( $forum_id, 'bbp_voting_forum_enable_topics', true ) | |
| 58 | - : get_post_meta( $forum_id, 'bbp_voting_forum_enable_replies', true ); | |
| 68 | + // Get post-specific settings | |
| 69 | + $post_setting = ($this_post_type === $topic_post_type) | |
| 70 | + ? get_post_meta( $forum_id, 'bbp_voting_forum_enable_topics', true ) | |
| 71 | + : get_post_meta( $forum_id, 'bbp_voting_forum_enable_replies', true ); | |
| 59 | 72 | |
| 60 | - $broad_disable = ($this_post_type === $topic_post_type) | |
| 61 | - ? bbpc_get_opt( 'is_voting_disabled_topics', 0 ) | |
| 62 | - : bbpc_get_opt( 'is_voting_disabled_replies', 0 ); | |
| 73 | + $broad_disable = ($this_post_type === $topic_post_type) | |
| 74 | + ? forumax_get_opt( 'is_voting_disabled_topics', 0 ) | |
| 75 | + : forumax_get_opt( 'is_voting_disabled_replies', 0 ); | |
| 63 | 76 | |
| 64 | - // Check if voting is disabled | |
| 65 | - if ( ! empty( $post_setting ) && $post_setting === 'false' ) return; | |
| 66 | - if ( empty( $post_setting ) && $broad_disable === '0' ) return; | |
| 67 | - } | |
| 77 | + // Check if voting is disabled | |
| 78 | + if ( ! empty( $post_setting ) && $post_setting === 'false' ) return; | |
| 79 | + if ( empty( $post_setting ) && $broad_disable === '0' ) return; | |
| 68 | 80 | |
| 69 | 81 | $post_id = $post->ID; |
| 70 | 82 | |
| 71 | 83 | $score = (int) get_post_meta( $post_id, 'bbp_voting_score', true ); |
| @@ -86,13 +98,13 @@ | ||
| 86 | 98 | $existing_vote = $voting_log[ $identifier ] ?? 0; |
| 87 | 99 | |
| 88 | 100 | // Check if admin can bypass voting restrictions |
| 89 | 101 | $admin_bypass = current_user_can( 'administrator' ) && |
| 90 | - bbpc_get_opt( 'is_admin_can_vote_unlimited', false ); | |
| 102 | + forumax_get_opt( 'is_admin_can_vote_unlimited', false ); | |
| 91 | 103 | |
| 92 | 104 | // Determine if this is view-only mode |
| 93 | 105 | $view_only = ! is_user_logged_in() && |
| 94 | - bbpc_get_opt( 'is_disabled_voting_for_non_logged_users', false ); | |
| 106 | + forumax_get_opt( 'is_disabled_voting_for_non_logged_users', false ); | |
| 95 | 107 | |
| 96 | 108 | // Additional view-only checks for regular context (not CPT) |
| 97 | 109 | if ( ! $view_only && $current_action !== 'bbp_voting_cpt' ) { |
| 98 | 110 | // Check if topic is closed and voting on closed topics is disabled |
| @@ -99,29 +111,26 @@ | ||
| 99 | 111 | $topic_id = ( $this_post_type === $topic_post_type ) ? |
| 100 | 112 | $post_id : bbp_get_reply_topic_id( $post_id ); |
| 101 | 113 | |
| 102 | 114 | if ( get_post_status( $topic_id ) === 'closed' && |
| 103 | - bbpc_get_opt( 'is_disabled_voting_closed_topics', false ) ) { | |
| 115 | + forumax_get_opt( 'is_disabled_voting_closed_topics', false ) ) { | |
| 104 | 116 | $view_only = true; |
| 105 | 117 | } |
| 106 | 118 | |
| 107 | 119 | // Check if voting on own posts is disabled |
| 108 | 120 | if ( ! $view_only && |
| 109 | - bbpc_get_opt( 'is_disabled_voting_own_topic_reply', false ) && | |
| 121 | + forumax_get_opt( 'is_disabled_voting_own_topic_reply', false ) && | |
| 110 | 122 | $post->post_author == get_current_user_id() ) { |
| 111 | 123 | $view_only = true; |
| 112 | 124 | } |
| 113 | 125 | } |
| 114 | 126 | |
| 115 | - $disable_down = bbpc_get_opt( 'is_down_votes_disabled', 0 ); | |
| 116 | - $vote_number_display = bbpc_get_opt( 'vote_numbers_display', 'hover' ); | |
| 127 | + $disable_down = forumax_get_opt( 'is_down_votes_disabled', 0 ); | |
| 128 | + $vote_number_display = forumax_get_opt( 'vote_numbers_display', 'hover' ); | |
| 117 | 129 | $display_vote_nums = 'num-' . $vote_number_display; |
| 118 | 130 | |
| 119 | 131 | // Determine if floating style should be applied |
| 120 | - $float = in_array( $current_action, [ | |
| 121 | - 'bbp_theme_before_reply_content', | |
| 122 | - 'bbp_voting_cpt' | |
| 123 | - ] ); | |
| 132 | + $float = ( $current_action === 'bbp_voting_cpt' ); | |
| 124 | 133 | |
| 125 | 134 | // Build CSS classes for the voting container |
| 126 | 135 | $vote_classes = [ |
| 127 | 136 | 'bbp-voting', |
| @@ -143,11 +152,11 @@ | ||
| 143 | 152 | // Start building HTML |
| 144 | 153 | $html = '<div class="' . implode(' ', $vote_classes) . '">'; |
| 145 | 154 | |
| 146 | 155 | // Add label if enabled |
| 147 | - $is_label = bbpc_get_opt( 'is_label', true ); | |
| 156 | + $is_label = forumax_get_opt( 'is_label', true ); | |
| 148 | 157 | if ( $is_label ) { |
| 149 | - $upvote_label = bbpc_get_opt( 'upvote_label' ); | |
| 158 | + $upvote_label = forumax_get_opt( 'upvote_label' ); | |
| 150 | 159 | if ( $upvote_label ) { |
| 151 | 160 | $html .= '<div class="bbp-voting-label helpful">' . esc_html( $upvote_label ) . '</div>'; |
| 152 | 161 | } |
| 153 | 162 | } |
| @@ -173,10 +182,11 @@ | ||
| 173 | 182 | esc_attr( $post_id ), |
| 174 | 183 | esc_html( $plusups ) |
| 175 | 184 | ); |
| 176 | 185 | |
| 186 | + | |
| 177 | 187 | // Downvote form (if enabled) |
| 178 | - if ( $disable_down === '1' ) { | |
| 188 | + if ( $disable_down == 1 ) { | |
| 179 | 189 | $html .= sprintf( |
| 180 | 190 | '<form name="amp-form%1$s" method="post" action-xhr="%2$s" target="_top" on="submit-success: AMP.setState({\'votedown%1$s\': %3$s})"> |
| 181 | 191 | <input type="hidden" name="action" value="bbpress_post_vote_link_clicked"> |
| 182 | 192 | <input type="hidden" name="post_id" value="%4$s" /> |
| @@ -199,9 +209,9 @@ | ||
| 199 | 209 | $post_id . ', 1); return false;">Up</a>'; |
| 200 | 210 | |
| 201 | 211 | $html .= '<div class="score">' . $score . '</div>'; |
| 202 | 212 | |
| 203 | - if ( $disable_down === '1' ) { | |
| 213 | + if ( $disable_down == 1 ) { | |
| 204 | 214 | $html .= '<a class="vote down ' . esc_attr( $display_vote_nums ) . '" data-votes="' . |
| 205 | 215 | ( $downs ? $downs : '' ) . '" onclick="bbpress_post_vote_link_clicked(' . |
| 206 | 216 | $post_id . ', -1); return false;">Down</a>'; |
| 207 | 217 | } |
| @@ -206,10 +216,10 @@ | ||
| 206 | 216 | $post_id . ', -1); return false;">Down</a>'; |
| 207 | 217 | } |
| 208 | 218 | } |
| 209 | 219 | |
| 210 | - if ( $disable_down === '1' && $is_label ) { | |
| 211 | - $downvote_label = bbpc_get_opt( 'downvote_label' ); | |
| 220 | + if ( $disable_down == 1 && $is_label ) { | |
| 221 | + $downvote_label = forumax_get_opt( 'downvote_label' ); | |
| 212 | 222 | if ( $downvote_label ) { |
| 213 | 223 | $html .= '<div class="bbp-voting-label not-helpful">' . esc_html( $downvote_label ) . '</div>'; |
| 214 | 224 | } |
| 215 | 225 | } |
| @@ -219,11 +229,11 @@ | ||
| 219 | 229 | $html = apply_filters( 'bbp_voting_after_reply_voting_buttons', $html, $post_id ); |
| 220 | 230 | } |
| 221 | 231 | |
| 222 | 232 | // Close the voting container |
| 223 | - $html .= '</div><span style="display:none;">::</span>'; | |
| 233 | + $html .= '</div>'; | |
| 224 | 234 | |
| 225 | - echo wp_kses_post( $html ); | |
| 235 | + echo $html; | |
| 226 | 236 | } |
| 227 | 237 | |
| 228 | 238 | |
| 229 | 239 | // Sort by Votes |
| @@ -241,14 +251,14 @@ | ||
| 241 | 251 | switch ( $current_filter ) { |
| 242 | 252 | case 'bbp_has_topics_query': |
| 243 | 253 | $this_post_type = $forum_post_type; |
| 244 | 254 | $post_setting = get_post_meta( $forum_id, 'sort_bbpress_topics_by_votes_on_forum', true ); |
| 245 | - $broad_enable = bbpc_get_opt( 'is_sort_topic_by_votes', 0 ); | |
| 255 | + $broad_enable = forumax_get_opt( 'is_sort_topic_by_votes', false ); | |
| 246 | 256 | break; |
| 247 | 257 | case 'bbp_has_replies_query': |
| 248 | 258 | $this_post_type = $topic_post_type; |
| 249 | 259 | $post_setting = get_post_meta( $forum_id, 'sort_bbpress_replies_by_votes_on_forum', true ); |
| 250 | - $broad_enable = bbpc_get_opt( 'is_sort_reply_by_votes', 0 ); | |
| 260 | + $broad_enable = forumax_get_opt( 'is_sort_reply_by_votes', false ); | |
| 251 | 261 | break; |
| 252 | 262 | default: |
| 253 | 263 | return $args; |
| 254 | 264 | } |
| @@ -287,22 +297,29 @@ | ||
| 287 | 297 | // Get the meta key to sort by |
| 288 | 298 | $sort_meta_key = apply_filters( 'bbp_voting_sort_meta_key', 'bbp_voting_score' ); |
| 289 | 299 | |
| 290 | 300 | // Find and fill any posts missing the sort meta key |
| 291 | - $missing_meta_query = [ | |
| 292 | - 'meta_query' => [ | |
| 293 | - [ | |
| 294 | - 'key' => $sort_meta_key, | |
| 295 | - 'compare' => 'NOT EXISTS', | |
| 296 | - 'value' => '', | |
| 301 | + $cache_key = 'frmx_vote_backfill_' . md5( serialize( $args ) ); | |
| 302 | + if ( false === get_transient( $cache_key ) ) { | |
| 303 | + $missing_meta_query = [ | |
| 304 | + 'meta_query' => [ | |
| 305 | + [ | |
| 306 | + 'key' => $sort_meta_key, | |
| 307 | + 'compare' => 'NOT EXISTS', | |
| 308 | + 'value' => '', | |
| 309 | + ], | |
| 297 | 310 | ], |
| 298 | - ], | |
| 299 | - ]; | |
| 311 | + 'fields' => 'ids', | |
| 312 | + ]; | |
| 300 | 313 | |
| 301 | - $query = new WP_Query( array_merge( $args, $missing_meta_query ) ); | |
| 302 | - foreach ( $query->posts as $post ) { | |
| 303 | - $default_value = apply_filters( 'bbp_voting_sort_meta_key_default_value', '0', $post->ID ); | |
| 304 | - update_post_meta( $post->ID, $sort_meta_key, $default_value ); | |
| 314 | + $query = new WP_Query( array_merge( $args, $missing_meta_query ) ); | |
| 315 | + foreach ( $query->posts as $post_id ) { | |
| 316 | + $default_value = apply_filters( 'bbp_voting_sort_meta_key_default_value', '0', $post_id ); | |
| 317 | + update_post_meta( $post_id, $sort_meta_key, $default_value ); | |
| 318 | + } | |
| 319 | + | |
| 320 | + // Cache for 24 hours to skip subsequent checks | |
| 321 | + set_transient( $cache_key, 'done', DAY_IN_SECONDS ); | |
| 305 | 322 | } |
| 306 | 323 | |
| 307 | 324 | // Clear existing sort parameters |
| 308 | 325 | $args = array_diff_key( $args, array_flip( ['meta_key', 'meta_type', 'orderby', 'order'] ) ); |
| @@ -342,8 +359,8 @@ | ||
| 342 | 359 | } |
| 343 | 360 | |
| 344 | 361 | add_action( 'init', 'bbp_voting_lead_topic' ); |
| 345 | 362 | function bbp_voting_lead_topic() { |
| 346 | - if ( bbpc_get_opt( 'is_lead_topic_broken', false ) ) { | |
| 363 | + if ( forumax_get_opt( 'is_lead_topic_broken', false ) ) { | |
| 347 | 364 | add_filter( 'bbp_show_lead_topic', '__return_true' ); |
| 348 | 365 | } |
| 349 | 366 | } |