Ticket #3428: 3428.03.patch
File 3428.03.patch, 16.3 KB (added by , 3 years ago) |
---|
-
src/includes/common/functions.php
304 304 * Get the forum statistics 305 305 * 306 306 * @since 2.0.0 bbPress (r2769) 307 * @since 2.6.0 bbPress (r6055) Introduced the `count_pending_topics` and 308 * `count_pending_replies` arguments. 307 * @since 2.6.0 bbPress (r6055) Added: 308 * `count_pending_topics` 309 * `count_pending_replies` 310 * @since 2.6.10 bbPress (r7235) Renamed: 311 * `count_trashed_topics` to `count_trash_topics` 312 * `count_trashed_replies` to `count_trash_replies` 313 * `count_spammed_topics` to `count_spam_topics` 314 * `count_spammed_replies` to `count_spam_replies` 315 * Added: 316 * `count_hidden_topics` 317 * `count_hidden_replies` 309 318 * 310 319 * @param array $args Optional. The function supports these arguments (all 311 * default to true): 312 * - count_users: Count users? 313 * - count_forums: Count forums? 314 * - count_topics: Count topics? If set to false, private, spammed and trashed 315 * topics are also not counted. 316 * - count_pending_topics: Count pending topics? (only counted if the current 320 * default to true): 321 * 322 * - count_users: Count users? 323 * - count_forums: Count forums? 324 * - count_topics: Count topics? If set to false, private, spam and 325 * trash topics are also not counted. 326 * - count_pending_topics: Count pending topics? (only counted if the current 317 327 * user has edit_others_topics cap) 318 * - count_private_topics: Count private topics? (only counted if the current328 * - count_private_topics: Count private topics? (only counted if the current 319 329 * user has read_private_topics cap) 320 * - count_spammed_topics: Count spammed topics? (only counted if the current 330 * - count_hidden_topics: Count hidden topics? (only counted if the current 331 * user has read_hidden_topics cap) 332 * - count_spam_topics: Count spam topics? (only counted if the current 321 333 * user has edit_others_topics cap) 322 * - count_trash ed_topics: Count trashedtopics? (only counted if the current334 * - count_trash_topics: Count trash topics? (only counted if the current 323 335 * user has view_trash cap) 324 * - count_replies: Count replies? If set to false, private, spammedand325 * trashedreplies are also not counted.336 * - count_replies: Count replies? If set to false, private, spam and 337 * trash replies are also not counted. 326 338 * - count_pending_replies: Count pending replies? (only counted if the current 327 339 * user has edit_others_replies cap) 328 340 * - count_private_replies: Count private replies? (only counted if the current 329 341 * user has read_private_replies cap) 330 * - count_spammed_replies: Count spammed replies? (only counted if the current 342 * - count_hidden_replies: Count hidden replies? (only counted if the current 343 * user has read_hidden_replies cap) 344 * - count_spam_replies: Count spam replies? (only counted if the current 331 345 * user has edit_others_replies cap) 332 * - count_trash ed_replies: Count trashedreplies? (only counted if the current346 * - count_trash_replies: Count trash replies? (only counted if the current 333 347 * user has view_trash cap) 334 * - count_tags: Count tags? If set to false, empty tags are also not counted 335 * - count_empty_tags: Count empty tags? 336 * @return object Walked forum tree 348 * - count_tags: Count tags? If set to false, empty tags are also 349 * not counted 350 * - count_empty_tags: Count empty tags? 351 * 352 * @return array Array of statistics 337 353 */ 338 354 function bbp_get_statistics( $args = array() ) { 339 355 … … 350 366 'count_topics' => true, 351 367 'count_pending_topics' => true, 352 368 'count_private_topics' => true, 353 'count_spammed_topics' => true, 354 'count_trashed_topics' => true, 369 'count_spam_topics' => true, 370 'count_trash_topics' => true, 371 'count_hidden_topics' => true, 355 372 356 373 // Replies 357 374 'count_replies' => true, 358 375 'count_pending_replies' => true, 359 376 'count_private_replies' => true, 360 'count_spammed_replies' => true, 361 'count_trashed_replies' => true, 377 'count_spam_replies' => true, 378 'count_trash_replies' => true, 379 'count_hidden_replies' => true, 362 380 363 381 // Topic tags 364 382 'count_tags' => true, … … 372 390 $topic_tag_count = $empty_topic_tag_count = 0; 373 391 $hidden_topic_title = $hidden_reply_title = ''; 374 392 393 // Post statuses 394 $publish = bbp_get_public_status_id(); 395 $closed = bbp_get_closed_status_id(); 396 $pending = bbp_get_pending_status_id(); 397 $private = bbp_get_private_status_id(); 398 $hidden = bbp_get_hidden_status_id(); 399 $spam = bbp_get_spam_status_id(); 400 $trash = bbp_get_trash_status_id(); 401 375 402 // Users 376 403 $user_count = ! empty( $r['count_users'] ) 377 404 ? bbp_get_total_users() … … 379 406 380 407 // Forums 381 408 $forum_count = ! empty( $r['count_forums'] ) 382 ? wp_count_posts( bbp_get_forum_post_type() )-> publish409 ? wp_count_posts( bbp_get_forum_post_type() )->{$publish} 383 410 : 0; 384 411 385 // Post statuses 386 $pending = bbp_get_pending_status_id(); 387 $private = bbp_get_private_status_id(); 388 $spam = bbp_get_spam_status_id(); 389 $trash = bbp_get_trash_status_id(); 390 $closed = bbp_get_closed_status_id(); 412 // Default capabilities 413 $caps = array( 414 'view_trash' => false, 415 'read_private_topics' => false, 416 'edit_others_topics' => false, 417 'read_private_replies' => false, 418 'edit_others_replies' => false, 419 'edit_topic_tags' => false 420 ); 391 421 422 // Get capabilities 423 foreach ( $caps as $key => $cap ) { 424 $caps[ $key ] = current_user_can( $cap ); 425 } 426 392 427 // Topics 393 428 if ( ! empty( $r['count_topics'] ) ) { 429 430 // Count all topics 394 431 $all_topics = wp_count_posts( bbp_get_topic_post_type() ); 395 432 396 433 // Published (publish + closed) 397 $topic_count = $all_topics-> publish+ $all_topics->{$closed};434 $topic_count = $all_topics->{$publish} + $all_topics->{$closed}; 398 435 399 if ( current_user_can( 'read_private_topics' ) || current_user_can( 'edit_others_topics' ) || current_user_can( 'view_trash' ) ) { 436 // Declare empty arrays 437 $topics = $topic_titles = array_fill_keys( bbp_get_non_public_topic_statuses(), '' ); 400 438 401 // Declare empty arrays 402 $topics = $topic_titles = array(); 439 // Pending 440 if ( ! empty( $r['count_pending_topics'] ) && ! empty( $caps['edit_others_topics'] ) ) { 441 $topics[ $pending ] = bbp_number_not_negative( $all_topics->{$pending} ); 442 $topic_titles[ $pending ] = sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $pending ] ) ); 443 } 403 444 404 // Pending 405 $topics['pending'] = ( ! empty( $r['count_pending_topics'] ) && current_user_can( 'edit_others_topics' ) ) 406 ? (int) $all_topics->{$pending} 407 : 0; 445 // Private 446 if ( ! empty( $r['count_private_topics'] ) && ! empty( $caps['read_private_topics'] ) ) { 447 $topics[ $private ] = bbp_number_not_negative( $all_topics->{$private} ); 448 $topic_titles[ $private ] = sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $private ] ) ); 449 } 408 450 409 // Private 410 $topics['private'] = ( ! empty( $r['count_private_topics'] ) && current_user_can( 'read_private_topics' ) ) 411 ? (int) $all_topics->{$private} 412 : 0; 451 // Hidden 452 if ( ! empty( $r['count_hidden_topics'] ) && ! empty( $caps['read_hidden_topics'] ) ) { 453 $topics[ $hidden ] = bbp_number_not_negative( $all_topics->{$hidden} ); 454 $topic_titles[ $hidden ] = sprintf( esc_html__( 'Hidden: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $hidden ] ) ); 455 } 413 456 414 // Spam 415 $topics['spammed'] = ( ! empty( $r['count_spammed_topics'] ) && current_user_can( 'edit_others_topics' ) ) 416 ? (int) $all_topics->{$spam} 417 : 0; 457 // Spam 458 if ( ! empty( $r['count_spam_topics'] ) && ! empty( $caps['edit_others_topics'] ) ) { 459 $topics[ $spam ] = bbp_number_not_negative( $all_topics->{$spam} ); 460 $topic_titles[ $spam ] = sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $spam ] ) ); 461 } 418 462 419 // Trash 420 $topics['trashed'] = ( ! empty( $r['count_trashed_topics'] ) && current_user_can( 'view_trash' ) ) 421 ? (int) $all_topics->{$trash} 422 : 0; 463 // Trash 464 if ( ! empty( $r['count_trash_topics'] ) && ! empty( $caps['view_trash'] ) ) { 465 $topics[ $trash ] = bbp_number_not_negative( $all_topics->{$trash} ); 466 $topic_titles[ $trash ] = sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $topics[ $trash ] ) ); 467 } 423 468 424 // Total hidden (pending + private + spam +trash)425 $topic_count_hidden = $topics['pending'] + $topics['private'] + $topics['spammed'] + $topics['trashed'];469 // Total hidden (pending, private, hidden, spam, trash) 470 $topic_count_hidden = array_sum( array_filter( $topics ) ); 426 471 427 // Generate the hidden topic count's title attribute 428 $topic_titles[] = ! empty( $topics['pending'] ) 429 ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $topics['pending'] ) ) 430 : ''; 431 432 $topic_titles[] = ! empty( $topics['private'] ) 433 ? sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $topics['private'] ) ) 434 : ''; 435 436 $topic_titles[] = ! empty( $topics['spammed'] ) 437 ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['spammed'] ) ) 438 : ''; 439 440 $topic_titles[] = ! empty( $topics['trashed'] ) 441 ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['trashed'] ) ) 442 : ''; 443 444 // Compile the hidden topic title 445 $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) ); 446 } 472 // Compile the hidden topic title 473 $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) ); 447 474 } 448 475 449 476 // Replies 450 477 if ( ! empty( $r['count_replies'] ) ) { 451 478 479 // Count all replies 452 480 $all_replies = wp_count_posts( bbp_get_reply_post_type() ); 453 481 454 482 // Published 455 $reply_count = $all_replies-> publish;483 $reply_count = $all_replies->{$publish}; 456 484 457 if ( current_user_can( 'read_private_replies' ) || current_user_can( 'edit_others_replies' ) || current_user_can( 'view_trash' ) ) { 485 // Declare empty arrays 486 $topics = $topic_titles = array_fill_keys( bbp_get_non_public_reply_statuses(), '' ); 458 487 459 // Declare empty arrays 460 $replies = $reply_titles = array(); 488 // Pending 489 if ( ! empty( $r['count_pending_replies'] ) && ! empty( $caps['edit_others_replies'] ) ) { 490 $replies[ $pending ] = bbp_number_not_negative( $all_replies->{$pending} ); 491 $reply_titles[ $pending ] = sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $pending ] ) ); 492 } 461 493 462 // Pending 463 $replies['pending'] = ( ! empty( $r['count_pending_replies'] ) && current_user_can( 'edit_others_replies' ) ) 464 ? (int) $all_replies->{$pending} 465 : 0; 494 // Private 495 if ( ! empty( $r['count_private_replies'] ) && ! empty( $caps['read_private_replies'] ) ) { 496 $replies[ $private ] = bbp_number_not_negative( $all_replies->{$private} ); 497 $reply_titles[ $private ] = sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $private ] ) ); 498 } 466 499 467 // Private 468 $replies['private'] = ( ! empty( $r['count_private_replies'] ) && current_user_can( 'read_private_replies' ) ) 469 ? (int) $all_replies->{$private} 470 : 0; 500 // Hidden 501 if ( ! empty( $r['count_hidden_replies'] ) && ! empty( $caps['read_hidden_replies'] ) ) { 502 $replies[ $hidden ] = bbp_number_not_negative( $all_replies->{$hidden} ); 503 $reply_titles[ $hidden ] = sprintf( esc_html__( 'Hidden: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $hidden ] ) ); 504 } 471 505 472 // Spam 473 $replies['spammed'] = ( ! empty( $r['count_spammed_replies'] ) && current_user_can( 'edit_others_replies' ) ) 474 ? (int) $all_replies->{$spam} 475 : 0; 506 // Spam 507 if ( ! empty( $r['count_spam_replies'] ) && ! empty( $caps['edit_others_replies'] ) ) { 508 $replies[ $spam ] = bbp_number_not_negative( $all_replies->{$spam} ); 509 $reply_titles[ $spam ] = sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $spam ] ) ); 510 } 476 511 477 // Trash 478 $replies['trashed'] = ( ! empty( $r['count_trashed_replies'] ) && current_user_can( 'view_trash' ) ) 479 ? (int) $all_replies->{$trash} 480 : 0; 512 // Trash 513 if ( ! empty( $r['count_trash_replies'] ) && ! empty( $caps['view_trash'] ) ) { 514 $replies[ $trash ] = bbp_number_not_negative( $all_replies->{$trash} ); 515 $reply_titles[ $trash ] = sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $replies[ $trash ] ) ); 516 } 481 517 482 // Total hidden (pending + private + spam +trash)483 $reply_count_hidden = $replies['pending'] + $replies['private'] + $replies['spammed'] + $replies['trashed'];518 // Total hidden (pending, private, hidden, spam, trash) 519 $reply_count_hidden = array_sum( $replies ); 484 520 485 // Generate the hidden topic count's title attribute 486 $reply_titles[] = ! empty( $replies['pending'] ) 487 ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $replies['pending'] ) ) 488 : ''; 489 $reply_titles[] = ! empty( $replies['private'] ) 490 ? sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $replies['private'] ) ) 491 : ''; 492 493 $reply_titles[] = ! empty( $replies['spammed'] ) 494 ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['spammed'] ) ) 495 : ''; 496 497 $reply_titles[] = ! empty( $replies['trashed'] ) 498 ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['trashed'] ) ) 499 : ''; 500 501 // Compile the hidden replies title 502 $hidden_reply_title = implode( ' | ', array_filter( $reply_titles ) ); 503 } 521 // Compile the hidden replies title 522 $hidden_reply_title = implode( ' | ', $reply_titles ); 504 523 } 505 524 506 525 // Topic Tags 507 526 if ( ! empty( $r['count_tags'] ) && bbp_allow_topic_tags() ) { 508 527 528 // Get the topic-tag taxonomy ID 529 $tt_id = bbp_get_topic_tag_tax_id(); 530 509 531 // Get the count 510 $topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id(), array( 'hide_empty' => true ) );532 $topic_tag_count = wp_count_terms( $tt_id, array( 'hide_empty' => true ) ); 511 533 512 534 // Empty tags 513 if ( ! empty( $r['count_empty_tags'] ) && current_user_can( 'edit_topic_tags' ) ) {514 $empty_topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id()) - $topic_tag_count;535 if ( ! empty( $r['count_empty_tags'] ) && ! empty( 'edit_topic_tags' ) ) { 536 $empty_topic_tag_count = wp_count_terms( $tt_id ) - $topic_tag_count; 515 537 } 516 538 } 517 539 518 540 // Tally the tallies 519 $counts = array_ filter( array_map( 'absint', compact(541 $counts = array_map( 'absint', compact( 520 542 'user_count', 521 543 'forum_count', 522 544 'topic_count', … … 525 547 'reply_count_hidden', 526 548 'topic_tag_count', 527 549 'empty_topic_tag_count' 528 ) ) );550 ) ); 529 551 530 552 // Define return value 531 553 $statistics = array(); 532 554 533 // Loop through and store the integer and i18n formatted counts .555 // Loop through and store the integer and i18n formatted counts 534 556 foreach ( $counts as $key => $count ) { 535 $statistics[ $key ] = bbp_number_format_i18n( $count ); 536 $statistics[ "{$key}_int" ] = $count; 557 $not_negative = bbp_number_not_negative( $count ); 558 $statistics[ $key ] = bbp_number_format_i18n( $not_negative ); 559 $statistics[ "{$key}_int" ] = $not_negative; 537 560 } 538 561 539 // Add the hidden (topic/reply) count title attribute strings because we 540 // don't need to run the math functions on these (see above) 562 // Add the hidden (topic/reply) count title attribute strings 541 563 $statistics['hidden_topic_title'] = $hidden_topic_title; 542 564 $statistics['hidden_reply_title'] = $hidden_reply_title; 543 565 -
src/templates/default/bbpress/content-statistics.php
13 13 // Get the statistics 14 14 $stats = bbp_get_statistics(); ?> 15 15 16 <dl role="main" >16 <dl role="main" class="bbp-stats"> 17 17 18 18 <?php do_action( 'bbp_before_statistics' ); ?> 19 19