Ticket #1799: 1799.14.voice_counts.diff
File 1799.14.voice_counts.diff, 14.8 KB (added by , 8 years ago) |
---|
-
src/includes/admin/tools/repair.php
diff --git src/includes/admin/tools/repair.php src/includes/admin/tools/repair.php index 34cb8b9..e49cf5c 100644
function bbp_admin_repair_topic_reply_count() { 227 227 */ 228 228 function bbp_admin_repair_topic_voice_count() { 229 229 230 // Define variables 230 // Define variables. 231 231 $bbp_db = bbp_db(); 232 232 $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' ); 233 233 $result = __( 'Failed!', 'bbpress' ); 234 234 235 $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count'";235 $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_voice_id')"; 236 236 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 237 237 return array( 1, sprintf( $statement, $result ) ); 238 238 } 239 239 240 // Post types and status 240 // Post types and status. 241 241 $tpt = bbp_get_topic_post_type(); 242 242 $rpt = bbp_get_reply_post_type(); 243 243 $pps = bbp_get_public_status_id(); 244 244 $cps = bbp_get_closed_status_id(); 245 245 246 $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) ( 247 SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value` 248 FROM `{$bbp_db->posts}` AS `posts` 249 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta` 250 ON `posts`.`ID` = `postmeta`.`post_id` 251 AND `postmeta`.`meta_key` = '_bbp_topic_id' 252 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' ) 253 AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' ) 254 AND `posts`.`post_author` != '0' 255 GROUP BY `postmeta`.`meta_value`)"; 246 $voice_id_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) ( 247 SELECT postmeta.meta_value, '_bbp_voice_id', posts.post_author 248 FROM {$bbp_db->posts} AS posts 249 LEFT JOIN {$bbp_db->postmeta} AS postmeta 250 ON posts.ID = postmeta.post_id 251 AND postmeta.meta_key = '_bbp_topic_id' 252 WHERE posts.post_type IN (%s, %s) 253 AND posts.post_status IN (%s, %s))", $tpt, $rpt, $pps, $cps ); 256 254 257 if ( is_wp_error( $bbp_db->query( $ sql ) ) ) {255 if ( is_wp_error( $bbp_db->query( $voice_id_sql ) ) ) { 258 256 return array( 2, sprintf( $statement, $result ) ); 259 257 } 260 258 259 $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) ( 260 SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value) 261 FROM {$bbp_db->postmeta} 262 WHERE meta_key = '_bbp_voice_id' 263 GROUP BY post_id ORDER BY post_id)"; 264 265 if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) { 266 return array( 3, sprintf( $statement, $result ) ); 267 } 268 261 269 return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) ); 262 270 } 263 271 -
src/includes/core/actions.php
diff --git src/includes/core/actions.php src/includes/core/actions.php index 734d00d..5f1cf54 100644
add_action( 'bbp_delete_topic', 'bbp_delete_topic_replies' ); 316 316 add_action( 'bbp_spam_topic', 'bbp_spam_topic_replies' ); 317 317 add_action( 'bbp_unspam_topic', 'bbp_unspam_topic_replies' ); 318 318 319 // Voice counts. 320 add_action( 'bbp_new_reply', 'bbp_maybe_increase_topic_voice_count' ); 321 add_action( 'bbp_new_topic', 'bbp_maybe_increase_topic_voice_count' ); 322 add_action( 'bbp_approved_reply', 'bbp_maybe_increase_topic_voice_count' ); 323 add_action( 'bbp_unspammed_reply', 'bbp_maybe_increase_topic_voice_count' ); 324 add_action( 'bbp_untrashed_reply', 'bbp_maybe_increase_topic_voice_count' ); 325 add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 326 add_action( 'bbp_spammed_reply', 'bbp_maybe_decrease_topic_voice_count' ); 327 add_action( 'bbp_trashed_reply', 'bbp_maybe_decrease_topic_voice_count' ); 328 329 // Insert topic/reply voice counts. 330 add_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count' ); 331 add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count', 10, 2 ); 332 319 333 // User status 320 334 // @todo make these sub-actions 321 335 add_action( 'make_ham_user', 'bbp_make_ham_user' ); -
src/includes/core/update.php
diff --git src/includes/core/update.php src/includes/core/update.php index b62cba8..986be53 100644
function bbp_version_updater() { 326 326 bbp_admin_upgrade_user_favorites(); 327 327 bbp_admin_upgrade_user_topic_subscriptions(); 328 328 bbp_admin_upgrade_user_forum_subscriptions(); 329 bbp_admin_repair_topic_voice_count(); 329 330 } 330 331 } 331 332 } -
src/includes/replies/functions.php
diff --git src/includes/replies/functions.php src/includes/replies/functions.php index 3296bd1..ee5a1ba 100644
function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 1018 1018 // See https://bbpress.trac.wordpress.org/ticket/2838 1019 1019 bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time ); 1020 1020 1021 // Counts1022 bbp_update_topic_voice_count( $ancestor );1023 1024 1021 // Only update reply count if we're deleting a reply, or in the dashboard. 1025 1022 if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) { 1026 1023 bbp_update_topic_reply_count( $ancestor ); 1027 1024 bbp_update_topic_reply_count_hidden( $ancestor ); 1025 bbp_update_topic_voice_count( $ancestor ); 1028 1026 } 1029 1027 1030 1028 // Forum meta relating to most recent topic -
src/includes/topics/functions.php
diff --git src/includes/topics/functions.php src/includes/topics/functions.php index 42c8a3a..94d5094 100644
function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = false 910 910 bbp_update_topic_last_active_time ( $topic_id, $last_active ); 911 911 bbp_update_topic_reply_count ( $topic_id, 0 ); 912 912 bbp_update_topic_reply_count_hidden ( $topic_id, 0 ); 913 bbp_update_topic_voice_count ( $topic_id );914 913 915 914 // Walk up ancestors and do the dirty work 916 915 bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false ); … … function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 2660 2659 } 2661 2660 } 2662 2661 2662 /** 2663 * Maybe bump the topic voice count. 2664 * 2665 * @since 2.6.0 bbPress (rXXXX) 2666 * 2667 * @param int $topic_id The reply id. 2668 * @param int $reply_id The topic id. 2669 * @param string $action Expected `increase` or `decrease`. 2670 * 2671 * @return void|int The topic voice count. 2672 */ 2673 function bbp_maybe_bump_topic_voice_count( $topic_id = 0, $reply_id = 0, $action = '' ) { 2674 2675 // Valid our topic/reply ids. 2676 $topic_id = bbp_get_topic_id( $topic_id ); 2677 $reply_id = bbp_get_reply_id( $reply_id ); 2678 2679 // Bail early if we don't have valid ids. 2680 if ( empty( $topic_id ) && empty( $reply_id ) ) { 2681 return; 2682 } 2683 2684 // Bail if we don't have a valid action. 2685 if ( ! in_array( $action, array( 'increase', 'decrease' ), true ) ) { 2686 return; 2687 } 2688 2689 // Make sure we have a topic id. 2690 if ( empty( $topic_id ) ) { 2691 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2692 } 2693 2694 // Set some defaults. 2695 $published = $anonymous = false; 2696 $author_id = 0; 2697 2698 // Possibly update our author and published variables. 2699 if ( empty( $reply_id ) ) { 2700 $author_id = bbp_get_topic_author_id( $topic_id ); 2701 $published = bbp_is_topic_published( $topic_id ); 2702 } else { 2703 $author_id = bbp_get_reply_author_id( $reply_id ); 2704 $published = bbp_is_reply_published( $reply_id ); 2705 } 2706 2707 // Get the topic voice ids, and voice count. 2708 $voice_ids = bbp_get_topic_voice_ids( $topic_id ); 2709 $current_voice_count = count( array_unique( $voice_ids ) ); 2710 2711 // If we have a valid author and we're published, update the voice ids array. 2712 if ( $published && 'increase' === $action ) { 2713 2714 // Add the post meta for new voice. 2715 add_post_meta( $topic_id, '_bbp_voice_id', $author_id ); 2716 2717 // Add the new author id to the voice ids array. 2718 $voice_ids[] = $author_id; 2719 2720 } elseif ( ! $published && 'decrease' === $action ) { 2721 2722 // Add filter to only remove one instance of the `_bbp_voice_id` value. 2723 $bbp_db = bbp_db(); 2724 $sql = $bbp_db->prepare( "SELECT meta_id FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = '_bbp_voice_id' AND meta_value = %d ORDER BY meta_id LIMIT 1", $topic_id, $author_id ); 2725 $mid = $bbp_db->get_var( $sql ); 2726 2727 // Remove one instance of the author id from the post meta. 2728 delete_metadata_by_mid( 'post', $mid ); 2729 2730 // Remove one occurrence of the author id from the voice ids array. 2731 $key = array_search( $author_id, $voice_ids, true ); 2732 if ( false !== $key ) { 2733 unset( $voice_ids[ $key ] ); 2734 } 2735 } 2736 2737 // Count the unique voices. 2738 $voice_count = count( array_unique( $voice_ids ) ); 2739 2740 // Update the back-compat key if the count has changed. 2741 if ( $voice_count !== $current_voice_count ) { 2742 update_post_meta( $topic_id, '_bbp_voice_count', $voice_count ); 2743 } 2744 2745 /** 2746 * Filters the return of a voice count bump for a topic. 2747 * 2748 * @since 2.6.0 bbPress (rXXXX) 2749 * 2750 * @param int $voice_count The topic voice count. 2751 * @param array $voice_ids Array of voice ids. 2752 * @param int $topic_id The topic id. 2753 * @param int $reply_id The reply id. 2754 * @param string $action The action being taken (`increase` or `decrease`). 2755 */ 2756 return (int) apply_filters( 'bbp_maybe_bump_topic_voice_count', $voice_count, $voice_ids, $topic_id, $reply_id, $action ); 2757 } 2758 2759 /** 2760 * Maybe increase the topic voice count. 2761 * 2762 * @since 2.6.0 bbPress (rXXXX) 2763 * 2764 * @param int $topic_id The topic id. 2765 * @param int $reply_id The reply id. 2766 * 2767 * @return void|int The topic voice count. 2768 */ 2769 function bbp_maybe_increase_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2770 2771 // If it's a reply, then get the topic id, and validate the reply id. 2772 if ( bbp_is_reply( $topic_id ) ) { 2773 $reply_id = bbp_get_reply_id( $topic_id ); 2774 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2775 2776 // If it's a topic, then validate the passed ids. 2777 } elseif ( bbp_is_topic( $topic_id ) ) { 2778 $reply_id = bbp_get_reply_id( $reply_id ); 2779 $topic_id = bbp_get_topic_id( $topic_id ); 2780 2781 // Bail if the passed id isn't a topic or reply. 2782 } else { 2783 return; 2784 } 2785 2786 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'increase' ); 2787 } 2788 2789 /** 2790 * Maybe decrease the topic voice count. 2791 * 2792 * @since 2.6.0 bbPress (rXXXX) 2793 * 2794 * @param int $topic_id The reply id. 2795 * @param int $reply_id The topic id. 2796 * 2797 * @return void|int The topic voice count. 2798 */ 2799 function bbp_maybe_decrease_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2800 2801 // If it's a reply, then get the topic id, and validate the reply id. 2802 if ( bbp_is_reply( $topic_id ) ) { 2803 $reply_id = bbp_get_reply_id( $topic_id ); 2804 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2805 2806 // If it's a topic, then validate the passed ids. 2807 } elseif ( bbp_is_topic( $topic_id ) ) { 2808 $reply_id = bbp_get_reply_id( $reply_id ); 2809 $topic_id = bbp_get_topic_id( $topic_id ); 2810 2811 // Bail if the passed id isn't a topic or reply. 2812 } else { 2813 return; 2814 } 2815 2816 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'decrease' ); 2817 } 2818 2663 2819 /** Topic Updaters ************************************************************/ 2664 2820 2665 2821 /** … … function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) { 2952 3108 * @uses update_post_meta() To update the topic voice count meta 2953 3109 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice 2954 3110 * count and topic id 2955 * @return int Voice count3111 * @return void|int Voice count. 2956 3112 */ 2957 3113 function bbp_update_topic_voice_count( $topic_id = 0 ) { 2958 3114 … … function bbp_update_topic_voice_count( $topic_id = 0 ) { 2966 3122 } 2967 3123 2968 3124 // Query the DB to get voices in this topic 2969 $bbp_db = bbp_db(); 2970 $query = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ); 2971 $voices = (int) $bbp_db->get_var( $query ); 3125 $bbp_db = bbp_db(); 3126 $query = $bbp_db->prepare( "SELECT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ); 3127 $voice_ids = $bbp_db->get_col( $query ); 3128 $voice_ids = array_map( 'intval', (array) $voice_ids ); 3129 $count = count( array_unique( $voice_ids ) ); 3130 3131 // Remove all voice id meta for the topic. 3132 delete_post_meta( $topic_id, '_bbp_voice_id' ); 3133 3134 // Update the voice ids and count for this topic id. 3135 foreach( $voice_ids as $voice_id ) { 3136 add_post_meta( $topic_id, '_bbp_voice_id', $voice_id ); 3137 } 3138 3139 // Update the voice count meta. 3140 update_post_meta( $topic_id, '_bbp_voice_count', $count ); 2972 3141 2973 // Update the voice count for this topic id 2974 update_post_meta( $topic_id, '_bbp_voice_count', $voices ); 3142 $count = apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id ); 2975 3143 2976 return (int) apply_filters( 'bbp_update_topic_voice_count', $voices, $topic_id);3144 return bbp_number_not_negative( $count ); 2977 3145 } 2978 3146 2979 3147 /** -
src/includes/topics/template.php
diff --git src/includes/topics/template.php src/includes/topics/template.php index 785bcb3..61bf793 100644
function bbp_topic_voice_count( $topic_id = 0, $integer = false ) { 2390 2390 } 2391 2391 2392 2392 /** 2393 * Return an array of topic voice ids. 2394 * 2395 * All `post_author` ids of public replies, along with public and closed topics 2396 * are included in this array. Authors of anonymous topics are counted as one 2397 * voice. Anonymous replies are not included. Voice ids are stored in the 2398 * post_meta table as a comma-separated list, and converted to an array before 2399 * being returned. 2400 * 2401 * @since 2.6.0 bbPress (rXXXX) 2402 * 2403 * @param int $topic_id The topic id. Required. 2404 * 2405 * @return void|array An array of voice ids. False on failure. 2406 */ 2407 function bbp_get_topic_voice_ids( $topic_id = 0 ) { 2408 2409 // Validate the topic id. 2410 $topic_id = bbp_get_topic_id( $topic_id ); 2411 2412 // Bail if the topic id isn't valid. 2413 if ( empty( $topic_id ) ) { 2414 return; 2415 } 2416 2417 // Get the voice id post meta. 2418 $voice_ids = get_post_meta( $topic_id, '_bbp_voice_id' ); 2419 2420 /* 2421 * Clean up the voice ids array. `wp_parse_id_list()` doesn't fit our needs 2422 * here. We don't want empty strings converted to (int) 0, and we don't want 2423 * the array to be trimmed to unique values only. 2424 */ 2425 if ( ! empty( $voice_ids ) ) { 2426 $voice_ids = array_filter( array_map( 'trim', $voice_ids ), 'strlen' ); 2427 $voice_ids = array_map( 'intval', $voice_ids ); 2428 } else { 2429 $voice_ids = array(); 2430 } 2431 2432 /** 2433 * Filters the voice ids array for a topic. 2434 * 2435 * @since 2.6.0 bbPress (rXXXX) 2436 * 2437 * @param array $voice_ids Array of voice ids. 2438 * @param int $topic_id The topic id. 2439 */ 2440 return (array) apply_filters( 'bbp_get_topic_voice_ids', $voice_ids, $topic_id ); 2441 } 2442 2443 /** 2393 2444 * Output a the tags of a topic 2394 2445 * 2395 2446 * @since 2.0.0 bbPress (r2688)