| 2662 | /** |
| 2663 | * Bump the voice count of a topic. |
| 2664 | * |
| 2665 | * @since x.x.x bbPress (rXXXX) |
| 2666 | * |
| 2667 | * @param int $topic_id Optional. Topic id. |
| 2668 | * @param int $difference Optional. Default 1. |
| 2669 | * |
| 2670 | * @return int Topic voice count. |
| 2671 | */ |
| 2672 | function bbp_bump_topic_voice_count( $topic_id = 0, $difference = 1 ) { |
| 2673 | |
| 2674 | // Bail if no bump. |
| 2675 | if ( empty( $difference ) ) { |
| 2676 | return false; |
| 2677 | } |
| 2678 | |
| 2679 | // Get counts. |
| 2680 | $topic_id = bbp_get_topic_id( $topic_id ); |
| 2681 | $voice_count = bbp_get_topic_voice_count( $topic_id, true ); |
| 2682 | $difference = (int) $difference; |
| 2683 | $new_count = (int) ( $voice_count + $difference ); |
| 2684 | |
| 2685 | // Update this topic id's voice count. |
| 2686 | update_post_meta( $topic_id, '_bbp_voice_count', $new_count ); |
| 2687 | |
| 2688 | return (int) apply_filters( 'bbp_bump_topic_voice_count', $new_count, $topic_id, $difference ); |
| 2689 | } |
| 2690 | |
| 2691 | /** |
| 2692 | * Increase the voice count of a topic by one. |
| 2693 | * |
| 2694 | * @since x.x.x bbPress (rXXXX) |
| 2695 | * |
| 2696 | * @param int $topic_id The topic id. |
| 2697 | * |
| 2698 | * @return void |
| 2699 | */ |
| 2700 | function bbp_increase_topic_voice_count( $topic_id = 0 ) { |
| 2701 | |
| 2702 | // Bail early if no id is passed. |
| 2703 | if ( empty( $topic_id ) ) { |
| 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | // If it's a reply, get the topic id. |
| 2708 | if ( bbp_is_reply( $topic_id ) ) { |
| 2709 | $topic_id = bbp_get_reply_topic_id( $topic_id ); |
| 2710 | } |
| 2711 | |
| 2712 | bbp_bump_topic_voice_count( $topic_id, 1 ); |
| 2713 | } |
| 2714 | |
| 2715 | /** |
| 2716 | * Decrease the voice count of a topic by one. |
| 2717 | * |
| 2718 | * @since x.x.x bbPress (rXXXX) |
| 2719 | * |
| 2720 | * @param int $topic_id The topic id. |
| 2721 | * |
| 2722 | * @return void |
| 2723 | */ |
| 2724 | function bbp_decrease_topic_voice_count( $topic_id = 0 ) { |
| 2725 | |
| 2726 | // Bail early if no id is passed. |
| 2727 | if ( empty( $topic_id ) ) { |
| 2728 | return; |
| 2729 | } |
| 2730 | |
| 2731 | // If it's a reply, get the topic id. |
| 2732 | if ( bbp_is_reply( $topic_id ) ) { |
| 2733 | $topic_id = bbp_get_reply_topic_id( $topic_id ); |
| 2734 | } |
| 2735 | |
| 2736 | bbp_bump_topic_voice_count( $topic_id, -1 ); |
| 2737 | } |
| 2738 | |
| 3073 | * Maybe update the topic voice count. |
| 3074 | * |
| 3075 | * Runs additional checks over `bbp_update_topic_voice_count()` to only |
| 3076 | * add/remove meta entries when absolutely necessary. |
| 3077 | * |
| 3078 | * @since x.x.x bbPress (rXXXX) |
| 3079 | * |
| 3080 | * @param int $topic_id The topic id (or reply id). |
| 3081 | * @param string $action The action. Expects `increase` or `decrease`. |
| 3082 | * |
| 3083 | * @return void |
| 3084 | */ |
| 3085 | function bbp_maybe_update_topic_voice_count( $topic_id = 0, $action = '' ) { |
| 3086 | |
| 3087 | // Make sure we have a valid action. |
| 3088 | if ( ! in_array( $action, array( 'increase', 'decrease' ) ) ) { |
| 3089 | return; |
| 3090 | } |
| 3091 | |
| 3092 | // If it's a reply, then get the parent (topic id). |
| 3093 | if ( bbp_is_reply( $topic_id ) ) { |
| 3094 | $author_id = bbp_get_reply_author_id( $topic_id ); |
| 3095 | $published = bbp_is_reply_published( $topic_id ); |
| 3096 | $topic_id = bbp_get_reply_topic_id( $topic_id ); |
| 3097 | } elseif ( bbp_is_topic( $topic_id ) ) { |
| 3098 | $author_id = bbp_get_topic_author_id( $topic_id ); |
| 3099 | $published = true; |
| 3100 | $topic_id = bbp_get_topic_id( $topic_id ); |
| 3101 | } else { |
| 3102 | return; |
| 3103 | } |
| 3104 | |
| 3105 | // Get the current voice count. |
| 3106 | $voice_count = bbp_get_topic_voice_count( $topic_id, true ); |
| 3107 | |
| 3108 | // Attempt to add an engagement. |
| 3109 | if ( 'increase' === $action && $published ) { |
| 3110 | bbp_add_user_engagement( $author_id, $topic_id ); |
| 3111 | |
| 3112 | // If we're decreasing, make sure the reply author isn't the topic author. |
| 3113 | } elseif ( $author_id !== bbp_get_topic_author_id( $topic_id ) ) { |
| 3114 | |
| 3115 | // Check for other replies by the user. |
| 3116 | $replies = new WP_Query( array( |
| 3117 | 'author' => $author_id, |
| 3118 | 'fields' => 'ids', |
| 3119 | 'nopaging' => true, |
| 3120 | 'no_found_rows' => true, |
| 3121 | 'post_parent' => $topic_id, |
| 3122 | 'post_status' => bbp_get_public_status_id(), |
| 3123 | 'post_type' => bbp_get_reply_post_type(), |
| 3124 | ) ); |
| 3125 | |
| 3126 | // Remove the user engagement if you no more replies. |
| 3127 | if ( 0 === ( count( $replies->posts ) ) ) { |
| 3128 | bbp_remove_user_engagement( $author_id, $topic_id ); |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | // Get the engagements count, and calculate the difference. |
| 3133 | $engagements_count = count( bbp_get_topic_engagements( $topic_id ) ); |
| 3134 | $difference = (int) ( $engagements_count - $voice_count ); |
| 3135 | |
| 3136 | bbp_bump_topic_voice_count( $topic_id, $difference ); |
| 3137 | } |
| 3138 | |
| 3139 | /** |
| 3140 | * Maybe increase the topic voice count. |
| 3141 | * |
| 3142 | * @since x.x.x bbPress (rXXXX) |
| 3143 | * |
| 3144 | * @param int $topic_id The topic id (or reply id). |
| 3145 | * |
| 3146 | * @return void |
| 3147 | */ |
| 3148 | function bbp_maybe_increase_topic_voice_count( $topic_id = 0 ) { |
| 3149 | |
| 3150 | if ( empty( $topic_id ) ) { |
| 3151 | return; |
| 3152 | } |
| 3153 | |
| 3154 | bbp_maybe_update_topic_voice_count( $topic_id, 'increase' ); |
| 3155 | } |
| 3156 | |
| 3157 | /** |
| 3158 | * Maybe decrease the topic voice count. |
| 3159 | * |
| 3160 | * @since x.x.x bbPress (rXXXX) |
| 3161 | * |
| 3162 | * @param int $topic_id The topic id (or reply id). |
| 3163 | * |
| 3164 | * @return void |
| 3165 | */ |
| 3166 | function bbp_maybe_decrease_topic_voice_count( $topic_id = 0 ) { |
| 3167 | |
| 3168 | if ( empty( $topic_id ) ) { |
| 3169 | return; |
| 3170 | } |
| 3171 | |
| 3172 | bbp_maybe_update_topic_voice_count( $topic_id, 'decrease' ); |
| 3173 | } |
| 3174 | |
| 3175 | /** |