Ticket #1320: counting-123.diff
File counting-123.diff, 19.4 KB (added by , 14 years ago) |
---|
-
bbp-admin/bbp-functions.php
164 164 /** 165 165 * Recount forum topics 166 166 * 167 * @todo Forum total topic recount 168 * 167 169 * @since bbPress (r2613) 168 170 * 169 171 * @uses wpdb::query() To run our recount sql queries … … 176 178 $statement = __( 'Counting the number of topics in each forum… %s', 'bbpress' ); 177 179 $result = __( 'Failed!', 'bbpress' ); 178 180 179 $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_forum_topic_count';";181 $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_forum_topic_count', '_bbp_forum_total_topic_count' );"; 180 182 if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) 181 183 return array( 1, sprintf( $statement, $result ) ); 182 184 183 $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_forum_topic_count', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$bbp->topic_id}' AND `post_status` = 'publish'GROUP BY `post_parent`);";185 $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_forum_topic_count', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$bbp->topic_id}' AND `post_status` IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) GROUP BY `post_parent`);"; 184 186 if ( is_wp_error( $wpdb->query( $sql ) ) ) 185 187 return array( 2, sprintf( $statement, $result ) ); 186 188 … … 191 193 /** 192 194 * Recount forum replies 193 195 * 196 * @todo Make the recounts actually work 197 * 194 198 * @since bbPress (r2613) 195 199 * 196 200 * @uses wpdb::query() To run our recount sql queries … … 203 207 $statement = __( 'Counting the number of replies in each forum… %s', 'bbpress' ); 204 208 $result = __( 'Failed!', 'bbpress' ); 205 209 206 $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_forum_reply_count';";210 $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_forum_reply_count', '_bbp_forum_total_reply_count' );"; 207 211 if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) 208 212 return array( 1, sprintf( $statement, $result ) ); 209 213 -
bbp-includes/bbp-forum-functions.php
236 236 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it 237 237 * is a topic or a forum. If it's a topic, its parent, 238 238 * i.e. the forum is automatically retrieved. 239 * @param bool $total_count Optional. To return the total count or normal 240 * count? 239 241 * @uses get_post_field() To check whether the supplied id is a topic 240 242 * @uses bbp_get_topic_forum_id() To get the topic's forum id 241 243 * @uses wpdb::prepare() To prepare the sql statement 242 244 * @uses wpdb::get_col() To execute the query and get the column back 245 * @uses bbp_get_topic_status() To get the topic status 243 246 * @uses update_post_meta() To update the forum's topic count meta 244 247 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic 245 * count and forum id248 * count, forum id and total count bool 246 249 * @return int Forum topic count 247 250 */ 248 function bbp_update_forum_topic_count( $forum_id = 0 ) {251 function bbp_update_forum_topic_count( $forum_id = 0, $total_count = true ) { 249 252 global $wpdb, $bbp; 250 253 251 254 $forum_id = bbp_get_forum_id( $forum_id ); 252 255 253 // If it's a reply, then get the parent (topic id) 254 if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) ) 256 // If it's a topic, then get the parent (forum id) 257 if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) ) { 258 $topic_id = $forum_id; 255 259 $forum_id = bbp_get_topic_forum_id( $forum_id ); 260 } 256 261 257 // Get topics count 258 $topics = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->topic_id . "';", $forum_id ) ) ); 262 $topics = 0; 263 $children_topic_count = 0; 264 $children = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) ); 259 265 266 foreach ( (array) $children as $child ) { 267 $children_topic_count += (int) bbp_get_forum_topic_count( $child->ID ); 268 } 269 270 // Don't count topics if the forum is a category 271 if ( !bbp_is_forum_category( $forum_id ) ) { 272 273 if ( empty( $topic_id ) || !$topics = (int) get_post_meta( $forum_id, '_bbp_forum_topic_count', true ) ) { 274 $topics = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) AND post_type = '" . $bbp->topic_id . "';", $forum_id ) ); 275 } else { 276 if ( in_array( bbp_get_topic_status( $topic_id ), array( 'publish', $bbp->closed_status_id ) ) ) 277 $topics++; 278 else 279 $topics--; 280 } 281 282 } 283 284 $total_topics = $topics + $children_topic_count; 285 260 286 // Update the count 261 update_post_meta( $forum_id, '_bbp_forum_topic_count', (int) $topics ); 287 update_post_meta( $forum_id, '_bbp_forum_topic_count', $topics ); 288 update_post_meta( $forum_id, '_bbp_forum_total_topic_count', $total_topics ); 262 289 263 return apply_filters( 'bbp_update_forum_topic_count', (int) $topics, $forum_id ); 290 if ( $parent = bbp_get_forum_parent( $forum_id ) ) 291 bbp_update_forum_topic_count( $parent ); 292 293 return apply_filters( 'bbp_update_forum_topic_count', empty( $total_count ) ? $topics : $total_topics, $forum_id, $total_count ); 264 294 } 265 295 266 296 /** … … 270 300 * 271 301 * @since bbPress (r2464) 272 302 * 273 * @param int $forum_id Optional. Forum id or reply id. It is checked whether it 274 * is a reply or a forum. If it's a reply, its forum is 275 * automatically retrieved. 303 * @param int $forum_id Optional. Forum id or topic id reply id. It is checked 304 * whether it is a reply or a topic or a forum and the 305 * forum id is automatically retrieved. 306 * @param bool $total_count Optional. To return the total count or normal 307 * count? 276 308 * @uses get_post_field() To check whether the supplied id is a reply 277 * @uses bbp_get_reply_ topic_id() To get the reply's topicid309 * @uses bbp_get_reply_forum_id() To get the reply's forum id 278 310 * @uses bbp_get_topic_forum_id() To get the topic's forum id 279 311 * @uses wpdb::prepare() To prepare the sql statement 280 312 * @uses wpdb::get_col() To execute the query and get the column back 313 * @uses wpdb::get_var() To execute the query and get the var back 314 * @uses bbp_get_reply_status() To get the reply status 281 315 * @uses update_post_meta() To update the forum's reply count meta 282 316 * @uses apply_filters() Calls 'bbp_update_forum_reply_count' with the reply 283 * count and forum id317 * count, forum id and total count bool 284 318 * @return int Forum reply count 285 319 */ 286 function bbp_update_forum_reply_count( $forum_id = 0 ) {320 function bbp_update_forum_reply_count( $forum_id = 0, $total_count = true ) { 287 321 global $wpdb, $bbp; 288 322 289 323 $forum_id = bbp_get_forum_id( $forum_id ); 290 324 291 // If it's a reply, then get the parent (topicid)325 // If it's a reply, then get the grandparent (forum id) 292 326 if ( $bbp->reply_id == get_post_field( 'post_type', $forum_id ) ) { 293 $ topic_id = bbp_get_reply_topic_id( $forum_id );294 $forum_id = bbp_get_ topic_forum_id( $topic_id );327 $reply_id = $forum_id; 328 $forum_id = bbp_get_reply_forum_id( $forum_id ); 295 329 } 296 330 297 // There should always be at least 1 voice 298 $replies = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';", $forum_id ) ) ); 331 // If it's a topic, then get the parent (forum id) 332 if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) ) 333 $forum_id = bbp_get_topic_forum_id( $forum_id ); 299 334 335 $replies = 0; 336 $children_reply_count = 0; 337 $children = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) ); 338 339 foreach ( (array) $children as $child ) { 340 $children_reply_count += (int) bbp_get_forum_reply_count( $child->ID ); 341 } 342 343 // Don't count replies if the forum is a category 344 if ( !bbp_is_forum_category( $forum_id ) ) { 345 346 if ( empty( $reply_id ) || !$replies = (int) get_post_meta( $forum_id, '_bbp_forum_reply_count', true ) ) { 347 $topics = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->topic_id . "';", $forum_id ) ); 348 $replies = (int) !empty( $topics ) ? $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topics ) . " ) AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';" ) : 0; 349 } else { 350 if ( 'publish' == bbp_get_reply_status( $reply_id ) ) 351 $replies++; 352 else 353 $replies--; 354 } 355 356 } 357 358 $total_replies = $replies + $children_reply_count; 359 300 360 // Update the count 301 update_post_meta( $forum_id, '_bbp_forum_reply_count', (int) $replies ); 361 update_post_meta( $forum_id, '_bbp_forum_reply_count', $replies ); 362 update_post_meta( $forum_id, '_bbp_forum_total_reply_count', $total_replies ); 302 363 303 return apply_filters( 'bbp_update_forum_reply_count', (int) $replies, $forum_id ); 364 if ( $parent = bbp_get_forum_parent( $forum_id ) ) 365 bbp_update_forum_reply_count( $parent ); 366 367 return apply_filters( 'bbp_update_forum_reply_count', empty( $total_count ) ? $replies : $total_replies, $forum_id, $total_count ); 304 368 } 305 369 306 370 /** -
bbp-includes/bbp-forum-template.php
40 40 $r = wp_parse_args( $args, $default ); 41 41 42 42 // Don't show private forums to normal users 43 if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) { 44 $r['meta_key'] = '_bbp_forum_visibility'; 45 $r['meta_value'] = 'public'; 46 $r['meta_compare'] = '=='; 43 if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) { 44 $r['meta_key'] = '_bbp_forum_visibility'; 45 $r['meta_value'] = 'public'; 47 46 } 48 47 49 48 $bbp->forum_query = new WP_Query( $r ); … … 397 396 $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] ); 398 397 399 398 // Don't show private forums to normal users 400 if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) { 401 $r['meta_key'] = '_bbp_forum_visibility'; 402 $r['meta_value'] = 'public'; 403 $r['meta_compare'] = '=='; 399 if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) { 400 $r['meta_key'] = '_bbp_forum_visibility'; 401 $r['meta_value'] = 'public'; 404 402 } 405 403 406 404 // No forum passed … … 467 465 if ( !empty( $show_topic_count ) && !bbp_is_forum_category( $sub_forum->ID ) ) 468 466 $topic_count = ' (' . bbp_get_forum_topic_count( $sub_forum->ID ) . ')'; 469 467 470 //if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )471 //$reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')';468 if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) ) 469 $reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')'; 472 470 473 471 $output .= $link_before . '<a href="' . $permalink . '" class="bbp-forum-link">' . $title . $topic_count . $reply_count . '</a>' . $show_sep . $link_after; 474 472 } … … 866 864 * @since bbPress (r2464) 867 865 * 868 866 * @param int $forum_id Optional. Forum id 867 * @param bool $total_count Optional. To get the total count or normal count? 869 868 * @uses bbp_get_forum_topic_count() To get the forum topic count 870 869 */ 871 function bbp_forum_topic_count( $forum_id = 0 ) {870 function bbp_forum_topic_count( $forum_id = 0, $total_count = true ) { 872 871 echo bbp_get_forum_topic_count( $forum_id ); 873 872 } 874 873 /** … … 877 876 * @since bbPress (r2464) 878 877 * 879 878 * @param int $forum_id Optional. Forum id 879 * @param bool $total_count Optional. To get the total count or normal 880 * count? 880 881 * @uses bbp_get_forum_id() To get the forum id 881 882 * @uses get_post_meta() To get the forum topic count 882 883 * @uses bbp_update_forum_topic_count() To update the topic count if … … 885 886 * topic count and forum id 886 887 * @return int Forum topic count 887 888 */ 888 function bbp_get_forum_topic_count( $forum_id = 0 ) {889 function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true ) { 889 890 $forum_id = bbp_get_forum_id( $forum_id ); 890 $topics = get_post_meta( $forum_id, '_bbp_forum_topic_count', true );891 $topics = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_topic_count' : '_bbp_forum_total_topic_count', true ); 891 892 892 893 if ( '' === $topics ) 893 $topics = bbp_update_forum_topic_count( $forum_id );894 $topics = bbp_update_forum_topic_count( $forum_id, $total_count ); 894 895 895 896 return apply_filters( 'bbp_get_forum_topic_count', (int) $topics, $forum_id ); 896 897 } … … 901 902 * @since bbPress (r2464) 902 903 * 903 904 * @param int $forum_id Optional. Forum id 905 * @param bool $total_count Optional. To get the total count or normal count? 904 906 * @uses bbp_get_forum_reply_count() To get the forum reply count 905 907 */ 906 function bbp_forum_reply_count( $forum_id = 0 ) {907 echo bbp_get_forum_reply_count( $forum_id );908 function bbp_forum_reply_count( $forum_id = 0, $total_count = true ) { 909 echo bbp_get_forum_reply_count( $forum_id, $total_count ); 908 910 } 909 911 /** 910 912 * Return total post count of a forum … … 912 914 * @since bbPress (r2464) 913 915 * 914 916 * @param int $forum_id Optional. Forum id 917 * @param bool $total_count Optional. To get the total count or normal 918 * count? 915 919 * @uses bbp_get_forum_id() To get the forum id 916 920 * @uses get_post_meta() To get the forum reply count 917 921 * @uses bbp_update_forum_reply_count() To update the reply count if … … 920 924 * reply count and forum id 921 925 * @return int Forum reply count 922 926 */ 923 function bbp_get_forum_reply_count( $forum_id = 0 ) {927 function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true ) { 924 928 $forum_id = bbp_get_forum_id( $forum_id ); 925 $replies = get_post_meta( $forum_id, '_bbp_forum_reply_count', true );929 $replies = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_reply_count' : '_bbp_forum_total_reply_count', true ); 926 930 927 931 if ( '' === $replies ) 928 $replies = bbp_update_forum_reply_count( $forum_id );932 $replies = bbp_update_forum_reply_count( $forum_id, $total_count ); 929 933 930 934 return apply_filters( 'bbp_get_forum_reply_count', (int) $replies, $forum_id ); 931 935 } -
bbp-includes/bbp-hooks.php
152 152 add_action( 'untrashed_post', 'bbp_update_forum_reply_count' ); 153 153 add_action( 'deleted_post', 'bbp_update_forum_reply_count' ); 154 154 add_action( 'bbp_new_reply', 'bbp_update_forum_reply_count' ); 155 add_action( 'bbp_edit_ relpy', 'bbp_update_forum_reply_count' );155 add_action( 'bbp_edit_topic', 'bbp_update_forum_reply_count' ); 156 156 add_action( 'bbp_move_topic', 'bbp_update_forum_reply_count' ); 157 157 add_action( 'bbp_spammed_reply', 'bbp_update_forum_reply_count' ); 158 158 add_action( 'bbp_unspammed_reply', 'bbp_update_forum_reply_count' ); -
bbp-includes/bbp-reply-template.php
489 489 $author = bbp_get_reply_author_link( array( 'link_text' => bbp_get_reply_author( $revision->ID ), 'reply_id' => $revision->ID ) ); 490 490 $since = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) ); 491 491 492 $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item " class="bbp-reply-revision-log-item">' . "\n";492 $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item-' . $revision->ID . '" class="bbp-reply-revision-log-item">' . "\n"; 493 493 $r .= "\t\t" . sprintf( __( empty( $reason ) ? 'This reply was modified %1$s ago by %2$s.' : 'This reply was modified %1$s ago by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n"; 494 494 $r .= "\t" . '</li>' . "\n"; 495 495 -
bbp-includes/bbp-topic-functions.php
589 589 590 590 // Forum Topic Counts 591 591 bbp_update_forum_topic_count( $source_topic_forum_id ); 592 bbp_update_forum_topic_count( $destination_topic_id );592 // bbp_update_forum_topic_count( $destination_topic_id ); 593 593 594 594 // Forum Reply Counts 595 595 bbp_update_forum_reply_count( $source_topic_forum_id ); 596 bbp_update_forum_reply_count( $destination_topic_id );596 // bbp_update_forum_reply_count( $destination_topic_id ); 597 597 598 598 // Forum Voice Counts 599 599 bbp_update_forum_voice_count( $source_topic_forum_id ); … … 807 807 function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) { 808 808 809 809 // Forum Topic Counts 810 bbp_update_forum_topic_count( $source_topic_id );810 // bbp_update_forum_topic_count( $source_topic_id ); 811 811 bbp_update_forum_topic_count( $destination_topic_id ); 812 812 813 813 // Forum Reply Counts 814 bbp_update_forum_reply_count( $source_topic_id );814 // bbp_update_forum_reply_count( $source_topic_id ); 815 815 bbp_update_forum_reply_count( $destination_topic_id ); 816 816 817 817 // Forum Voice Counts -
bbp-includes/bbp-topic-template.php
525 525 $author = bbp_get_topic_author_link( array( 'link_text' => bbp_get_topic_author( $revision->ID ), 'topic_id' => $revision->ID ) ); 526 526 $since = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) ); 527 527 528 $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item " class="bbp-topic-revision-log-item">' . "\n";528 $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item-' . $revision->ID . '" class="bbp-topic-revision-log-item">' . "\n"; 529 529 $r .= "\t\t" . sprintf( __( empty( $reason ) ? 'This topic was modified %1$s ago by %2$s.' : 'This topic was modified %1$s ago by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n"; 530 530 $r .= "\t" . '</li>' . "\n"; 531 531 -
bbp-themes/bbp-twentyten/css/bbpress.css
339 339 /* =Stickies 340 340 -------------------------------------------------------------- */ 341 341 342 tr.super-sticky td,342 .bbp-topics-front tr.super-sticky td, 343 343 .bbp-forum-info tr.sticky td { 344 344 background-color: #ffffe0 !important; 345 345 font-size: 1.1em;